KeyboardManager: a class to manage the polling of a switch-matrix keyboard
kbd_mgr/KeyboardMonitor.h@3:1310c57aca77, 2011-02-03 (annotated)
- Committer:
- osmeest
- Date:
- Thu Feb 03 22:01:57 2011 +0000
- Revision:
- 3:1310c57aca77
- Parent:
- 2:eb4cc53ff33d
improve code structure, add key mapping and long key press handling
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
osmeest | 3:1310c57aca77 | 1 | #ifndef KEYBOARD_MONITOR_H_ |
osmeest | 3:1310c57aca77 | 2 | #define KEYBOARD_MONITOR_H_ |
osmeest | 3:1310c57aca77 | 3 | |
osmeest | 3:1310c57aca77 | 4 | #include "kbd_mgr/KeyboardStateEventServer.h" |
osmeest | 3:1310c57aca77 | 5 | #include "kbd_mgr/KeyboardState.h" |
osmeest | 3:1310c57aca77 | 6 | |
osmeest | 3:1310c57aca77 | 7 | #include "mbed.h" |
osmeest | 3:1310c57aca77 | 8 | #include <vector> |
osmeest | 3:1310c57aca77 | 9 | |
osmeest | 3:1310c57aca77 | 10 | namespace kbd_mgr { |
osmeest | 3:1310c57aca77 | 11 | |
osmeest | 3:1310c57aca77 | 12 | /** |
osmeest | 3:1310c57aca77 | 13 | * @brief A class that polls the state of a switch-matrix keyboard. |
osmeest | 3:1310c57aca77 | 14 | * For efficiency reasons, this class uses contiguous bits in a GPIO ports for reading the state of the keys in a row. |
osmeest | 3:1310c57aca77 | 15 | * Key rows are activated one by one using a set of digital outputs. |
osmeest | 3:1310c57aca77 | 16 | * When a full keyboard scan has been done, it is provided to the registered handler. |
osmeest | 3:1310c57aca77 | 17 | */ |
osmeest | 3:1310c57aca77 | 18 | class KeyboardMonitor : public KeyboardStateEventServer { |
osmeest | 3:1310c57aca77 | 19 | public: |
osmeest | 3:1310c57aca77 | 20 | typedef std::vector<PinName> OutPinsSet; |
osmeest | 3:1310c57aca77 | 21 | |
osmeest | 3:1310c57aca77 | 22 | /** |
osmeest | 3:1310c57aca77 | 23 | * @param inPort Port to be used for reading the key state. |
osmeest | 3:1310c57aca77 | 24 | * @param numKeysPerRow Number of keys in a row (N) |
osmeest | 3:1310c57aca77 | 25 | * @param inLowestBit Index of the lowest bit of inPort (using inLowestBit to inLowestBit+N). |
osmeest | 3:1310c57aca77 | 26 | * @param outPins Pins to be used for powering each key row. |
osmeest | 3:1310c57aca77 | 27 | */ |
osmeest | 3:1310c57aca77 | 28 | KeyboardMonitor(PortName inPort, std::size_t numKeysPerRow, std::size_t inLowestBit, |
osmeest | 3:1310c57aca77 | 29 | const OutPinsSet &outPins); |
osmeest | 3:1310c57aca77 | 30 | |
osmeest | 3:1310c57aca77 | 31 | ~KeyboardMonitor(); |
osmeest | 3:1310c57aca77 | 32 | |
osmeest | 3:1310c57aca77 | 33 | /** |
osmeest | 3:1310c57aca77 | 34 | * @brief Starts the polling of the keyboard state. |
osmeest | 3:1310c57aca77 | 35 | */ |
osmeest | 3:1310c57aca77 | 36 | void start(float pollingPeriod = 0.001); |
osmeest | 3:1310c57aca77 | 37 | |
osmeest | 3:1310c57aca77 | 38 | /** |
osmeest | 3:1310c57aca77 | 39 | * @brief Disables the polling of the keyboard state. |
osmeest | 3:1310c57aca77 | 40 | */ |
osmeest | 3:1310c57aca77 | 41 | void stop(); |
osmeest | 3:1310c57aca77 | 42 | |
osmeest | 3:1310c57aca77 | 43 | private: |
osmeest | 3:1310c57aca77 | 44 | void timerHandler(); |
osmeest | 3:1310c57aca77 | 45 | |
osmeest | 3:1310c57aca77 | 46 | PortIn in; |
osmeest | 3:1310c57aca77 | 47 | int inBitShift; |
osmeest | 3:1310c57aca77 | 48 | OutPinsSet outPins; |
osmeest | 3:1310c57aca77 | 49 | |
osmeest | 3:1310c57aca77 | 50 | Ticker ticker; |
osmeest | 3:1310c57aca77 | 51 | std::size_t scanRow; /*!< next key row to be scanned */ |
osmeest | 3:1310c57aca77 | 52 | KeyboardState currentState; /*!< currently being built keyboard state */ |
osmeest | 3:1310c57aca77 | 53 | }; |
osmeest | 3:1310c57aca77 | 54 | |
osmeest | 3:1310c57aca77 | 55 | } // kbd_mgr |
osmeest | 3:1310c57aca77 | 56 | |
osmeest | 3:1310c57aca77 | 57 | #endif // KEYBOARD_MONITOR_H_ |