This program is to help people
Dependencies: MMA8451Q USBDevice mbed
Fork of USB_Mouse by
main.cpp@2:19fae3417fa1, 2014-09-24 (annotated)
- Committer:
- jls1080
- Date:
- Wed Sep 24 21:59:45 2014 +0000
- Revision:
- 2:19fae3417fa1
- Parent:
- 1:69b4787b6f1f
This program help people
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pabian | 0:8a454b51ee4b | 1 | #include "mbed.h" |
pabian | 0:8a454b51ee4b | 2 | #include "USBMouse.h" |
pabian | 0:8a454b51ee4b | 3 | #include "MMA8451Q.h" |
pabian | 0:8a454b51ee4b | 4 | |
pabian | 0:8a454b51ee4b | 5 | #define LED_ON 0 //outON, pwmON |
pabian | 0:8a454b51ee4b | 6 | #define LED_OFF 1 //outOFF,pwmOFF |
jls1080 | 1:69b4787b6f1f | 7 | DigitalOut gLED(LED_GREEN); |
jls1080 | 1:69b4787b6f1f | 8 | DigitalOut rLED(LED_RED); |
jls1080 | 1:69b4787b6f1f | 9 | int copyY=0; |
jls1080 | 1:69b4787b6f1f | 10 | int copyX=0; |
jls1080 | 1:69b4787b6f1f | 11 | int absoluteValY; |
jls1080 | 1:69b4787b6f1f | 12 | int absoluteValX; |
pabian | 0:8a454b51ee4b | 13 | |
pabian | 0:8a454b51ee4b | 14 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
pabian | 0:8a454b51ee4b | 15 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
pabian | 0:8a454b51ee4b | 16 | |
pabian | 0:8a454b51ee4b | 17 | USBMouse mouse; |
pabian | 0:8a454b51ee4b | 18 | |
pabian | 0:8a454b51ee4b | 19 | struct KL46_SENSOR_DATA { |
jls1080 | 1:69b4787b6f1f | 20 | |
jls1080 | 1:69b4787b6f1f | 21 | float accValX; //Values of the axis in a variable FLOAT |
pabian | 0:8a454b51ee4b | 22 | float accValY; |
pabian | 0:8a454b51ee4b | 23 | float accValZ; |
pabian | 0:8a454b51ee4b | 24 | } sensorData; |
pabian | 0:8a454b51ee4b | 25 | #define sD sensorData |
pabian | 0:8a454b51ee4b | 26 | |
pabian | 0:8a454b51ee4b | 27 | int main(void) |
pabian | 0:8a454b51ee4b | 28 | { |
jls1080 | 2:19fae3417fa1 | 29 | int sensiblity=100; //This is sensibility and this will be a parameter to configurate by the users |
jls1080 | 1:69b4787b6f1f | 30 | |
jls1080 | 1:69b4787b6f1f | 31 | while(1) { //This is a infinite loop to reset the program |
jls1080 | 2:19fae3417fa1 | 32 | |
jls1080 | 2:19fae3417fa1 | 33 | wait(1); //Time what you have to wait to do the next selection |
jls1080 | 2:19fae3417fa1 | 34 | |
jls1080 | 1:69b4787b6f1f | 35 | while (1) { |
jls1080 | 2:19fae3417fa1 | 36 | |
jls1080 | 2:19fae3417fa1 | 37 | |
jls1080 | 2:19fae3417fa1 | 38 | |
jls1080 | 1:69b4787b6f1f | 39 | gLED = LED_OFF;// Red led is off |
jls1080 | 1:69b4787b6f1f | 40 | rLED = LED_OFF;// Green led is off |
jls1080 | 1:69b4787b6f1f | 41 | |
jls1080 | 1:69b4787b6f1f | 42 | sensorData.accValX = acc.getAccX(); //accX[-1..1]->mouse |
jls1080 | 1:69b4787b6f1f | 43 | sensorData.accValY = acc.getAccY(); //accY[-1..1]->mouse |
jls1080 | 1:69b4787b6f1f | 44 | sensorData.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED |
jls1080 | 1:69b4787b6f1f | 45 | |
pabian | 0:8a454b51ee4b | 46 | |
jls1080 | 1:69b4787b6f1f | 47 | //accXY -> mouse movements from FRDM XY inclination |
jls1080 | 1:69b4787b6f1f | 48 | mouse.move(sensorData.accValX*30, sensorData.accValY*30); //This is the magnitude of how much whe want to move the mouse and this also will be a parameter to configurate by the users |
jls1080 | 1:69b4787b6f1f | 49 | |
jls1080 | 1:69b4787b6f1f | 50 | wait(0.1); //wait 100ms |
jls1080 | 1:69b4787b6f1f | 51 | |
jls1080 | 1:69b4787b6f1f | 52 | |
jls1080 | 1:69b4787b6f1f | 53 | absoluteValY=sensorData.accValY*sensiblity; //Getting absolute values of X axis |
jls1080 | 1:69b4787b6f1f | 54 | absoluteValX=sensorData.accValX*sensiblity; //Getting absolute values of Y axis |
jls1080 | 1:69b4787b6f1f | 55 | |
jls1080 | 1:69b4787b6f1f | 56 | if(absoluteValY==copyY&&absoluteValY==copyX) { //This "if" compare the values of the axis and when it is 'true' we are interpreting that the user did the selection |
jls1080 | 1:69b4787b6f1f | 57 | break; //breking the program |
jls1080 | 1:69b4787b6f1f | 58 | |
jls1080 | 1:69b4787b6f1f | 59 | } |
jls1080 | 1:69b4787b6f1f | 60 | copyY=absoluteValY; //Copy the value of X axis to compare and detect changes in the value |
jls1080 | 1:69b4787b6f1f | 61 | copyX=absoluteValX; //Copy the value of Y axis to compare and detect changes in the value |
pabian | 0:8a454b51ee4b | 62 | |
jls1080 | 1:69b4787b6f1f | 63 | } |
jls1080 | 1:69b4787b6f1f | 64 | |
jls1080 | 1:69b4787b6f1f | 65 | |
jls1080 | 1:69b4787b6f1f | 66 | |
jls1080 | 1:69b4787b6f1f | 67 | |
jls1080 | 1:69b4787b6f1f | 68 | do { //In this part of the program we choose a rank of numbers that we interpret like the user not choose the click |
jls1080 | 1:69b4787b6f1f | 69 | |
jls1080 | 1:69b4787b6f1f | 70 | gLED = LED_OFF; //Waiting and indicating that the micro is ready to do clic or double clic the code only do a flicker leds |
jls1080 | 1:69b4787b6f1f | 71 | rLED = LED_ON; |
jls1080 | 1:69b4787b6f1f | 72 | wait(.1); |
jls1080 | 1:69b4787b6f1f | 73 | gLED = LED_ON; |
jls1080 | 1:69b4787b6f1f | 74 | rLED = LED_OFF; |
jls1080 | 1:69b4787b6f1f | 75 | wait(.1); |
jls1080 | 1:69b4787b6f1f | 76 | sensorData.accValX = acc.getAccX(); |
jls1080 | 1:69b4787b6f1f | 77 | } while(sensorData.accValX>-.3&&sensorData.accValX<.3); |
pabian | 0:8a454b51ee4b | 78 | |
jls1080 | 1:69b4787b6f1f | 79 | sensorData.accValX = acc.getAccX(); //Gettting the value of the X axis |
jls1080 | 1:69b4787b6f1f | 80 | |
jls1080 | 1:69b4787b6f1f | 81 | |
jls1080 | 1:69b4787b6f1f | 82 | |
jls1080 | 1:69b4787b6f1f | 83 | if(sensorData.accValX>=.3) { // This part is true when the user move the FRD enought to select only clic |
jls1080 | 1:69b4787b6f1f | 84 | |
jls1080 | 2:19fae3417fa1 | 85 | mouse.click(MOUSE_RIGHT); |
pabian | 0:8a454b51ee4b | 86 | |
jls1080 | 1:69b4787b6f1f | 87 | gLED = LED_OFF; |
jls1080 | 1:69b4787b6f1f | 88 | rLED = LED_ON; |
pabian | 0:8a454b51ee4b | 89 | |
jls1080 | 1:69b4787b6f1f | 90 | } else { // The oposite part of the other one is that the user select double clic |
jls1080 | 2:19fae3417fa1 | 91 | mouse.click(MOUSE_LEFT); |
jls1080 | 1:69b4787b6f1f | 92 | gLED = LED_ON; |
jls1080 | 1:69b4787b6f1f | 93 | rLED = LED_OFF; |
jls1080 | 1:69b4787b6f1f | 94 | } |
pabian | 0:8a454b51ee4b | 95 | } |
pabian | 0:8a454b51ee4b | 96 | } |