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

Dependents:   KeyboardTest

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?

UserRevisionLine numberNew contents of line
osmeest 3:1310c57aca77 1 #ifndef KEYBOARD_STATE_HANDLER_H_
osmeest 3:1310c57aca77 2 #define KEYBOARD_STATE_HANDLER_H_
osmeest 3:1310c57aca77 3
osmeest 3:1310c57aca77 4 #include "kbd_mgr/KeyboardState.h"
osmeest 3:1310c57aca77 5
osmeest 3:1310c57aca77 6 namespace kbd_mgr {
osmeest 3:1310c57aca77 7
osmeest 3:1310c57aca77 8 /**
osmeest 3:1310c57aca77 9 * @brief Interface used to report a keyboard state.
osmeest 3:1310c57aca77 10 */
osmeest 3:1310c57aca77 11 class KeyboardStateHandler {
osmeest 3:1310c57aca77 12 public:
osmeest 3:1310c57aca77 13 virtual void handleState(const KeyboardState &newState) = 0;
osmeest 3:1310c57aca77 14 virtual ~KeyboardStateHandler() { }
osmeest 3:1310c57aca77 15 };
osmeest 3:1310c57aca77 16
osmeest 3:1310c57aca77 17 template <class T>
osmeest 3:1310c57aca77 18 class MemberKeyboardStateHandler : public KeyboardStateHandler {
osmeest 3:1310c57aca77 19 public:
osmeest 3:1310c57aca77 20 typedef void (T::*MemberFunction)(const KeyboardState &);
osmeest 3:1310c57aca77 21
osmeest 3:1310c57aca77 22 MemberKeyboardStateHandler(T *obj, MemberFunction fn) :
osmeest 3:1310c57aca77 23 object(obj), func(fn)
osmeest 3:1310c57aca77 24 { }
osmeest 3:1310c57aca77 25
osmeest 3:1310c57aca77 26 virtual void handleState(const KeyboardState &newState) {
osmeest 3:1310c57aca77 27 (object->*func)(newState);
osmeest 3:1310c57aca77 28 }
osmeest 3:1310c57aca77 29
osmeest 3:1310c57aca77 30 private:
osmeest 3:1310c57aca77 31 T *object;
osmeest 3:1310c57aca77 32 MemberFunction func;
osmeest 3:1310c57aca77 33 };
osmeest 3:1310c57aca77 34
osmeest 3:1310c57aca77 35 class FunctionKeyboardStateHandler : public KeyboardStateHandler {
osmeest 3:1310c57aca77 36 public:
osmeest 3:1310c57aca77 37 typedef void (*HandlerFunction)(const KeyboardState &);
osmeest 3:1310c57aca77 38
osmeest 3:1310c57aca77 39 FunctionKeyboardStateHandler(HandlerFunction fn) :
osmeest 3:1310c57aca77 40 func(fn)
osmeest 3:1310c57aca77 41 { }
osmeest 3:1310c57aca77 42
osmeest 3:1310c57aca77 43 virtual void handleState(const KeyboardState &newState) {
osmeest 3:1310c57aca77 44 func(newState);
osmeest 3:1310c57aca77 45 }
osmeest 3:1310c57aca77 46
osmeest 3:1310c57aca77 47 private:
osmeest 3:1310c57aca77 48 HandlerFunction func;
osmeest 3:1310c57aca77 49 };
osmeest 3:1310c57aca77 50
osmeest 3:1310c57aca77 51 } // kbd_mgr
osmeest 3:1310c57aca77 52
osmeest 3:1310c57aca77 53 #endif // KEYBOARD_STATE_HANDLER_H_