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

Dependents:   KeyboardTest

Revision:
2:eb4cc53ff33d
Child:
3:1310c57aca77
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kbd_mgr/KeyboardManager.h	Sun Jan 23 23:15:36 2011 +0000
@@ -0,0 +1,52 @@
+#ifndef KEYBOARD_MANAGER_HPP_
+#define KEYBOARD_MANAGER_HPP_
+
+#include "kbd_mgr/KeyPressEventHandler.h"
+
+#include "mbed.h"
+#include <memory>
+#include <vector>
+
+namespace kbd_mgr {
+
+/**
+ * @brief A class that polls the state of a NxM 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.
+ * Each time a key press or a key release is detected, the specified event handler is invoked.
+ * As the managed switch matrix is not protected against cross-overs, multiple key presses are
+ * ignored (only the state changes of the first key pressed are reported).
+ */
+class KeyboardManager {
+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 key presses and releases.
+     */
+    KeyboardManager(PortName inPort, std::size_t numKeysPerRow, int inLowestBit, 
+            const OutPinsSet &outPins, KeyPressEventHandler *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 = 1e-3);
+    
+    /**
+     * @brief Disables the polling of the keyboard state.
+     */
+    void stop();
+    
+private:
+    struct Impl; // opaque structure hiding implementation details
+    std::auto_ptr<Impl> pimpl;
+};
+
+} // kbd_mgr
+
+#endif // KEYBOARD_MANAGER_HPP_