Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: PinDetect USBDevice mbed
main.cpp@0:286a51986128, 2014-09-21 (annotated)
- Committer:
- douglasc
- Date:
- Sun Sep 21 19:11:55 2014 +0000
- Revision:
- 0:286a51986128
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| douglasc | 0:286a51986128 | 1 | /*** |
| douglasc | 0:286a51986128 | 2 | * Interactive Device Design, Fall 2014 |
| douglasc | 0:286a51986128 | 3 | * Homework 2 - Text Entry Device |
| douglasc | 0:286a51986128 | 4 | * Doug Cook |
| douglasc | 0:286a51986128 | 5 | * |
| douglasc | 0:286a51986128 | 6 | * This program will setup a 7-button text-entry device |
| douglasc | 0:286a51986128 | 7 | * with 5 keys corresponding to the binary numbers 1 - 32 |
| douglasc | 0:286a51986128 | 8 | * which then map to the alphabet, starting at 1. The |
| douglasc | 0:286a51986128 | 9 | * remaining two keys act as space and shift keys. Extra |
| douglasc | 0:286a51986128 | 10 | * numbers beyond the 26 alpha character map to the |
| douglasc | 0:286a51986128 | 11 | * question mark. This program allows the Freedom KL25Z |
| douglasc | 0:286a51986128 | 12 | * act as a USB Keyboard (using the non-debug USB port). |
| douglasc | 0:286a51986128 | 13 | * |
| douglasc | 0:286a51986128 | 14 | */ |
| douglasc | 0:286a51986128 | 15 | |
| douglasc | 0:286a51986128 | 16 | #include "mbed.h" |
| douglasc | 0:286a51986128 | 17 | #include "PinDetect.h" |
| douglasc | 0:286a51986128 | 18 | #include "KeyManager.h" |
| douglasc | 0:286a51986128 | 19 | #include "USBKeyboard.h" |
| douglasc | 0:286a51986128 | 20 | |
| douglasc | 0:286a51986128 | 21 | // Constants |
| douglasc | 0:286a51986128 | 22 | int PINDETECT_SAMPLE_FREQUENCY = 10000; |
| douglasc | 0:286a51986128 | 23 | |
| douglasc | 0:286a51986128 | 24 | int main() { |
| douglasc | 0:286a51986128 | 25 | // LED configuration |
| douglasc | 0:286a51986128 | 26 | DigitalOut red(LED1); |
| douglasc | 0:286a51986128 | 27 | DigitalOut green(LED2); |
| douglasc | 0:286a51986128 | 28 | DigitalOut blue(LED3); |
| douglasc | 0:286a51986128 | 29 | red = 1; |
| douglasc | 0:286a51986128 | 30 | green = 1; |
| douglasc | 0:286a51986128 | 31 | blue = 1; |
| douglasc | 0:286a51986128 | 32 | |
| douglasc | 0:286a51986128 | 33 | // Push button declarations |
| douglasc | 0:286a51986128 | 34 | PinDetect button6(D11); |
| douglasc | 0:286a51986128 | 35 | PinDetect button5(D10); |
| douglasc | 0:286a51986128 | 36 | PinDetect button4(D9); |
| douglasc | 0:286a51986128 | 37 | PinDetect button3(D8); |
| douglasc | 0:286a51986128 | 38 | PinDetect button2(D7); |
| douglasc | 0:286a51986128 | 39 | PinDetect button1(D6); |
| douglasc | 0:286a51986128 | 40 | PinDetect button0(D5); |
| douglasc | 0:286a51986128 | 41 | |
| douglasc | 0:286a51986128 | 42 | // PC connection |
| douglasc | 0:286a51986128 | 43 | //Serial pc(USBTX, USBRX); |
| douglasc | 0:286a51986128 | 44 | USBKeyboard keyboard; |
| douglasc | 0:286a51986128 | 45 | |
| douglasc | 0:286a51986128 | 46 | // initialize key manager |
| douglasc | 0:286a51986128 | 47 | KeyManager keys = KeyManager(); |
| douglasc | 0:286a51986128 | 48 | |
| douglasc | 0:286a51986128 | 49 | // Initialize push buttons and callbacks |
| douglasc | 0:286a51986128 | 50 | button0.mode(PullUp); |
| douglasc | 0:286a51986128 | 51 | button1.mode(PullUp); |
| douglasc | 0:286a51986128 | 52 | button2.mode(PullUp); |
| douglasc | 0:286a51986128 | 53 | button3.mode(PullUp); |
| douglasc | 0:286a51986128 | 54 | button4.mode(PullUp); |
| douglasc | 0:286a51986128 | 55 | button5.mode(PullUp); |
| douglasc | 0:286a51986128 | 56 | button6.mode(PullUp); |
| douglasc | 0:286a51986128 | 57 | button0.attach_asserted( &keys, &KeyManager::key0Off ); |
| douglasc | 0:286a51986128 | 58 | button0.attach_deasserted( &keys, &KeyManager::key0On ); |
| douglasc | 0:286a51986128 | 59 | button0.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); |
| douglasc | 0:286a51986128 | 60 | button1.attach_asserted( &keys, &KeyManager::key1Off ); |
| douglasc | 0:286a51986128 | 61 | button1.attach_deasserted( &keys, &KeyManager::key1On ); |
| douglasc | 0:286a51986128 | 62 | button1.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); |
| douglasc | 0:286a51986128 | 63 | button2.attach_asserted( &keys, &KeyManager::key2Off ); |
| douglasc | 0:286a51986128 | 64 | button2.attach_deasserted( &keys, &KeyManager::key2On ); |
| douglasc | 0:286a51986128 | 65 | button2.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); |
| douglasc | 0:286a51986128 | 66 | button3.attach_asserted( &keys, &KeyManager::key3Off ); |
| douglasc | 0:286a51986128 | 67 | button3.attach_deasserted( &keys, &KeyManager::key3On ); |
| douglasc | 0:286a51986128 | 68 | button3.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); |
| douglasc | 0:286a51986128 | 69 | button4.attach_asserted( &keys, &KeyManager::key4Off ); |
| douglasc | 0:286a51986128 | 70 | button4.attach_deasserted( &keys, &KeyManager::key4On ); |
| douglasc | 0:286a51986128 | 71 | button4.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); |
| douglasc | 0:286a51986128 | 72 | button5.attach_asserted( &keys, &KeyManager::keySpaceOff ); |
| douglasc | 0:286a51986128 | 73 | button5.attach_deasserted( &keys, &KeyManager::keySpaceOn ); |
| douglasc | 0:286a51986128 | 74 | button5.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); |
| douglasc | 0:286a51986128 | 75 | button6.attach_asserted( &keys, &KeyManager::keyMOff ); |
| douglasc | 0:286a51986128 | 76 | button6.attach_deasserted( &keys, &KeyManager::keyMOn ); |
| douglasc | 0:286a51986128 | 77 | button6.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY ); |
| douglasc | 0:286a51986128 | 78 | |
| douglasc | 0:286a51986128 | 79 | // listen for keystrokes |
| douglasc | 0:286a51986128 | 80 | while(1) { |
| douglasc | 0:286a51986128 | 81 | if (keys.keysPressed()) { |
| douglasc | 0:286a51986128 | 82 | // wait to read the character in case the user |
| douglasc | 0:286a51986128 | 83 | // intended to press multiple keys and didn't hit |
| douglasc | 0:286a51986128 | 84 | // them all at once. |
| douglasc | 0:286a51986128 | 85 | wait(0.1); |
| douglasc | 0:286a51986128 | 86 | // pc.printf("%c",keys.getCharacter()); // DEBUG |
| douglasc | 0:286a51986128 | 87 | keyboard.printf("%c", keys.getCharacter()); |
| douglasc | 0:286a51986128 | 88 | |
| douglasc | 0:286a51986128 | 89 | // wait a longer period to ensure multiple key |
| douglasc | 0:286a51986128 | 90 | // presses don't trigger unintentional characters. |
| douglasc | 0:286a51986128 | 91 | wait (0.25); |
| douglasc | 0:286a51986128 | 92 | } else { |
| douglasc | 0:286a51986128 | 93 | // cycle again |
| douglasc | 0:286a51986128 | 94 | wait(0.05); |
| douglasc | 0:286a51986128 | 95 | } |
| douglasc | 0:286a51986128 | 96 | } |
| douglasc | 0:286a51986128 | 97 | }; |