SensorStream Blinky demo

Dependencies:   C12832_lcd EthernetInterface SensorDataParser USBDevice mbed-rtos mbed

Revision:
0:14ca3444e0bf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Oct 27 18:06:48 2013 +0000
@@ -0,0 +1,76 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "C12832_lcd.h"
+#include "SensorDataParser.h"
+#include <math.h>
+
+static EthernetInterface eth;
+static C12832_LCD lcd;
+static Serial pc(USBTX, USBRX);
+
+DigitalOut led_left(LED1);
+DigitalOut led_up(LED2);
+DigitalOut led_down(LED3);
+DigitalOut led_right(LED4);
+
+// Configuration
+#define SERVER_PORT     5555
+#define MAX_BUF_SIZE    512
+#define SENSE_DELTA     0.1
+
+static void ethernet_init() {
+    eth.init();
+    if(eth.connect(30000) == 0)
+        pc.printf("Connect OK\n\r");
+
+    lcd.locate(0,0);
+    lcd.printf("IP:%s", eth.getIPAddress());
+
+    pc.printf("IP Address: %s\n\r", eth.getIPAddress());
+}
+
+static void main_loop() {
+    UDPSocket server;
+    Endpoint client;
+    char buffer[MAX_BUF_SIZE];
+    SENSOR_DATA pd;
+    
+    server.bind(SERVER_PORT);
+    while (true) {
+        int n = server.receiveFrom(client, buffer, sizeof(buffer) - 1);
+        if (n == sizeof(buffer) - 1)
+            continue;
+        buffer[n] = 0;
+
+        if (parse_sensor_packet(buffer, &pd) == 0) continue;
+        
+        if(fabs(pd.ax) > SENSE_DELTA) {
+            //printf("ax: %f\r\n", pd.ax);
+        }
+        if(fabs(pd.ay) > SENSE_DELTA) {
+            //printf("ay: %f\r\n", pd.ay);
+        }
+
+        if (pd.ax > 0) {
+            led_left = 0;
+            led_right = fabs(pd.ax) * 2;
+        } else {
+            led_left = fabs(pd.ax) * 2;
+            led_right = 0;
+        }
+
+        if (pd.ay > 0) {
+            led_up = 0;
+            led_down = fabs(pd.ay) * 2;
+        } else {
+            led_up = fabs(pd.ay) * 2;
+            led_down = 0;
+        }
+    }
+}
+
+int main() {
+    lcd.cls(); 
+    ethernet_init();
+    main_loop();
+}