Code for Doug's text entry device: a five-button mouse with shift and space keys.
Dependencies: PinDetect USBDevice mbed
Revision 0:286a51986128, committed 2014-09-21
- Comitter:
- douglasc
- Date:
- Sun Sep 21 19:11:55 2014 +0000
- Commit message:
- Initial commit
Changed in this revision
diff -r 000000000000 -r 286a51986128 KeyManager.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/KeyManager.h Sun Sep 21 19:11:55 2014 +0000 @@ -0,0 +1,140 @@ +/** + * Class KeyManager + * + * This class tracks the state of the seven input + * buttons. It exposes (void) callback methods to + * flag each button as active or not and provides + * a mapping between key combinations and the + * alphabet for text input. + * + */ + +#ifndef KEYMANAGER +#define KEYMANAGER + +#include <string> +#include <ctype.h> + +class KeyManager { +private: + // key definitions + bool keyModifier; + bool keySpace; + bool key0; + bool key1; + bool key2; + bool key3; + bool key4; + + // static character map and constants + static char characters[27]; + static char space; + static char unknown; + +public: + + // constructor + KeyManager() { + keyModifier = false; + keySpace = false; + key0 = false; + key1 = false; + key2 = false; + key3 = false; + key4 = false; + } + + // DEBUG: return the status of which keys are pressed + char* getStatusString() { + std::string status = "the following keys are pressed:"; + if (key0) { + status += " 0"; + } + if (key1) { + status += " 1"; + } + if (key2) { + status += " 2"; + } + if (key3) { + status += " 3"; + } + if (key4) { + status += " 4"; + } + status += "\n"; + return (char*)status.c_str(); + } + + // get the numeric code corresponding to + // the currently pressed keys. + int getCode() { + int result = 0; + if (key0) { + result = result | 1; + } + if (key1) { + result = result | 2; + } + if (key2) { + result = result | 4; + } + if (key3) { + result = result | 8; + } + if (key4) { + result = result | 16; + } + return result; + } + + // map the current code to its character + char getCharacter() { + if (keySpace) { + return space; + } else { + int code = this->getCode(); + if (code < 28 && !keyModifier) { + return characters[code-1]; + } else if (code < 28 && keyModifier) { + return toupper(characters[code-1]); + } else { + return unknown; + } + } + } + + // check whether any of the character keys or the space + // key are pressed + bool keysPressed() { + if (key0 || key1 || key2 || key3 || key4 || keySpace) { + return true; + } + return false; + } + + // Interrupt callback functions + void keyMOn() { keyModifier = true; } + void keyMOff() { keyModifier = false; } + void keySpaceOn() { keySpace = true; } + void keySpaceOff() { keySpace = false; } + void key0On() { key0 = true; } + void key0Off() { key0 = false; } + void key1On() { key1 = true; } + void key1Off() { key1 = false; } + void key2On() { key2 = true; } + void key2Off() { key2 = false; } + void key3On() { key3 = true; } + void key3Off() { key3 = false; } + void key4On() { key4 = true; } + void key4Off() { key4 = false; } +}; + +// define statics +char KeyManager::characters[27] = {'a','b','c','d','e','f','g', + 'h','i','j','k','l','m','n','o','p','q','r','s','t', + 'u','v','w','x','y','z','.' }; +char KeyManager::space = ' '; +char KeyManager::unknown = '?'; + +#endif \ No newline at end of file
diff -r 000000000000 -r 286a51986128 PinDetect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Sun Sep 21 19:11:55 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/douglasc/code/PinDetect/#74bc6d7814e9
diff -r 000000000000 -r 286a51986128 USBDevice.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBDevice.lib Sun Sep 21 19:11:55 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/USBDevice/#5bf05f9b3c7b
diff -r 000000000000 -r 286a51986128 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Sep 21 19:11:55 2014 +0000 @@ -0,0 +1,97 @@ +/*** + * Interactive Device Design, Fall 2014 + * Homework 2 - Text Entry Device + * Doug Cook + * + * This program will setup a 7-button text-entry device + * with 5 keys corresponding to the binary numbers 1 - 32 + * which then map to the alphabet, starting at 1. The + * remaining two keys act as space and shift keys. Extra + * numbers beyond the 26 alpha character map to the + * question mark. This program allows the Freedom KL25Z + * act as a USB Keyboard (using the non-debug USB port). + * + */ + +#include "mbed.h" +#include "PinDetect.h" +#include "KeyManager.h" +#include "USBKeyboard.h" + +// Constants +int PINDETECT_SAMPLE_FREQUENCY = 10000; + +int main() { + // LED configuration + DigitalOut red(LED1); + DigitalOut green(LED2); + DigitalOut blue(LED3); + red = 1; + green = 1; + blue = 1; + + // Push button declarations + PinDetect button6(D11); + PinDetect button5(D10); + PinDetect button4(D9); + PinDetect button3(D8); + PinDetect button2(D7); + PinDetect button1(D6); + PinDetect button0(D5); + + // PC connection + //Serial pc(USBTX, USBRX); + USBKeyboard keyboard; + + // initialize key manager + KeyManager keys = KeyManager(); + + // Initialize push buttons and callbacks + button0.mode(PullUp); + button1.mode(PullUp); + button2.mode(PullUp); + button3.mode(PullUp); + button4.mode(PullUp); + button5.mode(PullUp); + button6.mode(PullUp); + button0.attach_asserted( &keys, &KeyManager::key0Off ); + button0.attach_deasserted( &keys, &KeyManager::key0On ); + button0.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); + button1.attach_asserted( &keys, &KeyManager::key1Off ); + button1.attach_deasserted( &keys, &KeyManager::key1On ); + button1.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); + button2.attach_asserted( &keys, &KeyManager::key2Off ); + button2.attach_deasserted( &keys, &KeyManager::key2On ); + button2.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); + button3.attach_asserted( &keys, &KeyManager::key3Off ); + button3.attach_deasserted( &keys, &KeyManager::key3On ); + button3.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); + button4.attach_asserted( &keys, &KeyManager::key4Off ); + button4.attach_deasserted( &keys, &KeyManager::key4On ); + button4.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); + button5.attach_asserted( &keys, &KeyManager::keySpaceOff ); + button5.attach_deasserted( &keys, &KeyManager::keySpaceOn ); + button5.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); + button6.attach_asserted( &keys, &KeyManager::keyMOff ); + button6.attach_deasserted( &keys, &KeyManager::keyMOn ); + button6.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); + + // listen for keystrokes + while(1) { + if (keys.keysPressed()) { + // wait to read the character in case the user + // intended to press multiple keys and didn't hit + // them all at once. + wait(0.1); + // pc.printf("%c",keys.getCharacter()); // DEBUG + keyboard.printf("%c", keys.getCharacter()); + + // wait a longer period to ensure multiple key + // presses don't trigger unintentional characters. + wait (0.25); + } else { + // cycle again + wait(0.05); + } + } +};
diff -r 000000000000 -r 286a51986128 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Sep 21 19:11:55 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1 \ No newline at end of file