:)

Dependencies:   MbedJSONValue DebounceIn TextLCD USBDevice mbed WebSocketClient cc3000_hostdriver_mbedsocket Adafruit_LEDBackpack_2

Revision:
14:2d47b97c2028
Parent:
13:209da1dcb6e1
Child:
15:236d90e38951
--- a/main.cpp	Wed Dec 03 00:14:32 2014 +0000
+++ b/main.cpp	Mon Dec 08 19:52:15 2014 +0000
@@ -20,6 +20,14 @@
 I2C i2c_right(D7,D6);
 Adafruit_24bargraph ledbar_right=Adafruit_24bargraph(&i2c_right);
 
+void set_led(int index, int color) { // index range from 0-47
+    if (index<24) {
+        ledbar_left.setBar(index,color);
+    } else {
+        ledbar_right.setBar(index-24,color);
+    }
+}
+
 DebounceIn upbutton(PTA16);
 DebounceIn downbutton(PTC12);
 DebounceIn leftbutton(PTC17);
@@ -42,9 +50,25 @@
 
 
 //LCD stuff
-//extLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2);
+//TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2);
 TextLCD lcd(PTC6,PTC5,D5,D4,D3,D2,TextLCD::LCD20x4);
 
+void lcd_write_selected_info(int selected) {
+    lcd.cls();
+    lcd.locate(0,0);
+    lcd.printf("The ToastBoard");
+    lcd.locate(0,1);
+    lcd.printf("Selected row: %d",selected);
+}
+
+void lcd_write_voltage_info(float vddval,int selected,float rowval) {
+    lcd.cls();
+    lcd.locate(0,1);
+    lcd.printf("Vdd: %1.1f V",vddval);
+    lcd.locate(0,0);
+    lcd.printf("Row %d: %1.1f V",selected+1,rowval);
+}
+
 
 //WIFI STUFF
 
@@ -60,44 +84,107 @@
 // plus wifi network SSID, password, security level and smart-configuration flag.
 
 mbed_cc3000::cc3000 wifi(D8, D9, D10, SPI(D11, D12, D13),
-                         "SSID", "PASSWORD", WPA2, false);
+                        "SSID", "PASSWORD", WPA2, false);
 Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
 
-
-// json encoding
-std::string s;
-std::string t;
-std::back_insert_iterator<std::string> json_str = std::back_inserter(t);
-char row[1] = ""; // holder for row tokens
-char rowvoltage[4] = ""; // holder for voltage values
-
-void jsoncopy(const std::string& s, std::back_insert_iterator<std::string> oi) {
-    std::copy(s.begin(), s.end(), oi);
-}
-
 void add_to_json(const std::string& s, std::back_insert_iterator<std::string> oi) {
-    // this chunk of code lifted from the MbedJSONValue library
+    // this chunk of code lifted from the MbedJSONValue 
+    char buf[7];
     for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
-        switch (*i) {
-#define MAP(val, sym) case val: jsoncopy(sym, oi); break
-                MAP('"', "\\\"");
-                MAP('\\', "\\\\");
-                MAP('/', "\\/");
-                MAP('\b', "\\b");
-                MAP('\f', "\\f");
-                MAP('\n', "\\n");
-                MAP('\r', "\\r");
-                MAP('\t', "\\t");
-#undef MAP
-            default:
-                if ((unsigned char)*i < 0x20 || *i == 0x7f) {
-                    char buf[7];
-                    sprintf(buf, "\\u%04x", *i & 0xff);
-                    copy(buf, buf + 6, oi);
-                } else {
-                    *oi++ = *i;
-                }
-                break;
+        if ((unsigned char)*i < 0x20 || *i == 0x7f) {
+            sprintf(buf, "\\u%04x", *i & 0xff);
+            copy(buf, buf + 6, oi);
+        } else {
+            *oi++ = *i;
+        }
+    }
+}
+
+std::string build_json(float vddval,int selected,float clientdata[48]) {
+    std::string s;
+    std::back_insert_iterator<std::string> json_str = std::back_inserter(s);
+    char row[1] = ""; // holder for row tokens
+    char rowvoltage[4] = ""; // holder for voltage values
+    add_to_json("{\"vddval\":",json_str);
+    sprintf(rowvoltage,"%.1f",vddval);
+    add_to_json(rowvoltage,json_str);
+    add_to_json(", \"selected\":",json_str);
+    sprintf(rowvoltage,"%d",selected);
+    add_to_json(", \"rows\": [",json_str);
+    int append_comma = 0;
+    for (int i= 0; i < 48; i++) {
+        if (clientdata[i] != 100.0) { // don't pass on floating row vals
+            if (append_comma == 1) {
+                add_to_json(",",json_str);
+            } else {
+                append_comma = 1;
+            }
+            add_to_json("{\"",json_str);
+            sprintf(row, "%d", i);
+            add_to_json(row,json_str);
+            add_to_json("\":",json_str);
+            sprintf(rowvoltage,"%.1f",clientdata[i]);
+            add_to_json(rowvoltage,json_str);
+            add_to_json("}",json_str);
+        }
+    }
+    add_to_json("]}",json_str);
+    return s;
+}
+
+bool voltages_equal(float voltage1,float voltage2) {
+    return (voltage1 > voltage2-0.01) && (voltage1 < voltage2+0.01);
+}
+
+void read_voltages(float voltages[48]) {
+    int sn = 0;
+    for (int x=0;x<2;x++) { // iterate over two columns of breadboard
+        for (int y=0;y<24;y++) { // iterate over 24 rows of each column
+            sn = (x*24)+y+1; // maybe?????
+            A_in = (sn-1)%2;
+            B_in = ((sn-1)/2)%2;
+            C_in = ((sn-1)/4)%2;
+            D_in = ((sn-1)/8)%2;
+            E_in = ((sn-1)/16)%2;
+            F_in = ((sn-1)/32)%2;
+
+            voltages[sn-1] = adc.read();
+        }
+    }
+}
+
+void compare_voltages(float voltages[48], float clientdata[48], int colselect, int rowselect, float vddval) {
+    // get selected row voltage
+    float rowval = voltages[(colselect*24)+rowselect];
+    for (int i=0;i<48;i++) {
+        int client_index = (colselect*24)+rowselect;
+        int sn = i+1;//(colselect+1)*(rowselect+1); 
+        A_in = (sn-1)%2;
+        B_in = ((sn-1)/2)%2;
+        C_in = ((sn-1)/4)%2;
+        D_in = ((sn-1)/8)%2;
+        E_in = ((sn-1)/16)%2;
+        F_in = ((sn-1)/32)%2;
+
+        float in_val = adc.read();
+        if (!voltages_equal(voltages[i],in_val)) {
+            // this row is floating
+            set_led(i,LED_RED);
+            clientdata[client_index] = 100.0;
+        } else if (voltages_equal(voltages[i],vddval)) {
+            // this row matches vdd
+            set_led(i,LED_RED);
+            clientdata[client_index] = vddval;
+        } else if (voltages_equal(voltages[i],0.0)) {
+            // this row matches ground
+            set_led(i,LED_YELLOW);
+            clientdata[client_index] = 0.0;
+        } else if (voltages_equal(voltages[i],rowval)) {
+            // this row matches selected row
+            set_led(i,LED_GREEN);
+            clientdata[client_index] = rowval;
+        } else {
+            clientdata[client_index] = voltages[i];
         }
     }
 }
@@ -107,11 +194,9 @@
 {
 
     //Scan init
-
-    int con_mat[24][2] = {};
-    float volt_mat[24][2] = {};
+    float originalvoltages[48] = {};
     float clientdata[48] = {};
-    float rowval = 0, vddval = 0;
+    float vddval = 0.0;
 
     //LED init
     int rowselect = 0, moved = 1, colselect = 0, selected = 0;
@@ -125,13 +210,9 @@
 
     //LCD init
     lcd.cls();
-    
-    // wifi init
+    int written = 0;
+
     wifi.init();
-    wifi.connect();
-    // websocket init
-    Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
-    ws.connect();
 
     //Osci
     int loopcount = 0, pressed = 0;
@@ -139,34 +220,38 @@
 
 
     while(1) {
+        if (wifi.is_connected() == false) {
+            pc.printf("trying to connect to wifi\r\n");
+            if (wifi.connect() == -1) {
+                pc.printf("Failed to connect\r\n");
+            } else {
+                pc.printf("IP address: %s \r\n", wifi.getIPAddress());
+            }
+        } else {
+            Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
+            ws.connect();
+        }
 
         //Display
-
-        if (colselect == 0 && moved == 1) {
+        if (moved ==1) {
             ledbar_left.clear();
             ledbar_right.clear();
-            ledbar_left.setBar(rowselect,LED_GREEN);
+            set_led((colselect*24+rowselect),LED_GREEN);
             ledbar_left.writeDisplay();
             ledbar_right.writeDisplay();
-        } else if (colselect == 1 && moved == 1) {
-            ledbar_left.clear();
-            ledbar_right.clear();
-            ledbar_right.setBar(rowselect,LED_GREEN);
-            ledbar_right.writeDisplay();
-            ledbar_left.writeDisplay();
         }
         //Moving the selected row
         if (upbutton.read() == 0) {
-
             rowselect = rowselect-1;
             moved = 1;
+            written = 0;
             wait(0.75);
         }
 
         if (downbutton.read() == 0) {
-
             rowselect = rowselect+1;
             moved = 1;
+            written = 0;
             wait(0.75);
         }
 
@@ -174,17 +259,16 @@
             colselect = 0;
             ledbar_right.clear();
             moved =1;
+            written = 0;
             wait(0.75);
-
         }
 
         if (rightbutton.read() == 0) {
             colselect = 1;
             ledbar_left.clear();
             moved =1;
+            written = 0;
             wait(0.75);
-
-
         }
 
         //Boundary checking
@@ -207,164 +291,57 @@
 
             vddval = adc.read();
 
-            //SET MULTIPLEXER TO READ SELECTED ROW
-            // Sample the 'selected' node
-
-            int sn = (colselect + 1)*(rowselect + 1);
-            A_in = (sn-1)&2;
-            B_in = ((sn-1)/2)%2;
-            C_in = ((sn-1)/4)%2;
-            D_in = ((sn-1)/8)%2;
-            E_in = ((sn-1)/16)%2;
-            F_in = ((sn-1)/32)%2;
-            rowval = adc.read();
-
             ledbar_left.clear();
             ledbar_right.clear();
 
-            for (int u = 0; u < 2; u++) {
-
-                for (int x = 0; x < 2; x++) {
-                    for (int y = 0; y < 24; y++) {
-
-
-                        int sn = (colselect+1)*(rowselect+1);
-                        A_in = (sn-1)&2;
-                        B_in = ((sn-1)/2)%2;
-                        C_in = ((sn-1)/4)%2;
-                        D_in = ((sn-1)/8)%2;
-                        E_in = ((sn-1)/16)%2;
-                        F_in = ((sn-1)/32)%2;
-
-                        float in_val = adc.read();
-
-
-                        if (u == 0) {
-                            old_volt_map[x][y] = in_val
-                        } else {
-                            volt_map[x][y] = in_val
-                            if (old_volt_map[x][y] != volt_map[x][y] ) {
-                                volt_map[x][y] = 100;
-                            }
-                            if (x == 1) {
-                                int z = y+24;
-                                clientdata[z] = in_val;
-                            } else {
-                                clientdata[y] = in_val;
-                            }
+            // first set of voltages read into old_volt_mat
+            read_voltages(originalvoltages);
+            // second set for comparison, read into clientdata
+            compare_voltages(originalvoltages,clientdata,colselect,rowselect,vddval);
 
-                           
-                            if ((in_val > vddval-0.01) && (in_val < vddval+0.01)) {
-                                con_mat[x][y] = 2;
-                                if (x == 0) {
-                                    ledbar_left.setBar(y,LED_RED);
-                                } else {
-                                    ledbar_right.setBar(y,LED_RED);
-                                }
-                            } else if (in_val == 0) {
-                                con_mat[x][y] = 3;
-                                if (x == 0) {
-                                    ledbar_left.setBar(y,LED_YELLOW);
-                                } else {
-                                    ledbar_right.setBar(y,LED_YELLOW);
-                                }
-                            } else if ((in_val > rowval-0.01) && (in_val < rowval+0.01)) {
-                                con_mat[x][y] = 1;
-                                if (x == 0) {
-                                    ledbar_left.setBar(y,LED_GREEN);
-                                } else {
-                                    ledbar_right.setBar(y,LED_GREEN);
-                                }
-                            } 
-                            else if (volt_mat[x][y]==100) {
-                                con_mat[x][y] = 4;
-                                if (x == 0) {
-                                    ledbar_left.setBar(y,LED_OFF);
-                                } else {
-                                    ledbar_right.setBar(y,LED_OFF);
-                                }
-                            }
-                            else {
-                                con_mat[x][y] = 0;
-                            }
+            selected = rowselect;
+            if (colselect == 1) {
+                selected = selected+24;
+            }
 
-                }}} //END OF ROWSCAN FOR LOOP
-
-                selected = rowselect;
-                if (colselect == 1) {
-                    selected = selected+24;
-                }
-
-                
-                //STUFF INTO JSON FORMAT
-                add_to_json("{\"vddval\":",json_str);
-                sprintf(rowvoltage,"%.f",vddval);
-                add_to_json(rowvoltage,json_str);
-                add_to_json(", \"selected\":",json_str);
-                selected = rowselect;
-                sprintf(rowvoltage,"%d",selected);
-                add_to_json(", \"rows\": [",json_str);
-                
-                char str[1];
-                for (int i= 0; i < 48; i++) {
-                    if (i != 0) {
-                        add_to_json(",",json_str);
-                    }
-                    add_to_json("{\"",json_str);
-                    sprintf(str, "%d", i);
-                    add_to_json(str,json_str);
-                    add_to_json("\":",json_str);
-                    sprintf(rowvoltage,"%.f",clientdata[i]);
-                    add_to_json(rowvoltage,json_str);
-                    add_to_json("}",json_str);
-                }
-                
-                
-
-                moved = 0;
-
+            written = 0;
+            moved = 0;
 
                 //DEBUGGING TOOLS //////////////
 
-                pc.printf("\r\n%1.3f %1.3f \r\n", vddval, rowval);
-                for (int i = 0; i<48; i++) {
-                    pc.printf(" %1.3f ", clientdata[i]);
-                }
-                pc.printf(" \r\n \r\n ");
-                for (int x = 0; x < 2; x++) {
-                    for (int y = 0; y < 24; y++) {
-                        pc.printf("%1.3f \r\n", volt_mat[x][y]);
-                    }
-                }
-                ////////////////////////
+            pc.printf("\r\n%1.3f %1.3f \r\n", vddval, clientdata[selected]);
+            for (int i = 0; i<48; i++) {
+                pc.printf(" %1.3f ", clientdata[i]);
+            }
+            pc.printf(" \r\n \r\n ");
+            for (int x = 0; x < 48; x++) {
+                    pc.printf("%1.3f \r\n", originalvoltages[x]);
+            }
+            ////////////////////////
 
 
-                wait(1);  //THIS NEEDS TO GO AWAY! :}
+            wait(1);  //THIS NEEDS TO GO AWAY! 
 
             } // END OF SCANBUTTON IF STATEMENT
 
-            if (moved==0)  {
+            if (moved==0 && written == 0)  {
                 ledbar_left.writeDisplay();
                 ledbar_right.writeDisplay();
-                lcd.cls();
-                lcd.locate(0,1);
-                lcd.printf("Vdd: %1.1f V",vddval);
-                lcd.locate(0,0);
-                lcd.printf("Row %d: %1.1f V",selected,rowval);
+                lcd_write_voltage_info(vddval,selected+1,clientdata[selected]);
                 // send data to websocket
-                char * writable = new char[t.size() + 1];
-                std::copy(t.begin(), t.end(), writable);
-                writable[t.size()] = '\0';
-                ws.send(writable);
-                delete[] writable;
-            } else {
-                lcd.cls();
-                string programname = "The ToastBoard";
-                string instructions = "Select a row";
-                lcd.locate(0,0);
-                lcd.printf("%s",programname.c_str());
-                lcd.locate(0,1);
-                lcd.printf("%s",instructions.c_str());
+                if (wifi.is_connected()) {
+                    std::string json = build_json(vddval,selected,clientdata);
+                    char * writable = new char[json.size() + 1];
+                    std::copy(json.begin(), json.end(), writable);
+                    writable[json.size()] = '\0';
+                    ws.send(writable);
+                    delete[] writable;
+                    delete[] &json;
+                }
+                written = 1;
+            } else if (written == 0) {
+                lcd_write_selected_info(selected);
+                written = 1;
             }
 
 
@@ -392,12 +369,12 @@
      add another "for" around the two for the 48 row checker:
 
     if (u == 0){
-    old_volt_map[x,y] = in_val
+    old_volt_mat[x,y] = in_val
     }
     else{
-    volt_map[x,y] = in_val
-    if (old_volt_map[x,y] != volt_map[x,y] ) {
-        volt_map[x,y] = 100;
+    volt_mat[x,y] = in_val
+    if (old_volt_mat[x,y] != volt_mat[x,y] ) {
+        volt_mat[x,y] = 100;
         }
 
     PUT ALL ROW LIGHTING (CON_MAT) STUFF HERE