This program is to help people

Dependencies:   MMA8451Q USBDevice mbed

Fork of USB_Mouse by Pabian.EU

Committer:
jls1080
Date:
Wed Sep 24 16:04:05 2014 +0000
Revision:
1:69b4787b6f1f
Parent:
0:8a454b51ee4b
Child:
2:19fae3417fa1
A003888; Taller vertical Itesm Equipo 22

Who changed what in which revision?

UserRevisionLine numberNew 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 1:69b4787b6f1f 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 1:69b4787b6f1f 32
jls1080 1:69b4787b6f1f 33 while (1) {
jls1080 1:69b4787b6f1f 34
jls1080 1:69b4787b6f1f 35 wait(1); //Time what you have to wait to do the next selection
jls1080 1:69b4787b6f1f 36
jls1080 1:69b4787b6f1f 37 gLED = LED_OFF;// Red led is off
jls1080 1:69b4787b6f1f 38 rLED = LED_OFF;// Green led is off
jls1080 1:69b4787b6f1f 39
jls1080 1:69b4787b6f1f 40 sensorData.accValX = acc.getAccX(); //accX[-1..1]->mouse
jls1080 1:69b4787b6f1f 41 sensorData.accValY = acc.getAccY(); //accY[-1..1]->mouse
jls1080 1:69b4787b6f1f 42 sensorData.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED
jls1080 1:69b4787b6f1f 43
pabian 0:8a454b51ee4b 44
jls1080 1:69b4787b6f1f 45 //accXY -> mouse movements from FRDM XY inclination
jls1080 1:69b4787b6f1f 46 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 47
jls1080 1:69b4787b6f1f 48 wait(0.1); //wait 100ms
jls1080 1:69b4787b6f1f 49
jls1080 1:69b4787b6f1f 50
jls1080 1:69b4787b6f1f 51 absoluteValY=sensorData.accValY*sensiblity; //Getting absolute values of X axis
jls1080 1:69b4787b6f1f 52 absoluteValX=sensorData.accValX*sensiblity; //Getting absolute values of Y axis
jls1080 1:69b4787b6f1f 53
jls1080 1:69b4787b6f1f 54 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 55 break; //breking the program
jls1080 1:69b4787b6f1f 56
jls1080 1:69b4787b6f1f 57 }
jls1080 1:69b4787b6f1f 58 copyY=absoluteValY; //Copy the value of X axis to compare and detect changes in the value
jls1080 1:69b4787b6f1f 59 copyX=absoluteValX; //Copy the value of Y axis to compare and detect changes in the value
pabian 0:8a454b51ee4b 60
jls1080 1:69b4787b6f1f 61 }
jls1080 1:69b4787b6f1f 62
jls1080 1:69b4787b6f1f 63
jls1080 1:69b4787b6f1f 64
jls1080 1:69b4787b6f1f 65
jls1080 1:69b4787b6f1f 66 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 67
jls1080 1:69b4787b6f1f 68 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 69 rLED = LED_ON;
jls1080 1:69b4787b6f1f 70 wait(.1);
jls1080 1:69b4787b6f1f 71 gLED = LED_ON;
jls1080 1:69b4787b6f1f 72 rLED = LED_OFF;
jls1080 1:69b4787b6f1f 73 wait(.1);
jls1080 1:69b4787b6f1f 74 sensorData.accValX = acc.getAccX();
jls1080 1:69b4787b6f1f 75 } while(sensorData.accValX>-.3&&sensorData.accValX<.3);
pabian 0:8a454b51ee4b 76
jls1080 1:69b4787b6f1f 77 sensorData.accValX = acc.getAccX(); //Gettting the value of the X axis
jls1080 1:69b4787b6f1f 78
jls1080 1:69b4787b6f1f 79
jls1080 1:69b4787b6f1f 80
jls1080 1:69b4787b6f1f 81 if(sensorData.accValX>=.3) { // This part is true when the user move the FRD enought to select only clic
jls1080 1:69b4787b6f1f 82
jls1080 1:69b4787b6f1f 83 mouse.click(MOUSE_LEFT);
pabian 0:8a454b51ee4b 84
jls1080 1:69b4787b6f1f 85 gLED = LED_OFF;
jls1080 1:69b4787b6f1f 86 rLED = LED_ON;
pabian 0:8a454b51ee4b 87
jls1080 1:69b4787b6f1f 88 } else { // The oposite part of the other one is that the user select double clic
jls1080 1:69b4787b6f1f 89 mouse.click(MOUSE_RIGHT);
jls1080 1:69b4787b6f1f 90 gLED = LED_ON;
jls1080 1:69b4787b6f1f 91 rLED = LED_OFF;
jls1080 1:69b4787b6f1f 92 }
pabian 0:8a454b51ee4b 93 }
pabian 0:8a454b51ee4b 94 }