:)

Dependencies:   MbedJSONValue DebounceIn TextLCD USBDevice mbed WebSocketClient cc3000_hostdriver_mbedsocket Adafruit_LEDBackpack_2

Committer:
jn80842
Date:
Wed Dec 10 06:47:50 2014 +0000
Revision:
19:fea62e9c1ac4
Parent:
18:f7a00c31ff58
Child:
20:b30b958dbd86
more fixes

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"
jn80842 3:161fed8bd027 7 #include "cc3000.h"
jn80842 3:161fed8bd027 8 #include "Websocket.h"
ddrew73 0:16d2619002d7 9
ddrew73 8:725d938b301b 10 //Debug
ddrew73 8:725d938b301b 11 Serial pc(USBTX, USBRX); // tx, rx
ddrew73 8:725d938b301b 12
ddrew73 8:725d938b301b 13 //For oscilliscope
ddrew73 8:725d938b301b 14 Timer timer;
ddrew73 8:725d938b301b 15
ddrew73 0:16d2619002d7 16 //LED stuff
ddrew73 9:5fa90677ff3e 17 I2C i2c_left(PTC11,PTC10);
ddrew73 7:7fbe8dc5e4fa 18 Adafruit_24bargraph ledbar_left=Adafruit_24bargraph(&i2c_left);
ddrew73 4:5d27966f93e4 19
ddrew73 9:5fa90677ff3e 20 I2C i2c_right(D7,D6);
ddrew73 7:7fbe8dc5e4fa 21 Adafruit_24bargraph ledbar_right=Adafruit_24bargraph(&i2c_right);
ddrew73 4:5d27966f93e4 22
jn80842 14:2d47b97c2028 23 void set_led(int index, int color) { // index range from 0-47
jn80842 14:2d47b97c2028 24 if (index<24) {
jn80842 14:2d47b97c2028 25 ledbar_left.setBar(index,color);
jn80842 14:2d47b97c2028 26 } else {
jn80842 14:2d47b97c2028 27 ledbar_right.setBar(index-24,color);
jn80842 14:2d47b97c2028 28 }
jn80842 14:2d47b97c2028 29 }
jn80842 14:2d47b97c2028 30
ddrew73 9:5fa90677ff3e 31 DebounceIn upbutton(PTA16);
ddrew73 10:cfd60b63ed11 32 DebounceIn downbutton(PTC12);
ddrew73 10:cfd60b63ed11 33 DebounceIn leftbutton(PTC17);
ddrew73 10:cfd60b63ed11 34 DebounceIn rightbutton(PTC16);
ddrew73 0:16d2619002d7 35
ddrew73 0:16d2619002d7 36 //Scanner stuff
ddrew73 0:16d2619002d7 37
ddrew73 10:cfd60b63ed11 38 DebounceIn scanbutton(PTC13);
ddrew73 0:16d2619002d7 39
ddrew73 9:5fa90677ff3e 40 DigitalOut A_in(PTB10);
ddrew73 9:5fa90677ff3e 41 DigitalOut B_in(PTB11);
ddrew73 9:5fa90677ff3e 42 DigitalOut C_in(PTE2);
ddrew73 9:5fa90677ff3e 43 DigitalOut D_in(PTE3);
ddrew73 9:5fa90677ff3e 44 DigitalOut E_in(PTE4);
ddrew73 9:5fa90677ff3e 45 DigitalOut F_in(PTE5);
ddrew73 0:16d2619002d7 46
ddrew73 16:028a8548ab22 47 DigitalOut scan_select(PTC3);
ddrew73 16:028a8548ab22 48
ddrew73 0:16d2619002d7 49 AnalogIn adc(PTB0);
ddrew73 16:028a8548ab22 50 AnalogOut dac(PTE30);
ddrew73 8:725d938b301b 51
ddrew73 0:16d2619002d7 52
ddrew73 0:16d2619002d7 53
ddrew73 0:16d2619002d7 54 //LCD stuff
jn80842 14:2d47b97c2028 55 //TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2);
ddrew73 9:5fa90677ff3e 56 TextLCD lcd(PTC6,PTC5,D5,D4,D3,D2,TextLCD::LCD20x4);
ddrew73 0:16d2619002d7 57
jn80842 14:2d47b97c2028 58 void lcd_write_selected_info(int selected) {
jn80842 14:2d47b97c2028 59 lcd.cls();
jn80842 14:2d47b97c2028 60 lcd.locate(0,0);
jn80842 14:2d47b97c2028 61 lcd.printf("The ToastBoard");
jn80842 14:2d47b97c2028 62 lcd.locate(0,1);
jn80842 14:2d47b97c2028 63 lcd.printf("Selected row: %d",selected);
jn80842 14:2d47b97c2028 64 }
jn80842 14:2d47b97c2028 65
jn80842 14:2d47b97c2028 66 void lcd_write_voltage_info(float vddval,int selected,float rowval) {
jn80842 14:2d47b97c2028 67 lcd.cls();
jn80842 14:2d47b97c2028 68 lcd.locate(0,1);
jn80842 14:2d47b97c2028 69 lcd.printf("Vdd: %1.1f V",vddval);
jn80842 14:2d47b97c2028 70 lcd.locate(0,0);
ddrew73 15:236d90e38951 71 lcd.printf("Row %d: %1.1f V",selected,rowval);
jn80842 14:2d47b97c2028 72 }
jn80842 14:2d47b97c2028 73
ddrew73 10:cfd60b63ed11 74
ddrew73 10:cfd60b63ed11 75 //WIFI STUFF
jn80842 13:209da1dcb6e1 76
ddrew73 0:16d2619002d7 77
jn80842 3:161fed8bd027 78 // cc3000 KL25Z wifi connection
jn80842 3:161fed8bd027 79 // we need to define connection pins for:
jn80842 3:161fed8bd027 80 // - IRQ => (pin D3)
jn80842 3:161fed8bd027 81 // - Enable => (pin D5)
jn80842 3:161fed8bd027 82 // - SPI CS => (pin D10)
jn80842 3:161fed8bd027 83 // - SPI MOSI => (pin D11)
jn80842 3:161fed8bd027 84 // - SPI MISO => (pin D12)
jn80842 3:161fed8bd027 85 // - SPI CLK => (pin D13)
jn80842 3:161fed8bd027 86 // plus wifi network SSID, password, security level and smart-configuration flag.
jn80842 3:161fed8bd027 87
ddrew73 17:fcc378db08f4 88
ddrew73 15:236d90e38951 89
ddrew73 17:fcc378db08f4 90 mbed_cc3000::cc3000 wifi(D8, D9, D10, SPI(D11, D12, D13), // spi goes mosi, miso, clk
jn80842 14:2d47b97c2028 91 "SSID", "PASSWORD", WPA2, false);
jn80842 3:161fed8bd027 92 Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
jn80842 3:161fed8bd027 93
ddrew73 15:236d90e38951 94
jn80842 12:c4b046975527 95 void add_to_json(const std::string& s, std::back_insert_iterator<std::string> oi) {
jn80842 14:2d47b97c2028 96 // this chunk of code lifted from the MbedJSONValue
jn80842 14:2d47b97c2028 97 char buf[7];
jn80842 12:c4b046975527 98 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
jn80842 14:2d47b97c2028 99 if ((unsigned char)*i < 0x20 || *i == 0x7f) {
jn80842 14:2d47b97c2028 100 sprintf(buf, "\\u%04x", *i & 0xff);
jn80842 14:2d47b97c2028 101 copy(buf, buf + 6, oi);
jn80842 14:2d47b97c2028 102 } else {
jn80842 14:2d47b97c2028 103 *oi++ = *i;
jn80842 14:2d47b97c2028 104 }
jn80842 14:2d47b97c2028 105 }
jn80842 14:2d47b97c2028 106 }
jn80842 14:2d47b97c2028 107
jn80842 14:2d47b97c2028 108 std::string build_json(float vddval,int selected,float clientdata[48]) {
jn80842 14:2d47b97c2028 109 std::string s;
jn80842 14:2d47b97c2028 110 std::back_insert_iterator<std::string> json_str = std::back_inserter(s);
jn80842 14:2d47b97c2028 111 char row[1] = ""; // holder for row tokens
jn80842 14:2d47b97c2028 112 char rowvoltage[4] = ""; // holder for voltage values
jn80842 14:2d47b97c2028 113 add_to_json("{\"vddval\":",json_str);
jn80842 19:fea62e9c1ac4 114 sprintf(rowvoltage,"%.1f",vddval*3.3);
jn80842 14:2d47b97c2028 115 add_to_json(rowvoltage,json_str);
jn80842 14:2d47b97c2028 116 add_to_json(", \"selected\":",json_str);
jn80842 18:f7a00c31ff58 117 sprintf(row,"%d",selected);
jn80842 18:f7a00c31ff58 118 add_to_json(row,json_str);
jn80842 14:2d47b97c2028 119 add_to_json(", \"rows\": [",json_str);
jn80842 14:2d47b97c2028 120 int append_comma = 0;
jn80842 14:2d47b97c2028 121 for (int i= 0; i < 48; i++) {
jn80842 14:2d47b97c2028 122 if (clientdata[i] != 100.0) { // don't pass on floating row vals
jn80842 14:2d47b97c2028 123 if (append_comma == 1) {
jn80842 14:2d47b97c2028 124 add_to_json(",",json_str);
jn80842 14:2d47b97c2028 125 } else {
jn80842 14:2d47b97c2028 126 append_comma = 1;
jn80842 14:2d47b97c2028 127 }
jn80842 14:2d47b97c2028 128 add_to_json("{\"",json_str);
jn80842 14:2d47b97c2028 129 sprintf(row, "%d", i);
jn80842 14:2d47b97c2028 130 add_to_json(row,json_str);
jn80842 14:2d47b97c2028 131 add_to_json("\":",json_str);
jn80842 19:fea62e9c1ac4 132 sprintf(rowvoltage,"%.1f",clientdata[i]*3.3);
jn80842 14:2d47b97c2028 133 add_to_json(rowvoltage,json_str);
jn80842 14:2d47b97c2028 134 add_to_json("}",json_str);
jn80842 14:2d47b97c2028 135 }
jn80842 14:2d47b97c2028 136 }
jn80842 14:2d47b97c2028 137 add_to_json("]}",json_str);
jn80842 14:2d47b97c2028 138 return s;
jn80842 14:2d47b97c2028 139 }
ddrew73 17:fcc378db08f4 140
jn80842 14:2d47b97c2028 141
jn80842 14:2d47b97c2028 142 bool voltages_equal(float voltage1,float voltage2) {
ddrew73 17:fcc378db08f4 143 return (voltage1 > voltage2-0.008) && (voltage1 < voltage2+0.008);
jn80842 14:2d47b97c2028 144 }
jn80842 14:2d47b97c2028 145
jn80842 14:2d47b97c2028 146 void read_voltages(float voltages[48]) {
ddrew73 16:028a8548ab22 147 scan_select = 0;
jn80842 14:2d47b97c2028 148 int sn = 0;
jn80842 19:fea62e9c1ac4 149 for (int i=0;i<48;i++) {
ddrew73 16:028a8548ab22 150 sn = i;
ddrew73 15:236d90e38951 151 D_in = (sn)%2;
ddrew73 15:236d90e38951 152 E_in = ((sn)/2)%2;
ddrew73 15:236d90e38951 153 F_in = ((sn)/4)%2;
ddrew73 15:236d90e38951 154 A_in = ((sn)/8)%2;
ddrew73 15:236d90e38951 155 B_in = ((sn)/16)%2;
ddrew73 15:236d90e38951 156 C_in = ((sn)/32)%2;
jn80842 14:2d47b97c2028 157
ddrew73 15:236d90e38951 158 voltages[sn] = adc.read();
ddrew73 15:236d90e38951 159
jn80842 14:2d47b97c2028 160 }
jn80842 14:2d47b97c2028 161 }
jn80842 14:2d47b97c2028 162
ddrew73 16:028a8548ab22 163 void float_check(float voltages[48],float dacval) {
ddrew73 16:028a8548ab22 164 int sn = 0;
ddrew73 16:028a8548ab22 165 for (int i=0;i<48;i++) { // iterate over two columns of breadboard
ddrew73 16:028a8548ab22 166 // iterate over 24 rows of each column
ddrew73 16:028a8548ab22 167 sn = i;
ddrew73 16:028a8548ab22 168 D_in = (sn)%2;
ddrew73 16:028a8548ab22 169 E_in = ((sn)/2)%2;
ddrew73 16:028a8548ab22 170 F_in = ((sn)/4)%2;
ddrew73 16:028a8548ab22 171 A_in = ((sn)/8)%2;
ddrew73 16:028a8548ab22 172 B_in = ((sn)/16)%2;
ddrew73 16:028a8548ab22 173 C_in = ((sn)/32)%2;
ddrew73 16:028a8548ab22 174
ddrew73 16:028a8548ab22 175 scan_select = 1;
ddrew73 16:028a8548ab22 176 scan_select = 0;
ddrew73 17:fcc378db08f4 177 wait(0.02);
ddrew73 16:028a8548ab22 178 float in_val = adc.read();
ddrew73 16:028a8548ab22 179 voltages[sn] = in_val;
ddrew73 16:028a8548ab22 180
ddrew73 17:fcc378db08f4 181 wait(0.035);
ddrew73 16:028a8548ab22 182
ddrew73 16:028a8548ab22 183 scan_select = 1;
ddrew73 16:028a8548ab22 184 scan_select = 0;
ddrew73 17:fcc378db08f4 185 wait(0.02);
ddrew73 16:028a8548ab22 186 in_val = adc.read();
ddrew73 16:028a8548ab22 187
ddrew73 16:028a8548ab22 188
ddrew73 17:fcc378db08f4 189 if ((in_val < voltages[sn]-0.006) || (in_val > voltages[sn]+0.006)){
jn80842 19:fea62e9c1ac4 190 voltages[sn] = 100.0;
ddrew73 16:028a8548ab22 191
ddrew73 16:028a8548ab22 192 }
ddrew73 16:028a8548ab22 193
ddrew73 16:028a8548ab22 194
ddrew73 16:028a8548ab22 195 }
ddrew73 16:028a8548ab22 196 }
ddrew73 16:028a8548ab22 197
ddrew73 16:028a8548ab22 198
ddrew73 16:028a8548ab22 199
ddrew73 16:028a8548ab22 200 void compare_voltages(float voltages[48], float clientdata[48], int colselect, int rowselect, float vddval, float floatchecked[48]) {
jn80842 14:2d47b97c2028 201 // get selected row voltage
ddrew73 16:028a8548ab22 202 scan_select = 0;
ddrew73 15:236d90e38951 203 float rowval = voltages[(colselect*24)+ rowselect];
jn80842 14:2d47b97c2028 204 for (int i=0;i<48;i++) {
ddrew73 15:236d90e38951 205 int sn = i;//(colselect+1)*(rowselect+1);
ddrew73 15:236d90e38951 206 D_in = (sn)%2;
ddrew73 15:236d90e38951 207 E_in = ((sn)/2)%2;
ddrew73 15:236d90e38951 208 F_in = ((sn)/4)%2;
ddrew73 15:236d90e38951 209 A_in = ((sn)/8)%2;
ddrew73 15:236d90e38951 210 B_in = ((sn)/16)%2;
ddrew73 15:236d90e38951 211 C_in = ((sn)/32)%2;
jn80842 14:2d47b97c2028 212
jn80842 14:2d47b97c2028 213 float in_val = adc.read();
ddrew73 15:236d90e38951 214
ddrew73 16:028a8548ab22 215 if (floatchecked[sn] == 100){
ddrew73 16:028a8548ab22 216 set_led(i,LED_OFF);
jn80842 19:fea62e9c1ac4 217 clientdata[i] = 100.0;
ddrew73 16:028a8548ab22 218 }
ddrew73 16:028a8548ab22 219 else {
ddrew73 15:236d90e38951 220
jn80842 14:2d47b97c2028 221 if (!voltages_equal(voltages[i],in_val)) {
jn80842 14:2d47b97c2028 222 // this row is floating
ddrew73 15:236d90e38951 223 set_led(i,LED_OFF);
jn80842 19:fea62e9c1ac4 224 clientdata[i] = 100.0;
jn80842 14:2d47b97c2028 225 } else if (voltages_equal(voltages[i],vddval)) {
jn80842 14:2d47b97c2028 226 // this row matches vdd
jn80842 14:2d47b97c2028 227 set_led(i,LED_RED);
jn80842 19:fea62e9c1ac4 228 clientdata[i] = vddval;
jn80842 14:2d47b97c2028 229 } else if (voltages_equal(voltages[i],0.0)) {
jn80842 14:2d47b97c2028 230 // this row matches ground
jn80842 14:2d47b97c2028 231 set_led(i,LED_YELLOW);
jn80842 19:fea62e9c1ac4 232 clientdata[i] = 0.0;
jn80842 14:2d47b97c2028 233 } else if (voltages_equal(voltages[i],rowval)) {
jn80842 14:2d47b97c2028 234 // this row matches selected row
jn80842 14:2d47b97c2028 235 set_led(i,LED_GREEN);
jn80842 19:fea62e9c1ac4 236 clientdata[i] = rowval;
jn80842 14:2d47b97c2028 237 } else {
ddrew73 15:236d90e38951 238 set_led(i,LED_OFF);
jn80842 19:fea62e9c1ac4 239 clientdata[i] = voltages[i];
jn80842 12:c4b046975527 240 }
ddrew73 16:028a8548ab22 241 } // else if closing
ddrew73 16:028a8548ab22 242 } //for loop closing
ddrew73 16:028a8548ab22 243 } //compare_voltages function closing
jn80842 12:c4b046975527 244
ddrew73 7:7fbe8dc5e4fa 245
ddrew73 0:16d2619002d7 246 int main()
ddrew73 0:16d2619002d7 247 {
ddrew73 0:16d2619002d7 248
ddrew73 0:16d2619002d7 249 //Scan init
jn80842 14:2d47b97c2028 250 float originalvoltages[48] = {};
ddrew73 7:7fbe8dc5e4fa 251 float clientdata[48] = {};
jn80842 14:2d47b97c2028 252 float vddval = 0.0;
ddrew73 15:236d90e38951 253 float rowval = 0.0;
ddrew73 16:028a8548ab22 254 float dacval = 0.1;
ddrew73 16:028a8548ab22 255
ddrew73 16:028a8548ab22 256 float floatout[48] = {};
ddrew73 0:16d2619002d7 257
ddrew73 0:16d2619002d7 258 //LED init
ddrew73 15:236d90e38951 259 int rowselect = 0, moved = 1, colselect = 0, selected = 0, oldselected = 49;
ddrew73 4:5d27966f93e4 260 ledbar_left.begin(0x70);
ddrew73 4:5d27966f93e4 261 ledbar_left.clear();
ddrew73 4:5d27966f93e4 262 ledbar_left.writeDisplay();
ddrew73 5:03d75ad40d5e 263
ddrew73 4:5d27966f93e4 264 ledbar_right.begin(0x70);
ddrew73 4:5d27966f93e4 265 ledbar_right.clear();
ddrew73 4:5d27966f93e4 266 ledbar_right.writeDisplay();
ddrew73 0:16d2619002d7 267
ddrew73 0:16d2619002d7 268 //LCD init
ddrew73 0:16d2619002d7 269 lcd.cls();
jn80842 14:2d47b97c2028 270 int written = 0;
jn80842 14:2d47b97c2028 271
ddrew73 17:fcc378db08f4 272 wifi.init();
jn80842 19:fea62e9c1ac4 273 Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
ddrew73 11:b520586dc0a1 274
ddrew73 9:5fa90677ff3e 275 //Osci
ddrew73 9:5fa90677ff3e 276 int loopcount = 0, pressed = 0;
ddrew73 9:5fa90677ff3e 277 float begintime = 0, endtime = 0, elapsed = 0;
ddrew73 0:16d2619002d7 278
ddrew73 0:16d2619002d7 279
ddrew73 0:16d2619002d7 280 while(1) {
ddrew73 17:fcc378db08f4 281
jn80842 14:2d47b97c2028 282 if (wifi.is_connected() == false) {
jn80842 14:2d47b97c2028 283 pc.printf("trying to connect to wifi\r\n");
jn80842 14:2d47b97c2028 284 if (wifi.connect() == -1) {
jn80842 14:2d47b97c2028 285 pc.printf("Failed to connect\r\n");
jn80842 14:2d47b97c2028 286 } else {
jn80842 14:2d47b97c2028 287 pc.printf("IP address: %s \r\n", wifi.getIPAddress());
jn80842 14:2d47b97c2028 288 }
jn80842 19:fea62e9c1ac4 289 } else if (ws.is_connected() == false) {
jn80842 19:fea62e9c1ac4 290 pc.printf("connecting to ws\r\n");
jn80842 14:2d47b97c2028 291 ws.connect();
jn80842 14:2d47b97c2028 292 }
jn80842 19:fea62e9c1ac4 293
jn80842 18:f7a00c31ff58 294 pc.printf("Okay took care of wifi stuff\r\n");
ddrew73 5:03d75ad40d5e 295
ddrew73 17:fcc378db08f4 296
ddrew73 15:236d90e38951 297
ddrew73 15:236d90e38951 298
ddrew73 0:16d2619002d7 299 //Display
jn80842 14:2d47b97c2028 300 if (moved ==1) {
ddrew73 5:03d75ad40d5e 301 ledbar_left.clear();
ddrew73 5:03d75ad40d5e 302 ledbar_right.clear();
jn80842 14:2d47b97c2028 303 set_led((colselect*24+rowselect),LED_GREEN);
ddrew73 5:03d75ad40d5e 304 ledbar_left.writeDisplay();
ddrew73 5:03d75ad40d5e 305 ledbar_right.writeDisplay();
ddrew73 7:7fbe8dc5e4fa 306 }
ddrew73 0:16d2619002d7 307 //Moving the selected row
ddrew73 0:16d2619002d7 308 if (upbutton.read() == 0) {
ddrew73 0:16d2619002d7 309 rowselect = rowselect-1;
ddrew73 0:16d2619002d7 310 moved = 1;
jn80842 14:2d47b97c2028 311 written = 0;
jn80842 18:f7a00c31ff58 312 pc.printf("moved up\r\n");
ddrew73 15:236d90e38951 313 wait(0.3);
ddrew73 0:16d2619002d7 314 }
ddrew73 0:16d2619002d7 315
ddrew73 0:16d2619002d7 316 if (downbutton.read() == 0) {
ddrew73 0:16d2619002d7 317 rowselect = rowselect+1;
ddrew73 0:16d2619002d7 318 moved = 1;
jn80842 14:2d47b97c2028 319 written = 0;
jn80842 18:f7a00c31ff58 320 pc.printf("moved down\r\n");
ddrew73 15:236d90e38951 321 wait(0.3);
ddrew73 0:16d2619002d7 322 }
ddrew73 5:03d75ad40d5e 323
ddrew73 4:5d27966f93e4 324 if (leftbutton.read() == 0) {
ddrew73 4:5d27966f93e4 325 colselect = 0;
ddrew73 5:03d75ad40d5e 326 ledbar_right.clear();
ddrew73 5:03d75ad40d5e 327 moved =1;
jn80842 14:2d47b97c2028 328 written = 0;
jn80842 18:f7a00c31ff58 329 pc.printf("moved left\r\n");
ddrew73 15:236d90e38951 330 wait(0.3);
ddrew73 4:5d27966f93e4 331 }
ddrew73 5:03d75ad40d5e 332
ddrew73 4:5d27966f93e4 333 if (rightbutton.read() == 0) {
ddrew73 4:5d27966f93e4 334 colselect = 1;
ddrew73 5:03d75ad40d5e 335 ledbar_left.clear();
ddrew73 5:03d75ad40d5e 336 moved =1;
jn80842 14:2d47b97c2028 337 written = 0;
jn80842 18:f7a00c31ff58 338 pc.printf("moved right\r\n");
ddrew73 15:236d90e38951 339 wait(0.3);
ddrew73 5:03d75ad40d5e 340 }
ddrew73 0:16d2619002d7 341
ddrew73 0:16d2619002d7 342 //Boundary checking
ddrew73 0:16d2619002d7 343 if (rowselect > 23) {
ddrew73 0:16d2619002d7 344 rowselect = 23;
ddrew73 0:16d2619002d7 345 }
ddrew73 0:16d2619002d7 346 if (rowselect < 0) {
ddrew73 0:16d2619002d7 347 rowselect = 0;
ddrew73 0:16d2619002d7 348 }
ddrew73 0:16d2619002d7 349
ddrew73 15:236d90e38951 350 selected = (colselect * 24) + (rowselect+1);
jn80842 18:f7a00c31ff58 351 pc.printf("selected is %d\r\n",selected);
ddrew73 15:236d90e38951 352
ddrew73 0:16d2619002d7 353 //Implementing scanning
ddrew73 0:16d2619002d7 354
ddrew73 0:16d2619002d7 355 if (scanbutton.read() == 0) {
ddrew73 15:236d90e38951 356 A_in = 0;
ddrew73 7:7fbe8dc5e4fa 357 B_in = 1;
ddrew73 7:7fbe8dc5e4fa 358 C_in = 1;
ddrew73 7:7fbe8dc5e4fa 359 D_in = 1;
ddrew73 15:236d90e38951 360 E_in = 0;
ddrew73 15:236d90e38951 361 F_in = 0;
ddrew73 11:b520586dc0a1 362
ddrew73 7:7fbe8dc5e4fa 363 vddval = adc.read();
ddrew73 0:16d2619002d7 364
ddrew73 7:7fbe8dc5e4fa 365 ledbar_left.clear();
ddrew73 7:7fbe8dc5e4fa 366 ledbar_right.clear();
ddrew73 1:0183a7d38878 367
jn80842 14:2d47b97c2028 368 // first set of voltages read into old_volt_mat
ddrew73 17:fcc378db08f4 369 float_check(floatout,dacval);
ddrew73 17:fcc378db08f4 370 wait(0.2);
jn80842 14:2d47b97c2028 371 read_voltages(originalvoltages);
ddrew73 17:fcc378db08f4 372
jn80842 14:2d47b97c2028 373 // second set for comparison, read into clientdata
ddrew73 17:fcc378db08f4 374 wait(0.2);
ddrew73 16:028a8548ab22 375 compare_voltages(originalvoltages,clientdata,colselect,rowselect,vddval,floatout);
ddrew73 16:028a8548ab22 376
ddrew73 17:fcc378db08f4 377
ddrew73 0:16d2619002d7 378
ddrew73 15:236d90e38951 379 selected = (colselect * 24) + (rowselect+1);
ddrew73 17:fcc378db08f4 380 rowval = originalvoltages[selected-1];
ddrew73 11:b520586dc0a1 381
jn80842 14:2d47b97c2028 382 written = 0;
jn80842 14:2d47b97c2028 383 moved = 0;
ddrew73 11:b520586dc0a1 384
ddrew73 8:725d938b301b 385 //DEBUGGING TOOLS //////////////
ddrew73 11:b520586dc0a1 386
ddrew73 15:236d90e38951 387 pc.printf("\r\n%1.3f %1.3f \r\n", vddval, rowval);
jn80842 14:2d47b97c2028 388 for (int i = 0; i<48; i++) {
jn80842 14:2d47b97c2028 389 pc.printf(" %1.3f ", clientdata[i]);
jn80842 14:2d47b97c2028 390 }
jn80842 14:2d47b97c2028 391 pc.printf(" \r\n \r\n ");
jn80842 14:2d47b97c2028 392 for (int x = 0; x < 48; x++) {
ddrew73 17:fcc378db08f4 393 pc.printf("%1.4f %1.4f %d \r\n", originalvoltages[x], floatout[x], x+1);
jn80842 14:2d47b97c2028 394 }
jn80842 14:2d47b97c2028 395 ////////////////////////
ddrew73 11:b520586dc0a1 396
ddrew73 11:b520586dc0a1 397
ddrew73 17:fcc378db08f4 398 //THIS NEEDS TO GO AWAY!
ddrew73 11:b520586dc0a1 399
ddrew73 11:b520586dc0a1 400 } // END OF SCANBUTTON IF STATEMENT
ddrew73 11:b520586dc0a1 401
jn80842 14:2d47b97c2028 402 if (moved==0 && written == 0) {
ddrew73 7:7fbe8dc5e4fa 403 ledbar_left.writeDisplay();
ddrew73 7:7fbe8dc5e4fa 404 ledbar_right.writeDisplay();
ddrew73 15:236d90e38951 405 lcd_write_voltage_info(vddval*3.3,selected,rowval*3.3);
ddrew73 15:236d90e38951 406
ddrew73 17:fcc378db08f4 407
jn80842 13:209da1dcb6e1 408 // send data to websocket
jn80842 14:2d47b97c2028 409 if (wifi.is_connected()) {
jn80842 14:2d47b97c2028 410 std::string json = build_json(vddval,selected,clientdata);
jn80842 14:2d47b97c2028 411 char * writable = new char[json.size() + 1];
jn80842 14:2d47b97c2028 412 std::copy(json.begin(), json.end(), writable);
jn80842 14:2d47b97c2028 413 writable[json.size()] = '\0';
jn80842 18:f7a00c31ff58 414 pc.printf(writable);
jn80842 18:f7a00c31ff58 415 pc.printf("\r\n");
jn80842 19:fea62e9c1ac4 416 pc.printf("ws is connected ? %d\r\n", ws.is_connected()); ws.send(writable);
jn80842 14:2d47b97c2028 417 delete[] writable;
jn80842 14:2d47b97c2028 418 }
ddrew73 17:fcc378db08f4 419
ddrew73 15:236d90e38951 420
jn80842 14:2d47b97c2028 421 written = 1;
ddrew73 15:236d90e38951 422 } else if (oldselected != selected) {
jn80842 14:2d47b97c2028 423 lcd_write_selected_info(selected);
ddrew73 15:236d90e38951 424
ddrew73 11:b520586dc0a1 425 }
ddrew73 15:236d90e38951 426 oldselected = selected;
ddrew73 5:03d75ad40d5e 427
ddrew73 5:03d75ad40d5e 428
ddrew73 5:03d75ad40d5e 429
ddrew73 5:03d75ad40d5e 430
ddrew73 4:5d27966f93e4 431
ddrew73 11:b520586dc0a1 432
jn80842 18:f7a00c31ff58 433 pc.printf("end of while loop!\r\n");
ddrew73 7:7fbe8dc5e4fa 434
ddrew73 0:16d2619002d7 435
ddrew73 0:16d2619002d7 436
ddrew73 4:5d27966f93e4 437
ddrew73 4:5d27966f93e4 438
ddrew73 4:5d27966f93e4 439
ddrew73 7:7fbe8dc5e4fa 440 } // END OF WHILE(1)
ddrew73 7:7fbe8dc5e4fa 441
ddrew73 7:7fbe8dc5e4fa 442
ddrew73 7:7fbe8dc5e4fa 443
ddrew73 4:5d27966f93e4 444
ddrew73 7:7fbe8dc5e4fa 445 } // END OF INT MAIN
ddrew73 7:7fbe8dc5e4fa 446
ddrew73 7:7fbe8dc5e4fa 447
ddrew73 11:b520586dc0a1 448 /*
ddrew73 11:b520586dc0a1 449 OSCILLISCOPE
ddrew73 7:7fbe8dc5e4fa 450
ddrew73 7:7fbe8dc5e4fa 451
ddrew73 11:b520586dc0a1 452 while (scanbutton == 0){
ddrew73 11:b520586dc0a1 453 if (loopcount = 0){
ddrew73 11:b520586dc0a1 454 timer.reset();
ddrew73 11:b520586dc0a1 455 timer.start();
ddrew73 11:b520586dc0a1 456 begintime = timer.read_ms();
ddrew73 11:b520586dc0a1 457 pressed == 1;
ddrew73 11:b520586dc0a1 458 }
ddrew73 7:7fbe8dc5e4fa 459
ddrew73 11:b520586dc0a1 460 int sn = (colselect + 1)*(rowselect + 1);
ddrew73 11:b520586dc0a1 461 A_in = (sn-1)&2;
ddrew73 11:b520586dc0a1 462 B_in = ((sn-1)/2)%2;
ddrew73 11:b520586dc0a1 463 C_in = ((sn-1)/4)%2;
ddrew73 11:b520586dc0a1 464 D_in = ((sn-1)/8)%2;
ddrew73 11:b520586dc0a1 465 E_in = ((sn-1)/16)%2;
ddrew73 11:b520586dc0a1 466 F_in = ((sn-1)/32)%2;
ddrew73 11:b520586dc0a1 467 volt_buffer[loopcount] = adc.read();
ddrew73 8:725d938b301b 468
ddrew73 8:725d938b301b 469
ddrew73 11:b520586dc0a1 470 loopcount = loopcount +1;
ddrew73 11:b520586dc0a1 471 } // BREAK THE WHILE SCANBUTTON
ddrew73 8:725d938b301b 472
ddrew73 11:b520586dc0a1 473 if (pressed == 1){
ddrew73 11:b520586dc0a1 474 timer.stop();
ddrew73 11:b520586dc0a1 475 endtime = timer.read_ms();
ddrew73 11:b520586dc0a1 476 elapsed = endtime - begintime;
ddrew73 8:725d938b301b 477
ddrew73 11:b520586dc0a1 478 DO ENTIRE SCAN STUFF
ddrew73 8:725d938b301b 479
ddrew73 11:b520586dc0a1 480 pressed = 0;
ddrew73 11:b520586dc0a1 481 }
ddrew73 8:725d938b301b 482
ddrew73 11:b520586dc0a1 483 */