:)
Dependencies: MbedJSONValue DebounceIn TextLCD USBDevice mbed WebSocketClient cc3000_hostdriver_mbedsocket Adafruit_LEDBackpack_2
main.cpp@18:f7a00c31ff58, 2014-12-10 (annotated)
- Committer:
- jn80842
- Date:
- Wed Dec 10 01:25:43 2014 +0000
- Revision:
- 18:f7a00c31ff58
- Parent:
- 17:fcc378db08f4
- Child:
- 19:fea62e9c1ac4
bug fixes for json
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" |
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 | 14:2d47b97c2028 | 114 | sprintf(rowvoltage,"%.1f",vddval); |
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 | 14:2d47b97c2028 | 132 | sprintf(rowvoltage,"%.1f",clientdata[i]); |
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; |
ddrew73 | 15:236d90e38951 | 149 | for (int i=0;i<48;i++) { // iterate over two columns of breadboard |
ddrew73 | 15:236d90e38951 | 150 | // iterate over 24 rows of each column |
ddrew73 | 16:028a8548ab22 | 151 | sn = i; |
ddrew73 | 15:236d90e38951 | 152 | D_in = (sn)%2; |
ddrew73 | 15:236d90e38951 | 153 | E_in = ((sn)/2)%2; |
ddrew73 | 15:236d90e38951 | 154 | F_in = ((sn)/4)%2; |
ddrew73 | 15:236d90e38951 | 155 | A_in = ((sn)/8)%2; |
ddrew73 | 15:236d90e38951 | 156 | B_in = ((sn)/16)%2; |
ddrew73 | 15:236d90e38951 | 157 | C_in = ((sn)/32)%2; |
jn80842 | 14:2d47b97c2028 | 158 | |
ddrew73 | 15:236d90e38951 | 159 | voltages[sn] = adc.read(); |
ddrew73 | 15:236d90e38951 | 160 | |
jn80842 | 14:2d47b97c2028 | 161 | } |
jn80842 | 14:2d47b97c2028 | 162 | } |
jn80842 | 14:2d47b97c2028 | 163 | |
ddrew73 | 16:028a8548ab22 | 164 | void float_check(float voltages[48],float dacval) { |
ddrew73 | 16:028a8548ab22 | 165 | int sn = 0; |
ddrew73 | 16:028a8548ab22 | 166 | for (int i=0;i<48;i++) { // iterate over two columns of breadboard |
ddrew73 | 16:028a8548ab22 | 167 | // iterate over 24 rows of each column |
ddrew73 | 16:028a8548ab22 | 168 | sn = i; |
ddrew73 | 16:028a8548ab22 | 169 | D_in = (sn)%2; |
ddrew73 | 16:028a8548ab22 | 170 | E_in = ((sn)/2)%2; |
ddrew73 | 16:028a8548ab22 | 171 | F_in = ((sn)/4)%2; |
ddrew73 | 16:028a8548ab22 | 172 | A_in = ((sn)/8)%2; |
ddrew73 | 16:028a8548ab22 | 173 | B_in = ((sn)/16)%2; |
ddrew73 | 16:028a8548ab22 | 174 | C_in = ((sn)/32)%2; |
ddrew73 | 16:028a8548ab22 | 175 | |
ddrew73 | 16:028a8548ab22 | 176 | scan_select = 1; |
ddrew73 | 16:028a8548ab22 | 177 | scan_select = 0; |
ddrew73 | 17:fcc378db08f4 | 178 | wait(0.02); |
ddrew73 | 16:028a8548ab22 | 179 | float in_val = adc.read(); |
ddrew73 | 16:028a8548ab22 | 180 | voltages[sn] = in_val; |
ddrew73 | 16:028a8548ab22 | 181 | |
ddrew73 | 17:fcc378db08f4 | 182 | wait(0.035); |
ddrew73 | 16:028a8548ab22 | 183 | |
ddrew73 | 16:028a8548ab22 | 184 | scan_select = 1; |
ddrew73 | 16:028a8548ab22 | 185 | scan_select = 0; |
ddrew73 | 17:fcc378db08f4 | 186 | wait(0.02); |
ddrew73 | 16:028a8548ab22 | 187 | in_val = adc.read(); |
ddrew73 | 16:028a8548ab22 | 188 | |
ddrew73 | 16:028a8548ab22 | 189 | |
ddrew73 | 17:fcc378db08f4 | 190 | if ((in_val < voltages[sn]-0.006) || (in_val > voltages[sn]+0.006)){ |
ddrew73 | 16:028a8548ab22 | 191 | voltages[sn] = 100; |
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 | |
ddrew73 | 16:028a8548ab22 | 201 | void compare_voltages(float voltages[48], float clientdata[48], int colselect, int rowselect, float vddval, float floatchecked[48]) { |
jn80842 | 14:2d47b97c2028 | 202 | // get selected row voltage |
ddrew73 | 16:028a8548ab22 | 203 | scan_select = 0; |
ddrew73 | 15:236d90e38951 | 204 | float rowval = voltages[(colselect*24)+ rowselect]; |
jn80842 | 14:2d47b97c2028 | 205 | for (int i=0;i<48;i++) { |
jn80842 | 14:2d47b97c2028 | 206 | int client_index = (colselect*24)+rowselect; |
ddrew73 | 15:236d90e38951 | 207 | int sn = i;//(colselect+1)*(rowselect+1); |
ddrew73 | 15:236d90e38951 | 208 | D_in = (sn)%2; |
ddrew73 | 15:236d90e38951 | 209 | E_in = ((sn)/2)%2; |
ddrew73 | 15:236d90e38951 | 210 | F_in = ((sn)/4)%2; |
ddrew73 | 15:236d90e38951 | 211 | A_in = ((sn)/8)%2; |
ddrew73 | 15:236d90e38951 | 212 | B_in = ((sn)/16)%2; |
ddrew73 | 15:236d90e38951 | 213 | C_in = ((sn)/32)%2; |
jn80842 | 14:2d47b97c2028 | 214 | |
jn80842 | 14:2d47b97c2028 | 215 | float in_val = adc.read(); |
ddrew73 | 15:236d90e38951 | 216 | |
ddrew73 | 16:028a8548ab22 | 217 | if (floatchecked[sn] == 100){ |
ddrew73 | 16:028a8548ab22 | 218 | set_led(i,LED_OFF); |
ddrew73 | 16:028a8548ab22 | 219 | clientdata[client_index] = 100.0; |
ddrew73 | 16:028a8548ab22 | 220 | } |
ddrew73 | 16:028a8548ab22 | 221 | else { |
ddrew73 | 15:236d90e38951 | 222 | |
jn80842 | 14:2d47b97c2028 | 223 | if (!voltages_equal(voltages[i],in_val)) { |
jn80842 | 14:2d47b97c2028 | 224 | // this row is floating |
ddrew73 | 15:236d90e38951 | 225 | set_led(i,LED_OFF); |
jn80842 | 14:2d47b97c2028 | 226 | clientdata[client_index] = 100.0; |
jn80842 | 14:2d47b97c2028 | 227 | } else if (voltages_equal(voltages[i],vddval)) { |
jn80842 | 14:2d47b97c2028 | 228 | // this row matches vdd |
jn80842 | 14:2d47b97c2028 | 229 | set_led(i,LED_RED); |
jn80842 | 14:2d47b97c2028 | 230 | clientdata[client_index] = vddval; |
jn80842 | 14:2d47b97c2028 | 231 | } else if (voltages_equal(voltages[i],0.0)) { |
jn80842 | 14:2d47b97c2028 | 232 | // this row matches ground |
jn80842 | 14:2d47b97c2028 | 233 | set_led(i,LED_YELLOW); |
jn80842 | 14:2d47b97c2028 | 234 | clientdata[client_index] = 0.0; |
jn80842 | 14:2d47b97c2028 | 235 | } else if (voltages_equal(voltages[i],rowval)) { |
jn80842 | 14:2d47b97c2028 | 236 | // this row matches selected row |
jn80842 | 14:2d47b97c2028 | 237 | set_led(i,LED_GREEN); |
jn80842 | 14:2d47b97c2028 | 238 | clientdata[client_index] = rowval; |
jn80842 | 14:2d47b97c2028 | 239 | } else { |
ddrew73 | 15:236d90e38951 | 240 | set_led(i,LED_OFF); |
jn80842 | 14:2d47b97c2028 | 241 | clientdata[client_index] = voltages[i]; |
jn80842 | 12:c4b046975527 | 242 | } |
ddrew73 | 16:028a8548ab22 | 243 | } // else if closing |
ddrew73 | 16:028a8548ab22 | 244 | } //for loop closing |
ddrew73 | 16:028a8548ab22 | 245 | } //compare_voltages function closing |
jn80842 | 12:c4b046975527 | 246 | |
ddrew73 | 7:7fbe8dc5e4fa | 247 | |
ddrew73 | 0:16d2619002d7 | 248 | int main() |
ddrew73 | 0:16d2619002d7 | 249 | { |
ddrew73 | 0:16d2619002d7 | 250 | |
ddrew73 | 0:16d2619002d7 | 251 | //Scan init |
jn80842 | 14:2d47b97c2028 | 252 | float originalvoltages[48] = {}; |
ddrew73 | 7:7fbe8dc5e4fa | 253 | float clientdata[48] = {}; |
jn80842 | 14:2d47b97c2028 | 254 | float vddval = 0.0; |
ddrew73 | 15:236d90e38951 | 255 | float rowval = 0.0; |
ddrew73 | 16:028a8548ab22 | 256 | float dacval = 0.1; |
ddrew73 | 16:028a8548ab22 | 257 | |
ddrew73 | 16:028a8548ab22 | 258 | float floatout[48] = {}; |
ddrew73 | 0:16d2619002d7 | 259 | |
ddrew73 | 0:16d2619002d7 | 260 | //LED init |
ddrew73 | 15:236d90e38951 | 261 | int rowselect = 0, moved = 1, colselect = 0, selected = 0, oldselected = 49; |
ddrew73 | 4:5d27966f93e4 | 262 | ledbar_left.begin(0x70); |
ddrew73 | 4:5d27966f93e4 | 263 | ledbar_left.clear(); |
ddrew73 | 4:5d27966f93e4 | 264 | ledbar_left.writeDisplay(); |
ddrew73 | 5:03d75ad40d5e | 265 | |
ddrew73 | 4:5d27966f93e4 | 266 | ledbar_right.begin(0x70); |
ddrew73 | 4:5d27966f93e4 | 267 | ledbar_right.clear(); |
ddrew73 | 4:5d27966f93e4 | 268 | ledbar_right.writeDisplay(); |
ddrew73 | 0:16d2619002d7 | 269 | |
ddrew73 | 0:16d2619002d7 | 270 | //LCD init |
ddrew73 | 0:16d2619002d7 | 271 | lcd.cls(); |
jn80842 | 14:2d47b97c2028 | 272 | int written = 0; |
jn80842 | 14:2d47b97c2028 | 273 | |
ddrew73 | 17:fcc378db08f4 | 274 | wifi.init(); |
ddrew73 | 11:b520586dc0a1 | 275 | |
ddrew73 | 9:5fa90677ff3e | 276 | //Osci |
ddrew73 | 9:5fa90677ff3e | 277 | int loopcount = 0, pressed = 0; |
ddrew73 | 9:5fa90677ff3e | 278 | float begintime = 0, endtime = 0, elapsed = 0; |
ddrew73 | 0:16d2619002d7 | 279 | |
ddrew73 | 0:16d2619002d7 | 280 | |
ddrew73 | 0:16d2619002d7 | 281 | while(1) { |
ddrew73 | 17:fcc378db08f4 | 282 | |
jn80842 | 14:2d47b97c2028 | 283 | if (wifi.is_connected() == false) { |
jn80842 | 14:2d47b97c2028 | 284 | pc.printf("trying to connect to wifi\r\n"); |
jn80842 | 14:2d47b97c2028 | 285 | if (wifi.connect() == -1) { |
jn80842 | 14:2d47b97c2028 | 286 | pc.printf("Failed to connect\r\n"); |
jn80842 | 14:2d47b97c2028 | 287 | } else { |
jn80842 | 14:2d47b97c2028 | 288 | pc.printf("IP address: %s \r\n", wifi.getIPAddress()); |
jn80842 | 14:2d47b97c2028 | 289 | } |
jn80842 | 18:f7a00c31ff58 | 290 | } |
jn80842 | 18:f7a00c31ff58 | 291 | /* |
jn80842 | 18:f7a00c31ff58 | 292 | else { |
jn80842 | 14:2d47b97c2028 | 293 | Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw"); |
jn80842 | 14:2d47b97c2028 | 294 | ws.connect(); |
jn80842 | 14:2d47b97c2028 | 295 | } |
jn80842 | 18:f7a00c31ff58 | 296 | */ |
jn80842 | 18:f7a00c31ff58 | 297 | pc.printf("Okay took care of wifi stuff\r\n"); |
ddrew73 | 5:03d75ad40d5e | 298 | |
ddrew73 | 17:fcc378db08f4 | 299 | |
ddrew73 | 15:236d90e38951 | 300 | |
ddrew73 | 15:236d90e38951 | 301 | |
ddrew73 | 0:16d2619002d7 | 302 | //Display |
jn80842 | 14:2d47b97c2028 | 303 | if (moved ==1) { |
ddrew73 | 5:03d75ad40d5e | 304 | ledbar_left.clear(); |
ddrew73 | 5:03d75ad40d5e | 305 | ledbar_right.clear(); |
jn80842 | 14:2d47b97c2028 | 306 | set_led((colselect*24+rowselect),LED_GREEN); |
ddrew73 | 5:03d75ad40d5e | 307 | ledbar_left.writeDisplay(); |
ddrew73 | 5:03d75ad40d5e | 308 | ledbar_right.writeDisplay(); |
ddrew73 | 7:7fbe8dc5e4fa | 309 | } |
ddrew73 | 0:16d2619002d7 | 310 | //Moving the selected row |
ddrew73 | 0:16d2619002d7 | 311 | if (upbutton.read() == 0) { |
ddrew73 | 0:16d2619002d7 | 312 | rowselect = rowselect-1; |
ddrew73 | 0:16d2619002d7 | 313 | moved = 1; |
jn80842 | 14:2d47b97c2028 | 314 | written = 0; |
jn80842 | 18:f7a00c31ff58 | 315 | pc.printf("moved up\r\n"); |
ddrew73 | 15:236d90e38951 | 316 | wait(0.3); |
ddrew73 | 0:16d2619002d7 | 317 | } |
ddrew73 | 0:16d2619002d7 | 318 | |
ddrew73 | 0:16d2619002d7 | 319 | if (downbutton.read() == 0) { |
ddrew73 | 0:16d2619002d7 | 320 | rowselect = rowselect+1; |
ddrew73 | 0:16d2619002d7 | 321 | moved = 1; |
jn80842 | 14:2d47b97c2028 | 322 | written = 0; |
jn80842 | 18:f7a00c31ff58 | 323 | pc.printf("moved down\r\n"); |
ddrew73 | 15:236d90e38951 | 324 | wait(0.3); |
ddrew73 | 0:16d2619002d7 | 325 | } |
ddrew73 | 5:03d75ad40d5e | 326 | |
ddrew73 | 4:5d27966f93e4 | 327 | if (leftbutton.read() == 0) { |
ddrew73 | 4:5d27966f93e4 | 328 | colselect = 0; |
ddrew73 | 5:03d75ad40d5e | 329 | ledbar_right.clear(); |
ddrew73 | 5:03d75ad40d5e | 330 | moved =1; |
jn80842 | 14:2d47b97c2028 | 331 | written = 0; |
jn80842 | 18:f7a00c31ff58 | 332 | pc.printf("moved left\r\n"); |
ddrew73 | 15:236d90e38951 | 333 | wait(0.3); |
ddrew73 | 4:5d27966f93e4 | 334 | } |
ddrew73 | 5:03d75ad40d5e | 335 | |
ddrew73 | 4:5d27966f93e4 | 336 | if (rightbutton.read() == 0) { |
ddrew73 | 4:5d27966f93e4 | 337 | colselect = 1; |
ddrew73 | 5:03d75ad40d5e | 338 | ledbar_left.clear(); |
ddrew73 | 5:03d75ad40d5e | 339 | moved =1; |
jn80842 | 14:2d47b97c2028 | 340 | written = 0; |
jn80842 | 18:f7a00c31ff58 | 341 | pc.printf("moved right\r\n"); |
ddrew73 | 15:236d90e38951 | 342 | wait(0.3); |
ddrew73 | 5:03d75ad40d5e | 343 | } |
ddrew73 | 0:16d2619002d7 | 344 | |
ddrew73 | 0:16d2619002d7 | 345 | //Boundary checking |
ddrew73 | 0:16d2619002d7 | 346 | if (rowselect > 23) { |
ddrew73 | 0:16d2619002d7 | 347 | rowselect = 23; |
ddrew73 | 0:16d2619002d7 | 348 | } |
ddrew73 | 0:16d2619002d7 | 349 | if (rowselect < 0) { |
ddrew73 | 0:16d2619002d7 | 350 | rowselect = 0; |
ddrew73 | 0:16d2619002d7 | 351 | } |
ddrew73 | 0:16d2619002d7 | 352 | |
ddrew73 | 15:236d90e38951 | 353 | selected = (colselect * 24) + (rowselect+1); |
jn80842 | 18:f7a00c31ff58 | 354 | pc.printf("selected is %d\r\n",selected); |
ddrew73 | 15:236d90e38951 | 355 | |
ddrew73 | 0:16d2619002d7 | 356 | //Implementing scanning |
ddrew73 | 0:16d2619002d7 | 357 | |
ddrew73 | 0:16d2619002d7 | 358 | if (scanbutton.read() == 0) { |
ddrew73 | 15:236d90e38951 | 359 | A_in = 0; |
ddrew73 | 7:7fbe8dc5e4fa | 360 | B_in = 1; |
ddrew73 | 7:7fbe8dc5e4fa | 361 | C_in = 1; |
ddrew73 | 7:7fbe8dc5e4fa | 362 | D_in = 1; |
ddrew73 | 15:236d90e38951 | 363 | E_in = 0; |
ddrew73 | 15:236d90e38951 | 364 | F_in = 0; |
ddrew73 | 11:b520586dc0a1 | 365 | |
ddrew73 | 7:7fbe8dc5e4fa | 366 | vddval = adc.read(); |
ddrew73 | 0:16d2619002d7 | 367 | |
ddrew73 | 7:7fbe8dc5e4fa | 368 | ledbar_left.clear(); |
ddrew73 | 7:7fbe8dc5e4fa | 369 | ledbar_right.clear(); |
ddrew73 | 1:0183a7d38878 | 370 | |
jn80842 | 14:2d47b97c2028 | 371 | // first set of voltages read into old_volt_mat |
ddrew73 | 17:fcc378db08f4 | 372 | float_check(floatout,dacval); |
ddrew73 | 17:fcc378db08f4 | 373 | wait(0.2); |
jn80842 | 14:2d47b97c2028 | 374 | read_voltages(originalvoltages); |
ddrew73 | 17:fcc378db08f4 | 375 | |
jn80842 | 14:2d47b97c2028 | 376 | // second set for comparison, read into clientdata |
ddrew73 | 17:fcc378db08f4 | 377 | wait(0.2); |
ddrew73 | 16:028a8548ab22 | 378 | compare_voltages(originalvoltages,clientdata,colselect,rowselect,vddval,floatout); |
ddrew73 | 16:028a8548ab22 | 379 | |
ddrew73 | 17:fcc378db08f4 | 380 | |
ddrew73 | 0:16d2619002d7 | 381 | |
ddrew73 | 15:236d90e38951 | 382 | selected = (colselect * 24) + (rowselect+1); |
ddrew73 | 17:fcc378db08f4 | 383 | rowval = originalvoltages[selected-1]; |
ddrew73 | 11:b520586dc0a1 | 384 | |
jn80842 | 14:2d47b97c2028 | 385 | written = 0; |
jn80842 | 14:2d47b97c2028 | 386 | moved = 0; |
ddrew73 | 11:b520586dc0a1 | 387 | |
ddrew73 | 8:725d938b301b | 388 | //DEBUGGING TOOLS ////////////// |
ddrew73 | 11:b520586dc0a1 | 389 | |
ddrew73 | 15:236d90e38951 | 390 | pc.printf("\r\n%1.3f %1.3f \r\n", vddval, rowval); |
jn80842 | 14:2d47b97c2028 | 391 | for (int i = 0; i<48; i++) { |
jn80842 | 14:2d47b97c2028 | 392 | pc.printf(" %1.3f ", clientdata[i]); |
jn80842 | 14:2d47b97c2028 | 393 | } |
jn80842 | 14:2d47b97c2028 | 394 | pc.printf(" \r\n \r\n "); |
jn80842 | 14:2d47b97c2028 | 395 | for (int x = 0; x < 48; x++) { |
ddrew73 | 17:fcc378db08f4 | 396 | pc.printf("%1.4f %1.4f %d \r\n", originalvoltages[x], floatout[x], x+1); |
jn80842 | 14:2d47b97c2028 | 397 | } |
jn80842 | 14:2d47b97c2028 | 398 | //////////////////////// |
ddrew73 | 11:b520586dc0a1 | 399 | |
ddrew73 | 11:b520586dc0a1 | 400 | |
ddrew73 | 17:fcc378db08f4 | 401 | //THIS NEEDS TO GO AWAY! |
ddrew73 | 11:b520586dc0a1 | 402 | |
ddrew73 | 11:b520586dc0a1 | 403 | } // END OF SCANBUTTON IF STATEMENT |
ddrew73 | 11:b520586dc0a1 | 404 | |
jn80842 | 14:2d47b97c2028 | 405 | if (moved==0 && written == 0) { |
ddrew73 | 7:7fbe8dc5e4fa | 406 | ledbar_left.writeDisplay(); |
ddrew73 | 7:7fbe8dc5e4fa | 407 | ledbar_right.writeDisplay(); |
ddrew73 | 15:236d90e38951 | 408 | lcd_write_voltage_info(vddval*3.3,selected,rowval*3.3); |
ddrew73 | 15:236d90e38951 | 409 | |
ddrew73 | 17:fcc378db08f4 | 410 | |
jn80842 | 13:209da1dcb6e1 | 411 | // send data to websocket |
jn80842 | 14:2d47b97c2028 | 412 | if (wifi.is_connected()) { |
jn80842 | 18:f7a00c31ff58 | 413 | Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw"); |
jn80842 | 18:f7a00c31ff58 | 414 | ws.connect(); |
jn80842 | 18:f7a00c31ff58 | 415 | pc.printf("create json string with vdd %.1f, selected %d\r\n",vddval,selected); |
jn80842 | 14:2d47b97c2028 | 416 | std::string json = build_json(vddval,selected,clientdata); |
jn80842 | 18:f7a00c31ff58 | 417 | pc.printf("declare writable\r\n"); |
jn80842 | 14:2d47b97c2028 | 418 | char * writable = new char[json.size() + 1]; |
jn80842 | 18:f7a00c31ff58 | 419 | pc.printf("copy json string into writable\r\n"); |
jn80842 | 14:2d47b97c2028 | 420 | std::copy(json.begin(), json.end(), writable); |
jn80842 | 18:f7a00c31ff58 | 421 | pc.printf("add string delimiter to writable\r\n"); |
jn80842 | 14:2d47b97c2028 | 422 | writable[json.size()] = '\0'; |
jn80842 | 18:f7a00c31ff58 | 423 | pc.printf("send writable to ws\r\n"); |
jn80842 | 18:f7a00c31ff58 | 424 | pc.printf(writable); |
jn80842 | 18:f7a00c31ff58 | 425 | pc.printf("\r\n"); |
jn80842 | 14:2d47b97c2028 | 426 | ws.send(writable); |
jn80842 | 18:f7a00c31ff58 | 427 | pc.printf("deallocate writable\r\n"); |
jn80842 | 14:2d47b97c2028 | 428 | delete[] writable; |
jn80842 | 14:2d47b97c2028 | 429 | } |
ddrew73 | 17:fcc378db08f4 | 430 | |
ddrew73 | 15:236d90e38951 | 431 | |
jn80842 | 14:2d47b97c2028 | 432 | written = 1; |
ddrew73 | 15:236d90e38951 | 433 | } else if (oldselected != selected) { |
jn80842 | 14:2d47b97c2028 | 434 | lcd_write_selected_info(selected); |
ddrew73 | 15:236d90e38951 | 435 | |
ddrew73 | 11:b520586dc0a1 | 436 | } |
ddrew73 | 15:236d90e38951 | 437 | oldselected = selected; |
ddrew73 | 5:03d75ad40d5e | 438 | |
ddrew73 | 5:03d75ad40d5e | 439 | |
ddrew73 | 5:03d75ad40d5e | 440 | |
ddrew73 | 5:03d75ad40d5e | 441 | |
ddrew73 | 4:5d27966f93e4 | 442 | |
ddrew73 | 11:b520586dc0a1 | 443 | |
jn80842 | 18:f7a00c31ff58 | 444 | pc.printf("end of while loop!\r\n"); |
ddrew73 | 7:7fbe8dc5e4fa | 445 | |
ddrew73 | 0:16d2619002d7 | 446 | |
ddrew73 | 0:16d2619002d7 | 447 | |
ddrew73 | 4:5d27966f93e4 | 448 | |
ddrew73 | 4:5d27966f93e4 | 449 | |
ddrew73 | 4:5d27966f93e4 | 450 | |
ddrew73 | 7:7fbe8dc5e4fa | 451 | } // END OF WHILE(1) |
ddrew73 | 7:7fbe8dc5e4fa | 452 | |
ddrew73 | 7:7fbe8dc5e4fa | 453 | |
ddrew73 | 7:7fbe8dc5e4fa | 454 | |
ddrew73 | 4:5d27966f93e4 | 455 | |
ddrew73 | 7:7fbe8dc5e4fa | 456 | } // END OF INT MAIN |
ddrew73 | 7:7fbe8dc5e4fa | 457 | |
ddrew73 | 7:7fbe8dc5e4fa | 458 | |
ddrew73 | 11:b520586dc0a1 | 459 | /* |
ddrew73 | 11:b520586dc0a1 | 460 | OSCILLISCOPE |
ddrew73 | 7:7fbe8dc5e4fa | 461 | |
ddrew73 | 7:7fbe8dc5e4fa | 462 | |
ddrew73 | 11:b520586dc0a1 | 463 | while (scanbutton == 0){ |
ddrew73 | 11:b520586dc0a1 | 464 | if (loopcount = 0){ |
ddrew73 | 11:b520586dc0a1 | 465 | timer.reset(); |
ddrew73 | 11:b520586dc0a1 | 466 | timer.start(); |
ddrew73 | 11:b520586dc0a1 | 467 | begintime = timer.read_ms(); |
ddrew73 | 11:b520586dc0a1 | 468 | pressed == 1; |
ddrew73 | 11:b520586dc0a1 | 469 | } |
ddrew73 | 7:7fbe8dc5e4fa | 470 | |
ddrew73 | 11:b520586dc0a1 | 471 | int sn = (colselect + 1)*(rowselect + 1); |
ddrew73 | 11:b520586dc0a1 | 472 | A_in = (sn-1)&2; |
ddrew73 | 11:b520586dc0a1 | 473 | B_in = ((sn-1)/2)%2; |
ddrew73 | 11:b520586dc0a1 | 474 | C_in = ((sn-1)/4)%2; |
ddrew73 | 11:b520586dc0a1 | 475 | D_in = ((sn-1)/8)%2; |
ddrew73 | 11:b520586dc0a1 | 476 | E_in = ((sn-1)/16)%2; |
ddrew73 | 11:b520586dc0a1 | 477 | F_in = ((sn-1)/32)%2; |
ddrew73 | 11:b520586dc0a1 | 478 | volt_buffer[loopcount] = adc.read(); |
ddrew73 | 8:725d938b301b | 479 | |
ddrew73 | 8:725d938b301b | 480 | |
ddrew73 | 11:b520586dc0a1 | 481 | loopcount = loopcount +1; |
ddrew73 | 11:b520586dc0a1 | 482 | } // BREAK THE WHILE SCANBUTTON |
ddrew73 | 8:725d938b301b | 483 | |
ddrew73 | 11:b520586dc0a1 | 484 | if (pressed == 1){ |
ddrew73 | 11:b520586dc0a1 | 485 | timer.stop(); |
ddrew73 | 11:b520586dc0a1 | 486 | endtime = timer.read_ms(); |
ddrew73 | 11:b520586dc0a1 | 487 | elapsed = endtime - begintime; |
ddrew73 | 8:725d938b301b | 488 | |
ddrew73 | 11:b520586dc0a1 | 489 | DO ENTIRE SCAN STUFF |
ddrew73 | 8:725d938b301b | 490 | |
ddrew73 | 11:b520586dc0a1 | 491 | pressed = 0; |
ddrew73 | 11:b520586dc0a1 | 492 | } |
ddrew73 | 8:725d938b301b | 493 | |
ddrew73 | 11:b520586dc0a1 | 494 | */ |