This library improves behaviour of push buttons. The automatic repeat is very convenient for e.g. entering time with up/down buttons. The library buffers presses, so no presses are missed.

Dependents:   Thermostat_NucleoF401

Committer:
hilgo
Date:
Sun Feb 20 18:47:18 2011 +0000
Revision:
0:8be79829ce90
Initial revision, tested with the example provided.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hilgo 0:8be79829ce90 1 /* mbed RepeatButton Library
hilgo 0:8be79829ce90 2 * Copyright (c) 2011 Jeroen Hilgers
hilgo 0:8be79829ce90 3 *
hilgo 0:8be79829ce90 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
hilgo 0:8be79829ce90 5 * of this software and associated documentation files (the "Software"), to deal
hilgo 0:8be79829ce90 6 * in the Software without restriction, including without limitation the rights
hilgo 0:8be79829ce90 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hilgo 0:8be79829ce90 8 * copies of the Software, and to permit persons to whom the Software is
hilgo 0:8be79829ce90 9 * furnished to do so, subject to the following conditions:
hilgo 0:8be79829ce90 10 *
hilgo 0:8be79829ce90 11 * The above copyright notice and this permission notice shall be included in
hilgo 0:8be79829ce90 12 * all copies or substantial portions of the Software.
hilgo 0:8be79829ce90 13 *
hilgo 0:8be79829ce90 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hilgo 0:8be79829ce90 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hilgo 0:8be79829ce90 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hilgo 0:8be79829ce90 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hilgo 0:8be79829ce90 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hilgo 0:8be79829ce90 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hilgo 0:8be79829ce90 20 * THE SOFTWARE.
hilgo 0:8be79829ce90 21 */
hilgo 0:8be79829ce90 22
hilgo 0:8be79829ce90 23 #ifndef __REPEAT_BUTTON_H__
hilgo 0:8be79829ce90 24 #define __REPEAT_BUTTON_H__
hilgo 0:8be79829ce90 25
hilgo 0:8be79829ce90 26 /** KeyBuffer and RepeatButton classes.
hilgo 0:8be79829ce90 27 **
hilgo 0:8be79829ce90 28 ** The RepeatButton provides removal of contact bounce and repeat
hilgo 0:8be79829ce90 29 ** functionality. The repeat functionality works similar to that of
hilgo 0:8be79829ce90 30 ** a PC keyboard. If you press a key, it will register once. If you
hilgo 0:8be79829ce90 31 ** keep the key down, it will start repeating after a small delay.
hilgo 0:8be79829ce90 32 **
hilgo 0:8be79829ce90 33 ** The KeyBuffer class provides a key buffer functionality. It stores
hilgo 0:8be79829ce90 34 ** presses and repeats from one or more RepeatButton instances untill
hilgo 0:8be79829ce90 35 ** the main loop requests them.
hilgo 0:8be79829ce90 36 *
hilgo 0:8be79829ce90 37 *
hilgo 0:8be79829ce90 38 * Example:
hilgo 0:8be79829ce90 39 * @code
hilgo 0:8be79829ce90 40 * #include "mbed.h"
hilgo 0:8be79829ce90 41 * #include "RepeatButton.h"
hilgo 0:8be79829ce90 42 *
hilgo 0:8be79829ce90 43 * Serial pc(USBTX, USBRX);
hilgo 0:8be79829ce90 44 *
hilgo 0:8be79829ce90 45 * #define REPEAT_DELAY 500
hilgo 0:8be79829ce90 46 * #define REPEAT_PERIOD 100
hilgo 0:8be79829ce90 47 * #define KEY_LEFT 1
hilgo 0:8be79829ce90 48 * #define KEY_UP 2
hilgo 0:8be79829ce90 49 * #define KEY_RIGHT 3
hilgo 0:8be79829ce90 50 * #define KEY_DOWN 4
hilgo 0:8be79829ce90 51 * #define KEY_PUSH 5
hilgo 0:8be79829ce90 52 *
hilgo 0:8be79829ce90 53 * KeyBuffer TheKeyBuffer(32);
hilgo 0:8be79829ce90 54 *
hilgo 0:8be79829ce90 55 * RepeatButton naviLeft(p5, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_LEFT);
hilgo 0:8be79829ce90 56 * RepeatButton naviUp(p6, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_UP);
hilgo 0:8be79829ce90 57 * RepeatButton naviRight(p7, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_RIGHT);
hilgo 0:8be79829ce90 58 * RepeatButton naviDown(p9, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_DOWN);
hilgo 0:8be79829ce90 59 * RepeatButton naviPush(p8, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_PUSH);
hilgo 0:8be79829ce90 60 *
hilgo 0:8be79829ce90 61 * DigitalOut led1(LED1);
hilgo 0:8be79829ce90 62 *
hilgo 0:8be79829ce90 63 * int main()
hilgo 0:8be79829ce90 64 * {
hilgo 0:8be79829ce90 65 * int x=0, y=0, value;
hilgo 0:8be79829ce90 66 * pc.baud(115200);
hilgo 0:8be79829ce90 67 * pc.printf("Repeat key demo.\r\n");
hilgo 0:8be79829ce90 68 * while(1)
hilgo 0:8be79829ce90 69 * {
hilgo 0:8be79829ce90 70 * // Read TheKeyBuffer until empty.
hilgo 0:8be79829ce90 71 * for(value = TheKeyBuffer.Read(); value != -1; value = TheKeyBuffer.Read())
hilgo 0:8be79829ce90 72 * {
hilgo 0:8be79829ce90 73 * switch(value)
hilgo 0:8be79829ce90 74 * {
hilgo 0:8be79829ce90 75 * case KEY_LEFT:
hilgo 0:8be79829ce90 76 * x--;
hilgo 0:8be79829ce90 77 * break;
hilgo 0:8be79829ce90 78 *
hilgo 0:8be79829ce90 79 * case KEY_RIGHT:
hilgo 0:8be79829ce90 80 * x++;
hilgo 0:8be79829ce90 81 * break;
hilgo 0:8be79829ce90 82 *
hilgo 0:8be79829ce90 83 * case KEY_DOWN:
hilgo 0:8be79829ce90 84 * y--;
hilgo 0:8be79829ce90 85 * break;
hilgo 0:8be79829ce90 86 *
hilgo 0:8be79829ce90 87 * case KEY_UP:
hilgo 0:8be79829ce90 88 * y++;
hilgo 0:8be79829ce90 89 * break;
hilgo 0:8be79829ce90 90 *
hilgo 0:8be79829ce90 91 * case KEY_PUSH:
hilgo 0:8be79829ce90 92 * x = 0;
hilgo 0:8be79829ce90 93 * y = 0;
hilgo 0:8be79829ce90 94 * break;
hilgo 0:8be79829ce90 95 * }
hilgo 0:8be79829ce90 96 * }
hilgo 0:8be79829ce90 97 * pc.printf("(x, y) = (%d, %d)\r\n", x, y);
hilgo 0:8be79829ce90 98 * wait(0.05);
hilgo 0:8be79829ce90 99 * led1 = led1^1;
hilgo 0:8be79829ce90 100 * }
hilgo 0:8be79829ce90 101 * }
hilgo 0:8be79829ce90 102 *
hilgo 0:8be79829ce90 103 * @endcode
hilgo 0:8be79829ce90 104 */
hilgo 0:8be79829ce90 105
hilgo 0:8be79829ce90 106
hilgo 0:8be79829ce90 107 class KeyBuffer
hilgo 0:8be79829ce90 108 {
hilgo 0:8be79829ce90 109 public:
hilgo 0:8be79829ce90 110 /** Create a KeyBuffer object and initizalize it.
hilgo 0:8be79829ce90 111 *
hilgo 0:8be79829ce90 112 * @param size Number of button events that can be stored in the buffer.
hilgo 0:8be79829ce90 113 */
hilgo 0:8be79829ce90 114 KeyBuffer(int size);
hilgo 0:8be79829ce90 115 ~KeyBuffer();
hilgo 0:8be79829ce90 116 /** Add a press or repeat to the buffer.
hilgo 0:8be79829ce90 117 *
hilgo 0:8be79829ce90 118 * @param value Identifier for the key pressed.
hilgo 0:8be79829ce90 119 */
hilgo 0:8be79829ce90 120 void Write(uint8_t value);
hilgo 0:8be79829ce90 121 /** Check if there is anything waiting in the buffer.
hilgo 0:8be79829ce90 122 *
hilgo 0:8be79829ce90 123 * @return value Returns -1 if the buffer is empty. Otherwise, the next value is returned and removed from the buffer.
hilgo 0:8be79829ce90 124 */
hilgo 0:8be79829ce90 125 int Read();
hilgo 0:8be79829ce90 126 private:
hilgo 0:8be79829ce90 127 int mSize;
hilgo 0:8be79829ce90 128 int mReadIndex;
hilgo 0:8be79829ce90 129 int mWriteIndex;
hilgo 0:8be79829ce90 130 uint8_t *mBuffer;
hilgo 0:8be79829ce90 131 };
hilgo 0:8be79829ce90 132
hilgo 0:8be79829ce90 133
hilgo 0:8be79829ce90 134 class RepeatButton
hilgo 0:8be79829ce90 135 {
hilgo 0:8be79829ce90 136 public:
hilgo 0:8be79829ce90 137 /** Create a RepeatButton object and initizalize it.
hilgo 0:8be79829ce90 138 *
hilgo 0:8be79829ce90 139 * @param pin The mbed pin to which the button is connected. A PullUp is connected to this pin, so it should connect to ground.
hilgo 0:8be79829ce90 140 * @param delay_ms The number of ms before the first repeat.
hilgo 0:8be79829ce90 141 * @param period_ms The number of ms between each repeat.
hilgo 0:8be79829ce90 142 * @param buf Pointer to the KeyBuffer instance where this RepeatButton can store its events.
hilgo 0:8be79829ce90 143 * @param value Value for the key. Needed to discriminate between different keys going into the same buffer.
hilgo 0:8be79829ce90 144 */
hilgo 0:8be79829ce90 145 RepeatButton(PinName pin, int delay_ms, int period_ms, KeyBuffer *buf, char value);
hilgo 0:8be79829ce90 146 private:
hilgo 0:8be79829ce90 147 void OnChange(); // Called on rising/falling edge.
hilgo 0:8be79829ce90 148
hilgo 0:8be79829ce90 149 Timeout mTimeout;
hilgo 0:8be79829ce90 150
hilgo 0:8be79829ce90 151 void OnBounce(); // Called upon expiracy of bounce delay.
hilgo 0:8be79829ce90 152 void OnRepeat(); // Called upon expiracy of delay / repeat period.
hilgo 0:8be79829ce90 153
hilgo 0:8be79829ce90 154 DigitalIn mIn;
hilgo 0:8be79829ce90 155 InterruptIn mIrq;
hilgo 0:8be79829ce90 156
hilgo 0:8be79829ce90 157 char mValue;
hilgo 0:8be79829ce90 158 int mDelay;
hilgo 0:8be79829ce90 159 int mPeriod;
hilgo 0:8be79829ce90 160
hilgo 0:8be79829ce90 161 KeyBuffer *mTarget; // Reference to target buffer.
hilgo 0:8be79829ce90 162 };
hilgo 0:8be79829ce90 163
hilgo 0:8be79829ce90 164 #endif // __REPEAT_BUTTON_H__