:)

Dependencies:   MbedJSONValue DebounceIn TextLCD USBDevice mbed WebSocketClient cc3000_hostdriver_mbedsocket Adafruit_LEDBackpack_2

Committer:
ddrew73
Date:
Wed Nov 26 00:39:46 2014 +0000
Revision:
7:7fbe8dc5e4fa
Parent:
6:d0a1eba7f943
Child:
8:725d938b301b
CompElation!;

Who changed what in which revision?

UserRevisionLine numberNew 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 7:7fbe8dc5e4fa 13 I2C i2c_left(D7,D6);
ddrew73 7:7fbe8dc5e4fa 14 Adafruit_24bargraph ledbar_left=Adafruit_24bargraph(&i2c_left);
ddrew73 4:5d27966f93e4 15
ddrew73 7:7fbe8dc5e4fa 16 I2C i2c_right(D5,D4);
ddrew73 7:7fbe8dc5e4fa 17 Adafruit_24bargraph ledbar_right=Adafruit_24bargraph(&i2c_right);
ddrew73 4:5d27966f93e4 18
ddrew73 0:16d2619002d7 19 DebounceIn upbutton(D9);
ddrew73 0:16d2619002d7 20 DebounceIn downbutton(D8);
ddrew73 7:7fbe8dc5e4fa 21 DebounceIn leftbutton(D3);
ddrew73 7:7fbe8dc5e4fa 22 DebounceIn rightbutton(D2);
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);
fil 6:d0a1eba7f943 31 DigitalOut D_in(D3);
fil 6:d0a1eba7f943 32 DigitalOut E_in(D4);
fil 6:d0a1eba7f943 33 DigitalOut F_in(D5);
ddrew73 0:16d2619002d7 34
ddrew73 0:16d2619002d7 35 DigitalOut A_out(PTC7);
ddrew73 0:16d2619002d7 36 DigitalOut B_out(PTC0);
ddrew73 0:16d2619002d7 37 DigitalOut C_out(PTC3);
ddrew73 0:16d2619002d7 38
ddrew73 0:16d2619002d7 39 AnalogOut dac(PTE30);
ddrew73 0:16d2619002d7 40 AnalogIn adc(PTB0);
ddrew73 0:16d2619002d7 41
ddrew73 0:16d2619002d7 42 //USBKeyboard keyboard;
ddrew73 0:16d2619002d7 43
ddrew73 0:16d2619002d7 44
ddrew73 0:16d2619002d7 45 //LCD stuff
ddrew73 1:0183a7d38878 46 TextLCD lcd(D13,D12,PTA17,PTA16,PTC17,PTC16,TextLCD::LCD20x4);
ddrew73 0:16d2619002d7 47
jn80842 3:161fed8bd027 48 // cc3000 KL25Z wifi connection
jn80842 3:161fed8bd027 49 // we need to define connection pins for:
jn80842 3:161fed8bd027 50 // - IRQ => (pin D3)
jn80842 3:161fed8bd027 51 // - Enable => (pin D5)
jn80842 3:161fed8bd027 52 // - SPI CS => (pin D10)
jn80842 3:161fed8bd027 53 // - SPI MOSI => (pin D11)
jn80842 3:161fed8bd027 54 // - SPI MISO => (pin D12)
jn80842 3:161fed8bd027 55 // - SPI CLK => (pin D13)
jn80842 3:161fed8bd027 56 // plus wifi network SSID, password, security level and smart-configuration flag.
jn80842 3:161fed8bd027 57
jn80842 3:161fed8bd027 58 mbed_cc3000::cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13),
jn80842 3:161fed8bd027 59 "SSID", "PASSWORD", WPA2, false);
jn80842 3:161fed8bd027 60 Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
jn80842 3:161fed8bd027 61
ddrew73 7:7fbe8dc5e4fa 62 MbedJSONValue demo;
ddrew73 7:7fbe8dc5e4fa 63
ddrew73 0:16d2619002d7 64 int main()
ddrew73 0:16d2619002d7 65 {
ddrew73 0:16d2619002d7 66
ddrew73 0:16d2619002d7 67 //Scan init
ddrew73 7:7fbe8dc5e4fa 68
ddrew73 4:5d27966f93e4 69 int con_mat[24][2] = {};
ddrew73 4:5d27966f93e4 70 float volt_mat[24][2] = {};
ddrew73 7:7fbe8dc5e4fa 71 float clientdata[48] = {};
ddrew73 7:7fbe8dc5e4fa 72 float rowval = 0, vddval = 0;
ddrew73 7:7fbe8dc5e4fa 73
ddrew73 0:16d2619002d7 74 //dac = test_val;
ddrew73 0:16d2619002d7 75
ddrew73 0:16d2619002d7 76
ddrew73 0:16d2619002d7 77 //LED init
ddrew73 7:7fbe8dc5e4fa 78 int rowselect = 0, moved = 1, colselect = 0, selected = 0;
ddrew73 4:5d27966f93e4 79 ledbar_left.begin(0x70);
ddrew73 4:5d27966f93e4 80 ledbar_left.clear();
ddrew73 4:5d27966f93e4 81 ledbar_left.writeDisplay();
ddrew73 5:03d75ad40d5e 82
ddrew73 4:5d27966f93e4 83 ledbar_right.begin(0x70);
ddrew73 4:5d27966f93e4 84 ledbar_right.clear();
ddrew73 4:5d27966f93e4 85 ledbar_right.writeDisplay();
ddrew73 0:16d2619002d7 86
ddrew73 0:16d2619002d7 87 //LCD init
ddrew73 0:16d2619002d7 88 lcd.cls();
ddrew73 0:16d2619002d7 89
ddrew73 0:16d2619002d7 90
ddrew73 0:16d2619002d7 91 while(1) {
ddrew73 5:03d75ad40d5e 92
ddrew73 0:16d2619002d7 93 //Display
ddrew73 0:16d2619002d7 94
ddrew73 4:5d27966f93e4 95 if (colselect == 0) {
ddrew73 5:03d75ad40d5e 96 ledbar_left.clear();
ddrew73 5:03d75ad40d5e 97 ledbar_right.clear();
ddrew73 5:03d75ad40d5e 98 ledbar_left.setBar(rowselect,LED_GREEN);
ddrew73 5:03d75ad40d5e 99 ledbar_left.writeDisplay();
ddrew73 5:03d75ad40d5e 100 ledbar_right.writeDisplay();
ddrew73 7:7fbe8dc5e4fa 101 }
ddrew73 7:7fbe8dc5e4fa 102 else {
ddrew73 7:7fbe8dc5e4fa 103 ledbar_left.clear();
ddrew73 5:03d75ad40d5e 104 ledbar_right.clear();
ddrew73 4:5d27966f93e4 105 ledbar_right.setBar(rowselect,LED_GREEN);
ddrew73 4:5d27966f93e4 106 ledbar_right.writeDisplay();
ddrew73 4:5d27966f93e4 107 ledbar_left.writeDisplay();
ddrew73 7:7fbe8dc5e4fa 108 }
ddrew73 0:16d2619002d7 109 //Moving the selected row
ddrew73 0:16d2619002d7 110 if (upbutton.read() == 0) {
ddrew73 7:7fbe8dc5e4fa 111
ddrew73 0:16d2619002d7 112 rowselect = rowselect-1;
ddrew73 0:16d2619002d7 113 moved = 1;
ddrew73 0:16d2619002d7 114 wait(0.75);
ddrew73 0:16d2619002d7 115 }
ddrew73 0:16d2619002d7 116
ddrew73 0:16d2619002d7 117 if (downbutton.read() == 0) {
ddrew73 7:7fbe8dc5e4fa 118
ddrew73 0:16d2619002d7 119 rowselect = rowselect+1;
ddrew73 0:16d2619002d7 120 moved = 1;
ddrew73 0:16d2619002d7 121 wait(0.75);
ddrew73 0:16d2619002d7 122 }
ddrew73 5:03d75ad40d5e 123
ddrew73 4:5d27966f93e4 124 if (leftbutton.read() == 0) {
ddrew73 4:5d27966f93e4 125 colselect = 0;
ddrew73 5:03d75ad40d5e 126 ledbar_right.clear();
ddrew73 5:03d75ad40d5e 127 moved =1;
ddrew73 4:5d27966f93e4 128 wait(0.75);
ddrew73 7:7fbe8dc5e4fa 129
ddrew73 4:5d27966f93e4 130 }
ddrew73 5:03d75ad40d5e 131
ddrew73 4:5d27966f93e4 132 if (rightbutton.read() == 0) {
ddrew73 4:5d27966f93e4 133 colselect = 1;
ddrew73 5:03d75ad40d5e 134 ledbar_left.clear();
ddrew73 5:03d75ad40d5e 135 moved =1;
ddrew73 4:5d27966f93e4 136 wait(0.75);
ddrew73 7:7fbe8dc5e4fa 137
ddrew73 7:7fbe8dc5e4fa 138
ddrew73 5:03d75ad40d5e 139 }
ddrew73 0:16d2619002d7 140
ddrew73 0:16d2619002d7 141 //Boundary checking
ddrew73 0:16d2619002d7 142 if (rowselect > 23) {
ddrew73 0:16d2619002d7 143 rowselect = 23;
ddrew73 0:16d2619002d7 144 }
ddrew73 0:16d2619002d7 145 if (rowselect < 0) {
ddrew73 0:16d2619002d7 146 rowselect = 0;
ddrew73 0:16d2619002d7 147 }
ddrew73 0:16d2619002d7 148
ddrew73 0:16d2619002d7 149 //Implementing scanning
ddrew73 0:16d2619002d7 150
ddrew73 0:16d2619002d7 151 if (scanbutton.read() == 0) {
ddrew73 7:7fbe8dc5e4fa 152 A_in = 1;
ddrew73 7:7fbe8dc5e4fa 153 B_in = 1;
ddrew73 7:7fbe8dc5e4fa 154 C_in = 1;
ddrew73 7:7fbe8dc5e4fa 155 D_in = 1;
ddrew73 7:7fbe8dc5e4fa 156 E_in = 1;
ddrew73 7:7fbe8dc5e4fa 157 F_in = 1;
fil 6:d0a1eba7f943 158
ddrew73 7:7fbe8dc5e4fa 159 vddval = adc.read();
ddrew73 0:16d2619002d7 160
ddrew73 7:7fbe8dc5e4fa 161 //SET MULTIPLEXER TO READ SELECTED ROW
ddrew73 7:7fbe8dc5e4fa 162 // Sample the 'selected' node
fil 6:d0a1eba7f943 163
ddrew73 7:7fbe8dc5e4fa 164 int sn = (colselect + 1)*(rowselect + 1);
ddrew73 7:7fbe8dc5e4fa 165 A_in = (sn-1)&2;
ddrew73 7:7fbe8dc5e4fa 166 B_in = ((sn-1)/2)%2;
ddrew73 7:7fbe8dc5e4fa 167 C_in = ((sn-1)/4)%2;
ddrew73 7:7fbe8dc5e4fa 168 D_in = ((sn-1)/8)%2;
ddrew73 7:7fbe8dc5e4fa 169 E_in = ((sn-1)/16)%2;
ddrew73 7:7fbe8dc5e4fa 170 F_in = ((sn-1)/32)%2;
ddrew73 7:7fbe8dc5e4fa 171 rowval = adc.read();
ddrew73 1:0183a7d38878 172
ddrew73 7:7fbe8dc5e4fa 173 ledbar_left.clear();
ddrew73 7:7fbe8dc5e4fa 174 ledbar_right.clear();
ddrew73 1:0183a7d38878 175
ddrew73 7:7fbe8dc5e4fa 176 for (int x = 0; x < 2; x++) {
ddrew73 7:7fbe8dc5e4fa 177 for (int y = 0; y < 24; y++) {
ddrew73 7:7fbe8dc5e4fa 178 int sn = (colselect+1)*(rowselect+1);
ddrew73 7:7fbe8dc5e4fa 179 A_in = (sn-1)&2;
ddrew73 7:7fbe8dc5e4fa 180 B_in = ((sn-1)/2)%2;
ddrew73 7:7fbe8dc5e4fa 181 C_in = ((sn-1)/4)%2;
ddrew73 7:7fbe8dc5e4fa 182 D_in = ((sn-1)/8)%2;
ddrew73 7:7fbe8dc5e4fa 183 E_in = ((sn-1)/16)%2;
ddrew73 7:7fbe8dc5e4fa 184 F_in = ((sn-1)/32)%2;
ddrew73 0:16d2619002d7 185
ddrew73 7:7fbe8dc5e4fa 186 float in_val = adc.read();
ddrew73 7:7fbe8dc5e4fa 187 volt_mat[x][y] = in_val;
fil 6:d0a1eba7f943 188
ddrew73 7:7fbe8dc5e4fa 189 if (x == 1){
ddrew73 7:7fbe8dc5e4fa 190 int z = y+24;
ddrew73 7:7fbe8dc5e4fa 191 clientdata[z] = in_val;
fil 6:d0a1eba7f943 192 }
fil 6:d0a1eba7f943 193 else {
ddrew73 7:7fbe8dc5e4fa 194 clientdata[y] = in_val;
ddrew73 7:7fbe8dc5e4fa 195 }
ddrew73 7:7fbe8dc5e4fa 196
ddrew73 7:7fbe8dc5e4fa 197
ddrew73 7:7fbe8dc5e4fa 198 if ((in_val > vddval-0.01) && (in_val < vddval+0.01)) {
ddrew73 7:7fbe8dc5e4fa 199 con_mat[x][y] = 2;
ddrew73 7:7fbe8dc5e4fa 200 if (x == 0) {
ddrew73 7:7fbe8dc5e4fa 201 ledbar_left.setBar(y,LED_RED);
ddrew73 7:7fbe8dc5e4fa 202 } else {
ddrew73 7:7fbe8dc5e4fa 203 ledbar_right.setBar(y,LED_RED);
ddrew73 7:7fbe8dc5e4fa 204 }
ddrew73 7:7fbe8dc5e4fa 205 }
ddrew73 7:7fbe8dc5e4fa 206 else if (in_val == 0) {
ddrew73 7:7fbe8dc5e4fa 207 con_mat[x][y] = 3;
ddrew73 7:7fbe8dc5e4fa 208 if (x == 0) {
ddrew73 7:7fbe8dc5e4fa 209 ledbar_left.setBar(y,LED_YELLOW);
ddrew73 7:7fbe8dc5e4fa 210 } else {
ddrew73 7:7fbe8dc5e4fa 211 ledbar_right.setBar(y,LED_YELLOW);
ddrew73 7:7fbe8dc5e4fa 212 }
ddrew73 7:7fbe8dc5e4fa 213 }
ddrew73 7:7fbe8dc5e4fa 214 else if ((in_val > rowval-0.01) && (in_val < rowval+0.01)) {
ddrew73 7:7fbe8dc5e4fa 215 con_mat[x][y] = 1;
ddrew73 7:7fbe8dc5e4fa 216 if (x == 0) {
ddrew73 7:7fbe8dc5e4fa 217 ledbar_left.setBar(y,LED_GREEN);
ddrew73 7:7fbe8dc5e4fa 218 } else {
ddrew73 7:7fbe8dc5e4fa 219 ledbar_right.setBar(y,LED_GREEN);
ddrew73 7:7fbe8dc5e4fa 220 }
ddrew73 7:7fbe8dc5e4fa 221 }
ddrew73 7:7fbe8dc5e4fa 222 else {
ddrew73 7:7fbe8dc5e4fa 223 con_mat[x][y] = 0;
fil 6:d0a1eba7f943 224 }
ddrew73 7:7fbe8dc5e4fa 225
ddrew73 7:7fbe8dc5e4fa 226
ddrew73 7:7fbe8dc5e4fa 227 } } //END OF ROWSCAN FOR LOOP
ddrew73 7:7fbe8dc5e4fa 228
ddrew73 7:7fbe8dc5e4fa 229 //STUFF INTO JSON FORMAT
ddrew73 7:7fbe8dc5e4fa 230 char str[1];
ddrew73 7:7fbe8dc5e4fa 231 for (int i= 0; i < 48; i++) {
ddrew73 7:7fbe8dc5e4fa 232 sprintf(str, "%d", i);
ddrew73 7:7fbe8dc5e4fa 233 demo[str] = clientdata[i];
ddrew73 7:7fbe8dc5e4fa 234 }
ddrew73 7:7fbe8dc5e4fa 235 demo["vdd"] = vddval;
ddrew73 7:7fbe8dc5e4fa 236 demo["rowval"] = rowval;
ddrew73 7:7fbe8dc5e4fa 237 selected = rowselect;
ddrew73 7:7fbe8dc5e4fa 238 if (colselect == 1){
ddrew73 7:7fbe8dc5e4fa 239 selected = selected+24;
ddrew73 7:7fbe8dc5e4fa 240 }
ddrew73 7:7fbe8dc5e4fa 241 demo["selected"] = selected;
ddrew73 7:7fbe8dc5e4fa 242
ddrew73 7:7fbe8dc5e4fa 243
ddrew73 7:7fbe8dc5e4fa 244
ddrew73 7:7fbe8dc5e4fa 245 moved = 0;
ddrew73 7:7fbe8dc5e4fa 246
ddrew73 7:7fbe8dc5e4fa 247 wait(2); //THIS NEEDS TO GO AWAY! :}
ddrew73 7:7fbe8dc5e4fa 248
ddrew73 7:7fbe8dc5e4fa 249 } // END OF SCANBUTTON IF STATEMENT
ddrew73 7:7fbe8dc5e4fa 250
ddrew73 7:7fbe8dc5e4fa 251 if (moved==0) {
ddrew73 7:7fbe8dc5e4fa 252 ledbar_left.writeDisplay();
ddrew73 7:7fbe8dc5e4fa 253 ledbar_right.writeDisplay();
ddrew73 7:7fbe8dc5e4fa 254 lcd.cls();
ddrew73 7:7fbe8dc5e4fa 255 lcd.locate(0,1);
ddrew73 7:7fbe8dc5e4fa 256 lcd.printf("Vdd: %1.1f V",vddval);
ddrew73 7:7fbe8dc5e4fa 257 lcd.locate(0,0);
ddrew73 7:7fbe8dc5e4fa 258 lcd.printf("Row %d: %1.1f V",selected,rowval);
ddrew73 7:7fbe8dc5e4fa 259 }
fil 6:d0a1eba7f943 260 else {
ddrew73 7:7fbe8dc5e4fa 261 lcd.cls();
ddrew73 7:7fbe8dc5e4fa 262 string programname = "The ToastBoard";
ddrew73 7:7fbe8dc5e4fa 263 string instructions = "Select a row and scan";
ddrew73 7:7fbe8dc5e4fa 264 lcd.locate(0,0);
ddrew73 7:7fbe8dc5e4fa 265 lcd.printf("%s",programname.c_str());
ddrew73 7:7fbe8dc5e4fa 266 lcd.locate(0,1);
ddrew73 7:7fbe8dc5e4fa 267 lcd.printf("%s",instructions.c_str());
ddrew73 7:7fbe8dc5e4fa 268 }
ddrew73 5:03d75ad40d5e 269
ddrew73 5:03d75ad40d5e 270
ddrew73 5:03d75ad40d5e 271
ddrew73 5:03d75ad40d5e 272
ddrew73 4:5d27966f93e4 273
ddrew73 7:7fbe8dc5e4fa 274
ddrew73 7:7fbe8dc5e4fa 275
ddrew73 7:7fbe8dc5e4fa 276
ddrew73 0:16d2619002d7 277
ddrew73 0:16d2619002d7 278
ddrew73 4:5d27966f93e4 279
ddrew73 4:5d27966f93e4 280
ddrew73 4:5d27966f93e4 281
ddrew73 7:7fbe8dc5e4fa 282 } // END OF WHILE(1)
ddrew73 7:7fbe8dc5e4fa 283
ddrew73 7:7fbe8dc5e4fa 284
ddrew73 7:7fbe8dc5e4fa 285
ddrew73 4:5d27966f93e4 286
ddrew73 7:7fbe8dc5e4fa 287 } // END OF INT MAIN
ddrew73 7:7fbe8dc5e4fa 288
ddrew73 7:7fbe8dc5e4fa 289
ddrew73 7:7fbe8dc5e4fa 290
ddrew73 7:7fbe8dc5e4fa 291
ddrew73 7:7fbe8dc5e4fa 292
ddrew73 7:7fbe8dc5e4fa 293