Code for Doug's text entry device: a five-button mouse with shift and space keys.

Dependencies:   PinDetect USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /***
00002  * Interactive Device Design, Fall 2014
00003  * Homework 2 - Text Entry Device
00004  * Doug Cook
00005  *
00006  * This program will setup a 7-button text-entry device
00007  * with 5 keys corresponding to the binary numbers 1 - 32
00008  * which then map to the alphabet, starting at 1. The
00009  * remaining two keys act as space and shift keys. Extra
00010  * numbers beyond the 26 alpha character map to the
00011  * question mark. This program allows the Freedom KL25Z
00012  * act as a USB Keyboard (using the non-debug USB port).
00013  *
00014  */
00015 
00016 #include "mbed.h"
00017 #include "PinDetect.h"
00018 #include "KeyManager.h"
00019 #include "USBKeyboard.h"
00020 
00021 // Constants
00022 int PINDETECT_SAMPLE_FREQUENCY = 10000;
00023 
00024 int main() {
00025     // LED configuration
00026     DigitalOut red(LED1);
00027     DigitalOut green(LED2);
00028     DigitalOut blue(LED3);
00029     red = 1;
00030     green = 1;
00031     blue = 1;
00032 
00033     // Push button declarations
00034     PinDetect button6(D11);
00035     PinDetect button5(D10);
00036     PinDetect button4(D9);
00037     PinDetect button3(D8);
00038     PinDetect button2(D7);
00039     PinDetect button1(D6);
00040     PinDetect button0(D5);
00041     
00042     // PC connection
00043     //Serial pc(USBTX, USBRX);
00044     USBKeyboard keyboard;
00045     
00046     // initialize key manager
00047     KeyManager keys = KeyManager();
00048     
00049     // Initialize push buttons and callbacks
00050     button0.mode(PullUp);
00051     button1.mode(PullUp);
00052     button2.mode(PullUp);
00053     button3.mode(PullUp);
00054     button4.mode(PullUp);
00055     button5.mode(PullUp);
00056     button6.mode(PullUp);
00057     button0.attach_asserted( &keys, &KeyManager::key0Off );
00058     button0.attach_deasserted( &keys, &KeyManager::key0On );
00059     button0.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY );
00060     button1.attach_asserted( &keys, &KeyManager::key1Off );
00061     button1.attach_deasserted( &keys, &KeyManager::key1On );
00062     button1.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY );
00063     button2.attach_asserted( &keys, &KeyManager::key2Off );
00064     button2.attach_deasserted( &keys, &KeyManager::key2On );
00065     button2.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY );
00066     button3.attach_asserted( &keys, &KeyManager::key3Off );
00067     button3.attach_deasserted( &keys, &KeyManager::key3On );
00068     button3.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY );
00069     button4.attach_asserted( &keys, &KeyManager::key4Off );
00070     button4.attach_deasserted( &keys, &KeyManager::key4On );
00071     button4.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY );
00072     button5.attach_asserted( &keys, &KeyManager::keySpaceOff );
00073     button5.attach_deasserted( &keys, &KeyManager::keySpaceOn );
00074     button5.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY );
00075     button6.attach_asserted( &keys, &KeyManager::keyMOff );
00076     button6.attach_deasserted( &keys, &KeyManager::keyMOn );
00077     button6.setSampleFrequency( PINDETECT_SAMPLE_FREQUENCY );
00078 
00079     // listen for keystrokes
00080     while(1) {
00081         if (keys.keysPressed()) {
00082             // wait to read the character in case the user 
00083             // intended to press multiple keys and didn't hit
00084             // them all at once.
00085             wait(0.1);
00086             // pc.printf("%c",keys.getCharacter());   // DEBUG
00087             keyboard.printf("%c", keys.getCharacter());
00088             
00089             // wait a longer period to ensure multiple key
00090             // presses don't trigger unintentional characters.
00091             wait (0.25);
00092         } else {
00093             // cycle again
00094             wait(0.05);
00095         }
00096     }
00097 };