test

Dependencies:   MMA8451Q USBDevice mbed-rtos mbed

Committer:
trisjph
Date:
Mon Dec 16 11:04:05 2013 +0000
Revision:
0:d270a9981e69
Inital;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
trisjph 0:d270a9981e69 1 #include "mbed.h"
trisjph 0:d270a9981e69 2 #include "rtos.h"
trisjph 0:d270a9981e69 3 #define MMA8451_I2C_ADDRESS (0x1d<<1)
trisjph 0:d270a9981e69 4 #include "MMA8451Q.h"
trisjph 0:d270a9981e69 5 #include "USBMouseKeyboard.h"
trisjph 0:d270a9981e69 6 #define PI 3.14159265
trisjph 0:d270a9981e69 7
trisjph 0:d270a9981e69 8 PwmOut led1(LED2);
trisjph 0:d270a9981e69 9 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
trisjph 0:d270a9981e69 10 AnalogIn ain(PTC2);
trisjph 0:d270a9981e69 11 Serial pc(USBTX, USBRX);
trisjph 0:d270a9981e69 12 USBMouseKeyboard key_mouse;
trisjph 0:d270a9981e69 13 Mutex USB_mutex;
trisjph 0:d270a9981e69 14 //USBHID key_mouse;
trisjph 0:d270a9981e69 15
trisjph 0:d270a9981e69 16 void click_thread(void const *args)
trisjph 0:d270a9981e69 17 {
trisjph 0:d270a9981e69 18 int press;
trisjph 0:d270a9981e69 19 while(true) {
trisjph 0:d270a9981e69 20 if( (ain.read()>0.3) && (press == 0) ) {
trisjph 0:d270a9981e69 21 press = 1;
trisjph 0:d270a9981e69 22 led1 != led1;
trisjph 0:d270a9981e69 23 pc.printf("%i\n", press);
trisjph 0:d270a9981e69 24 USB_mutex.lock();
trisjph 0:d270a9981e69 25 key_mouse.press(MOUSE_LEFT);
trisjph 0:d270a9981e69 26 USB_mutex.unlock();
trisjph 0:d270a9981e69 27 } else if ( (ain.read()<0.3) && (press == 1) ) {
trisjph 0:d270a9981e69 28 press = 0;
trisjph 0:d270a9981e69 29 pc.printf("%i\n", press);
trisjph 0:d270a9981e69 30 USB_mutex.lock();
trisjph 0:d270a9981e69 31 key_mouse.release(MOUSE_LEFT);
trisjph 0:d270a9981e69 32 USB_mutex.unlock();
trisjph 0:d270a9981e69 33 }
trisjph 0:d270a9981e69 34 Thread::wait(100);
trisjph 0:d270a9981e69 35 }
trisjph 0:d270a9981e69 36 }
trisjph 0:d270a9981e69 37
trisjph 0:d270a9981e69 38 void mouse_thread(void const *args)
trisjph 0:d270a9981e69 39 {
trisjph 0:d270a9981e69 40 while(true) {
trisjph 0:d270a9981e69 41
trisjph 0:d270a9981e69 42 USB_mutex.lock();
trisjph 0:d270a9981e69 43 key_mouse.move(-acc.getAccX()*10, 0);
trisjph 0:d270a9981e69 44 key_mouse.move(0, acc.getAccY()*10);
trisjph 0:d270a9981e69 45 USB_mutex.unlock();
trisjph 0:d270a9981e69 46 Thread::wait(1);
trisjph 0:d270a9981e69 47 }
trisjph 0:d270a9981e69 48 }
trisjph 0:d270a9981e69 49
trisjph 0:d270a9981e69 50 void heartbeat_thread(void const *args)
trisjph 0:d270a9981e69 51 {
trisjph 0:d270a9981e69 52 int i;
trisjph 0:d270a9981e69 53 led1.period(0.001);
trisjph 0:d270a9981e69 54 while(true) {
trisjph 0:d270a9981e69 55 for(i=0; i<180; i++) {
trisjph 0:d270a9981e69 56 led1 = sin(i*PI/180);
trisjph 0:d270a9981e69 57 Thread::wait(10);
trisjph 0:d270a9981e69 58 }
trisjph 0:d270a9981e69 59 }
trisjph 0:d270a9981e69 60
trisjph 0:d270a9981e69 61 }
trisjph 0:d270a9981e69 62
trisjph 0:d270a9981e69 63 int main()
trisjph 0:d270a9981e69 64 {
trisjph 0:d270a9981e69 65 Thread click(click_thread);
trisjph 0:d270a9981e69 66 Thread mouse(mouse_thread);
trisjph 0:d270a9981e69 67 Thread heartbeat(heartbeat_thread);
trisjph 0:d270a9981e69 68
trisjph 0:d270a9981e69 69 led1 = 1.0;
trisjph 0:d270a9981e69 70
trisjph 0:d270a9981e69 71 while (true) {
trisjph 0:d270a9981e69 72 }
trisjph 0:d270a9981e69 73 }