KeyboardTest: a test program for KeyboardManager lib

Dependencies:   mbed KeyboardManager

Committer:
osmeest
Date:
Thu Feb 03 22:03:24 2011 +0000
Revision:
4:392068d3f56d
Parent:
3:c20e3d23ce5f
adapt to latest improvements in the lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
osmeest 3:c20e3d23ce5f 1 #include "kbd_mgr/KeyboardManager.h"
osmeest 3:c20e3d23ce5f 2
osmeest 3:c20e3d23ce5f 3 #include <iostream>
osmeest 3:c20e3d23ce5f 4
osmeest 3:c20e3d23ce5f 5 using namespace std;
osmeest 3:c20e3d23ce5f 6 using namespace kbd_mgr;
osmeest 0:ff6e0ea888e8 7
osmeest 4:392068d3f56d 8 void handleKeyPress(const KeyEvent &keypress) {
osmeest 4:392068d3f56d 9 char ch = (keypress.keyChar >= 32 && keypress.keyChar < 127 ? keypress.keyChar : '?');
osmeest 4:392068d3f56d 10
osmeest 4:392068d3f56d 11 std::string eventName;
osmeest 4:392068d3f56d 12 switch (keypress.event) {
osmeest 4:392068d3f56d 13 case KeyEvent::KeyDown:
osmeest 4:392068d3f56d 14 eventName = "Down";
osmeest 4:392068d3f56d 15 break;
osmeest 4:392068d3f56d 16 case KeyEvent::KeyUp:
osmeest 4:392068d3f56d 17 eventName = "Up";
osmeest 4:392068d3f56d 18 break;
osmeest 4:392068d3f56d 19 case KeyEvent::KeyPress:
osmeest 4:392068d3f56d 20 eventName = "Press";
osmeest 4:392068d3f56d 21 break;
osmeest 4:392068d3f56d 22 case KeyEvent::RepeatedKeyPress:
osmeest 4:392068d3f56d 23 eventName = "RepeatedPress";
osmeest 4:392068d3f56d 24 break;
osmeest 4:392068d3f56d 25 case KeyEvent::LongKeyPress:
osmeest 4:392068d3f56d 26 eventName = "LongPress";
osmeest 4:392068d3f56d 27 break;
osmeest 4:392068d3f56d 28 default:
osmeest 4:392068d3f56d 29 eventName = "???";
osmeest 4:392068d3f56d 30 break;
osmeest 3:c20e3d23ce5f 31 }
osmeest 4:392068d3f56d 32
osmeest 4:392068d3f56d 33 cout << "Key state change: " << keypress.keyCode << " '" << ch << "' " << eventName << "\r" << endl;
osmeest 4:392068d3f56d 34 }
osmeest 0:ff6e0ea888e8 35
osmeest 0:ff6e0ea888e8 36 int main() {
osmeest 3:c20e3d23ce5f 37 cout << "\n\r\nKeyboardTest " __DATE__ "-" __TIME__ "\r" << endl;
osmeest 3:c20e3d23ce5f 38
osmeest 3:c20e3d23ce5f 39 PinName outPinsArray[] = { p8, p7, p6, p5 };
osmeest 4:392068d3f56d 40 KeyboardMonitor::OutPinsSet outPins(&outPinsArray[0], &outPinsArray[4]);
osmeest 4:392068d3f56d 41
osmeest 4:392068d3f56d 42 KeyMap keyMap("123A456B789C*0#D");
osmeest 4:392068d3f56d 43 keyMap(KeyEvent::LongKeyPress, 12, '@')(KeyEvent::LongKeyPress, 14, '$');
osmeest 4:392068d3f56d 44
osmeest 4:392068d3f56d 45 KeyboardMonitor kbdMonitor(Port0, 4, 15, outPins);
osmeest 4:392068d3f56d 46 KeyboardStateChangeMonitor changeMonitor;
osmeest 4:392068d3f56d 47 SingleKeyPressMonitor keyPressMonitor;
osmeest 4:392068d3f56d 48 LongKeyPressMonitor longKeyPressMonitor;
osmeest 4:392068d3f56d 49 KeyMapper keyMapper(keyMap);
osmeest 3:c20e3d23ce5f 50
osmeest 4:392068d3f56d 51 longKeyPressMonitor.autoRepeat(0.500,0.250)(0,15);
osmeest 4:392068d3f56d 52 longKeyPressMonitor.longKeyPress(0.500)(12)(14);
osmeest 0:ff6e0ea888e8 53
osmeest 4:392068d3f56d 54 keyMapper.attach(handleKeyPress);
osmeest 4:392068d3f56d 55 longKeyPressMonitor.attach(keyMapper);
osmeest 4:392068d3f56d 56 keyPressMonitor.attach(longKeyPressMonitor);
osmeest 4:392068d3f56d 57 changeMonitor.attach(keyPressMonitor);
osmeest 4:392068d3f56d 58 kbdMonitor.attach(changeMonitor);
osmeest 4:392068d3f56d 59
osmeest 4:392068d3f56d 60 kbdMonitor.start();
osmeest 4:392068d3f56d 61
osmeest 4:392068d3f56d 62 while (1) {
osmeest 0:ff6e0ea888e8 63 wait(10);
osmeest 0:ff6e0ea888e8 64 }
osmeest 0:ff6e0ea888e8 65 }