Mouse demo

Dependencies:   C12832_lcd EthernetInterface SensorDataParser USBDevice mbed-rtos mbed

Fork of SensorStream_BlinkyDemo by -deleted-

main.cpp

Committer:
wm
Date:
2013-10-27
Revision:
1:5b0ded645617
Parent:
0:14ca3444e0bf

File content as of revision 1:5b0ded645617:

#include "mbed.h"
#include "EthernetInterface.h"
#include "C12832_lcd.h"
#include "SensorDataParser.h"
#include <math.h>
#include "USBMouse.h"
 
static EthernetInterface eth;
static C12832_LCD lcd;
static Serial pc(USBTX, USBRX);

USBMouse mouse;

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;
        }

        mouse.move(pd.ax*20, pd.ay*30);
    }
}

int main() {
    lcd.cls(); 
    ethernet_init();
    main_loop();
}