Fork

Dependencies:   Motordriver mbed-rtos mbed

Fork of ESP8266-WEB-Mbed-LPC1768-Controller by jim hamblen

Revision:
5:cfceccd5ccb1
Parent:
4:40dd020463ea
diff -r 40dd020463ea -r cfceccd5ccb1 main.cpp
--- a/main.cpp	Fri Aug 28 01:37:50 2015 +0000
+++ b/main.cpp	Tue Oct 20 18:21:16 2015 +0000
@@ -2,9 +2,11 @@
 
 #include "mbed.h"
 //#include "DS18B20.h"
+#include "motordriver.h"
+#include "rtos.h"
 
-Serial pc(USBTX, USBRX);
-Serial esp(p28, p27); // tx, rx
+RawSerial pc(USBTX, USBRX);
+RawSerial esp(p28, p27); // tx, rx
 
 //DS18B20 thermom(A0, DS18B20::RES_12_BIT);
 
@@ -12,12 +14,15 @@
 DigitalOut  led1(LED1);      // (PTB18)
 DigitalOut  led2(LED2);    // (PTB19)
 DigitalOut  led3(LED3);     // (PTD1)
+DigitalOut led4(LED4);
+Motor left(p21, p23, p22, 1); // pwm, fwd, rev, has brake feature
+Motor right(p26, p25, p24, 1);
 
 // Digital Out and In pins, can be configured to any suitable pin depending on Platform
 DigitalOut  Out1(p6);
 DigitalOut  Out2(p7);
 DigitalOut  Out3(p8);
-DigitalOut  reset(p26);
+DigitalOut  reset(p29);
 
 DigitalIn  In1(p9);
 DigitalIn  In2(p10);
@@ -30,6 +35,14 @@
 Timer t1;
 Timer t2;
 
+void motorThread(void const *args);
+bool gMotorLatch = false;
+int gMotorCommand = 0;
+#define CMD_FWD 1
+#define CMD_BACK 2
+#define CMD_LEFT 3
+#define CMD_RIGHT 4
+
 struct tm t;
 
 int bufflen, DataRX, count, getcount, replycount, servreq, timeout;
@@ -53,6 +66,9 @@
 void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck();
 void gettime(),gettemp(),getbattery(),setRTC(),beep();
 
+bool simpleResponse = false;
+void sendSimplePage();
+
 // manual set RTC values
 int minute      =00;    // 0-59
 int hour        =12;    // 2-23
@@ -67,10 +83,14 @@
 void callback()
 {
     led3=1;
+    led4 = 0;
     while (esp.readable()) {
+        led4 = !led4;
         webbuff[count] = esp.getc();
         count++;
     }
+    
+    led2 = 0;
     if(strlen(webbuff)>bufflen) {
         DataRX=1;
         led3=0;
@@ -78,7 +98,7 @@
 }
 
 int main()
-{
+{   
     reset=0;
     pc.baud(115200);
 
@@ -96,12 +116,19 @@
     beep();
     startserver();
 
+    Thread mThreadYo(motorThread);
+
     while(1) {
         if(DataRX==1) {
             ReadWebData();
             beep();
             if (servreq == 1 && weberror == 0) {
-                sendpage();
+                if (simpleResponse == true) {
+                    sendSimplePage();
+                    simpleResponse = false;
+                } else {
+                    sendpage();
+                }
             }
             esp.attach(&callback);
             pc.printf(" IPD Data:\r\n\n Link ID = %d,\r\n IPD Header Length = %d \r\n IPD Type = %s\r\n", linkID, ipdLen, type);
@@ -113,6 +140,21 @@
         }
     }
 }
+void sendSimplePage() {
+    strcpy(webbuff, "<!DOCTYPE html><html><head></head><body>OK</body></html>");
+
+// end of WEB page data
+    bufl = strlen(webbuff); // get total page buffer length
+    sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
+    timeout=200;
+    getcount=7;
+    SendCMD();
+    getreply();
+    SendWEB();  // send web page
+    memset(webbuff, '\0', sizeof(webbuff));
+    sendcheck();
+}
+
 // Static WEB page
 void sendpage()
 {
@@ -255,29 +297,39 @@
     memset(webdata, '\0', sizeof(webdata));
     int x = strcspn (webbuff,"+");
     if(x) {
+        pc.printf("DATA: %s", webdata);
         strcpy(webdata, webbuff + x);
         weberror=0;
         int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
-        if( strstr(webdata, "led1=1") != NULL ) {
-            led1=1;
+        if( strstr(webdata, "led1=1") != NULL || strstr(webdata, "keycode=fwd") != NULL) {
+            gMotorLatch = true;
+            gMotorCommand = CMD_FWD;
+            
+            simpleResponse = true;
         }
         if( strstr(webdata, "led1=0") != NULL ) {
             led1=0;
         }
-        if( strstr(webdata, "Out1=1") != NULL ) {
-            Out1=1;
+        if( strstr(webdata, "Out1=1") != NULL || strstr(webdata, "keycode=back") != NULL) {
+            gMotorLatch = true;
+            gMotorCommand = CMD_BACK;
+            simpleResponse = true;
         }
         if( strstr(webdata, "Out1=0") != NULL ) {
             Out1=0;
         }
-        if( strstr(webdata, "Out2=1") != NULL ) {
-            Out2=1;
+        if( strstr(webdata, "Out2=1") != NULL || strstr(webdata, "keycode=left") != NULL) {
+            gMotorLatch = true;
+            gMotorCommand = CMD_LEFT;
+            simpleResponse = true;
         }
         if( strstr(webdata, "Out2=0") != NULL ) {
             Out2=0;
         }
-        if( strstr(webdata, "Out3=1") != NULL ) {
-            Out3=1;
+        if( strstr(webdata, "Out3=1") != NULL || strstr(webdata, "keycode=right") != NULL) {
+            gMotorLatch = true;
+            gMotorCommand = CMD_RIGHT;
+            simpleResponse = true;
         }
         if( strstr(webdata, "Out3=0") != NULL ) {
             Out3=0;
@@ -434,3 +486,55 @@
     t.tm_year = ((year)+100);   // year since 1900,  current DCF year + 100 + 1900 = correct year
     set_time(mktime(&t));       // set RTC clock
 }
+
+// Control motor related things
+void motorThread(void const *args) {
+    pc.printf("motorThread started\n\r");
+    
+    while (1) {
+        while (gMotorLatch == false) {
+            Thread::wait(100);
+        }
+        
+        pc.printf("motorThread Latched!\n\r");
+        
+        gMotorLatch = false;
+        
+        float lSpeed = 0.0f;
+        float rSpeed = 0.0f;
+        float delayMs = 1000;
+        switch(gMotorCommand) {
+            case CMD_FWD:
+                lSpeed = 0.5f;
+                rSpeed = 0.5f;
+                break;
+            
+            case CMD_BACK:
+                lSpeed = -0.5f;
+                rSpeed = -0.5f;
+                break;
+            
+            case CMD_LEFT:
+                lSpeed = -0.5f;
+                rSpeed = 0.5f;
+                delayMs = 500;
+                break;
+            
+            case CMD_RIGHT:
+                lSpeed = 0.5f;
+                rSpeed = -0.5f;
+                delayMs = 500;
+                break;
+            
+            default:
+                delayMs = 0;
+                break;
+        }
+        
+        left.speed(lSpeed);
+        right.speed(rSpeed);
+        Thread::wait(delayMs);
+        left.speed(0);
+        right.speed(0);
+    }
+}
\ No newline at end of file