ELEC350 - Team Q / Mbed OS z_compare_commands_NEW_METHOD

Dependencies:   mbed-os

Fork of z_compare_commands by ELEC350 - Team Q

Committer:
osmith2
Date:
Sat Jan 06 02:15:13 2018 +0000
Revision:
4:d03967db9991
Parent:
2:690dd6bdb9a7
Child:
5:63aa16394fdb
now with all the commands, including ability to set parameters. Written to the spec set out in requirement 7. ; ; many print statements have been left in for debugging, they can be commented out/deleted.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
osmith2 0:f46e16e62b0c 1 //http://www.cplusplus.com/reference/cstring/strcmp/
osmith2 0:f46e16e62b0c 2
osmith2 0:f46e16e62b0c 3 #include "mbed.h"
eawhite 2:690dd6bdb9a7 4 #include <iostream>
eawhite 2:690dd6bdb9a7 5 #include <string>
eawhite 2:690dd6bdb9a7 6 #include <stdio.h>
eawhite 2:690dd6bdb9a7 7 #include <ctype.h>
osmith2 4:d03967db9991 8 #include <sstream>
osmith2 0:f46e16e62b0c 9
eawhite 2:690dd6bdb9a7 10 //Digital outputs
eawhite 2:690dd6bdb9a7 11 DigitalOut onBoardLED(LED2);
eawhite 2:690dd6bdb9a7 12 DigitalOut redLED(PE_15);
eawhite 2:690dd6bdb9a7 13 DigitalOut yellowLED(PB_10);
eawhite 2:690dd6bdb9a7 14 DigitalOut greenLED(PB_11);
eawhite 2:690dd6bdb9a7 15
eawhite 2:690dd6bdb9a7 16 char rxBuffer[80];
osmith2 4:d03967db9991 17 char puttyNum[20];
eawhite 2:690dd6bdb9a7 18
eawhite 2:690dd6bdb9a7 19 char i = 0;
eawhite 2:690dd6bdb9a7 20 char c = 0;
eawhite 2:690dd6bdb9a7 21
osmith2 4:d03967db9991 22 int puttyRes;
osmith2 4:d03967db9991 23 float puttyFloat;
osmith2 4:d03967db9991 24
osmith2 4:d03967db9991 25 // commands
osmith2 4:d03967db9991 26 string command = "READ ALL";
osmith2 4:d03967db9991 27 string command2 = "DELETE ALL";
osmith2 4:d03967db9991 28 string command3 = "READ";
osmith2 4:d03967db9991 29 int readN;
osmith2 4:d03967db9991 30 string command4 = "DELETE";
osmith2 4:d03967db9991 31 int deleteN = 0;
osmith2 4:d03967db9991 32 string command5 = "SETDATE";
osmith2 4:d03967db9991 33 int setDay = 0;
osmith2 4:d03967db9991 34 int setMonth = 0;
osmith2 4:d03967db9991 35 int setYear = 0;
osmith2 4:d03967db9991 36 string command6 = "SETTIME";
osmith2 4:d03967db9991 37 int setHour = 0;
osmith2 4:d03967db9991 38 int setMinute = 0;
osmith2 4:d03967db9991 39 int setSecond = 0;
osmith2 4:d03967db9991 40 string command7 = "SETT";
osmith2 4:d03967db9991 41 float Tmin = 0.1;
osmith2 4:d03967db9991 42 float setT;
osmith2 4:d03967db9991 43 string command8 = "STATE";
osmith2 4:d03967db9991 44 int stateX =0;
osmith2 4:d03967db9991 45 string command9 = "LOGGING";
osmith2 4:d03967db9991 46 int logX = 0;
osmith2 0:f46e16e62b0c 47
osmith2 0:f46e16e62b0c 48
eawhite 2:690dd6bdb9a7 49 RawSerial pc(USBTX, USBRX, 9600);
eawhite 2:690dd6bdb9a7 50 Thread serialRX(osPriorityNormal);
eawhite 2:690dd6bdb9a7 51
eawhite 2:690dd6bdb9a7 52 // Rx Interupt routine
eawhite 2:690dd6bdb9a7 53 void Rx_interrupt(){
eawhite 2:690dd6bdb9a7 54 pc.attach(NULL, Serial::RxIrq); // Disable Rx interrupt
eawhite 2:690dd6bdb9a7 55 serialRX.signal_set(0xA); // Set signal for Rx thread
eawhite 2:690dd6bdb9a7 56 }
osmith2 0:f46e16e62b0c 57
eawhite 2:690dd6bdb9a7 58 // Read received chars from UART
eawhite 2:690dd6bdb9a7 59 void rx_thread(){
eawhite 2:690dd6bdb9a7 60 while (true) {
eawhite 2:690dd6bdb9a7 61 Thread::signal_wait(0xA);
eawhite 2:690dd6bdb9a7 62 Thread::signal_clr(0xA);
eawhite 2:690dd6bdb9a7 63 //memset(rxBuffer, 0, sizeof(rxBuffer));
eawhite 2:690dd6bdb9a7 64 while (pc.readable()) {
eawhite 2:690dd6bdb9a7 65 c = pc.getc();
eawhite 2:690dd6bdb9a7 66 rxBuffer[i] = c;
eawhite 2:690dd6bdb9a7 67 i = i + 1;
eawhite 2:690dd6bdb9a7 68 yellowLED = !yellowLED;
eawhite 2:690dd6bdb9a7 69 }
eawhite 2:690dd6bdb9a7 70
eawhite 2:690dd6bdb9a7 71 pc.attach(&Rx_interrupt); // Enable Rx interrupt
eawhite 2:690dd6bdb9a7 72 redLED = !redLED;
eawhite 2:690dd6bdb9a7 73
eawhite 2:690dd6bdb9a7 74
eawhite 2:690dd6bdb9a7 75
eawhite 2:690dd6bdb9a7 76 if (c == '\r') {
eawhite 2:690dd6bdb9a7 77 greenLED = !greenLED;
eawhite 2:690dd6bdb9a7 78 string rxBufferSTR = rxBuffer;
eawhite 2:690dd6bdb9a7 79 int c = rxBufferSTR.length();
osmith2 4:d03967db9991 80
osmith2 4:d03967db9991 81 pc.printf("rxBuffer: %srxBuffer size: %d\n", rxBuffer, c);
eawhite 2:690dd6bdb9a7 82
osmith2 4:d03967db9991 83 // Command check here
eawhite 2:690dd6bdb9a7 84
osmith2 4:d03967db9991 85 if (rxBufferSTR.find(command) != string::npos) {
osmith2 4:d03967db9991 86 pc.printf("Command READ ALL\n");
osmith2 4:d03967db9991 87 }
osmith2 4:d03967db9991 88 else if (rxBufferSTR.find(command2) != string::npos){
osmith2 4:d03967db9991 89 pc.printf("Command DELETE ALL\n");
osmith2 4:d03967db9991 90 //FOO
osmith2 4:d03967db9991 91 pc.printf("DELETED N RECORDS\n");
osmith2 4:d03967db9991 92 }
osmith2 4:d03967db9991 93 else if (rxBufferSTR.find(command3) != string::npos){
osmith2 4:d03967db9991 94 pc.printf("Command READ\n");
osmith2 4:d03967db9991 95
osmith2 4:d03967db9991 96 // for commands with numbers
osmith2 4:d03967db9991 97 int commandSize = command3.length();
osmith2 4:d03967db9991 98 pc.printf("size of key %d\n", commandSize);
osmith2 4:d03967db9991 99 int j=0;
osmith2 4:d03967db9991 100 for(i=commandSize+1; i<c; i++) {
osmith2 4:d03967db9991 101 puttyNum[j] = rxBuffer[i];
osmith2 4:d03967db9991 102 j = j + 1;
osmith2 4:d03967db9991 103 if (j > 2) {
osmith2 4:d03967db9991 104 break;
osmith2 4:d03967db9991 105 }
osmith2 4:d03967db9991 106 }
osmith2 4:d03967db9991 107 pc.printf("puttyNum %s\n", puttyNum);
osmith2 4:d03967db9991 108 stringstream ss;
osmith2 4:d03967db9991 109 for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
osmith2 4:d03967db9991 110 ss << puttyNum[z];
osmith2 4:d03967db9991 111 }
osmith2 4:d03967db9991 112 ss >> readN;
osmith2 4:d03967db9991 113 printf("Reading %i records\n", readN);
osmith2 4:d03967db9991 114 }
osmith2 4:d03967db9991 115 else if (rxBufferSTR.find(command4) != string::npos){
osmith2 4:d03967db9991 116 pc.printf("Command DELETE\n");
osmith2 4:d03967db9991 117 // for commands with numbers
osmith2 4:d03967db9991 118 int commandSize = command4.length();
osmith2 4:d03967db9991 119 pc.printf("size of key %d\n", commandSize);
osmith2 4:d03967db9991 120 int j=0;
osmith2 4:d03967db9991 121 for(i=commandSize+1; i<c; i++) {
osmith2 4:d03967db9991 122 puttyNum[j] = rxBuffer[i];
osmith2 4:d03967db9991 123 j = j + 1;
osmith2 4:d03967db9991 124 if (j > 2) {
osmith2 4:d03967db9991 125 break;
osmith2 4:d03967db9991 126 }
osmith2 4:d03967db9991 127 }
osmith2 4:d03967db9991 128 pc.printf("puttyNum %s\n", puttyNum);
osmith2 4:d03967db9991 129 stringstream ss;
osmith2 4:d03967db9991 130 for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
osmith2 4:d03967db9991 131 ss << puttyNum[z];
osmith2 4:d03967db9991 132 }
osmith2 4:d03967db9991 133 ss >> deleteN;
osmith2 4:d03967db9991 134 printf("DELETED %i RECORDS\n", deleteN);
osmith2 4:d03967db9991 135 }
osmith2 4:d03967db9991 136 else if (rxBufferSTR.find(command5) != string::npos){
osmith2 4:d03967db9991 137 pc.printf("Command SETDATE\n");
osmith2 4:d03967db9991 138 // for commands with numbers
osmith2 4:d03967db9991 139 int commandSize = command5.length();
osmith2 4:d03967db9991 140 pc.printf("size of key %d\n", commandSize);
osmith2 4:d03967db9991 141 int j=0;
osmith2 4:d03967db9991 142 for(i=commandSize+1; i<c; i++) {
osmith2 4:d03967db9991 143 puttyNum[j] = rxBuffer[i];
osmith2 4:d03967db9991 144 j = j + 1;
osmith2 4:d03967db9991 145 if (j > 10) {
osmith2 4:d03967db9991 146 break;
osmith2 4:d03967db9991 147 }
osmith2 4:d03967db9991 148 }
osmith2 4:d03967db9991 149 pc.printf("puttyNum %s\n", puttyNum);
osmith2 4:d03967db9991 150 stringstream ss;
osmith2 4:d03967db9991 151 for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
osmith2 4:d03967db9991 152 ss << puttyNum[z];
osmith2 4:d03967db9991 153 }
osmith2 4:d03967db9991 154 ss >> setDay;
osmith2 4:d03967db9991 155 for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
osmith2 4:d03967db9991 156 ss << puttyNum[z];
osmith2 4:d03967db9991 157 }
osmith2 4:d03967db9991 158 ss >> setMonth;
osmith2 4:d03967db9991 159 for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
osmith2 4:d03967db9991 160 ss << puttyNum[z];
osmith2 4:d03967db9991 161 }
osmith2 4:d03967db9991 162 ss >> setYear;
osmith2 4:d03967db9991 163 printf("DATE UPDATED TO %i.%i.%i\n", setDay, setMonth, setYear);
osmith2 4:d03967db9991 164 }
osmith2 4:d03967db9991 165 else if (rxBufferSTR.find(command6) != string::npos){
osmith2 4:d03967db9991 166 pc.printf("Command SETTIME\n");
osmith2 4:d03967db9991 167 // for commands with numbers
osmith2 4:d03967db9991 168 int commandSize = command6.length();
osmith2 4:d03967db9991 169 pc.printf("size of key %d\n", commandSize);
osmith2 4:d03967db9991 170 int j=0;
osmith2 4:d03967db9991 171 for(i=commandSize+1; i<c; i++) {
osmith2 4:d03967db9991 172 puttyNum[j] = rxBuffer[i];
osmith2 4:d03967db9991 173 j = j + 1;
osmith2 4:d03967db9991 174 if (j > 10) {
osmith2 4:d03967db9991 175 break;
osmith2 4:d03967db9991 176 }
osmith2 4:d03967db9991 177 }
osmith2 4:d03967db9991 178 pc.printf("puttyNum %s\n", puttyNum);
osmith2 4:d03967db9991 179 stringstream ss;
osmith2 4:d03967db9991 180 for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
osmith2 4:d03967db9991 181 ss << puttyNum[z];
osmith2 4:d03967db9991 182 }
osmith2 4:d03967db9991 183 ss >> setHour;
osmith2 4:d03967db9991 184 for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
osmith2 4:d03967db9991 185 ss << puttyNum[z];
osmith2 4:d03967db9991 186 }
osmith2 4:d03967db9991 187 ss >> setMinute;
osmith2 4:d03967db9991 188 for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
osmith2 4:d03967db9991 189 ss << puttyNum[z];
osmith2 4:d03967db9991 190 }
osmith2 4:d03967db9991 191 ss >> setSecond;
osmith2 4:d03967db9991 192 printf("TIME UPDATED TO %i.%i.%i\n", setHour, setMinute, setSecond);
osmith2 4:d03967db9991 193 }
osmith2 4:d03967db9991 194 else if (rxBufferSTR.find(command7) != string::npos){
osmith2 4:d03967db9991 195 pc.printf("Command SETT\n");
osmith2 4:d03967db9991 196 // for commands with numbers
osmith2 4:d03967db9991 197 int commandSize = command7.length();
osmith2 4:d03967db9991 198 pc.printf("size of key %d\n", commandSize);
osmith2 4:d03967db9991 199 int j=0;
osmith2 4:d03967db9991 200 for(i=commandSize+1; i<c; i++) {
osmith2 4:d03967db9991 201 puttyNum[j] = rxBuffer[i];
osmith2 4:d03967db9991 202 j = j + 1;
osmith2 4:d03967db9991 203 if (j > 3) {
osmith2 4:d03967db9991 204 break;
osmith2 4:d03967db9991 205 }
osmith2 4:d03967db9991 206 }
osmith2 4:d03967db9991 207 pc.printf("puttyNum %s\n", puttyNum);
osmith2 4:d03967db9991 208 stringstream ss;
osmith2 4:d03967db9991 209 for (int z = 0; z < sizeof(puttyNum) / sizeof(puttyNum[0]); z++) {
osmith2 4:d03967db9991 210 ss << puttyNum[z];
osmith2 4:d03967db9991 211 }
osmith2 4:d03967db9991 212 ss >> puttyFloat;
osmith2 4:d03967db9991 213 printf("puttyFloat %6.4f\n", puttyFloat);
osmith2 4:d03967db9991 214
osmith2 4:d03967db9991 215 if ((Tmin <= puttyFloat) && ( puttyFloat <= 60)) {
osmith2 4:d03967db9991 216 setT = puttyFloat;
osmith2 4:d03967db9991 217 printf("T UPDATED TO %6.4f\n", setT);
osmith2 4:d03967db9991 218 }
osmith2 4:d03967db9991 219 else {
osmith2 4:d03967db9991 220 printf("OUT OF RANGE\n");
osmith2 4:d03967db9991 221 }
osmith2 4:d03967db9991 222
osmith2 4:d03967db9991 223 }
osmith2 4:d03967db9991 224 else if (rxBufferSTR.find(command8) != string::npos){
osmith2 4:d03967db9991 225 pc.printf("Command STATE\n");
osmith2 4:d03967db9991 226 stateX = !stateX;
osmith2 4:d03967db9991 227 printf("SAMPLING %i\n", stateX);
osmith2 4:d03967db9991 228
osmith2 4:d03967db9991 229 }
osmith2 4:d03967db9991 230 else if (rxBufferSTR.find(command9) != string::npos){
osmith2 4:d03967db9991 231 pc.printf("Command LOGGING\n");
osmith2 4:d03967db9991 232 logX = !logX;
osmith2 4:d03967db9991 233 printf("LOGGING %i\n", logX);
osmith2 4:d03967db9991 234 }
osmith2 4:d03967db9991 235 else {
osmith2 4:d03967db9991 236 pc.printf("Command not found.\n");
osmith2 4:d03967db9991 237 }
eawhite 2:690dd6bdb9a7 238
osmith2 4:d03967db9991 239 memset(puttyNum, 0, sizeof(puttyNum));
eawhite 2:690dd6bdb9a7 240 memset(rxBuffer, 0, sizeof(rxBuffer)); //only reset after strcmp
eawhite 2:690dd6bdb9a7 241 i = 0;
eawhite 2:690dd6bdb9a7 242 }
eawhite 2:690dd6bdb9a7 243 }
eawhite 2:690dd6bdb9a7 244
eawhite 2:690dd6bdb9a7 245 }
osmith2 0:f46e16e62b0c 246
osmith2 0:f46e16e62b0c 247
osmith2 0:f46e16e62b0c 248 int main() {
eawhite 2:690dd6bdb9a7 249
eawhite 2:690dd6bdb9a7 250 serialRX.start(rx_thread); //start serial interrupt thread
eawhite 2:690dd6bdb9a7 251 pc.attach(&Rx_interrupt); //attach interrupt function that triggers when data is available
osmith2 4:d03967db9991 252 pc.printf("Please enter command in full caps\n");
osmith2 0:f46e16e62b0c 253 while(1) {
eawhite 2:690dd6bdb9a7 254 Thread::wait(2500);
osmith2 0:f46e16e62b0c 255 }
osmith2 0:f46e16e62b0c 256
osmith2 0:f46e16e62b0c 257 }