KeyboardManager: a class to manage the polling of a switch-matrix keyboard

Dependents:   KeyboardTest

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?

UserRevisionLine numberNew contents of line
osmeest 2:eb4cc53ff33d 1 #ifndef KEYBOARD_MONITOR_H_
osmeest 2:eb4cc53ff33d 2 #define KEYBOARD_MONITOR_H_
osmeest 2:eb4cc53ff33d 3
osmeest 2:eb4cc53ff33d 4 #include "kbd_mgr/KeyboardState.h"
osmeest 2:eb4cc53ff33d 5 #include "kbd_mgr/KeyboardStateHandler.h"
osmeest 2:eb4cc53ff33d 6
osmeest 2:eb4cc53ff33d 7 #include "mbed.h"
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 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 * When a full keyboard scan has been done, it is provided to the registered handler.
osmeest 2:eb4cc53ff33d 17 */
osmeest 2:eb4cc53ff33d 18 class KeyboardMonitor {
osmeest 2:eb4cc53ff33d 19 public:
osmeest 2:eb4cc53ff33d 20 typedef std::vector<PinName> OutPinsSet;
osmeest 2:eb4cc53ff33d 21
osmeest 2:eb4cc53ff33d 22 /**
osmeest 2:eb4cc53ff33d 23 * @param inPort Port to be used for reading the key state.
osmeest 2:eb4cc53ff33d 24 * @param numKeysPerRow Number of keys in a row (N)
osmeest 2:eb4cc53ff33d 25 * @param inLowestBit Index of the lowest bit of inPort (using inLowestBit to inLowestBit+N).
osmeest 2:eb4cc53ff33d 26 * @param outPins Pins to be used for powering each key row.
osmeest 2:eb4cc53ff33d 27 * @param handler Event handler called to report keyboard state after each complete scan.
osmeest 2:eb4cc53ff33d 28 */
osmeest 2:eb4cc53ff33d 29 KeyboardMonitor(PortName inPort, std::size_t numKeysPerRow, std::size_t inLowestBit,
osmeest 2:eb4cc53ff33d 30 const OutPinsSet &outPins, KeyboardStateHandler *handler);
osmeest 2:eb4cc53ff33d 31
osmeest 2:eb4cc53ff33d 32 /**
osmeest 2:eb4cc53ff33d 33 * @brief Enables the polling of the keyboard state.
osmeest 2:eb4cc53ff33d 34 * @param pollingPeriod Frequency of the polling ticker (1kHz by default, min 20 microsec).
osmeest 2:eb4cc53ff33d 35 */
osmeest 2:eb4cc53ff33d 36 void start(float pollingPeriod = 0.001);
osmeest 2:eb4cc53ff33d 37
osmeest 2:eb4cc53ff33d 38 /**
osmeest 2:eb4cc53ff33d 39 * @brief Disables the polling of the keyboard state.
osmeest 2:eb4cc53ff33d 40 */
osmeest 2:eb4cc53ff33d 41 void stop();
osmeest 2:eb4cc53ff33d 42
osmeest 2:eb4cc53ff33d 43 private:
osmeest 2:eb4cc53ff33d 44 void timerHandler();
osmeest 2:eb4cc53ff33d 45
osmeest 2:eb4cc53ff33d 46 PortIn in;
osmeest 2:eb4cc53ff33d 47 int inBitShift;
osmeest 2:eb4cc53ff33d 48 OutPinsSet outPins;
osmeest 2:eb4cc53ff33d 49 KeyboardStateHandler *handler;
osmeest 2:eb4cc53ff33d 50
osmeest 2:eb4cc53ff33d 51 Ticker ticker;
osmeest 2:eb4cc53ff33d 52 std::size_t scanRow; /*!< next key row to be scanned */
osmeest 2:eb4cc53ff33d 53 KeyboardState currentState; /*!< currently being built keyboard state */
osmeest 2:eb4cc53ff33d 54 };
osmeest 2:eb4cc53ff33d 55
osmeest 2:eb4cc53ff33d 56 } // kbd_mgr
osmeest 2:eb4cc53ff33d 57
osmeest 2:eb4cc53ff33d 58 #endif // KEYBOARD_MONITOR_H_