mbed application board demo includes lcd, pot, joystick, rgb led, websocket, mbed rtos. websocket demo url is => http://sockets.mbed.org/gordonlu/viewer
Dependencies: C12832 EthernetInterface WebSocketClient mbed-rtos mbed
Fork of rtos_basic by
Revision 9:999997e8e4cc, committed 2015-02-12
- Comitter:
- gordonlu
- Date:
- Thu Feb 12 01:57:25 2015 +0000
- Parent:
- 8:421c13d323a0
- Commit message:
- Initial version.
Changed in this revision
WebSocketClient.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebSocketClient.lib Thu Feb 12 01:57:25 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/samux/code/WebSocketClient/#4567996414a5
--- a/main.cpp Fri Feb 06 08:49:53 2015 +0000 +++ b/main.cpp Thu Feb 12 01:57:25 2015 +0000 @@ -3,8 +3,12 @@ #include "C12832.h" #include "EthernetInterface.h" #include "math.h" +#include "Websocket.h" -#define POT_SENSITIVITY (0.01f) +#define POT_SENSITIVITY (0.01f) +#define WS_CONNECT_RETRY (20) +#define WS_SEND_RETRY (5) + C12832 lcd(p5, p7, p6, p8, p11); DigitalOut myled1(LED1); DigitalOut myled2(LED2); @@ -18,15 +22,16 @@ BusIn joystick(p12, p13, p14, p15, p16); Mutex lcd_mutex; +Mutex second_mutex; - -const int LEDNUM = 4; +static unsigned int second = 0; +static float pt1 = 0; +static float pt2 = 0; +static int js = 0xFF; void potJoystickThread(void const *args) { - static float pt1 = 0; - static float pt2 = 0; - static int js = 0; + printf("PotJoysticThread Start\r\n"); @@ -51,7 +56,7 @@ else if(0x01 == js) b.write( b + 0.05); //down else if(0x08 == js) b.write( b - 0.05); //up - printf("Pot1 : %.2f Pot2 : %.2f Joystick:%02X\r\n", pt1, pt2, js); + //printf("Pot1 : %.2f Pot2 : %.2f Joystick:%02X\r\n", pt1, pt2, js); //lcd display lcd_mutex.lock(); @@ -67,13 +72,15 @@ void netThread(void const *args) { + bool is_eth_connected = false; printf("EthernetInterface Thread Start\r\n"); EthernetInterface eth; eth.init(); //Use DHCP printf("MAC:%s\r\n",eth.getMACAddress()); + lcd_mutex.lock(); lcd.locate(0,21); lcd.printf("MAC:%s\r\n",eth.getMACAddress()); - + lcd_mutex.unlock(); printf("Connecting...\r\n"); while(1) @@ -81,6 +88,18 @@ if(0 != eth.connect()) { printf("Connecting fail! Try again. \r\nConnecting....\r\n"); + if(is_eth_connected) + { + if(0 == eth.disconnect()) + { + printf("Disconnect OK\r\n"); + } + else + { + printf("Disconnect error\r\n"); + } + } + is_eth_connected = false; Thread::wait(100); } else @@ -90,30 +109,65 @@ lcd.locate(0,21); lcd.printf("IP ADDRESS : %s\r\n",eth.getIPAddress()); lcd_mutex.unlock(); - break; + is_eth_connected = true; + + } + + if(is_eth_connected) + { + Websocket ws("ws://sockets.mbed.org:443/ws/gordonlu/wo"); + int i = 0; + while(++i < WS_CONNECT_RETRY) + { + printf("[%d] WebSwerivce connect to ws://sockets.mbed.org:443/ws/gordonlu/wo \r\n",i); + if(ws.connect())break; + Thread::wait(1000); + } + + int errCnt = 0; + while (ws.is_connected()) + { + + char buf[100]; + int scnt = sprintf(buf, "[%u] Hello! Pot1 : %.2f, Pot2 : %.2f, Joystick:%02X\r\n", second, pt1, pt2, js); + if((ws.send(buf)) >= scnt) + { + printf(buf); + errCnt = 0; + } + else + { + printf("[%d] ws.send failed!!\r\n",++errCnt); + } + + if(errCnt > WS_SEND_RETRY) + { + printf("close ws and disconnect eth, retry to connect again!\r\n"); + ws.close(); + break; + } + + Thread::wait(1000); + } } + } +} + +void secondCounterThread(void const *args) +{ + while(1) + { + second_mutex.lock(); + ++second; + second_mutex.unlock(); + myled1 != myled1; + Thread::wait(1000); } } -void rollingLed() -{ - static int onled = 0; - int led[LEDNUM] = {0,0,0,0}; - - led[onled] = 1; - ++onled; - if(onled == 4) onled = 0; - - myled1 = led[0]; - myled2 = led[1]; - myled3 = led[2]; - myled4 = led[3]; -} int main() { - int i = 0; - lcd.cls(); r.period(0.001); @@ -125,16 +179,15 @@ b = 1; Thread t1(netThread); - Thread t2(potJoystickThread); - + Thread t2(potJoystickThread); + Thread t3(secondCounterThread); + while(true) { lcd_mutex.lock(); lcd.locate(0,1); - lcd.printf("Second : %06d ",i++); + lcd.printf("Second : %06d ",second); lcd_mutex.unlock(); - - rollingLed(); Thread::wait(1000); } } \ No newline at end of file