Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MbedJSONValue DebounceIn TextLCD USBDevice mbed WebSocketClient cc3000_hostdriver_mbedsocket Adafruit_LEDBackpack_2
main.cpp
00001 #include "mbed.h" 00002 #include <string> 00003 #include "Adafruit_LEDBackpack.h" 00004 #include "Adafruit_GFX.h" 00005 #include "TextLCD.h" 00006 #include "DebounceIn.h" 00007 #include "cc3000.h" 00008 #include "Websocket.h" 00009 00010 //Debug 00011 Serial pc(USBTX, USBRX); // tx, rx 00012 00013 //For oscilliscope 00014 Timer timer; 00015 00016 //LED stuff 00017 I2C i2c_left(PTC11,PTC10); 00018 Adafruit_24bargraph ledbar_left=Adafruit_24bargraph(&i2c_left); 00019 00020 I2C i2c_right(D7,D6); 00021 Adafruit_24bargraph ledbar_right=Adafruit_24bargraph(&i2c_right); 00022 00023 void set_led(int index, int color) { // index range from 0-47 00024 if (index<24) { 00025 ledbar_left.setBar(index,color); 00026 } else { 00027 ledbar_right.setBar(index-24,color); 00028 } 00029 } 00030 00031 DebounceIn upbutton(PTA16); 00032 DebounceIn downbutton(PTC12); 00033 DebounceIn leftbutton(PTC17); 00034 DebounceIn rightbutton(PTC16); 00035 00036 //Scanner stuff 00037 00038 DebounceIn scanbutton(PTC13); 00039 //DebounceIn scanbutton(D7); 00040 00041 DigitalOut A_in(PTB10); 00042 DigitalOut B_in(PTB11); 00043 DigitalOut C_in(PTE2); 00044 DigitalOut D_in(PTE3); 00045 DigitalOut E_in(PTE4); 00046 DigitalOut F_in(PTE5); 00047 00048 DigitalOut scan_select(PTC3); 00049 00050 AnalogIn adc(PTB0); 00051 AnalogOut dac(PTE30); 00052 00053 00054 00055 //LCD stuff 00056 //TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2); 00057 TextLCD lcd(PTC6,PTC5,D5,D4,D3,D2,TextLCD::LCD20x4); 00058 00059 void lcd_write_selected_info(int selected) { 00060 lcd.cls(); 00061 lcd.locate(0,0); 00062 lcd.printf("The ToastBoard"); 00063 lcd.locate(0,1); 00064 lcd.printf("Selected row: %d",selected); 00065 } 00066 00067 void lcd_write_voltage_info(float vddval,int selected,float rowval) { 00068 lcd.cls(); 00069 lcd.locate(0,1); 00070 lcd.printf("Vdd: %1.2f V",vddval); 00071 lcd.locate(0,0); 00072 lcd.printf("Row %d: %1.2f V",selected,rowval); 00073 } 00074 00075 00076 //WIFI STUFF 00077 00078 00079 // cc3000 KL25Z wifi connection 00080 // we need to define connection pins for: 00081 // - IRQ => (pin D3) 00082 // - Enable => (pin D5) 00083 // - SPI CS => (pin D10) 00084 // - SPI MOSI => (pin D11) 00085 // - SPI MISO => (pin D12) 00086 // - SPI CLK => (pin D13) 00087 // plus wifi network SSID, password, security level and smart-configuration flag. 00088 00089 00090 00091 mbed_cc3000::cc3000 wifi(D8, D9, D10, SPI(D11, D12, D13), // spi goes mosi, miso, clk 00092 "EECS-PSK", "Thequickbrown", WPA2, false); 00093 Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw"); 00094 00095 00096 void add_to_json(const std::string& s, std::back_insert_iterator<std::string> oi) { 00097 // this chunk of code lifted from the MbedJSONValue 00098 char buf[7]; 00099 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { 00100 if ((unsigned char)*i < 0x20 || *i == 0x7f) { 00101 sprintf(buf, "\\u%04x", *i & 0xff); 00102 copy(buf, buf + 6, oi); 00103 } else { 00104 *oi++ = *i; 00105 } 00106 } 00107 } 00108 00109 std::string build_json(float vddval,int selected,float clientdata[48]) { 00110 std::string s; 00111 std::back_insert_iterator<std::string> json_str = std::back_inserter(s); 00112 char row[1] = ""; // holder for row tokens 00113 char rowvoltage[4] = ""; // holder for voltage values 00114 add_to_json("{\"vddval\":",json_str); 00115 sprintf(rowvoltage,"%.1f",vddval*3.3); 00116 add_to_json(rowvoltage,json_str); 00117 add_to_json(", \"selected\":",json_str); 00118 sprintf(row,"%d",selected); 00119 add_to_json(row,json_str); 00120 add_to_json(", \"rows\": [",json_str); 00121 int append_comma = 0; 00122 for (int i= 0; i < 48; i++) { 00123 if (clientdata[i] != 100.0) { // don't pass on floating row vals 00124 if (append_comma == 1) { 00125 add_to_json(",",json_str); 00126 } else { 00127 append_comma = 1; 00128 } 00129 add_to_json("{\"",json_str); 00130 sprintf(row, "%d", i); 00131 add_to_json(row,json_str); 00132 add_to_json("\":",json_str); 00133 sprintf(rowvoltage,"%.1f",clientdata[i]*3.3); 00134 add_to_json(rowvoltage,json_str); 00135 add_to_json("}",json_str); 00136 } 00137 } 00138 add_to_json("]}",json_str); 00139 return s; 00140 } 00141 00142 00143 00144 bool voltages_equal(float voltage1,float voltage2) { 00145 return (voltage1 > voltage2-0.005) && (voltage1 < voltage2+0.005); 00146 } 00147 00148 void read_voltages(float voltages[48]) { 00149 float voltbuffer[48] = {}; 00150 scan_select = 0; 00151 int sn = 0; 00152 for (int i=0;i<48;i++) { 00153 sn = i; 00154 D_in = (sn)%2; 00155 E_in = ((sn)/2)%2; 00156 F_in = ((sn)/4)%2; 00157 A_in = ((sn)/8)%2; 00158 B_in = ((sn)/16)%2; 00159 C_in = ((sn)/32)%2; 00160 00161 for (int j=0;j<10;j++) { 00162 wait(0.0025); 00163 voltbuffer[sn] = (adc.read()/10) + voltbuffer[sn]; 00164 } 00165 voltages[sn] = voltbuffer[sn]; 00166 } 00167 } 00168 00169 void float_check(float voltages[48],float dacval) { 00170 int sn = 0; 00171 for (int i=0;i<48;i++) { // iterate over two columns of breadboard 00172 // iterate over 24 rows of each column 00173 sn = i; 00174 D_in = (sn)%2; 00175 E_in = ((sn)/2)%2; 00176 F_in = ((sn)/4)%2; 00177 A_in = ((sn)/8)%2; 00178 B_in = ((sn)/16)%2; 00179 C_in = ((sn)/32)%2; 00180 00181 scan_select = 1; 00182 scan_select = 0; 00183 wait(0.02); 00184 float in_val = adc.read(); 00185 voltages[sn] = in_val; 00186 00187 wait(0.035); 00188 00189 scan_select = 1; 00190 scan_select = 0; 00191 wait(0.02); 00192 in_val = adc.read(); 00193 00194 00195 if ((in_val < voltages[sn]-0.008) || (in_val > voltages[sn]+0.008)){ 00196 voltages[sn] = 100.0; 00197 00198 } 00199 00200 00201 } 00202 } 00203 00204 00205 00206 void compare_voltages(float voltages[48], float clientdata[48], int colselect, int rowselect, float vddval, float floatchecked[48], float newvoltages[48]) { 00207 // get selected row voltage 00208 float voltbuffer[48] = {}; 00209 scan_select = 0; 00210 float rowval = voltages[(colselect*24)+ rowselect]; 00211 for (int i=0;i<48;i++) { 00212 int sn = i;//(colselect+1)*(rowselect+1); 00213 D_in = (sn)%2; 00214 E_in = ((sn)/2)%2; 00215 F_in = ((sn)/4)%2; 00216 A_in = ((sn)/8)%2; 00217 B_in = ((sn)/16)%2; 00218 C_in = ((sn)/32)%2; 00219 00220 for (int j=0;j<10;j++) { 00221 wait(0.0025); 00222 voltbuffer[sn] = (adc.read()/10) + voltbuffer[sn]; 00223 } 00224 newvoltages[sn] = voltbuffer[sn]; 00225 00226 // float in_val = adc.read(); 00227 // newvoltages[i] = in_val; 00228 00229 if (floatchecked[sn] == 100){ 00230 set_led(i,LED_OFF); 00231 clientdata[i] = 100.0; 00232 } 00233 else { 00234 00235 00236 if (!voltages_equal(voltages[i],newvoltages[sn])) { 00237 // this row is floating 00238 set_led(i,LED_OFF); 00239 clientdata[i] = 100.0; 00240 } else if (voltages_equal(voltages[i],vddval) || voltages_equal(newvoltages[i],vddval)) { 00241 // this row matches vdd 00242 set_led(i,LED_RED); 00243 clientdata[i] = vddval; 00244 } else if (voltages_equal(voltages[i],0.0) || voltages_equal(newvoltages[i],0.0)) { 00245 // this row matches ground 00246 set_led(i,LED_YELLOW); 00247 clientdata[i] = 0.0; 00248 } else if (voltages_equal(voltages[i],rowval) || voltages_equal(newvoltages[i],rowval)) { 00249 // this row matches selected row 00250 set_led(i,LED_GREEN); 00251 clientdata[i] = rowval; 00252 } else { 00253 set_led(i,LED_OFF); 00254 clientdata[i] = voltages[i]; 00255 } 00256 } // else if closing 00257 } //for loop closing 00258 } //compare_voltages function closing 00259 00260 00261 int main() 00262 { 00263 00264 //Scan init 00265 float originalvoltages[48] = {}; 00266 float newvoltages[48] = {}; 00267 00268 float clientdata[48] = {}; 00269 float vddval = 0.0; 00270 float rowval = 0.0; 00271 float dacval = 0.1; 00272 00273 float floatout[48] = {}; 00274 00275 //LED init 00276 int rowselect = 0, moved = 1, colselect = 0, selected = 0, oldselected = 49; 00277 ledbar_left.begin(0x70); 00278 ledbar_left.clear(); 00279 ledbar_left.writeDisplay(); 00280 00281 ledbar_right.begin(0x70); 00282 ledbar_right.clear(); 00283 ledbar_right.writeDisplay(); 00284 00285 //LCD init 00286 lcd.cls(); 00287 int written = 0; 00288 00289 wifi.init(); 00290 char * writable; 00291 00292 //Osci 00293 int loopcount = 0, pressed = 0; 00294 float begintime = 0, endtime = 0, elapsed = 0; 00295 00296 int datatosend = 0; 00297 00298 00299 00300 while(1) { 00301 if (wifi.is_connected() == false) { 00302 pc.printf("trying to connect to v\r\n"); 00303 if (wifi.connect() == -1) { 00304 pc.printf("Failed to connect\r\n"); 00305 } else { 00306 pc.printf("IP address: %s \r\n", wifi.getIPAddress()); 00307 } 00308 } else { 00309 if (datatosend) { 00310 pc.printf("json in queue to be sent out\r\n"); 00311 Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw"); 00312 ws.connect(); 00313 if (ws.send(writable) != -1) { 00314 datatosend = 0; 00315 pc.printf("we have sent a message!\r\n"); 00316 delete writable; 00317 } 00318 } 00319 00320 } 00321 00322 00323 00324 //Display 00325 if (moved ==1) { 00326 ledbar_left.clear(); 00327 ledbar_right.clear(); 00328 set_led((colselect*24+rowselect),LED_GREEN); 00329 ledbar_left.writeDisplay(); 00330 ledbar_right.writeDisplay(); 00331 } 00332 //Moving the selected row 00333 if (upbutton.read() == 0) { 00334 rowselect = rowselect-1; 00335 moved = 1; 00336 written = 0; 00337 pc.printf("moved up\r\n"); 00338 wait(0.3); 00339 } 00340 00341 if (downbutton.read() == 0) { 00342 rowselect = rowselect+1; 00343 moved = 1; 00344 written = 0; 00345 pc.printf("moved down\r\n"); 00346 wait(0.3); 00347 } 00348 00349 if (leftbutton.read() == 0) { 00350 colselect = 0; 00351 ledbar_right.clear(); 00352 moved =1; 00353 written = 0; 00354 pc.printf("moved left\r\n"); 00355 wait(0.3); 00356 } 00357 00358 if (rightbutton.read() == 0) { 00359 colselect = 1; 00360 ledbar_left.clear(); 00361 moved =1; 00362 written = 0; 00363 pc.printf("moved right\r\n"); 00364 wait(0.3); 00365 } 00366 00367 //Boundary checking 00368 if (rowselect > 23) { 00369 rowselect = 23; 00370 } 00371 if (rowselect < 0) { 00372 rowselect = 0; 00373 } 00374 00375 selected = (colselect * 24) + (rowselect+1); 00376 // pc.printf("selected is %d\r\n",selected); 00377 00378 //Implementing scanning 00379 00380 if (scanbutton.read() == 0) { 00381 00382 lcd.cls(); 00383 lcd.locate(0,0); 00384 lcd.printf("Scanning...."); 00385 00386 float vddbuff = 0; 00387 A_in = 0; 00388 B_in = 1; 00389 C_in = 1; 00390 D_in = 1; 00391 E_in = 0; 00392 F_in = 0; 00393 00394 for (int j=0;j<10;j++) { 00395 wait(0.0025); 00396 vddbuff = (adc.read()/10) + vddbuff; 00397 } 00398 vddval = vddbuff; 00399 00400 00401 00402 00403 ledbar_left.clear(); 00404 ledbar_right.clear(); 00405 00406 // first set of voltages read into old_volt_mat 00407 00408 //float_check(floatout,dacval); 00409 wait(0.2); 00410 read_voltages(originalvoltages); 00411 // second set for comparison, read into clientdata 00412 wait(0.2); 00413 compare_voltages(originalvoltages,clientdata,colselect,rowselect,vddval,floatout,newvoltages); 00414 00415 00416 00417 selected = (colselect * 24) + (rowselect+1); 00418 rowval = originalvoltages[selected-1]; 00419 00420 written = 0; 00421 moved = 0; 00422 00423 //DEBUGGING TOOLS ////////////// 00424 00425 pc.printf("\r\n%1.3f %1.3f \r\n", vddval, rowval); 00426 for (int i = 0; i<48; i++) { 00427 pc.printf(" %1.4f ", clientdata[i]); 00428 } 00429 pc.printf(" \r\n \r\n "); 00430 for (int x = 0; x < 48; x++) { 00431 pc.printf("%1.4f %1.4f %d \r\n", originalvoltages[x], newvoltages[x], x+1); 00432 } 00433 //////////////////////// 00434 00435 00436 //THIS NEEDS TO GO AWAY! 00437 00438 } // END OF SCANBUTTON IF STATEMENT 00439 00440 if (moved==0 && written == 0) { 00441 ledbar_left.writeDisplay(); 00442 ledbar_right.writeDisplay(); 00443 lcd_write_voltage_info(vddval*3.3,selected,rowval*3.3); 00444 00445 00446 // send data to websocket 00447 datatosend = 1; 00448 std::string json = build_json(vddval,selected,clientdata); 00449 // delete writable; 00450 writable = new char[json.size() + 1]; 00451 std::copy(json.begin(), json.end(), writable); 00452 writable[json.size()] = '\0'; 00453 pc.printf(writable); 00454 pc.printf("\r\n"); 00455 00456 00457 00458 written = 1; 00459 } else if (oldselected != selected) { 00460 lcd_write_selected_info(selected); 00461 00462 } 00463 oldselected = selected; 00464 00465 00466 00467 00468 00469 00470 00471 00472 00473 00474 00475 00476 } // END OF WHILE(1) 00477 00478 00479 00480 00481 } // END OF INT MAIN 00482 00483 00484 /* 00485 OSCILLISCOPE 00486 00487 00488 while (scanbutton == 0){ 00489 if (loopcount = 0){ 00490 timer.reset(); 00491 timer.start(); 00492 begintime = timer.read_ms(); 00493 pressed == 1; 00494 } 00495 00496 int sn = (colselect + 1)*(rowselect + 1); 00497 A_in = (sn-1)&2; 00498 B_in = ((sn-1)/2)%2; 00499 C_in = ((sn-1)/4)%2; 00500 D_in = ((sn-1)/8)%2; 00501 E_in = ((sn-1)/16)%2; 00502 F_in = ((sn-1)/32)%2; 00503 volt_buffer[loopcount] = adc.read(); 00504 00505 00506 loopcount = loopcount +1; 00507 } // BREAK THE WHILE SCANBUTTON 00508 00509 if (pressed == 1){ 00510 timer.stop(); 00511 endtime = timer.read_ms(); 00512 elapsed = endtime - begintime; 00513 00514 DO ENTIRE SCAN STUFF 00515 00516 pressed = 0; 00517 } 00518 00519 */
Generated on Fri Jul 22 2022 22:24:44 by
1.7.2