Demo Team / Mbed 2 deprecated SensorStream_BlinkyDemo

Dependencies:   C12832_lcd EthernetInterface SensorDataParser mbed-rtos mbed USBDevice

Fork of SensorStream_BlinkyDemo by Demo Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "C12832_lcd.h"
00004 #include "SensorDataParser.h"
00005 #include <math.h>
00006 #include "USBMouse.h"
00007  
00008 USBMouse mouse;
00009 static EthernetInterface eth;
00010 static C12832_LCD lcd;
00011 static Serial pc(USBTX, USBRX);
00012 
00013 DigitalOut led_left(LED1);
00014 DigitalOut led_up(LED2);
00015 DigitalOut led_right(LED3);
00016 DigitalOut led_down(LED4);
00017 
00018 // Configuration
00019 #define SERVER_PORT     5555
00020 #define MAX_BUF_SIZE    512
00021 #define SENSE_DELTA     0.1
00022 
00023 static void ethernet_init() {
00024     eth.init();
00025     if(eth.connect(30000) == 0)
00026         pc.printf("Connect OK\n\r");
00027 
00028     lcd.locate(0,0);
00029     lcd.printf("IP:%s", eth.getIPAddress());
00030 
00031     pc.printf("IP Address: %s\n\r", eth.getIPAddress());
00032 }
00033 
00034 static void main_loop() {
00035     UDPSocket server;
00036     Endpoint client;
00037     char buffer[MAX_BUF_SIZE];
00038     SENSOR_DATA pd;
00039     
00040     server.bind(SERVER_PORT);
00041     while (true) {
00042         // receive udp packet
00043         int n = server.receiveFrom(client, buffer, sizeof(buffer) - 1);
00044         if (n == sizeof(buffer) - 1) continue;
00045         
00046         buffer[n] = 0;
00047 
00048         // parse the packet
00049         if (parse_sensor_packet(buffer, &pd) == 0) continue;
00050         
00051         // accelerometer x axis
00052         if (pd.ax > 0) {
00053             led_left = 0;
00054             led_right = fabs(pd.ax) * 2;
00055         } else {
00056             led_left = fabs(pd.ax) * 2;
00057             led_right = 0;
00058         }
00059 
00060         // accelerometer y axis
00061         if (pd.ay > 0) {
00062             led_up = 0;
00063             led_down = fabs(pd.ay) * 2;
00064         } else {
00065             led_up = fabs(pd.ay) * 2;
00066             led_down = 0;
00067         }
00068         
00069         mouse.move(pd.ax*20, pd.ay*30);
00070     }
00071 }
00072 
00073 int main() {
00074     lcd.cls(); 
00075     ethernet_init();
00076     main_loop();
00077 }