KeyboardManager: a class to manage the polling of a switch-matrix keyboard
kbd_mgr/KeyboardManager.h@2:eb4cc53ff33d, 2011-01-23 (annotated)
- Committer:
- osmeest
- Date:
- Sun Jan 23 23:15:36 2011 +0000
- Revision:
- 2:eb4cc53ff33d
- Child:
- 3:1310c57aca77
Thorough split of the code to have one responsibility per class. Generalisation of the keyboard layout (not limited to 4x4 anymore). Introduction of the kbd_mgr namespace (also apparent in header filenames).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
osmeest | 2:eb4cc53ff33d | 1 | #ifndef KEYBOARD_MANAGER_HPP_ |
osmeest | 2:eb4cc53ff33d | 2 | #define KEYBOARD_MANAGER_HPP_ |
osmeest | 2:eb4cc53ff33d | 3 | |
osmeest | 2:eb4cc53ff33d | 4 | #include "kbd_mgr/KeyPressEventHandler.h" |
osmeest | 2:eb4cc53ff33d | 5 | |
osmeest | 2:eb4cc53ff33d | 6 | #include "mbed.h" |
osmeest | 2:eb4cc53ff33d | 7 | #include <memory> |
osmeest | 2:eb4cc53ff33d | 8 | #include <vector> |
osmeest | 2:eb4cc53ff33d | 9 | |
osmeest | 2:eb4cc53ff33d | 10 | namespace kbd_mgr { |
osmeest | 2:eb4cc53ff33d | 11 | |
osmeest | 2:eb4cc53ff33d | 12 | /** |
osmeest | 2:eb4cc53ff33d | 13 | * @brief A class that polls the state of a NxM switch-matrix keyboard. |
osmeest | 2:eb4cc53ff33d | 14 | * For efficiency reasons, this class uses contiguous bits in a GPIO ports for reading the state of the keys in a row. |
osmeest | 2:eb4cc53ff33d | 15 | * Key rows are activated one by one using a set of digital outputs. |
osmeest | 2:eb4cc53ff33d | 16 | * Each time a key press or a key release is detected, the specified event handler is invoked. |
osmeest | 2:eb4cc53ff33d | 17 | * As the managed switch matrix is not protected against cross-overs, multiple key presses are |
osmeest | 2:eb4cc53ff33d | 18 | * ignored (only the state changes of the first key pressed are reported). |
osmeest | 2:eb4cc53ff33d | 19 | */ |
osmeest | 2:eb4cc53ff33d | 20 | class KeyboardManager { |
osmeest | 2:eb4cc53ff33d | 21 | public: |
osmeest | 2:eb4cc53ff33d | 22 | typedef std::vector<PinName> OutPinsSet; |
osmeest | 2:eb4cc53ff33d | 23 | |
osmeest | 2:eb4cc53ff33d | 24 | /** |
osmeest | 2:eb4cc53ff33d | 25 | * @param inPort Port to be used for reading the key state. |
osmeest | 2:eb4cc53ff33d | 26 | * @param numKeysPerRow Number of keys in a row (N) |
osmeest | 2:eb4cc53ff33d | 27 | * @param inLowestBit Index of the lowest bit of inPort (using inLowestBit to inLowestBit+N). |
osmeest | 2:eb4cc53ff33d | 28 | * @param outPins Pins to be used for powering each key row. |
osmeest | 2:eb4cc53ff33d | 29 | * @param handler Event handler called to report key presses and releases. |
osmeest | 2:eb4cc53ff33d | 30 | */ |
osmeest | 2:eb4cc53ff33d | 31 | KeyboardManager(PortName inPort, std::size_t numKeysPerRow, int inLowestBit, |
osmeest | 2:eb4cc53ff33d | 32 | const OutPinsSet &outPins, KeyPressEventHandler *handler); |
osmeest | 2:eb4cc53ff33d | 33 | |
osmeest | 2:eb4cc53ff33d | 34 | /** |
osmeest | 2:eb4cc53ff33d | 35 | * @brief Enables the polling of the keyboard state. |
osmeest | 2:eb4cc53ff33d | 36 | * @param pollingPeriod Frequency of the polling ticker (1kHz by default, min 20 microsec). |
osmeest | 2:eb4cc53ff33d | 37 | */ |
osmeest | 2:eb4cc53ff33d | 38 | void start(float pollingPeriod = 1e-3); |
osmeest | 2:eb4cc53ff33d | 39 | |
osmeest | 2:eb4cc53ff33d | 40 | /** |
osmeest | 2:eb4cc53ff33d | 41 | * @brief Disables the polling of the keyboard state. |
osmeest | 2:eb4cc53ff33d | 42 | */ |
osmeest | 2:eb4cc53ff33d | 43 | void stop(); |
osmeest | 2:eb4cc53ff33d | 44 | |
osmeest | 2:eb4cc53ff33d | 45 | private: |
osmeest | 2:eb4cc53ff33d | 46 | struct Impl; // opaque structure hiding implementation details |
osmeest | 2:eb4cc53ff33d | 47 | std::auto_ptr<Impl> pimpl; |
osmeest | 2:eb4cc53ff33d | 48 | }; |
osmeest | 2:eb4cc53ff33d | 49 | |
osmeest | 2:eb4cc53ff33d | 50 | } // kbd_mgr |
osmeest | 2:eb4cc53ff33d | 51 | |
osmeest | 2:eb4cc53ff33d | 52 | #endif // KEYBOARD_MANAGER_HPP_ |