для управления турелью

Dependencies:   mbed

Revision:
3:e47c0c98f515
Parent:
2:a9d63ae515ad
--- a/uart.cpp	Sun Jan 15 22:32:04 2017 +0000
+++ b/uart.cpp	Thu Jan 19 05:22:19 2017 +0000
@@ -1,23 +1,27 @@
 #include "uart.hpp"
 #include "mbed.h"
-#include "rtos.h"
+//#include "rtos.h"
 #include <string> 
 #include <stdlib.h>
+#include "main.hpp"
 #include "servo.hpp"
 #include "gun.hpp"
+#include "led_color.hpp"
+
+StateLed UartStateLed;
 
 using namespace std;
 // скорость UART
-const int baudRate = 115200;
+const int baudRate = UART_BAUDRATE;
 // время обновления UART
 const int uartDelay = 10;
 //const int servoDigit = 10;
 
-Serial pc(SERIAL_TX, SERIAL_RX, baudRate);
-
+Serial pc(PIN_UART_TX, PIN_UART_RX, baudRate);
+/*
 void uartThread(void const *argument) {
     while (true) {
-        Thread::wait(uartDelay);
+        //Thread::wait(uartDelay);
         string inputData = "";
         if (pc.readable() > 0) { 
             while(true) {
@@ -30,6 +34,7 @@
                     break; 
                 } // if
             } // while
+            if (inputData.length() > 3) 
             if (inputData.substr(0, 3) == "get") {
                 pc.printf("servo 0 %f\n", getServoAngle(0));
                 pc.printf("servo 1 %d\n", getServoAngle(1));
@@ -70,18 +75,119 @@
                       setServoAngle(servoTwo, 1);
                 }
             } else
-            if (inputData.substr(0, 4) == "shot") {
+            if (inputData.length() >= 4 && inputData.substr(0, 4) == "shot") {
                 shotGun();
             } else
-            if (inputData.substr(0, 6) == "gun on") {
+            if (inputData.length() >= 6 && inputData.substr(0, 6) == "gun on") {
                 enabledGun();
             } else
-            if (inputData.substr(0, 7) == "gun off") {
+            if (inputData.length() >= 7 && inputData.substr(0, 7) == "gun off") {
                 disabledGun();
+            } else
+            if (inputData.length() >= 7 && inputData.substr(0, 7) == "led red") {
+                UartStateLed.setLedRed();
+            } else
+            if (inputData.length() >= 9 && inputData.substr(0, 9) == "led green") {
+                UartStateLed.setLedRed();
+            } else
+            if (inputData.length() >= 8 && inputData.substr(0, 8) == "led blue") {
+                UartStateLed.setLedRed();
+            } else
+            if (inputData.length() >= 8 && inputData.substr(0, 8) == "led null") {
+                UartStateLed.setLedNull();
+            } else
+            if (inputData.length() >= 9 && inputData.substr(0, 9) == "led white") {
+                UartStateLed.setLedRed();
             }
         } // if
         while(pc.readable() > 0) {
             char p = pc.getc(); 
         }
     }
+}
+*/
+
+void uartUpdate(void) {
+        string inputData = "";
+        if (pc.readable() > 0) { 
+            while(true) {
+                char p = pc.getc(); 
+                inputData = inputData + p;
+                if(p == '\n' || p == '\r') { 
+                    pc.printf("\n"); 
+                    pc.printf(inputData.c_str()); 
+                    pc.printf("\n"); 
+                    break; 
+                } // if
+            } // while
+            if (inputData.length() > 3) 
+            if (inputData.substr(0, 3) == "get") {
+                pc.printf("servo 0 %f\n", getServoAngle(0));
+                pc.printf("servo 1 %d\n", getServoAngle(1));
+            } else
+            if (inputData.substr(0, 3) == "set" && inputData.length() > 13) {
+                char* pEnd;
+                double servoOne, servoTwo;
+                // строка с командой
+                string setdata = inputData.substr(0, 13);
+                // строка с числом
+                string numer = inputData.substr(14, inputData.length());
+                if (setdata == "set all angle") {
+                    servoOne = strtod(numer.c_str(), &pEnd);
+                    servoTwo = strtod(pEnd, NULL);
+                    setServoAngle(servoOne, 0);
+                    setServoAngle(servoTwo, 1);
+                } else
+                if (setdata == "set all speed") {
+                    servoOne = strtod(numer.c_str(), &pEnd);
+                    servoTwo = strtod(pEnd, NULL);
+                    setServoSpeed(servoOne, 0);
+                    setServoSpeed(servoTwo, 1);
+                } else
+                if (setdata == "set one speed") {
+                     servoOne = strtod(numer.c_str(), NULL);
+                     setServoSpeed(servoOne, 0);
+                } else
+                if (setdata == "set two speed") {
+                     servoTwo = strtod(numer.c_str(), NULL);
+                     setServoSpeed(servoTwo, 1);
+                } else
+                if (setdata == "set one angle") {
+                     servoOne = strtod(numer.c_str(), NULL);
+                     setServoAngle(servoOne, 0);
+                } else
+                if (setdata == "set two angle") {
+                     servoTwo = strtod(numer.c_str(), NULL);
+                      setServoAngle(servoTwo, 1);
+                }
+            } else
+            if (inputData.length() >= 4 && inputData.substr(0, 4) == "shot") {
+                shotGun();
+            } else
+            if (inputData.length() >= 6 && inputData.substr(0, 6) == "gun on") {
+                enabledGun();
+            } else
+            if (inputData.length() >= 7 && inputData.substr(0, 7) == "gun off") {
+                disabledGun();
+            } else
+            if (inputData.length() >= 7 && inputData.substr(0, 7) == "led red") {
+                UartStateLed.setLedRed();
+            } else
+            if (inputData.length() >= 9 && inputData.substr(0, 9) == "led green") {
+                UartStateLed.setLedRed();
+            } else
+            if (inputData.length() >= 8 && inputData.substr(0, 8) == "led blue") {
+                UartStateLed.setLedRed();
+            } else
+            if (inputData.length() >= 8 && inputData.substr(0, 8) == "led null") {
+                UartStateLed.setLedNull();
+            } else
+            if (inputData.length() >= 9 && inputData.substr(0, 9) == "led white") {
+                UartStateLed.setLedRed();
+            }
+        } // if
+        while(pc.readable() > 0) {
+            char p = pc.getc(); 
+        }
+    //}
 }
\ No newline at end of file