ADSF

Dependencies:   mbed FRDM_MMA8451Q

Revision:
1:bf4ad12efb64
Parent:
0:0cccdf8e396f
Child:
2:b6569bd9af81
--- a/main.cpp	Tue Jun 14 14:45:41 2016 +0000
+++ b/main.cpp	Thu Jun 16 14:45:02 2016 +0000
@@ -1,61 +1,82 @@
 #include "mbed.h"
+#include "MMA8451Q.h"
 //#include "TSISensor.h"
 #define DEBUG
 #define INFOMESSAGES
 #define WARNMESSAGES
 #define ERRMESSAGES
- 
+
 #define FUNCNAME "MAIN"
- 
+
+
+
+#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)
+
+
 #ifdef NoDEBUG
 #define DBG(x, ...) pc.printf("    ["FUNCNAME" : DBG] "x" <line %d>\r\n", ##__VA_ARGS__,__LINE__);
 #else
 #define DBG(x, ...)
 #endif
- 
+
 #ifdef ERRMESSAGES
 #define ERR(x, ...) pc.printf(" ["FUNCNAME" : ERR] "x"\r\n", ##__VA_ARGS__);
 #else
 #define ERR(x, ...)
 #endif
- 
+
 #ifdef WARNMESSAGES
 #define WARN(x, ...) printf("["FUNCNAME" : WARN] "x"\r\n", ##__VA_ARGS__);
 #else
 #define WARN(x, ...)
 #endif
- 
+
 #ifdef INFOMESSAGES
-#define INFO(x, ...) pc.printf("["FUNCNAME" : INFO] "x"\r\n", ##__VA_ARGS__);
+#define INFO(x, ...) pc.printf("["FUNCNAME" : INFO] "x"\r\n",##__VA_ARGS__);
 #else
 #define INFO(x, ...)
 #endif
- 
+
 #define BUFF_SIZE 1024
 // PIN DEFS NOW FOR OLD FIRMWARE VERSION OF THE KL25Z
 Serial  pc(USBTX, USBRX);
 //RawSerial  dev(D1, D0);
-Serial  dev(PTD3,PTD2); //tx,rx
+Serial  dev(PTE22,PTE23); //tx,rx
 DigitalOut led1(LED1);
 DigitalOut led4(LED3); // CHANGE TO LED 3
-DigitalOut reset(PTA13,1);
- 
- 
-    PwmOut led(LED_GREEN);
+DigitalOut reset(PTB0,1);
+
+
+
+PwmOut led(LED_GREEN);
 //    TSISensor tsi;
 volatile int state=0;
 volatile int ready=0;
- 
+int glue=0;
 char ipAddress[20];
 char macAddress[32];
 char *buffer;
 unsigned int bufferPnt=0;
- 
+
 void dev_recv()
 {
     char c;
- 
+
     int count = 0;
+
     led1 = !led1;
     if(bufferPnt==0) {
         memset(buffer,0,BUFF_SIZE);
@@ -63,14 +84,14 @@
     while(dev.readable()) {
         c = (char)dev.getc();
 #ifdef DEBUG
-       pc.putc(c);
+        pc.putc(c);
 #endif
         buffer[bufferPnt]=c;
         bufferPnt++;
         if (bufferPnt>1000) {
             ready=1;
         }
-       // if ((c==0x0a)||(c==0x0d)){
+        // if ((c==0x0a)||(c==0x0d)){
         //     ready=1;
         //     }else
         if (c==0x0a) {
@@ -86,7 +107,7 @@
         }
     }
 }
- 
+
 void pc_recv()
 {
     char c;
@@ -98,11 +119,11 @@
         if(c==13) {
             dev.putc(10);
             pc.putc(10);
-            }
- 
+        }
+
     }
 }
- 
+
 char * OKResponse(char *test, const char *pattern)
 {
     char *p= strstr(test,pattern);
@@ -116,16 +137,27 @@
 }
 int main()
 {
+    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
     float touch;
     buffer=(char *)calloc(BUFF_SIZE,1);
     reset=0;
     int counter=0;
+    int zcount;
+    int xcount;
+    zcount=0;
+    xcount=0;
+    float a;
+    float b;
+    b=1.0;
+    a=1.0;
+    float x;
+    float z;
     pc.baud(115200);
     dev.baud(115200);
     pc.attach(&pc_recv, Serial::RxIrq);
     dev.attach(&dev_recv, Serial::RxIrq);
     pc.printf("Start up\n\r");
-    wait(1.5);
+    wait(3.0);
     reset=1;
     char * resp=NULL;
     pc.printf("Here \n\r");
@@ -159,7 +191,7 @@
                         dev.printf("AT+RST\r\n");
                         state++;
                     }
- 
+
                     break;
                 }
                 case 4: {
@@ -168,17 +200,17 @@
                         dev.printf("AT+CWMODE=1\r\n");
                         state++;
                     }
- 
+
                     break;
                 }
                 case 5: {
                     resp=OKResponse(buffer,"OK");
                     if (resp!=NULL) {
-                        
-                        dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
+
+                        dev.printf("AT+CWJAP=\"BTHub3-WXWX\",\"fdd6f7c682\"\r\n");
                         state++;
                     }
- 
+
                     break;
                 }
                 case 6: {
@@ -188,7 +220,7 @@
                         dev.printf("AT+CIFSR\r\n");
                         state++;
                     }
- 
+
                     break;
                 }
                 case 7: {
@@ -197,62 +229,115 @@
                         char *strt = strtok(buffer,"\"");
                         strcpy(ipAddress,strtok(NULL,"\""));
                         strtok(NULL,"\"");
-                         strcpy(macAddress,strtok(NULL,"\""));
+                        strcpy(macAddress,strtok(NULL,"\""));
                         INFO("mac Address = %s", macAddress);
                         INFO("IP Address = %s", ipAddress);
-                          dev.printf("AT+CIPMUX=1\r\n");
+                        dev.printf("AT+CIPMUX=1\r\n");
                         state++;
                     }
- 
+
                     break;
                 }
                 case 8: {
                     resp=OKResponse(buffer,"OK");
                     if (resp!=NULL) {
-                       INFO("Ready");
-                       dev.printf("AT+CIPSTART=4,\"TCP\",\"192.168.1.11\",8080\r\n");
-                       state++;
+
+                        INFO("Ready 8");
+                        dev.printf("AT+CIPSTART=4,\"TCP\",\"192.168.1.91\",8080\r\n");
+                        state++;
+                        wait(0.1);
+
                     }
- 
-                     break;
-                     }
+
+                    break;
+                }
                 case 9: {
+
                     resp=OKResponse(buffer,"OK");
                     if (resp!=NULL) {
-                       INFO("Ready");
-                       state++; 
-                       dev.printf("AT+CIPSEND=4,10\r\n");
+
+                        INFO("Ready 9");
+                        state++;
+                        zcount=0;
+                        a=1.0;
+
                     }
- 
-                     break;
-                     }
-                    case 10: {
-                    resp=OKResponse(buffer,">");
-                    if (resp!=NULL) {
-                       INFO("SENDING");
-                       state++; 
-  //                     touch=tsi.readPercentage();
-    //                   dev.printf("TSI=%1.2f\n\r",touch);
-                       pc.printf("AAAAAAAAAA");
-                       //INFO("sensor=%1.2f",float)tsi.readPercentage());
+
+                }
+                case 10: {
+
+                    while (true) {
+                        z = (acc.getAccZ());
+                        x = (acc.getAccX());
+
+                        if (a==1.0) {
+
+                            if (z<0.0) {
+                                a=0.0;
+                                zcount++;
+                                pc.printf(".......zc=%02u\r\n",zcount);
+
+                                resp=OKResponse(buffer,"OK");
+                                if (resp!=NULL) {
+                                    INFO("Ready 10");
+                                    dev.printf("AT+CIPSEND=4,2\r\n");
+                                    wait(0.1);
+
+                                }
+                                wait(0.1);
+                             //  while (OKResponse(buffer,">")!=NULL){
+                             //   printf(".\r\n");
+                              //  wait(1);
+                              //  };
+                                
+                                    INFO("SENDING");
+
+                                    wait(0.2);
+                                    dev.printf("%02u\r\n",zcount);
+
+                                
+
+
+                            }
+
+                        }
+                        if (z>0) {
+                            a=1.0;
+                        }
                     }
-                    case 11: {
-                    resp=OKResponse(buffer,"SEND OK");
-                    if (resp!=NULL) {
-                       INFO("SEND OK");
-                       state=9; 
-                       //wait(2);
-                       }
-                       wait(1);
-                       state=9; 
+                    if (a==0) {
+                        if (z<0) {
+                            a=0.0;
+                        }
+                        if (z>0) {
+                            a=1.0;
+                        }
                     }
- 
-                     break;
+                    if (b==1) {
+                        if (x<0) {
+                            b=0.0;
+                            xcount=xcount+1;
+                            //printf("Xcount: %u \n\r",xcount);
+
+                        }
+                        if (x>0) {
+                            b=1.0;
+                        }
+                    }
+                    if (b==0) {
+                        if (x<0) {
+                            b=0.0;
+                        }
+                        if (x>0) {
+                            b=1.0;
+                        }
+                    }
+
                 }
- 
- 
+                break;
             }
         }
         //__WFI(); // DELETED AS IT KILLS THE KL25 RESPONSIVITY
     }
-//}
\ No newline at end of file
+
+}