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
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_EVENT_SERVER_H_
osmeest 3:1310c57aca77 2 #define KEYBOARD_EVENT_SERVER_H_
osmeest 3:1310c57aca77 3
osmeest 3:1310c57aca77 4 #include <cstdlib>
osmeest 3:1310c57aca77 5
osmeest 3:1310c57aca77 6 namespace kbd_mgr {
osmeest 3:1310c57aca77 7
osmeest 3:1310c57aca77 8 template <class HandlerClass>
osmeest 3:1310c57aca77 9 class KeyboardEventServer {
osmeest 3:1310c57aca77 10 protected:
osmeest 3:1310c57aca77 11 KeyboardEventServer() : handler_(NULL), ownHandler_(false) { }
osmeest 3:1310c57aca77 12
osmeest 3:1310c57aca77 13 ~KeyboardEventServer()
osmeest 3:1310c57aca77 14 {
osmeest 3:1310c57aca77 15 clearHandler();
osmeest 3:1310c57aca77 16 }
osmeest 3:1310c57aca77 17
osmeest 3:1310c57aca77 18 void setHandler(HandlerClass *h, bool owner = true) {
osmeest 3:1310c57aca77 19 this->handler_ = h;
osmeest 3:1310c57aca77 20 this->ownHandler_ = owner;
osmeest 3:1310c57aca77 21 }
osmeest 3:1310c57aca77 22
osmeest 3:1310c57aca77 23 void clearHandler() {
osmeest 3:1310c57aca77 24 if (this->ownHandler_) {
osmeest 3:1310c57aca77 25 delete this->handler_;
osmeest 3:1310c57aca77 26 }
osmeest 3:1310c57aca77 27 this->handler_ = NULL;
osmeest 3:1310c57aca77 28 this->ownHandler_ = false;
osmeest 3:1310c57aca77 29 }
osmeest 3:1310c57aca77 30
osmeest 3:1310c57aca77 31 HandlerClass *handler() const { return this->handler_; }
osmeest 3:1310c57aca77 32
osmeest 3:1310c57aca77 33 bool hasHandler() const { return this->handler_ != NULL; }
osmeest 3:1310c57aca77 34
osmeest 3:1310c57aca77 35 public:
osmeest 3:1310c57aca77 36 void attach(HandlerClass *handler) {
osmeest 3:1310c57aca77 37 setHandler(handler, false);
osmeest 3:1310c57aca77 38 }
osmeest 3:1310c57aca77 39 void attach(HandlerClass &handler) {
osmeest 3:1310c57aca77 40 setHandler(&handler, false);
osmeest 3:1310c57aca77 41 }
osmeest 3:1310c57aca77 42
osmeest 3:1310c57aca77 43 void detach() {
osmeest 3:1310c57aca77 44 clearHandler();
osmeest 3:1310c57aca77 45 }
osmeest 3:1310c57aca77 46
osmeest 3:1310c57aca77 47 private:
osmeest 3:1310c57aca77 48 HandlerClass *handler_;
osmeest 3:1310c57aca77 49 bool ownHandler_;
osmeest 3:1310c57aca77 50 };
osmeest 3:1310c57aca77 51
osmeest 3:1310c57aca77 52 } // kbd_mgr
osmeest 3:1310c57aca77 53
osmeest 3:1310c57aca77 54 #endif // KEYBOARD_EVENT_SERVER_H_