:)
Dependencies: MbedJSONValue DebounceIn TextLCD USBDevice mbed WebSocketClient cc3000_hostdriver_mbedsocket Adafruit_LEDBackpack_2
Revision 13:209da1dcb6e1, committed 2014-12-03
- Comitter:
- jn80842
- Date:
- Wed Dec 03 00:14:32 2014 +0000
- Parent:
- 11:b520586dc0a1
- Parent:
- 12:c4b046975527
- Child:
- 14:2d47b97c2028
- Commit message:
- wifi and websocket handling
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp.orig | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Dec 02 23:25:02 2014 +0000
+++ b/main.cpp Wed Dec 03 00:14:32 2014 +0000
@@ -6,7 +6,6 @@
#include "DebounceIn.h"
#include "cc3000.h"
#include "Websocket.h"
-#include "MbedJSONValue.h"
//Debug
Serial pc(USBTX, USBRX); // tx, rx
@@ -48,7 +47,7 @@
//WIFI STUFF
-/*
+
// cc3000 KL25Z wifi connection
// we need to define connection pins for:
@@ -64,9 +63,44 @@
"SSID", "PASSWORD", WPA2, false);
Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
-MbedJSONValue demo;
-*/
+
+// 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
+ 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;
+ }
+ }
+}
int main()
@@ -91,6 +125,13 @@
//LCD init
lcd.cls();
+
+ // wifi init
+ wifi.init();
+ wifi.connect();
+ // websocket init
+ Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
+ ws.connect();
//Osci
int loopcount = 0, pressed = 0;
@@ -254,19 +295,31 @@
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++) {
- sprintf(str, "%d", i);
- demo[str] = clientdata[i];
+ if (i != 0) {
+ add_to_json(",",json_str);
}
- demo["vdd"] = vddval;
- demo["rowval"] = rowval;
-
- demo["selected"] = selected;
- */
-
+ 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;
@@ -298,6 +351,12 @@
lcd.printf("Vdd: %1.1f V",vddval);
lcd.locate(0,0);
lcd.printf("Row %d: %1.1f V",selected,rowval);
+ // 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";
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp.orig Wed Dec 03 00:14:32 2014 +0000
@@ -0,0 +1,384 @@
+#include "mbed.h"
+#include <string>
+#include "Adafruit_LEDBackpack.h"
+#include "Adafruit_GFX.h"
+#include "TextLCD.h"
+#include "DebounceIn.h"
+#include "cc3000.h"
+#include "Websocket.h"
+#include "MbedJSONValue.h"
+
+//Debug
+Serial pc(USBTX, USBRX); // tx, rx
+
+//For oscilliscope
+Timer timer;
+
+//LED stuff
+I2C i2c_left(PTC11,PTC10);
+Adafruit_24bargraph ledbar_left=Adafruit_24bargraph(&i2c_left);
+
+I2C i2c_right(D7,D6);
+Adafruit_24bargraph ledbar_right=Adafruit_24bargraph(&i2c_right);
+
+DebounceIn upbutton(PTA16);
+DebounceIn downbutton(PTC12);
+DebounceIn leftbutton(PTC17);
+DebounceIn rightbutton(PTC16);
+
+//Scanner stuff
+
+DebounceIn scanbutton(PTC13);
+
+DigitalOut A_in(PTB10);
+DigitalOut B_in(PTB11);
+DigitalOut C_in(PTE2);
+DigitalOut D_in(PTE3);
+DigitalOut E_in(PTE4);
+DigitalOut F_in(PTE5);
+
+AnalogIn adc(PTB0);
+
+
+
+
+//LCD stuff
+//extLCD(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);
+
+
+//WIFI STUFF
+/*
+
+// cc3000 KL25Z wifi connection
+// we need to define connection pins for:
+// - IRQ => (pin D3)
+// - Enable => (pin D5)
+// - SPI CS => (pin D10)
+// - SPI MOSI => (pin D11)
+// - SPI MISO => (pin D12)
+// - SPI CLK => (pin D13)
+// 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);
+Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
+
+MbedJSONValue demo;
+*/
+
+
+
+int main()
+{
+
+ //Scan init
+
+ int con_mat[24][2] = {};
+ float volt_mat[24][2] = {};
+ float clientdata[48] = {};
+ float rowval = 0, vddval = 0;
+
+ //LED init
+ int rowselect = 0, moved = 1, colselect = 0, selected = 0;
+ ledbar_left.begin(0x70);
+ ledbar_left.clear();
+ ledbar_left.writeDisplay();
+
+ ledbar_right.begin(0x70);
+ ledbar_right.clear();
+ ledbar_right.writeDisplay();
+
+ //LCD init
+ lcd.cls();
+
+ //Osci
+ int loopcount = 0, pressed = 0;
+ float begintime = 0, endtime = 0, elapsed = 0;
+
+
+ while(1) {
+
+ //Display
+
+ if (colselect == 0 && moved == 1) {
+ ledbar_left.clear();
+ ledbar_right.clear();
+ ledbar_left.setBar(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;
+ wait(0.75);
+ }
+
+ if (downbutton.read() == 0) {
+
+ rowselect = rowselect+1;
+ moved = 1;
+ wait(0.75);
+ }
+
+ if (leftbutton.read() == 0) {
+ colselect = 0;
+ ledbar_right.clear();
+ moved =1;
+ wait(0.75);
+
+ }
+
+ if (rightbutton.read() == 0) {
+ colselect = 1;
+ ledbar_left.clear();
+ moved =1;
+ wait(0.75);
+
+
+ }
+
+ //Boundary checking
+ if (rowselect > 23) {
+ rowselect = 23;
+ }
+ if (rowselect < 0) {
+ rowselect = 0;
+ }
+
+ //Implementing scanning
+
+ if (scanbutton.read() == 0) {
+ A_in = 1;
+ B_in = 1;
+ C_in = 1;
+ D_in = 1;
+ E_in = 1;
+ F_in = 1;
+
+ 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;
+ }
+
+
+ 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;
+ }
+
+ }}} //END OF ROWSCAN FOR LOOP
+
+ selected = rowselect;
+ if (colselect == 1) {
+ selected = selected+24;
+ }
+
+ /*
+ //STUFF INTO JSON FORMAT
+ char str[1];
+ for (int i= 0; i < 48; i++) {
+ sprintf(str, "%d", i);
+ demo[str] = clientdata[i];
+ }
+ demo["vdd"] = vddval;
+ demo["rowval"] = rowval;
+
+ demo["selected"] = selected;
+ */
+
+
+ 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]);
+ }
+ }
+ ////////////////////////
+
+
+ wait(1); //THIS NEEDS TO GO AWAY! :}
+
+ } // END OF SCANBUTTON IF STATEMENT
+
+ if (moved==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);
+ } 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());
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+
+ } // END OF WHILE(1)
+
+
+
+
+ } // END OF INT MAIN
+
+
+ /*
+ FLOAT NODE CHECKER
+ add another "for" around the two for the 48 row checker:
+
+ 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;
+ }
+
+ PUT ALL ROW LIGHTING (CON_MAT) STUFF HERE
+ }
+
+ */
+
+ /*
+ OSCILLISCOPE
+
+
+ while (scanbutton == 0){
+ if (loopcount = 0){
+ timer.reset();
+ timer.start();
+ begintime = timer.read_ms();
+ pressed == 1;
+ }
+
+ 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;
+ volt_buffer[loopcount] = adc.read();
+
+
+ loopcount = loopcount +1;
+ } // BREAK THE WHILE SCANBUTTON
+
+ if (pressed == 1){
+ timer.stop();
+ endtime = timer.read_ms();
+ elapsed = endtime - begintime;
+
+ DO ENTIRE SCAN STUFF
+
+ pressed = 0;
+ }
+
+ */
\ No newline at end of file