.

Dependencies:   mbed tsi_sensor MMA8451Q

Revision:
0:2641e3a39ede
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue May 12 15:39:23 2020 +0000
@@ -0,0 +1,260 @@
+#include "mbed.h"
+#include "MMA8451Q.h"
+#include "tsi_sensor.h"
+
+#if   defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
+PinName const SDA = PTE25;
+PinName const SCL = PTE24;
+#elif defined (TARGET_KL05Z)
+PinName const SDA = PTB4;
+PinName const SCL = PTB3;
+#elif defined (TARGET_K20D50M)
+PinName const SDA = PTB1;
+PinName const SCL = PTB0;
+#else
+#error TARGET NOT DEFINED
+#endif
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+
+#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
+#define ELEC0 9
+#define ELEC1 10
+#elif defined (TARGET_KL05Z)
+#define ELEC0 9
+#define ELEC1 8
+#else
+#error TARGET NOT DEFINED
+#endif
+
+//Defines para poder ver mas simple la maquina de estado
+#define INICIO  0
+#define MODO    1
+#define MUESTRO 2
+#define ACEL    3
+#define ANALOG  4
+#define DIGIT   6
+#define ERROR   5
+
+Serial pc(USBTX, USBRX);
+
+//Funciones
+void recibo();//Paso lo que recibo por pc para ver lo que pide
+void r_lrc();//Aca hace el lrc de lo recibido y lo imprime todo
+void sw();//funcion que sirve para saber si se prende o apaga
+void l_on();//funcion para el parpadeo cuando esta habilitado
+void l_off();//funcion para el parpadeo cuando esta habilitado
+
+//Variables
+char buffer[5];//Guardo lo que recibo de PC aca
+float xm=0,ym=0,zm=0,j=0,an=0,dig=0;//Variables de lo medido
+int estado=0,i=0,e=0,ace=0,hab=0,enc=0,t=0,dec=0,uni=0;
+int decx=0,unix=0,decy=0,uniy=0,decz=0,uniz=0,decan=0,unian=0;//Decena y unidad acelerometro
+
+//Entradas y salidas
+AnalogIn analog(PTB0);//Analogica
+DigitalIn digit(PTC3);//Digital
+DigitalOut rled(LED1);//Led Rojo
+DigitalOut gled(LED2);//Led Verde
+DigitalOut bled(LED3);//Led Azul
+
+//Timers
+Timer ton;//Timer para contar los ciclos de 0.3
+Timer ton2;//Timer para contar el segundo apagado
+Timer toff;//Timer para contar los ciclos de 0.25
+Timer tw;/*Timer para poder leer cada cierto tiempo el tsi, si no le ponia el 
+tiempo podia tirar un valor alto al azar cuando prendia y se habilitaba solo*/
+
+bool newdata = false;   //Se pone en true cuando hay nuevos datos
+void onCharReceived()
+{
+    //Copiamos lo leido en el buffer
+    pc.gets(buffer,4);
+    newdata = true;
+}
+/*
+ACLARACIONES
+-En la entrada analogica por ejemplo 54 representa 0,54V, osea que va de 0 a 0,99
+es decir hasta 99, pasa lo mismo con el acelerometro.
+-En la digital se envia =1 o =0.
+-El LRC se muestra en hexa.
+*/
+int main()
+{
+    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+    TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
+    rled=1;
+    gled=1;
+    bled=1;
+    pc.attach(&onCharReceived);
+
+    while (true) {
+        tw.start();
+        if(tw.read()>0.05) {
+            j=tsi.readPercentage();
+            tw.stop();
+            tw.reset();
+        }
+        sw();
+        xm = abs(acc.getAccX());
+        ym = abs(acc.getAccY());
+        zm = abs(acc.getAccZ());
+        if(enc==1) {
+            recibo();
+            l_on();
+        } else {
+            newdata=false;
+            l_off();
+        }
+
+
+    }
+}
+void r_lrc()
+{
+    char LRC=0;
+    for (int b = 0; b < sizeof(buffer); b++) {
+        LRC ^= buffer[b];
+    }
+    printf("\n\n %s%X$  \n\n",buffer,LRC);
+}
+void sw()
+{
+    if(j>0.0001&&hab==0) {
+        enc=!enc;
+        hab=1;
+    }
+    if(j<=0&&hab==1) {
+        hab=0;
+    }
+}
+void l_on()
+{
+    rled=1;
+    if(t<2) {
+        ton.start();
+        if(ton.read()>=0.3) {
+            gled=!gled;
+            if(gled==1) {
+                t++;
+            }
+            ton.stop();
+            ton.reset();
+        }
+    } else {
+        gled=1;
+        ton2.start();
+        if(ton2.read()>=1) {
+            t=0;
+            ton2.stop();
+            ton2.reset();
+        }
+    }
+}
+void l_off()
+{
+    ton.stop();
+    ton.reset();
+    ton2.stop();
+    ton2.reset();
+    t=0;
+    gled=1;
+    toff.start();
+    if(toff.read()>=0.25) {
+        rled=!rled;
+        toff.stop();
+        toff.reset();
+    }
+}
+
+void recibo()
+{
+    if(newdata) {
+
+        switch(estado) {
+            case INICIO:
+                if(buffer[0]=='@') {
+                    estado=MODO;
+                } else {
+                    estado=ERROR;
+                }
+                break;
+            case MODO:
+                if(buffer[1]=='x') {
+                    ace=1;
+                    estado=ACEL;
+                } else if(buffer[1]=='y') {
+                    ace=2;
+                    estado=ACEL;
+                } else if(buffer[1]=='z') {
+                    ace=3;
+                    estado=ACEL;
+                } else if(buffer[1]=='a') {
+                    estado=ANALOG;
+                } else if(buffer[1]=='d') {
+                    estado=DIGIT;
+                } else if(estado==1) {
+                    estado=ERROR;
+                }
+                break;
+            case MUESTRO:
+                estado=INICIO;
+                i=0;
+                r_lrc();
+                newdata=false;
+                break;
+            case ACEL:
+                if(ace==1&&buffer[2]=='$') {
+                    decx=xm*10;
+                    unix=xm*100-decx*10;
+                    buffer[2]=decx+48;
+                    buffer[3]=unix+48;
+                    estado=MUESTRO;
+                } else if(ace==2&&buffer[2]=='$') {
+
+                    decy=ym*10;
+                    uniy=ym*100-decy*10;
+                    buffer[2]=decy+48;
+                    buffer[3]=uniy+48;
+                    estado=MUESTRO;
+                } else if(ace==3&&buffer[2]=='$') {
+                    decz=zm*10;
+                    uniz=zm*100-decz*10;
+                    buffer[2]=decz+48;
+                    buffer[3]=uniz+48;
+                    estado=MUESTRO;
+                } else {
+                    estado=ERROR;
+                }
+                break;
+            case 4:
+                if(buffer[2]=='$') {
+                    gled=1;
+                    an=analog*100;
+                    decan=an/10;
+                    unian=an-decan*10;
+                    buffer[2]=decan+48;
+                    buffer[3]=unian+48;
+                    estado=MUESTRO;
+                } else {
+                    estado=ERROR;
+                }
+                break;
+            case ERROR:
+                newdata=false;
+                estado=INICIO;
+                break;
+            case DIGIT:
+                if(buffer[2]=='$') {
+                    dig=digit;
+                    buffer[2]='=';
+                    buffer[3]=digit+48;
+                    estado=MUESTRO;
+                } else {
+                    newdata=false;
+                    estado=ERROR;
+                }
+                break;
+        }
+    }
+}
\ No newline at end of file