:)
Dependencies: MbedJSONValue DebounceIn TextLCD USBDevice mbed WebSocketClient cc3000_hostdriver_mbedsocket Adafruit_LEDBackpack_2
main.cpp@4:5d27966f93e4, 2014-11-24 (annotated)
- Committer:
- ddrew73
- Date:
- Mon Nov 24 00:49:48 2014 +0000
- Revision:
- 4:5d27966f93e4
- Parent:
- 3:161fed8bd027
- Child:
- 5:03d75ad40d5e
Starting to convert code to 24x2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ddrew73 | 0:16d2619002d7 | 1 | #include "mbed.h" |
ddrew73 | 1:0183a7d38878 | 2 | #include <string> |
ddrew73 | 0:16d2619002d7 | 3 | #include "Adafruit_LEDBackpack.h" |
ddrew73 | 0:16d2619002d7 | 4 | #include "Adafruit_GFX.h" |
ddrew73 | 0:16d2619002d7 | 5 | #include "TextLCD.h" |
ddrew73 | 0:16d2619002d7 | 6 | #include "DebounceIn.h" |
ddrew73 | 0:16d2619002d7 | 7 | #include "USBKeyboard.h" |
jn80842 | 3:161fed8bd027 | 8 | #include "cc3000.h" |
jn80842 | 3:161fed8bd027 | 9 | #include "Websocket.h" |
jn80842 | 3:161fed8bd027 | 10 | #include "MbedJSONValue.h" |
ddrew73 | 0:16d2619002d7 | 11 | |
ddrew73 | 0:16d2619002d7 | 12 | //LED stuff |
ddrew73 | 0:16d2619002d7 | 13 | I2C i2c(D7,D6); |
ddrew73 | 4:5d27966f93e4 | 14 | Adafruit_24bargraph ledbar_left=Adafruit_24bargraph(&i2c); |
ddrew73 | 4:5d27966f93e4 | 15 | |
ddrew73 | 4:5d27966f93e4 | 16 | //I2C i2c(D7,D6); |
ddrew73 | 4:5d27966f93e4 | 17 | //Adafruit_24bargraph ledbar_right=Adafruit_24bargraph(&i2c); |
ddrew73 | 4:5d27966f93e4 | 18 | |
ddrew73 | 0:16d2619002d7 | 19 | DebounceIn upbutton(D9); |
ddrew73 | 0:16d2619002d7 | 20 | DebounceIn downbutton(D8); |
ddrew73 | 4:5d27966f93e4 | 21 | //DebounceIn leftbutton( ); |
ddrew73 | 4:5d27966f93e4 | 22 | //DebounceIn rightbutton( ); |
ddrew73 | 0:16d2619002d7 | 23 | |
ddrew73 | 0:16d2619002d7 | 24 | //Scanner stuff |
ddrew73 | 0:16d2619002d7 | 25 | |
ddrew73 | 0:16d2619002d7 | 26 | DebounceIn scanbutton(D10); |
ddrew73 | 0:16d2619002d7 | 27 | |
ddrew73 | 0:16d2619002d7 | 28 | DigitalOut A_in(D0); |
ddrew73 | 0:16d2619002d7 | 29 | DigitalOut B_in(D1); |
ddrew73 | 0:16d2619002d7 | 30 | DigitalOut C_in(D2); |
ddrew73 | 0:16d2619002d7 | 31 | |
ddrew73 | 0:16d2619002d7 | 32 | DigitalOut A_out(PTC7); |
ddrew73 | 0:16d2619002d7 | 33 | DigitalOut B_out(PTC0); |
ddrew73 | 0:16d2619002d7 | 34 | DigitalOut C_out(PTC3); |
ddrew73 | 0:16d2619002d7 | 35 | |
ddrew73 | 0:16d2619002d7 | 36 | AnalogOut dac(PTE30); |
ddrew73 | 0:16d2619002d7 | 37 | AnalogIn adc(PTB0); |
ddrew73 | 0:16d2619002d7 | 38 | |
ddrew73 | 0:16d2619002d7 | 39 | //USBKeyboard keyboard; |
ddrew73 | 0:16d2619002d7 | 40 | |
ddrew73 | 0:16d2619002d7 | 41 | |
ddrew73 | 0:16d2619002d7 | 42 | //LCD stuff |
ddrew73 | 1:0183a7d38878 | 43 | TextLCD lcd(D13,D12,PTA17,PTA16,PTC17,PTC16,TextLCD::LCD20x4); |
ddrew73 | 0:16d2619002d7 | 44 | |
jn80842 | 3:161fed8bd027 | 45 | // cc3000 KL25Z wifi connection |
jn80842 | 3:161fed8bd027 | 46 | // we need to define connection pins for: |
jn80842 | 3:161fed8bd027 | 47 | // - IRQ => (pin D3) |
jn80842 | 3:161fed8bd027 | 48 | // - Enable => (pin D5) |
jn80842 | 3:161fed8bd027 | 49 | // - SPI CS => (pin D10) |
jn80842 | 3:161fed8bd027 | 50 | // - SPI MOSI => (pin D11) |
jn80842 | 3:161fed8bd027 | 51 | // - SPI MISO => (pin D12) |
jn80842 | 3:161fed8bd027 | 52 | // - SPI CLK => (pin D13) |
jn80842 | 3:161fed8bd027 | 53 | // plus wifi network SSID, password, security level and smart-configuration flag. |
jn80842 | 3:161fed8bd027 | 54 | |
jn80842 | 3:161fed8bd027 | 55 | mbed_cc3000::cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13), |
jn80842 | 3:161fed8bd027 | 56 | "SSID", "PASSWORD", WPA2, false); |
jn80842 | 3:161fed8bd027 | 57 | Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw"); |
jn80842 | 3:161fed8bd027 | 58 | |
ddrew73 | 0:16d2619002d7 | 59 | int main() |
ddrew73 | 0:16d2619002d7 | 60 | { |
ddrew73 | 0:16d2619002d7 | 61 | |
ddrew73 | 0:16d2619002d7 | 62 | //Scan init |
ddrew73 | 0:16d2619002d7 | 63 | |
ddrew73 | 0:16d2619002d7 | 64 | float test_val = .001; |
ddrew73 | 1:0183a7d38878 | 65 | float row_volt = 0; |
ddrew73 | 4:5d27966f93e4 | 66 | int con_mat[24][2] = {}; |
ddrew73 | 0:16d2619002d7 | 67 | int a1,b1,c1,a2,b2,c2; |
ddrew73 | 4:5d27966f93e4 | 68 | float volt_mat[24][2] = {}; |
ddrew73 | 0:16d2619002d7 | 69 | //dac = test_val; |
ddrew73 | 0:16d2619002d7 | 70 | |
ddrew73 | 0:16d2619002d7 | 71 | |
ddrew73 | 0:16d2619002d7 | 72 | //LED init |
ddrew73 | 4:5d27966f93e4 | 73 | int redcount = 0, greencount = 0, rowselect = 0, rowselect_old = 0, moved = 1, colselect = 0; |
ddrew73 | 4:5d27966f93e4 | 74 | ledbar_left.begin(0x70); |
ddrew73 | 4:5d27966f93e4 | 75 | ledbar_left.clear(); |
ddrew73 | 4:5d27966f93e4 | 76 | ledbar_left.writeDisplay(); |
ddrew73 | 4:5d27966f93e4 | 77 | |
ddrew73 | 4:5d27966f93e4 | 78 | ledbar_right.begin(0x70); |
ddrew73 | 4:5d27966f93e4 | 79 | ledbar_right.clear(); |
ddrew73 | 4:5d27966f93e4 | 80 | ledbar_right.writeDisplay(); |
ddrew73 | 0:16d2619002d7 | 81 | |
ddrew73 | 0:16d2619002d7 | 82 | //LCD init |
ddrew73 | 0:16d2619002d7 | 83 | lcd.cls(); |
ddrew73 | 1:0183a7d38878 | 84 | string programname = "Dan's UI Test"; |
ddrew73 | 0:16d2619002d7 | 85 | |
ddrew73 | 1:0183a7d38878 | 86 | lcd.printf("%s",programname.c_str()); |
ddrew73 | 0:16d2619002d7 | 87 | |
ddrew73 | 0:16d2619002d7 | 88 | while(1) { |
ddrew73 | 1:0183a7d38878 | 89 | |
ddrew73 | 0:16d2619002d7 | 90 | //Display |
ddrew73 | 0:16d2619002d7 | 91 | |
ddrew73 | 4:5d27966f93e4 | 92 | if (colselect == 0) { |
ddrew73 | 4:5d27966f93e4 | 93 | ledbar_left.setBar(rowselect_old,LED_OFF); |
ddrew73 | 4:5d27966f93e4 | 94 | ledbar_left.setBar(rowselect,LED_GREEN); |
ddrew73 | 4:5d27966f93e4 | 95 | ledbar_left.writeDisplay(); |
ddrew73 | 4:5d27966f93e4 | 96 | ledbar_right.writeDisplay(); |
ddrew73 | 4:5d27966f93e4 | 97 | } |
ddrew73 | 4:5d27966f93e4 | 98 | else |
ddrew73 | 4:5d27966f93e4 | 99 | ledbar_right.setBar(rowselect_old,LED_OFF); |
ddrew73 | 4:5d27966f93e4 | 100 | ledbar_right.setBar(rowselect,LED_GREEN); |
ddrew73 | 4:5d27966f93e4 | 101 | ledbar_right.writeDisplay(); |
ddrew73 | 4:5d27966f93e4 | 102 | ledbar_left.writeDisplay(); |
ddrew73 | 0:16d2619002d7 | 103 | |
ddrew73 | 0:16d2619002d7 | 104 | //Moving the selected row |
ddrew73 | 0:16d2619002d7 | 105 | if (upbutton.read() == 0) { |
ddrew73 | 0:16d2619002d7 | 106 | rowselect_old = rowselect; |
ddrew73 | 0:16d2619002d7 | 107 | rowselect = rowselect-1; |
ddrew73 | 0:16d2619002d7 | 108 | moved = 1; |
ddrew73 | 0:16d2619002d7 | 109 | wait(0.75); |
ddrew73 | 0:16d2619002d7 | 110 | } |
ddrew73 | 0:16d2619002d7 | 111 | |
ddrew73 | 0:16d2619002d7 | 112 | if (downbutton.read() == 0) { |
ddrew73 | 0:16d2619002d7 | 113 | rowselect_old = rowselect; |
ddrew73 | 0:16d2619002d7 | 114 | rowselect = rowselect+1; |
ddrew73 | 0:16d2619002d7 | 115 | moved = 1; |
ddrew73 | 0:16d2619002d7 | 116 | wait(0.75); |
ddrew73 | 0:16d2619002d7 | 117 | } |
ddrew73 | 4:5d27966f93e4 | 118 | |
ddrew73 | 4:5d27966f93e4 | 119 | if (leftbutton.read() == 0) { |
ddrew73 | 4:5d27966f93e4 | 120 | colselect = 0; |
ddrew73 | 4:5d27966f93e4 | 121 | wait(0.75); |
ddrew73 | 4:5d27966f93e4 | 122 | ledbar_right.clear(); |
ddrew73 | 4:5d27966f93e4 | 123 | } |
ddrew73 | 4:5d27966f93e4 | 124 | |
ddrew73 | 4:5d27966f93e4 | 125 | if (rightbutton.read() == 0) { |
ddrew73 | 4:5d27966f93e4 | 126 | colselect = 1; |
ddrew73 | 4:5d27966f93e4 | 127 | wait(0.75); |
ddrew73 | 4:5d27966f93e4 | 128 | ledbar_left.clear(); |
ddrew73 | 4:5d27966f93e4 | 129 | } |
ddrew73 | 0:16d2619002d7 | 130 | |
ddrew73 | 0:16d2619002d7 | 131 | //Boundary checking |
ddrew73 | 0:16d2619002d7 | 132 | if (rowselect > 23) { |
ddrew73 | 0:16d2619002d7 | 133 | rowselect = 23; |
ddrew73 | 0:16d2619002d7 | 134 | } |
ddrew73 | 0:16d2619002d7 | 135 | if (rowselect < 0) { |
ddrew73 | 0:16d2619002d7 | 136 | rowselect = 0; |
ddrew73 | 0:16d2619002d7 | 137 | } |
ddrew73 | 0:16d2619002d7 | 138 | |
ddrew73 | 0:16d2619002d7 | 139 | //Implementing scanning |
ddrew73 | 0:16d2619002d7 | 140 | |
ddrew73 | 0:16d2619002d7 | 141 | if (scanbutton.read() == 0) { |
ddrew73 | 0:16d2619002d7 | 142 | |
ddrew73 | 0:16d2619002d7 | 143 | for (int x = 0; x < 8; x++) { |
ddrew73 | 0:16d2619002d7 | 144 | for (int y = 0; y < 8; y++) { |
ddrew73 | 0:16d2619002d7 | 145 | // Stupid logic to convert count value to binary... |
ddrew73 | 0:16d2619002d7 | 146 | A_in = x%2; |
ddrew73 | 0:16d2619002d7 | 147 | a1 = x%2; |
ddrew73 | 0:16d2619002d7 | 148 | B_in = (x/2)%2; |
ddrew73 | 0:16d2619002d7 | 149 | b1 = (x/2)%2; |
ddrew73 | 0:16d2619002d7 | 150 | C_in = (x/4)%2; |
ddrew73 | 0:16d2619002d7 | 151 | c1 = (x/4)%2; |
ddrew73 | 0:16d2619002d7 | 152 | |
ddrew73 | 0:16d2619002d7 | 153 | A_out = y%2; |
ddrew73 | 0:16d2619002d7 | 154 | int a2 = y%2; |
ddrew73 | 0:16d2619002d7 | 155 | B_out = (y/2)%2; |
ddrew73 | 0:16d2619002d7 | 156 | b2 = (y/2)%2; |
ddrew73 | 0:16d2619002d7 | 157 | C_out = (y/4)%2; |
ddrew73 | 0:16d2619002d7 | 158 | c2 = (y/4)%2; |
ddrew73 | 0:16d2619002d7 | 159 | |
ddrew73 | 0:16d2619002d7 | 160 | // Now we do the scan test, results are stored in con_mat |
ddrew73 | 0:16d2619002d7 | 161 | //wait(0.01); |
ddrew73 | 0:16d2619002d7 | 162 | dac = test_val; |
ddrew73 | 0:16d2619002d7 | 163 | float in_val = adc.read(); |
ddrew73 | 0:16d2619002d7 | 164 | |
ddrew73 | 0:16d2619002d7 | 165 | if ((in_val > .0010) && (in_val < .0025)) |
ddrew73 | 0:16d2619002d7 | 166 | con_mat[x][y] = 1; |
ddrew73 | 0:16d2619002d7 | 167 | else |
ddrew73 | 0:16d2619002d7 | 168 | con_mat[x][y] = 0; |
ddrew73 | 0:16d2619002d7 | 169 | |
ddrew73 | 0:16d2619002d7 | 170 | //keyboard.printf("%f %f \n",dac.read(),in_val); |
ddrew73 | 0:16d2619002d7 | 171 | //keyboard.printf("%d %d %d\n",a2,b2,c2); |
ddrew73 | 0:16d2619002d7 | 172 | } |
ddrew73 | 0:16d2619002d7 | 173 | } |
ddrew73 | 1:0183a7d38878 | 174 | |
ddrew73 | 1:0183a7d38878 | 175 | /* |
ddrew73 | 1:0183a7d38878 | 176 | for (int x=0;x<8;x++) { |
ddrew73 | 1:0183a7d38878 | 177 | for (int y=0;y<8;y++) { |
ddrew73 | 1:0183a7d38878 | 178 | keyboard.printf("%d ",con_mat[x][y]); |
ddrew73 | 1:0183a7d38878 | 179 | }} |
ddrew73 | 1:0183a7d38878 | 180 | */ |
ddrew73 | 0:16d2619002d7 | 181 | //Displaying all connected rows as red light! |
ddrew73 | 0:16d2619002d7 | 182 | ledbar.clear(); |
ddrew73 | 0:16d2619002d7 | 183 | for (int i=0; i<8; i++) { |
ddrew73 | 0:16d2619002d7 | 184 | if (con_mat[rowselect][i] == 1) { |
ddrew73 | 0:16d2619002d7 | 185 | ledbar.setBar(i,LED_RED); |
ddrew73 | 0:16d2619002d7 | 186 | } |
ddrew73 | 0:16d2619002d7 | 187 | } |
ddrew73 | 0:16d2619002d7 | 188 | ledbar.writeDisplay(); |
ddrew73 | 0:16d2619002d7 | 189 | moved = 0; |
ddrew73 | 1:0183a7d38878 | 190 | |
ddrew73 | 1:0183a7d38878 | 191 | float vddmeasure = 3.14259; |
ddrew73 | 1:0183a7d38878 | 192 | float vrowmeasure = 1.1234; |
ddrew73 | 1:0183a7d38878 | 193 | lcd.cls(); |
ddrew73 | 1:0183a7d38878 | 194 | lcd.locate(0,1); |
ddrew73 | 1:0183a7d38878 | 195 | lcd.printf("Vdd: %1.1f V",vddmeasure); |
ddrew73 | 1:0183a7d38878 | 196 | lcd.locate(0,0); |
ddrew73 | 1:0183a7d38878 | 197 | lcd.printf("Row: %1.1f V",vrowmeasure); |
ddrew73 | 1:0183a7d38878 | 198 | |
ddrew73 | 0:16d2619002d7 | 199 | wait(2.5); |
ddrew73 | 0:16d2619002d7 | 200 | ledbar.clear(); |
ddrew73 | 0:16d2619002d7 | 201 | ledbar.writeDisplay(); |
ddrew73 | 1:0183a7d38878 | 202 | lcd.cls(); |
ddrew73 | 1:0183a7d38878 | 203 | string programname = "Dan's UI Test"; |
ddrew73 | 1:0183a7d38878 | 204 | lcd.printf("%s",programname.c_str()); |
ddrew73 | 0:16d2619002d7 | 205 | |
ddrew73 | 0:16d2619002d7 | 206 | } |
ddrew73 | 0:16d2619002d7 | 207 | } |
ddrew73 | 0:16d2619002d7 | 208 | |
ddrew73 | 0:16d2619002d7 | 209 | |
ddrew73 | 0:16d2619002d7 | 210 | |
ddrew73 | 0:16d2619002d7 | 211 | |
ddrew73 | 0:16d2619002d7 | 212 | } |
ddrew73 | 0:16d2619002d7 | 213 | |
ddrew73 | 0:16d2619002d7 | 214 | |
ddrew73 | 4:5d27966f93e4 | 215 | /* |
ddrew73 | 4:5d27966f93e4 | 216 | |
ddrew73 | 4:5d27966f93e4 | 217 | //Scans rows connected to vdd and ground. Vdd = 2 in con mat, Ground = 3. Rows connected to selected row = 1. |
ddrew73 | 4:5d27966f93e4 | 218 | |
ddrew73 | 4:5d27966f93e4 | 219 | //SET MULTIPLEXER TO READ VDD |
ddrew73 | 4:5d27966f93e4 | 220 | vddval = adc.read(); |
ddrew73 | 4:5d27966f93e4 | 221 | |
ddrew73 | 4:5d27966f93e4 | 222 | //SET MULTIPLEXER TO READ SELECTED ROW |
ddrew73 | 4:5d27966f93e4 | 223 | rowval = adc.read(); |
ddrew73 | 4:5d27966f93e4 | 224 | |
ddrew73 | 4:5d27966f93e4 | 225 | for (int x = 0; x < 2; x++) { |
ddrew73 | 4:5d27966f93e4 | 226 | for (int y = 0; y < 24; y++) { |
ddrew73 | 4:5d27966f93e4 | 227 | A_in = x%2; |
ddrew73 | 4:5d27966f93e4 | 228 | a1 = x%2; |
ddrew73 | 4:5d27966f93e4 | 229 | B_in = (x/2)%2; |
ddrew73 | 4:5d27966f93e4 | 230 | b1 = (x/2)%2; |
ddrew73 | 4:5d27966f93e4 | 231 | C_in = (x/4)%2; |
ddrew73 | 4:5d27966f93e4 | 232 | c1 = (x/4)%2; |
ddrew73 | 4:5d27966f93e4 | 233 | |
ddrew73 | 4:5d27966f93e4 | 234 | A_out = y%2; |
ddrew73 | 4:5d27966f93e4 | 235 | int a2 = y%2; |
ddrew73 | 4:5d27966f93e4 | 236 | B_out = (y/2)%2; |
ddrew73 | 4:5d27966f93e4 | 237 | b2 = (y/2)%2; |
ddrew73 | 4:5d27966f93e4 | 238 | C_out = (y/4)%2; |
ddrew73 | 4:5d27966f93e4 | 239 | c2 = (y/4)%2; |
ddrew73 | 4:5d27966f93e4 | 240 | |
ddrew73 | 4:5d27966f93e4 | 241 | float in_val = adc.read(); |
ddrew73 | 4:5d27966f93e4 | 242 | volt_mat[x][y] = in_val; |
ddrew73 | 4:5d27966f93e4 | 243 | |
ddrew73 | 4:5d27966f93e4 | 244 | if (in_val == vddval){ |
ddrew73 | 4:5d27966f93e4 | 245 | con_mat[x][y] = 2; |
ddrew73 | 4:5d27966f93e4 | 246 | } |
ddrew73 | 4:5d27966f93e4 | 247 | if (in_val == 0){ |
ddrew73 | 4:5d27966f93e4 | 248 | con_mat[x][y] = 3; |
ddrew73 | 4:5d27966f93e4 | 249 | } |
ddrew73 | 4:5d27966f93e4 | 250 | if (in_val == rowval){ |
ddrew73 | 4:5d27966f93e4 | 251 | con_mat[x][y] = 1; |
ddrew73 | 4:5d27966f93e4 | 252 | } |
ddrew73 | 4:5d27966f93e4 | 253 | else |
ddrew73 | 4:5d27966f93e4 | 254 | con_mat[x][y] = 0; |
ddrew73 | 4:5d27966f93e4 | 255 | } |
ddrew73 | 4:5d27966f93e4 | 256 | } |
ddrew73 | 4:5d27966f93e4 | 257 | |
ddrew73 | 4:5d27966f93e4 | 258 | //Q: Do we need to do the same test for ground or can 0.00 be assumed? |
ddrew73 | 4:5d27966f93e4 | 259 | //Q: What is the most efficient way to compare all the "volt_mat" elements? |
ddrew73 | 4:5d27966f93e4 | 260 | //Q: Do we need to add a "confidence interval" around the rowval and vdd tests? |
ddrew73 | 0:16d2619002d7 | 261 | |
ddrew73 | 0:16d2619002d7 | 262 | |
ddrew73 | 4:5d27966f93e4 | 263 | |
ddrew73 | 4:5d27966f93e4 | 264 | |
ddrew73 | 4:5d27966f93e4 | 265 | |
ddrew73 | 4:5d27966f93e4 | 266 | */ |
ddrew73 | 4:5d27966f93e4 | 267 |