kL46Z-Lab1-lodz

Dependencies:   MMA8451Q USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 13:15:31 2014 +0000
Revision:
0:bcba7139305f
kL46Z-Lab1-lodz

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wue 0:bcba7139305f 1 #include "mbed.h"
wue 0:bcba7139305f 2 #include "USBMouse.h"
wue 0:bcba7139305f 3 #include "MMA8451Q.h"
wue 0:bcba7139305f 4
wue 0:bcba7139305f 5 #define LED_ON 0 //outON, pwmON
wue 0:bcba7139305f 6 #define LED_OFF 1 //outOFF,pwmOFF
wue 0:bcba7139305f 7 #define PRESS_ON 0
wue 0:bcba7139305f 8 #define PRESS_OFF 1
wue 0:bcba7139305f 9
wue 0:bcba7139305f 10 #define LED_PERIOD 150 //[ms]
wue 0:bcba7139305f 11
wue 0:bcba7139305f 12 #define MMA8451_I2C_ADDRESS (0x1d<<1)
wue 0:bcba7139305f 13
wue 0:bcba7139305f 14
wue 0:bcba7139305f 15 struct KL46_SENSOR_DATA {
wue 0:bcba7139305f 16 int sw1state;
wue 0:bcba7139305f 17 int sw2state;
wue 0:bcba7139305f 18 float accValX;
wue 0:bcba7139305f 19 float accValY;
wue 0:bcba7139305f 20 float accValZ;
wue 0:bcba7139305f 21 } sensorData;
wue 0:bcba7139305f 22
wue 0:bcba7139305f 23
wue 0:bcba7139305f 24
wue 0:bcba7139305f 25 int main(void)
wue 0:bcba7139305f 26 {
wue 0:bcba7139305f 27 USBMouse mouse;
wue 0:bcba7139305f 28 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
wue 0:bcba7139305f 29 DigitalOut gLED(LED_GREEN); //PTD5
wue 0:bcba7139305f 30 PwmOut rLED(LED_RED); //PTE29
wue 0:bcba7139305f 31 DigitalIn sw1(PTC3); //if(sw1) Release else Press
wue 0:bcba7139305f 32 DigitalIn sw2(PTC12); //while(sw3); wait for Press
wue 0:bcba7139305f 33
wue 0:bcba7139305f 34 sw1.mode(PullUp);
wue 0:bcba7139305f 35 sw2.mode(PullUp);
wue 0:bcba7139305f 36
wue 0:bcba7139305f 37 gLED = LED_ON;
wue 0:bcba7139305f 38 rLED = LED_OFF;
wue 0:bcba7139305f 39 rLED.period(LED_PERIOD);
wue 0:bcba7139305f 40
wue 0:bcba7139305f 41 while (1) {
wue 0:bcba7139305f 42 sensorData.accValX = acc.getAccX();
wue 0:bcba7139305f 43 sensorData.accValY = acc.getAccY();
wue 0:bcba7139305f 44 sensorData.accValZ = acc.getAccZ();
wue 0:bcba7139305f 45 sensorData.sw1state = sw1;
wue 0:bcba7139305f 46 sensorData.sw2state = sw2;
wue 0:bcba7139305f 47
wue 0:bcba7139305f 48
wue 0:bcba7139305f 49 rLED = abs(sensorData.accValZ);
wue 0:bcba7139305f 50 mouse.move(sensorData.accValX*16.0, sensorData.accValY*16.0);
wue 0:bcba7139305f 51
wue 0:bcba7139305f 52 if(sensorData.sw1state == PRESS_ON) mouse.press(MOUSE_LEFT);
wue 0:bcba7139305f 53 else mouse.release(MOUSE_LEFT);
wue 0:bcba7139305f 54
wue 0:bcba7139305f 55 if(sensorData.sw2state == PRESS_ON) mouse.press(MOUSE_RIGHT);
wue 0:bcba7139305f 56 else mouse.release(MOUSE_RIGHT);
wue 0:bcba7139305f 57
wue 0:bcba7139305f 58 wait(0.05);
wue 0:bcba7139305f 59 }
wue 0:bcba7139305f 60 }