Mouse demo
Dependencies: C12832_lcd EthernetInterface SensorDataParser USBDevice mbed-rtos mbed
Fork of SensorStream_BlinkyDemo by
main.cpp@1:5b0ded645617, 2013-10-27 (annotated)
- Committer:
- wm
- Date:
- Sun Oct 27 22:10:46 2013 +0000
- Revision:
- 1:5b0ded645617
- Parent:
- 0:14ca3444e0bf
Act as USB Device
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> |
wm | 1:5b0ded645617 | 6 | #include "USBMouse.h" |
wm | 1:5b0ded645617 | 7 | |
screamer | 0:14ca3444e0bf | 8 | static EthernetInterface eth; |
screamer | 0:14ca3444e0bf | 9 | static C12832_LCD lcd; |
screamer | 0:14ca3444e0bf | 10 | static Serial pc(USBTX, USBRX); |
screamer | 0:14ca3444e0bf | 11 | |
wm | 1:5b0ded645617 | 12 | USBMouse mouse; |
wm | 1:5b0ded645617 | 13 | |
screamer | 0:14ca3444e0bf | 14 | DigitalOut led_left(LED1); |
screamer | 0:14ca3444e0bf | 15 | DigitalOut led_up(LED2); |
screamer | 0:14ca3444e0bf | 16 | DigitalOut led_down(LED3); |
screamer | 0:14ca3444e0bf | 17 | DigitalOut led_right(LED4); |
screamer | 0:14ca3444e0bf | 18 | |
screamer | 0:14ca3444e0bf | 19 | // Configuration |
screamer | 0:14ca3444e0bf | 20 | #define SERVER_PORT 5555 |
screamer | 0:14ca3444e0bf | 21 | #define MAX_BUF_SIZE 512 |
screamer | 0:14ca3444e0bf | 22 | #define SENSE_DELTA 0.1 |
screamer | 0:14ca3444e0bf | 23 | |
screamer | 0:14ca3444e0bf | 24 | static void ethernet_init() { |
screamer | 0:14ca3444e0bf | 25 | eth.init(); |
screamer | 0:14ca3444e0bf | 26 | if(eth.connect(30000) == 0) |
screamer | 0:14ca3444e0bf | 27 | pc.printf("Connect OK\n\r"); |
screamer | 0:14ca3444e0bf | 28 | |
screamer | 0:14ca3444e0bf | 29 | lcd.locate(0,0); |
screamer | 0:14ca3444e0bf | 30 | lcd.printf("IP:%s", eth.getIPAddress()); |
screamer | 0:14ca3444e0bf | 31 | |
screamer | 0:14ca3444e0bf | 32 | pc.printf("IP Address: %s\n\r", eth.getIPAddress()); |
screamer | 0:14ca3444e0bf | 33 | } |
screamer | 0:14ca3444e0bf | 34 | |
screamer | 0:14ca3444e0bf | 35 | static void main_loop() { |
screamer | 0:14ca3444e0bf | 36 | UDPSocket server; |
screamer | 0:14ca3444e0bf | 37 | Endpoint client; |
screamer | 0:14ca3444e0bf | 38 | char buffer[MAX_BUF_SIZE]; |
screamer | 0:14ca3444e0bf | 39 | SENSOR_DATA pd; |
screamer | 0:14ca3444e0bf | 40 | |
screamer | 0:14ca3444e0bf | 41 | server.bind(SERVER_PORT); |
screamer | 0:14ca3444e0bf | 42 | while (true) { |
screamer | 0:14ca3444e0bf | 43 | int n = server.receiveFrom(client, buffer, sizeof(buffer) - 1); |
screamer | 0:14ca3444e0bf | 44 | if (n == sizeof(buffer) - 1) |
screamer | 0:14ca3444e0bf | 45 | continue; |
screamer | 0:14ca3444e0bf | 46 | buffer[n] = 0; |
screamer | 0:14ca3444e0bf | 47 | |
screamer | 0:14ca3444e0bf | 48 | if (parse_sensor_packet(buffer, &pd) == 0) continue; |
screamer | 0:14ca3444e0bf | 49 | |
screamer | 0:14ca3444e0bf | 50 | if(fabs(pd.ax) > SENSE_DELTA) { |
screamer | 0:14ca3444e0bf | 51 | //printf("ax: %f\r\n", pd.ax); |
screamer | 0:14ca3444e0bf | 52 | } |
screamer | 0:14ca3444e0bf | 53 | if(fabs(pd.ay) > SENSE_DELTA) { |
screamer | 0:14ca3444e0bf | 54 | //printf("ay: %f\r\n", pd.ay); |
screamer | 0:14ca3444e0bf | 55 | } |
screamer | 0:14ca3444e0bf | 56 | |
screamer | 0:14ca3444e0bf | 57 | if (pd.ax > 0) { |
screamer | 0:14ca3444e0bf | 58 | led_left = 0; |
screamer | 0:14ca3444e0bf | 59 | led_right = fabs(pd.ax) * 2; |
screamer | 0:14ca3444e0bf | 60 | } else { |
screamer | 0:14ca3444e0bf | 61 | led_left = fabs(pd.ax) * 2; |
screamer | 0:14ca3444e0bf | 62 | led_right = 0; |
screamer | 0:14ca3444e0bf | 63 | } |
screamer | 0:14ca3444e0bf | 64 | |
screamer | 0:14ca3444e0bf | 65 | if (pd.ay > 0) { |
screamer | 0:14ca3444e0bf | 66 | led_up = 0; |
screamer | 0:14ca3444e0bf | 67 | led_down = fabs(pd.ay) * 2; |
screamer | 0:14ca3444e0bf | 68 | } else { |
screamer | 0:14ca3444e0bf | 69 | led_up = fabs(pd.ay) * 2; |
screamer | 0:14ca3444e0bf | 70 | led_down = 0; |
screamer | 0:14ca3444e0bf | 71 | } |
wm | 1:5b0ded645617 | 72 | |
wm | 1:5b0ded645617 | 73 | mouse.move(pd.ax*20, pd.ay*30); |
screamer | 0:14ca3444e0bf | 74 | } |
screamer | 0:14ca3444e0bf | 75 | } |
screamer | 0:14ca3444e0bf | 76 | |
screamer | 0:14ca3444e0bf | 77 | int main() { |
screamer | 0:14ca3444e0bf | 78 | lcd.cls(); |
screamer | 0:14ca3444e0bf | 79 | ethernet_init(); |
screamer | 0:14ca3444e0bf | 80 | main_loop(); |
screamer | 0:14ca3444e0bf | 81 | } |