KeyboardManager: a class to manage the polling of a switch-matrix keyboard
Diff: kbd_mgr/KeyboardMonitor.h
- Revision:
- 2:eb4cc53ff33d
- Child:
- 3:1310c57aca77
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kbd_mgr/KeyboardMonitor.h Sun Jan 23 23:15:36 2011 +0000 @@ -0,0 +1,58 @@ +#ifndef KEYBOARD_MONITOR_H_ +#define KEYBOARD_MONITOR_H_ + +#include "kbd_mgr/KeyboardState.h" +#include "kbd_mgr/KeyboardStateHandler.h" + +#include "mbed.h" +#include <vector> + +namespace kbd_mgr { + +/** + * @brief A class that polls the state of a switch-matrix keyboard. + * For efficiency reasons, this class uses contiguous bits in a GPIO ports for reading the state of the keys in a row. + * Key rows are activated one by one using a set of digital outputs. + * When a full keyboard scan has been done, it is provided to the registered handler. + */ +class KeyboardMonitor { +public: + typedef std::vector<PinName> OutPinsSet; + + /** + * @param inPort Port to be used for reading the key state. + * @param numKeysPerRow Number of keys in a row (N) + * @param inLowestBit Index of the lowest bit of inPort (using inLowestBit to inLowestBit+N). + * @param outPins Pins to be used for powering each key row. + * @param handler Event handler called to report keyboard state after each complete scan. + */ + KeyboardMonitor(PortName inPort, std::size_t numKeysPerRow, std::size_t inLowestBit, + const OutPinsSet &outPins, KeyboardStateHandler *handler); + + /** + * @brief Enables the polling of the keyboard state. + * @param pollingPeriod Frequency of the polling ticker (1kHz by default, min 20 microsec). + */ + void start(float pollingPeriod = 0.001); + + /** + * @brief Disables the polling of the keyboard state. + */ + void stop(); + +private: + void timerHandler(); + + PortIn in; + int inBitShift; + OutPinsSet outPins; + KeyboardStateHandler *handler; + + Ticker ticker; + std::size_t scanRow; /*!< next key row to be scanned */ + KeyboardState currentState; /*!< currently being built keyboard state */ +}; + +} // kbd_mgr + +#endif // KEYBOARD_MONITOR_H_