Test application for Freedom KL25Z, mouse driven by accelerometer and click with TSI interface

Dependencies:   MMA8451Q TSI USBDevice mbed

Fork of Airmouse by Frederic Thierry

Committer:
matheusBordin
Date:
Sat Sep 10 19:22:49 2016 +0000
Revision:
1:1ab1608b4249
Parent:
0:a7df8e8cc00c
AirMouse in FRDM - KL25Z by Matheus Bordin Gomes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
matheusBordin 1:1ab1608b4249 1 /****************************************
matheusBordin 1:1ab1608b4249 2 * AirMouse em FRDM - KL25Z
matheusBordin 1:1ab1608b4249 3 * Utiliza os movimentos do acelerômetro nos
matheusBordin 1:1ab1608b4249 4 * eixos x e y para movimentar o cursor e o
matheusBordin 1:1ab1608b4249 5 * touch capacitivo como os botôes esquerdo
matheusBordin 1:1ab1608b4249 6 * e direito. Também possui a função de scroll
matheusBordin 1:1ab1608b4249 7 * utilizando o touch e o acelerômetro.
matheusBordin 1:1ab1608b4249 8 *
matheusBordin 1:1ab1608b4249 9 * Projeto feito por Matheus Bordin Gomes
matheusBordin 1:1ab1608b4249 10 ****************************************/
matheusBordin 1:1ab1608b4249 11
Future_FThierry 0:a7df8e8cc00c 12 #include "mbed.h"
matheusBordin 1:1ab1608b4249 13 #include "USBMouse.h" //Parte da biblioteca "USBDevice"
matheusBordin 1:1ab1608b4249 14 #include "MMA8451Q.h" //Biblioteca para utilizar o acelerômetro
matheusBordin 1:1ab1608b4249 15 #include "TSISensor.h" //Biblioteca do sensor touch capacitivo
Future_FThierry 0:a7df8e8cc00c 16
Future_FThierry 0:a7df8e8cc00c 17 #define MMA8451_I2C_ADDRESS (0x1d<<1)
Future_FThierry 0:a7df8e8cc00c 18
Future_FThierry 0:a7df8e8cc00c 19 USBMouse mouse;
matheusBordin 1:1ab1608b4249 20 TSISensor tsi;
matheusBordin 1:1ab1608b4249 21 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
matheusBordin 1:1ab1608b4249 22
matheusBordin 1:1ab1608b4249 23 DigitalOut ledRight(LED_RED);
matheusBordin 1:1ab1608b4249 24 DigitalOut ledLeft(LED_BLUE);
matheusBordin 1:1ab1608b4249 25 DigitalOut working(LED_GREEN);
Future_FThierry 0:a7df8e8cc00c 26
matheusBordin 1:1ab1608b4249 27 int main(){
matheusBordin 1:1ab1608b4249 28 float click = 0; //Representa os botões do mouse
matheusBordin 1:1ab1608b4249 29 int16_t x = 0; //Representa a coordenada x do cursor
matheusBordin 1:1ab1608b4249 30 int16_t y = 0; //Representa a coordenada y do cursor
matheusBordin 1:1ab1608b4249 31 int16_t scroll = 0; //Representa a velocidade de scroll
matheusBordin 1:1ab1608b4249 32
matheusBordin 1:1ab1608b4249 33 ledRight = 1; //Led de status do botão direito
matheusBordin 1:1ab1608b4249 34 ledLeft = 1; //Led de status do botão esquerdo
matheusBordin 1:1ab1608b4249 35 working = 0; //Led de status do mouse
Future_FThierry 0:a7df8e8cc00c 36
matheusBordin 1:1ab1608b4249 37 while (true){
matheusBordin 1:1ab1608b4249 38 x = -9.8 * acc.getAccY(); //Leitura do acelerômetro em y para o 'x'
matheusBordin 1:1ab1608b4249 39 y = 9.8 * acc.getAccX(); //Leitura do acelerômetro em x pra o 'y'
matheusBordin 1:1ab1608b4249 40 scroll = 1.8 * acc.getAccX(); //Leitura do acelerômetro em y para scroll
matheusBordin 1:1ab1608b4249 41
matheusBordin 1:1ab1608b4249 42 click = tsi.readPercentage(); //Leitura do touch capacitivo
matheusBordin 1:1ab1608b4249 43
matheusBordin 1:1ab1608b4249 44 /**
matheusBordin 1:1ab1608b4249 45 * Implementa a movimentação do cursor com base
matheusBordin 1:1ab1608b4249 46 * nos movimentos de rotação do microcontrolador
matheusBordin 1:1ab1608b4249 47 * nos eixos x e y.
matheusBordin 1:1ab1608b4249 48 **/
Future_FThierry 0:a7df8e8cc00c 49 mouse.move(x, y);
matheusBordin 1:1ab1608b4249 50
matheusBordin 1:1ab1608b4249 51 /**
matheusBordin 1:1ab1608b4249 52 * Implementa o botão direito do mouse
matheusBordin 1:1ab1608b4249 53 * na parte direita do touch capacitivo.
matheusBordin 1:1ab1608b4249 54 * Também acende o led amarelo para mostrar
matheusBordin 1:1ab1608b4249 55 * que o botão está pressionado.
matheusBordin 1:1ab1608b4249 56 **/
matheusBordin 1:1ab1608b4249 57 if((click<0.30) && (click>0)){
matheusBordin 1:1ab1608b4249 58 mouse.press(MOUSE_RIGHT);
matheusBordin 1:1ab1608b4249 59 working = 1;
matheusBordin 1:1ab1608b4249 60 ledRight = 0;
matheusBordin 1:1ab1608b4249 61 } else{
matheusBordin 1:1ab1608b4249 62 mouse.release(MOUSE_RIGHT);
matheusBordin 1:1ab1608b4249 63 ledRight = 1;
matheusBordin 1:1ab1608b4249 64 working = 0;
matheusBordin 1:1ab1608b4249 65 }
matheusBordin 1:1ab1608b4249 66
matheusBordin 1:1ab1608b4249 67 wait(0.001);
matheusBordin 1:1ab1608b4249 68
matheusBordin 1:1ab1608b4249 69 /**
matheusBordin 1:1ab1608b4249 70 * Implementa o botão esquerdo do mouse
matheusBordin 1:1ab1608b4249 71 * na parte esquerda do touch capacitivo.
matheusBordin 1:1ab1608b4249 72 * Também acende o led azul para mostrar
matheusBordin 1:1ab1608b4249 73 * que o botão está pressionado.
matheusBordin 1:1ab1608b4249 74 **/
matheusBordin 1:1ab1608b4249 75 if (click>0.70){
Future_FThierry 0:a7df8e8cc00c 76 mouse.press(MOUSE_LEFT);
matheusBordin 1:1ab1608b4249 77 working = 1;
matheusBordin 1:1ab1608b4249 78 ledLeft = 0;
matheusBordin 1:1ab1608b4249 79 } else{
Future_FThierry 0:a7df8e8cc00c 80 mouse.release(MOUSE_LEFT);
matheusBordin 1:1ab1608b4249 81 ledLeft = 1;
matheusBordin 1:1ab1608b4249 82 working = 0;
matheusBordin 1:1ab1608b4249 83 }
matheusBordin 1:1ab1608b4249 84
matheusBordin 1:1ab1608b4249 85 wait(0.001);
matheusBordin 1:1ab1608b4249 86
matheusBordin 1:1ab1608b4249 87 /**
matheusBordin 1:1ab1608b4249 88 * Implementa o scroll na parte central
matheusBordin 1:1ab1608b4249 89 * do touch capacitivo.
matheusBordin 1:1ab1608b4249 90 * Também acende o led rosa para mostrar
matheusBordin 1:1ab1608b4249 91 * que o scroll está em funcionamento.
matheusBordin 1:1ab1608b4249 92 **/
matheusBordin 1:1ab1608b4249 93 if((click>0.35) && (click<0.65)){
matheusBordin 1:1ab1608b4249 94 mouse.scroll(scroll);
matheusBordin 1:1ab1608b4249 95 working = 1;
matheusBordin 1:1ab1608b4249 96 ledRight = 0;
matheusBordin 1:1ab1608b4249 97 ledLeft = 0;
matheusBordin 1:1ab1608b4249 98 } else{
matheusBordin 1:1ab1608b4249 99 ledRight = 1;
matheusBordin 1:1ab1608b4249 100 ledLeft = 1;
matheusBordin 1:1ab1608b4249 101 working = 0;
matheusBordin 1:1ab1608b4249 102 }
Future_FThierry 0:a7df8e8cc00c 103
Future_FThierry 0:a7df8e8cc00c 104 wait(0.001);
Future_FThierry 0:a7df8e8cc00c 105 }
Future_FThierry 0:a7df8e8cc00c 106 }