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