ELEC350 - Team Q / Mbed OS z_compare_commands_NEW_METHOD

Dependencies:   mbed-os

Fork of z_compare_commands by ELEC350 - Team Q

Revision:
4:d03967db9991
Parent:
2:690dd6bdb9a7
Child:
5:63aa16394fdb
--- a/main.cpp	Fri Jan 05 17:49:55 2018 +0000
+++ b/main.cpp	Sat Jan 06 02:15:13 2018 +0000
@@ -5,6 +5,7 @@
 #include <string> 
 #include <stdio.h>
 #include <ctype.h>
+#include <sstream>
 
 //Digital outputs
 DigitalOut onBoardLED(LED2);
@@ -13,11 +14,36 @@
 DigitalOut greenLED(PB_11);
 
 char rxBuffer[80];
+char puttyNum[20];
 
 char i = 0;
 char c = 0;
 
-string key = "apple";
+int puttyRes;
+float puttyFloat;
+
+// commands
+string command = "READ ALL";
+string command2 = "DELETE ALL";
+string command3 = "READ";
+int readN;
+string command4 = "DELETE";
+int deleteN = 0;
+string command5 = "SETDATE";
+int setDay = 0;
+int setMonth = 0;
+int setYear = 0;
+string command6 = "SETTIME";
+int setHour = 0;
+int setMinute = 0;
+int setSecond = 0;
+string command7 = "SETT";
+float Tmin = 0.1;
+float setT;
+string command8 = "STATE";
+int stateX =0;
+string command9 = "LOGGING";
+int logX = 0;
 
 
 RawSerial pc(USBTX, USBRX, 9600);
@@ -51,34 +77,166 @@
             greenLED = !greenLED;
             string rxBufferSTR = rxBuffer;
             int c = rxBufferSTR.length();
-            rxBuffer[c-1] = NULL;           // CR character. Probably not needed
-            //string str(rxBuffer);
-            //printf("rxBuffer: %s\n", rxBuffer);
-            //printf("rxBuffer is %u characters long\n", (unsigned)strlen(rxBuffer));
+ 
+            pc.printf("rxBuffer: %srxBuffer size: %d\n", rxBuffer, c);
 
-            if (rxBufferSTR.find(key) != string::npos) {
-                pc.printf("Found");
-            }
-            /*
-            if (strcmp (key,rxBuffer) == 0) {
-                puts ("Apple is delicious!\n");
+            // Command check here
 
-                pc.printf ("The command entered is %u characters long.\n",(unsigned)strlen(key));
-                int i =0;
-                int keysize = (unsigned)strlen(key);
-                int endfor = keysize;
-
-                //check for number here
+            if (rxBufferSTR.find(command) != string::npos) {
+                pc.printf("Command READ ALL\n");
+            }
+            else if (rxBufferSTR.find(command2) != string::npos){
+                pc.printf("Command DELETE ALL\n");
+                //FOO
+                pc.printf("DELETED N RECORDS\n");
+                }
+            else if (rxBufferSTR.find(command3) != string::npos){
+                pc.printf("Command READ\n");
+                
+                // for commands with numbers
+                int commandSize = command3.length();
+                pc.printf("size of key %d\n", commandSize);
+                int j=0;
+                for(i=commandSize+1; i<c; i++) {
+                    puttyNum[j] = rxBuffer[i];
+                    j = j + 1;
+                    if (j > 2) {
+                        break;
+                    }
+                }
+                pc.printf("puttyNum %s\n", puttyNum);
+                stringstream ss;
+                for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
+                    ss << puttyNum[z];
+                }
+                ss >> readN;
+                printf("Reading %i records\n", readN);
+                }
+            else if (rxBufferSTR.find(command4) != string::npos){
+                pc.printf("Command DELETE\n");
+                 // for commands with numbers
+                int commandSize = command4.length();
+                pc.printf("size of key %d\n", commandSize);
+                int j=0;
+                for(i=commandSize+1; i<c; i++) {
+                    puttyNum[j] = rxBuffer[i];
+                    j = j + 1;
+                    if (j > 2) {
+                        break;
+                    }
+                }
+                pc.printf("puttyNum %s\n", puttyNum);
+                stringstream ss;
+                for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
+                    ss << puttyNum[z];
+                }
+                ss >> deleteN;
+                printf("DELETED %i RECORDS\n", deleteN);
+                }
+            else if (rxBufferSTR.find(command5) != string::npos){
+                pc.printf("Command SETDATE\n");
+                 // for commands with numbers
+                int commandSize = command5.length();
+                pc.printf("size of key %d\n", commandSize);
+                int j=0;
+                for(i=commandSize+1; i<c; i++) {
+                    puttyNum[j] = rxBuffer[i];
+                    j = j + 1;
+                    if (j > 10) {
+                        break;
+                    }
+                }
+                pc.printf("puttyNum %s\n", puttyNum);
+                stringstream ss;
+                for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
+                    ss << puttyNum[z];
+                }
+                ss >> setDay;
+                for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
+                    ss << puttyNum[z];
+                }
+                ss >> setMonth;
+                for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
+                    ss << puttyNum[z];
+                }
+                ss >> setYear;
+                printf("DATE UPDATED TO %i.%i.%i\n", setDay, setMonth, setYear);
+                }
+            else if (rxBufferSTR.find(command6) != string::npos){
+                pc.printf("Command SETTIME\n");
+                 // for commands with numbers
+                int commandSize = command6.length();
+                pc.printf("size of key %d\n", commandSize);
+                int j=0;
+                for(i=commandSize+1; i<c; i++) {
+                    puttyNum[j] = rxBuffer[i];
+                    j = j + 1;
+                    if (j > 10) {
+                        break;
+                    }
+                }
+                pc.printf("puttyNum %s\n", puttyNum);
+                stringstream ss;
+                for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
+                    ss << puttyNum[z];
+                }
+                ss >> setHour;
+                for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
+                    ss << puttyNum[z];
+                }
+                ss >> setMinute;
+                for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
+                    ss << puttyNum[z];
+                }
+                ss >> setSecond;
+                printf("TIME UPDATED TO %i.%i.%i\n", setHour, setMinute, setSecond);
+                }
+            else if (rxBufferSTR.find(command7) != string::npos){
+                pc.printf("Command SETT\n");
+                // for commands with numbers
+                int commandSize = command7.length();
+                pc.printf("size of key %d\n", commandSize);
+                int j=0;
+                for(i=commandSize+1; i<c; i++) {
+                    puttyNum[j] = rxBuffer[i];
+                    j = j + 1;
+                    if (j > 3) {
+                        break;
+                    }
+                }
+                pc.printf("puttyNum %s\n", puttyNum);
+                stringstream ss;
+                for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
+                    ss << puttyNum[z];
+                }
+                ss >> puttyFloat;
+                printf("puttyFloat %6.4f\n", puttyFloat);
+                
+                if ((Tmin <= puttyFloat) && ( puttyFloat <= 60)) {
+                    setT = puttyFloat;
+                    printf("T UPDATED TO %6.4f\n", setT);
+                    }
+                else {
+                    printf("OUT OF RANGE\n");    
+                    }
+                    
+                }
+            else if (rxBufferSTR.find(command8) != string::npos){
+                pc.printf("Command STATE\n");
+                stateX = !stateX;
+                printf("SAMPLING %i\n", stateX);
+                
+                }
+            else if (rxBufferSTR.find(command9) != string::npos){
+                pc.printf("Command LOGGING\n");
+                logX = !logX;
+                printf("LOGGING %i\n", logX);
+                }
+            else {
+                pc.printf("Command not found.\n");
+            }
 
-                for(i=0; i<10; i++) {
-                    pc.printf("%c", rxBuffer[i]);
-                }
-
-            } else {
-                puts ("try again loser!\n");
-            }
-            */
-            
+            memset(puttyNum, 0, sizeof(puttyNum));
             memset(rxBuffer, 0, sizeof(rxBuffer)); //only reset after strcmp
             i = 0;
         }
@@ -91,7 +249,7 @@
     
     serialRX.start(rx_thread);      //start serial interrupt thread
     pc.attach(&Rx_interrupt);           //attach interrupt function that triggers when data is available
-    
+    pc.printf("Please enter command in full caps\n");
     while(1) {
         Thread::wait(2500);
     }