IDD Fall 2015 / Mbed 2 deprecated idd_hw2_gtfierro_keypad

Dependencies:   USBDevice mbed

Fork of hw2 by Gabriel Fierro

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 #include "USBKeyboard.h"
00004 #include <string>
00005 #include <ctype.h>
00006 
00007 USBKeyboard key;
00008 
00009 Serial pc(USBTX, USBRX);
00010 
00011 PinDetect b1( D13 , PullUp);
00012 PinDetect b2( D12 , PullUp);
00013 PinDetect b3( D11 , PullUp);
00014 PinDetect b4( D10 , PullUp);
00015 PinDetect b5( D9 , PullUp);
00016 PinDetect b6( D8 , PullUp);
00017 PinDetect b7( D7 , PullUp);
00018 PinDetect b8( D6 , PullUp);
00019 PinDetect b9( D5 , PullUp);
00020 
00021 PinDetect shift( D4 , PullUp);
00022 
00023 DigitalOut green(LED_GREEN);
00024 DigitalOut red(LED_RED);
00025 DigitalOut blue(LED_BLUE);
00026 
00027 int pressed[2];         // 1   2   3   4   5   6   7   8   9
00028 const char* button1[9] = {"a", "", "","v", "", "", "", "", ""};
00029 const char* button2[9] = { "","n", "", "","l", "", "", "", ""};
00030 const char* button3[9] = { "", "","i", "", "","x", "", "", ""};
00031 const char* button4[9] = {"q", "", "","h","k", "","g", "", ""};
00032 const char* button5[9] = { "","u", "","c","o","b", "","d", ""};
00033 const char* button6[9] = { "", "","p", "","m","r", "", "","j"};
00034 const char* button7[9] = { "", "", "","y", "", "","t", "", ""};
00035 const char* button8[9] = { "", "", "", "","w", "","\b","e"," "};
00036 const char* button9[9] = { "", "", "", "", "","f", "","z","s"};
00037 
00038 bool upper = false;
00039 
00040 #define OFF 1
00041 #define ON 0
00042 
00043 
00044 void initializePressed() {
00045     pressed[0] = 0;
00046     pressed[1] = 0;
00047 }
00048 
00049 void deliverButton() {
00050     const char* x;
00051     switch (pressed[0]) {
00052     case 1:
00053         x = button1[pressed[1]-1];
00054         break;
00055     case 2:
00056         x = button2[pressed[1]-1];
00057         break;
00058     case 3:
00059         x = button3[pressed[1]-1];
00060         break;
00061     case 4:
00062         x = button4[pressed[1]-1];
00063         break;
00064     case 5:
00065         x = button5[pressed[1]-1];
00066         break;
00067     case 6:
00068         x = button6[pressed[1]-1];
00069         break;
00070     case 7:
00071         x = button7[pressed[1]-1];
00072         break;
00073     case 8:
00074         x = button8[pressed[1]-1];
00075         break;
00076     case 9:
00077         x = button9[pressed[1]-1];
00078         break;
00079     default:
00080         printf("pressed[0] was not 1\n");
00081         return;
00082     }
00083     if (upper) {
00084         pc.putc(toupper(x[0]));
00085         key.putc(toupper(x[0]));
00086     } else {
00087         pc.putc(x[0]);
00088         key.putc(x[0]);
00089         
00090     }
00091 }
00092 
00093 void fillButton(int button) {
00094     if (pressed[0] != 0 && pressed[1] == 0) {
00095         pressed[1] = button;
00096     } else if (pressed[0] == 0) {
00097         pressed[0] = button;
00098     } else {
00099         printf("WHAT\n");
00100     }
00101 }
00102 
00103 void b1Pressed(void) { fillButton(1); }
00104 void b2Pressed(void) { fillButton(2); }
00105 void b3Pressed(void) { fillButton(3); }
00106 void b4Pressed(void) { fillButton(4); }
00107 void b5Pressed(void) { fillButton(5); }
00108 void b6Pressed(void) { fillButton(6); }
00109 void b7Pressed(void) { fillButton(7); }
00110 void b8Pressed(void) { fillButton(8); }
00111 void b9Pressed(void) { fillButton(9); }
00112 
00113 void shiftOn(void) { green = ON; upper = true; }
00114 void shiftOff(void) { green = OFF; upper = false; }
00115 
00116 int main() {
00117     // turn off LED
00118     green = OFF;
00119     red = OFF;
00120     blue = OFF;
00121 
00122     initializePressed();
00123 
00124     b1.attach_asserted(&b1Pressed);
00125     b1.setAssertValue(0);
00126     b1.setSampleFrequency();
00127 
00128     b2.attach_asserted(&b2Pressed);
00129     b2.setAssertValue(0);
00130     b2.setSampleFrequency();
00131 
00132     b3.attach_asserted(&b3Pressed);
00133     b3.setAssertValue(0);
00134     b3.setSampleFrequency();
00135 
00136     b4.attach_asserted(&b4Pressed);
00137     b4.setAssertValue(0);
00138     b4.setSampleFrequency();
00139 
00140     b5.attach_asserted(&b5Pressed);
00141     b5.setAssertValue(0);
00142     b5.setSampleFrequency();
00143 
00144     b6.attach_asserted(&b6Pressed);
00145     b6.setAssertValue(0);
00146     b6.setSampleFrequency();
00147 
00148     b7.attach_asserted(&b7Pressed);
00149     b7.setAssertValue(0);
00150     b7.setSampleFrequency();
00151 
00152     b8.attach_asserted(&b8Pressed);
00153     b8.setAssertValue(0);
00154     b8.setSampleFrequency();
00155 
00156     b9.attach_asserted(&b9Pressed);
00157     b9.setAssertValue(0);
00158     b9.setSampleFrequency();
00159 
00160     shift.attach_asserted(&shiftOn);
00161     shift.attach_deasserted(&shiftOff);
00162     shift.setSampleFrequency();
00163 
00164     while(1) {
00165         if (pressed[0] != 0 && pressed[1] != 0) {
00166             deliverButton();
00167             initializePressed();
00168         }
00169     }
00170 }