![](/media/cache/profiles/c913303f392ffc643f7240b180602652.50x50_q85.jpg)
SensorStream Blinky demo
Dependencies: C12832_lcd EthernetInterface SensorDataParser USBDevice mbed-rtos mbed
main.cpp@0:14ca3444e0bf, 2013-10-27 (annotated)
- Committer:
- screamer
- Date:
- Sun Oct 27 18:06:48 2013 +0000
- Revision:
- 0:14ca3444e0bf
Initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:14ca3444e0bf | 1 | #include "mbed.h" |
screamer | 0:14ca3444e0bf | 2 | #include "EthernetInterface.h" |
screamer | 0:14ca3444e0bf | 3 | #include "C12832_lcd.h" |
screamer | 0:14ca3444e0bf | 4 | #include "SensorDataParser.h" |
screamer | 0:14ca3444e0bf | 5 | #include <math.h> |
screamer | 0:14ca3444e0bf | 6 | |
screamer | 0:14ca3444e0bf | 7 | static EthernetInterface eth; |
screamer | 0:14ca3444e0bf | 8 | static C12832_LCD lcd; |
screamer | 0:14ca3444e0bf | 9 | static Serial pc(USBTX, USBRX); |
screamer | 0:14ca3444e0bf | 10 | |
screamer | 0:14ca3444e0bf | 11 | DigitalOut led_left(LED1); |
screamer | 0:14ca3444e0bf | 12 | DigitalOut led_up(LED2); |
screamer | 0:14ca3444e0bf | 13 | DigitalOut led_down(LED3); |
screamer | 0:14ca3444e0bf | 14 | DigitalOut led_right(LED4); |
screamer | 0:14ca3444e0bf | 15 | |
screamer | 0:14ca3444e0bf | 16 | // Configuration |
screamer | 0:14ca3444e0bf | 17 | #define SERVER_PORT 5555 |
screamer | 0:14ca3444e0bf | 18 | #define MAX_BUF_SIZE 512 |
screamer | 0:14ca3444e0bf | 19 | #define SENSE_DELTA 0.1 |
screamer | 0:14ca3444e0bf | 20 | |
screamer | 0:14ca3444e0bf | 21 | static void ethernet_init() { |
screamer | 0:14ca3444e0bf | 22 | eth.init(); |
screamer | 0:14ca3444e0bf | 23 | if(eth.connect(30000) == 0) |
screamer | 0:14ca3444e0bf | 24 | pc.printf("Connect OK\n\r"); |
screamer | 0:14ca3444e0bf | 25 | |
screamer | 0:14ca3444e0bf | 26 | lcd.locate(0,0); |
screamer | 0:14ca3444e0bf | 27 | lcd.printf("IP:%s", eth.getIPAddress()); |
screamer | 0:14ca3444e0bf | 28 | |
screamer | 0:14ca3444e0bf | 29 | pc.printf("IP Address: %s\n\r", eth.getIPAddress()); |
screamer | 0:14ca3444e0bf | 30 | } |
screamer | 0:14ca3444e0bf | 31 | |
screamer | 0:14ca3444e0bf | 32 | static void main_loop() { |
screamer | 0:14ca3444e0bf | 33 | UDPSocket server; |
screamer | 0:14ca3444e0bf | 34 | Endpoint client; |
screamer | 0:14ca3444e0bf | 35 | char buffer[MAX_BUF_SIZE]; |
screamer | 0:14ca3444e0bf | 36 | SENSOR_DATA pd; |
screamer | 0:14ca3444e0bf | 37 | |
screamer | 0:14ca3444e0bf | 38 | server.bind(SERVER_PORT); |
screamer | 0:14ca3444e0bf | 39 | while (true) { |
screamer | 0:14ca3444e0bf | 40 | int n = server.receiveFrom(client, buffer, sizeof(buffer) - 1); |
screamer | 0:14ca3444e0bf | 41 | if (n == sizeof(buffer) - 1) |
screamer | 0:14ca3444e0bf | 42 | continue; |
screamer | 0:14ca3444e0bf | 43 | buffer[n] = 0; |
screamer | 0:14ca3444e0bf | 44 | |
screamer | 0:14ca3444e0bf | 45 | if (parse_sensor_packet(buffer, &pd) == 0) continue; |
screamer | 0:14ca3444e0bf | 46 | |
screamer | 0:14ca3444e0bf | 47 | if(fabs(pd.ax) > SENSE_DELTA) { |
screamer | 0:14ca3444e0bf | 48 | //printf("ax: %f\r\n", pd.ax); |
screamer | 0:14ca3444e0bf | 49 | } |
screamer | 0:14ca3444e0bf | 50 | if(fabs(pd.ay) > SENSE_DELTA) { |
screamer | 0:14ca3444e0bf | 51 | //printf("ay: %f\r\n", pd.ay); |
screamer | 0:14ca3444e0bf | 52 | } |
screamer | 0:14ca3444e0bf | 53 | |
screamer | 0:14ca3444e0bf | 54 | if (pd.ax > 0) { |
screamer | 0:14ca3444e0bf | 55 | led_left = 0; |
screamer | 0:14ca3444e0bf | 56 | led_right = fabs(pd.ax) * 2; |
screamer | 0:14ca3444e0bf | 57 | } else { |
screamer | 0:14ca3444e0bf | 58 | led_left = fabs(pd.ax) * 2; |
screamer | 0:14ca3444e0bf | 59 | led_right = 0; |
screamer | 0:14ca3444e0bf | 60 | } |
screamer | 0:14ca3444e0bf | 61 | |
screamer | 0:14ca3444e0bf | 62 | if (pd.ay > 0) { |
screamer | 0:14ca3444e0bf | 63 | led_up = 0; |
screamer | 0:14ca3444e0bf | 64 | led_down = fabs(pd.ay) * 2; |
screamer | 0:14ca3444e0bf | 65 | } else { |
screamer | 0:14ca3444e0bf | 66 | led_up = fabs(pd.ay) * 2; |
screamer | 0:14ca3444e0bf | 67 | led_down = 0; |
screamer | 0:14ca3444e0bf | 68 | } |
screamer | 0:14ca3444e0bf | 69 | } |
screamer | 0:14ca3444e0bf | 70 | } |
screamer | 0:14ca3444e0bf | 71 | |
screamer | 0:14ca3444e0bf | 72 | int main() { |
screamer | 0:14ca3444e0bf | 73 | lcd.cls(); |
screamer | 0:14ca3444e0bf | 74 | ethernet_init(); |
screamer | 0:14ca3444e0bf | 75 | main_loop(); |
screamer | 0:14ca3444e0bf | 76 | } |