![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Code for proj2
Fork of hw2 by
main.cpp@3:636bf7891502, 2015-09-12 (annotated)
- Committer:
- gtfierro
- Date:
- Sat Sep 12 21:30:25 2015 +0000
- Revision:
- 3:636bf7891502
- Parent:
- 1:33d22b2c88ad
latest
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gtfierro | 0:9b39c62126ea | 1 | #include "mbed.h" |
gtfierro | 1:33d22b2c88ad | 2 | #include "PinDetect.h" |
gtfierro | 1:33d22b2c88ad | 3 | #include "USBKeyboard.h" |
gtfierro | 1:33d22b2c88ad | 4 | #include <string> |
gtfierro | 3:636bf7891502 | 5 | #include <ctype.h> |
gtfierro | 3:636bf7891502 | 6 | |
gtfierro | 3:636bf7891502 | 7 | USBKeyboard key; |
gtfierro | 1:33d22b2c88ad | 8 | |
gtfierro | 1:33d22b2c88ad | 9 | Serial pc(USBTX, USBRX); |
gtfierro | 1:33d22b2c88ad | 10 | |
gtfierro | 1:33d22b2c88ad | 11 | PinDetect b1( D13 , PullUp); |
gtfierro | 1:33d22b2c88ad | 12 | PinDetect b2( D12 , PullUp); |
gtfierro | 1:33d22b2c88ad | 13 | PinDetect b3( D11 , PullUp); |
gtfierro | 1:33d22b2c88ad | 14 | PinDetect b4( D10 , PullUp); |
gtfierro | 1:33d22b2c88ad | 15 | PinDetect b5( D9 , PullUp); |
gtfierro | 1:33d22b2c88ad | 16 | PinDetect b6( D8 , PullUp); |
gtfierro | 1:33d22b2c88ad | 17 | PinDetect b7( D7 , PullUp); |
gtfierro | 1:33d22b2c88ad | 18 | PinDetect b8( D6 , PullUp); |
gtfierro | 1:33d22b2c88ad | 19 | PinDetect b9( D5 , PullUp); |
gtfierro | 1:33d22b2c88ad | 20 | |
gtfierro | 1:33d22b2c88ad | 21 | PinDetect shift( D4 , PullUp); |
gtfierro | 1:33d22b2c88ad | 22 | |
gtfierro | 1:33d22b2c88ad | 23 | DigitalOut green(LED_GREEN); |
gtfierro | 1:33d22b2c88ad | 24 | DigitalOut red(LED_RED); |
gtfierro | 1:33d22b2c88ad | 25 | DigitalOut blue(LED_BLUE); |
gtfierro | 1:33d22b2c88ad | 26 | |
gtfierro | 1:33d22b2c88ad | 27 | int pressed[2]; // 1 2 3 4 5 6 7 8 9 |
gtfierro | 1:33d22b2c88ad | 28 | const char* button1[9] = {"a", "", "","v", "", "", "", "", ""}; |
gtfierro | 1:33d22b2c88ad | 29 | const char* button2[9] = { "","n", "", "","l", "", "", "", ""}; |
gtfierro | 1:33d22b2c88ad | 30 | const char* button3[9] = { "", "","i", "", "","x", "", "", ""}; |
gtfierro | 1:33d22b2c88ad | 31 | const char* button4[9] = {"q", "", "","h","k", "","g", "", ""}; |
gtfierro | 1:33d22b2c88ad | 32 | const char* button5[9] = { "","u", "","c","o","b", "","d", ""}; |
gtfierro | 1:33d22b2c88ad | 33 | const char* button6[9] = { "", "","p", "","m","r", "", "","j"}; |
gtfierro | 1:33d22b2c88ad | 34 | const char* button7[9] = { "", "", "","y", "", "","t", "", ""}; |
gtfierro | 1:33d22b2c88ad | 35 | const char* button8[9] = { "", "", "", "","w", "","\b","e"," "}; |
gtfierro | 1:33d22b2c88ad | 36 | const char* button9[9] = { "", "", "", "", "","f", "","z","s"}; |
gtfierro | 1:33d22b2c88ad | 37 | |
gtfierro | 1:33d22b2c88ad | 38 | bool upper = false; |
gtfierro | 1:33d22b2c88ad | 39 | |
gtfierro | 1:33d22b2c88ad | 40 | #define OFF 1 |
gtfierro | 1:33d22b2c88ad | 41 | #define ON 0 |
gtfierro | 1:33d22b2c88ad | 42 | |
gtfierro | 3:636bf7891502 | 43 | |
gtfierro | 1:33d22b2c88ad | 44 | void initializePressed() { |
gtfierro | 1:33d22b2c88ad | 45 | pressed[0] = 0; |
gtfierro | 1:33d22b2c88ad | 46 | pressed[1] = 0; |
gtfierro | 1:33d22b2c88ad | 47 | } |
gtfierro | 0:9b39c62126ea | 48 | |
gtfierro | 1:33d22b2c88ad | 49 | void deliverButton() { |
gtfierro | 1:33d22b2c88ad | 50 | const char* x; |
gtfierro | 1:33d22b2c88ad | 51 | switch (pressed[0]) { |
gtfierro | 1:33d22b2c88ad | 52 | case 1: |
gtfierro | 1:33d22b2c88ad | 53 | x = button1[pressed[1]-1]; |
gtfierro | 1:33d22b2c88ad | 54 | break; |
gtfierro | 1:33d22b2c88ad | 55 | case 2: |
gtfierro | 1:33d22b2c88ad | 56 | x = button2[pressed[1]-1]; |
gtfierro | 1:33d22b2c88ad | 57 | break; |
gtfierro | 1:33d22b2c88ad | 58 | case 3: |
gtfierro | 1:33d22b2c88ad | 59 | x = button3[pressed[1]-1]; |
gtfierro | 1:33d22b2c88ad | 60 | break; |
gtfierro | 1:33d22b2c88ad | 61 | case 4: |
gtfierro | 1:33d22b2c88ad | 62 | x = button4[pressed[1]-1]; |
gtfierro | 1:33d22b2c88ad | 63 | break; |
gtfierro | 1:33d22b2c88ad | 64 | case 5: |
gtfierro | 1:33d22b2c88ad | 65 | x = button5[pressed[1]-1]; |
gtfierro | 1:33d22b2c88ad | 66 | break; |
gtfierro | 1:33d22b2c88ad | 67 | case 6: |
gtfierro | 1:33d22b2c88ad | 68 | x = button6[pressed[1]-1]; |
gtfierro | 1:33d22b2c88ad | 69 | break; |
gtfierro | 1:33d22b2c88ad | 70 | case 7: |
gtfierro | 1:33d22b2c88ad | 71 | x = button7[pressed[1]-1]; |
gtfierro | 1:33d22b2c88ad | 72 | break; |
gtfierro | 1:33d22b2c88ad | 73 | case 8: |
gtfierro | 1:33d22b2c88ad | 74 | x = button8[pressed[1]-1]; |
gtfierro | 1:33d22b2c88ad | 75 | break; |
gtfierro | 1:33d22b2c88ad | 76 | case 9: |
gtfierro | 1:33d22b2c88ad | 77 | x = button9[pressed[1]-1]; |
gtfierro | 1:33d22b2c88ad | 78 | break; |
gtfierro | 1:33d22b2c88ad | 79 | default: |
gtfierro | 1:33d22b2c88ad | 80 | printf("pressed[0] was not 1\n"); |
gtfierro | 1:33d22b2c88ad | 81 | return; |
gtfierro | 1:33d22b2c88ad | 82 | } |
gtfierro | 1:33d22b2c88ad | 83 | if (upper) { |
gtfierro | 3:636bf7891502 | 84 | pc.putc(toupper(x[0])); |
gtfierro | 3:636bf7891502 | 85 | key.putc(toupper(x[0])); |
gtfierro | 1:33d22b2c88ad | 86 | } else { |
gtfierro | 1:33d22b2c88ad | 87 | pc.putc(x[0]); |
gtfierro | 3:636bf7891502 | 88 | key.putc(x[0]); |
gtfierro | 3:636bf7891502 | 89 | |
gtfierro | 1:33d22b2c88ad | 90 | } |
gtfierro | 1:33d22b2c88ad | 91 | } |
gtfierro | 1:33d22b2c88ad | 92 | |
gtfierro | 1:33d22b2c88ad | 93 | void fillButton(int button) { |
gtfierro | 1:33d22b2c88ad | 94 | if (pressed[0] != 0 && pressed[1] == 0) { |
gtfierro | 1:33d22b2c88ad | 95 | pressed[1] = button; |
gtfierro | 1:33d22b2c88ad | 96 | } else if (pressed[0] == 0) { |
gtfierro | 1:33d22b2c88ad | 97 | pressed[0] = button; |
gtfierro | 1:33d22b2c88ad | 98 | } else { |
gtfierro | 1:33d22b2c88ad | 99 | printf("WHAT\n"); |
gtfierro | 1:33d22b2c88ad | 100 | } |
gtfierro | 1:33d22b2c88ad | 101 | } |
gtfierro | 1:33d22b2c88ad | 102 | |
gtfierro | 1:33d22b2c88ad | 103 | void b1Pressed(void) { fillButton(1); } |
gtfierro | 1:33d22b2c88ad | 104 | void b2Pressed(void) { fillButton(2); } |
gtfierro | 1:33d22b2c88ad | 105 | void b3Pressed(void) { fillButton(3); } |
gtfierro | 1:33d22b2c88ad | 106 | void b4Pressed(void) { fillButton(4); } |
gtfierro | 1:33d22b2c88ad | 107 | void b5Pressed(void) { fillButton(5); } |
gtfierro | 1:33d22b2c88ad | 108 | void b6Pressed(void) { fillButton(6); } |
gtfierro | 1:33d22b2c88ad | 109 | void b7Pressed(void) { fillButton(7); } |
gtfierro | 1:33d22b2c88ad | 110 | void b8Pressed(void) { fillButton(8); } |
gtfierro | 1:33d22b2c88ad | 111 | void b9Pressed(void) { fillButton(9); } |
gtfierro | 1:33d22b2c88ad | 112 | |
gtfierro | 3:636bf7891502 | 113 | void shiftOn(void) { green = ON; upper = true; } |
gtfierro | 3:636bf7891502 | 114 | void shiftOff(void) { green = OFF; upper = false; } |
gtfierro | 0:9b39c62126ea | 115 | |
gtfierro | 0:9b39c62126ea | 116 | int main() { |
gtfierro | 1:33d22b2c88ad | 117 | // turn off LED |
gtfierro | 1:33d22b2c88ad | 118 | green = OFF; |
gtfierro | 1:33d22b2c88ad | 119 | red = OFF; |
gtfierro | 1:33d22b2c88ad | 120 | blue = OFF; |
gtfierro | 1:33d22b2c88ad | 121 | |
gtfierro | 1:33d22b2c88ad | 122 | initializePressed(); |
gtfierro | 1:33d22b2c88ad | 123 | |
gtfierro | 1:33d22b2c88ad | 124 | b1.attach_asserted(&b1Pressed); |
gtfierro | 1:33d22b2c88ad | 125 | b1.setAssertValue(0); |
gtfierro | 1:33d22b2c88ad | 126 | b1.setSampleFrequency(); |
gtfierro | 1:33d22b2c88ad | 127 | |
gtfierro | 1:33d22b2c88ad | 128 | b2.attach_asserted(&b2Pressed); |
gtfierro | 1:33d22b2c88ad | 129 | b2.setAssertValue(0); |
gtfierro | 1:33d22b2c88ad | 130 | b2.setSampleFrequency(); |
gtfierro | 1:33d22b2c88ad | 131 | |
gtfierro | 1:33d22b2c88ad | 132 | b3.attach_asserted(&b3Pressed); |
gtfierro | 1:33d22b2c88ad | 133 | b3.setAssertValue(0); |
gtfierro | 1:33d22b2c88ad | 134 | b3.setSampleFrequency(); |
gtfierro | 1:33d22b2c88ad | 135 | |
gtfierro | 1:33d22b2c88ad | 136 | b4.attach_asserted(&b4Pressed); |
gtfierro | 1:33d22b2c88ad | 137 | b4.setAssertValue(0); |
gtfierro | 1:33d22b2c88ad | 138 | b4.setSampleFrequency(); |
gtfierro | 1:33d22b2c88ad | 139 | |
gtfierro | 1:33d22b2c88ad | 140 | b5.attach_asserted(&b5Pressed); |
gtfierro | 1:33d22b2c88ad | 141 | b5.setAssertValue(0); |
gtfierro | 1:33d22b2c88ad | 142 | b5.setSampleFrequency(); |
gtfierro | 1:33d22b2c88ad | 143 | |
gtfierro | 1:33d22b2c88ad | 144 | b6.attach_asserted(&b6Pressed); |
gtfierro | 1:33d22b2c88ad | 145 | b6.setAssertValue(0); |
gtfierro | 1:33d22b2c88ad | 146 | b6.setSampleFrequency(); |
gtfierro | 1:33d22b2c88ad | 147 | |
gtfierro | 1:33d22b2c88ad | 148 | b7.attach_asserted(&b7Pressed); |
gtfierro | 1:33d22b2c88ad | 149 | b7.setAssertValue(0); |
gtfierro | 1:33d22b2c88ad | 150 | b7.setSampleFrequency(); |
gtfierro | 1:33d22b2c88ad | 151 | |
gtfierro | 1:33d22b2c88ad | 152 | b8.attach_asserted(&b8Pressed); |
gtfierro | 1:33d22b2c88ad | 153 | b8.setAssertValue(0); |
gtfierro | 1:33d22b2c88ad | 154 | b8.setSampleFrequency(); |
gtfierro | 1:33d22b2c88ad | 155 | |
gtfierro | 1:33d22b2c88ad | 156 | b9.attach_asserted(&b9Pressed); |
gtfierro | 1:33d22b2c88ad | 157 | b9.setAssertValue(0); |
gtfierro | 1:33d22b2c88ad | 158 | b9.setSampleFrequency(); |
gtfierro | 1:33d22b2c88ad | 159 | |
gtfierro | 1:33d22b2c88ad | 160 | shift.attach_asserted(&shiftOn); |
gtfierro | 1:33d22b2c88ad | 161 | shift.attach_deasserted(&shiftOff); |
gtfierro | 1:33d22b2c88ad | 162 | shift.setSampleFrequency(); |
gtfierro | 1:33d22b2c88ad | 163 | |
gtfierro | 3:636bf7891502 | 164 | while(1) { |
gtfierro | 3:636bf7891502 | 165 | if (pressed[0] != 0 && pressed[1] != 0) { |
gtfierro | 3:636bf7891502 | 166 | deliverButton(); |
gtfierro | 3:636bf7891502 | 167 | initializePressed(); |
gtfierro | 3:636bf7891502 | 168 | } |
gtfierro | 3:636bf7891502 | 169 | } |
gtfierro | 0:9b39c62126ea | 170 | } |