test to establish how we can share the project on MBED
Dependencies: BME280 BMP280 ELEC350-Coursework-2017 TextLCD
Fork of ELEC350-CWTEMPLATE-2017 by
serial_protocol/serial_protocol.cpp@14:b5baf59f53b9, 2017-12-15 (annotated)
- Committer:
- bdarling
- Date:
- Fri Dec 15 16:22:19 2017 +0000
- Revision:
- 14:b5baf59f53b9
- Parent:
- 13:aa200749dba6
Commented out getData() and readSerial() from main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bdarling | 13:aa200749dba6 | 1 | /*This module enables a user to communicate with the system using a serial |
bdarling | 13:aa200749dba6 | 2 | connection, commands can be sent using a terminal emulator |
bdarling | 13:aa200749dba6 | 3 | */ |
bdarling | 6:c7220f7c1ae0 | 4 | #include "serial_protocol.hpp" |
bdarling | 6:c7220f7c1ae0 | 5 | #include "sample_hardware.hpp" |
bdarling | 6:c7220f7c1ae0 | 6 | #include <string> |
bdarling | 8:e3aff00ef600 | 7 | #include <ctype.h> |
bdarling | 13:aa200749dba6 | 8 | |
bdarling | 13:aa200749dba6 | 9 | #define SETT_UPPER_BOUND 60.0f //upper bound for sampling period |
bdarling | 13:aa200749dba6 | 10 | #define SETT_LOWER_BOUND 0.1f //lower bound for sampling period |
bdarling | 13:aa200749dba6 | 11 | |
bdarling | 13:aa200749dba6 | 12 | |
bdarling | 6:c7220f7c1ae0 | 13 | Serial pc(SERIAL_TX, SERIAL_RX); |
bdarling | 6:c7220f7c1ae0 | 14 | |
bdarling | 6:c7220f7c1ae0 | 15 | |
bdarling | 13:aa200749dba6 | 16 | char string1[32]; |
bdarling | 13:aa200749dba6 | 17 | char string2[32]; |
bdarling | 6:c7220f7c1ae0 | 18 | |
bdarling | 6:c7220f7c1ae0 | 19 | char dayStr[3]; |
bdarling | 6:c7220f7c1ae0 | 20 | char monthStr[3]; |
bdarling | 6:c7220f7c1ae0 | 21 | char yearStr[5]; |
bdarling | 9:6fb498cf6156 | 22 | char hourStr[3]; |
bdarling | 9:6fb498cf6156 | 23 | char minuteStr[3]; |
bdarling | 9:6fb498cf6156 | 24 | char secondStr[3]; |
bdarling | 6:c7220f7c1ae0 | 25 | int day; |
bdarling | 6:c7220f7c1ae0 | 26 | int month; |
bdarling | 6:c7220f7c1ae0 | 27 | int year; |
bdarling | 13:aa200749dba6 | 28 | int readSamples=0; |
bdarling | 13:aa200749dba6 | 29 | int deleteSamples=0; |
bdarling | 13:aa200749dba6 | 30 | float sampleRate=0; |
bdarling | 6:c7220f7c1ae0 | 31 | |
bdarling | 6:c7220f7c1ae0 | 32 | |
bdarling | 13:aa200749dba6 | 33 | //reads serial keyboard input from pc |
bdarling | 6:c7220f7c1ae0 | 34 | void getData(){ |
bdarling | 6:c7220f7c1ae0 | 35 | pc.scanf("%s %s", string1, string2); |
bdarling | 8:e3aff00ef600 | 36 | //pc.printf("%s %s\n",string1, string2); \\for debugging |
bdarling | 6:c7220f7c1ae0 | 37 | } |
bdarling | 6:c7220f7c1ae0 | 38 | |
bdarling | 13:aa200749dba6 | 39 | //compares strings received from getData function against available commands |
bdarling | 13:aa200749dba6 | 40 | //can alter global variables |
bdarling | 6:c7220f7c1ae0 | 41 | void readSerial(){ |
bdarling | 6:c7220f7c1ae0 | 42 | if((strcmp(string1,"READ")==0) && (strcmp(string2,"ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 43 | pc.printf("reading all\n"); |
bdarling | 6:c7220f7c1ae0 | 44 | } |
bdarling | 6:c7220f7c1ae0 | 45 | |
bdarling | 6:c7220f7c1ae0 | 46 | else if((strcmp(string1,"DELETE")==0) && (strcmp(string2,"ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 47 | pc.printf("deleting all\n"); |
bdarling | 6:c7220f7c1ae0 | 48 | } |
bdarling | 6:c7220f7c1ae0 | 49 | |
bdarling | 6:c7220f7c1ae0 | 50 | else if(strcmp(string1,"READ")==0){ |
bdarling | 13:aa200749dba6 | 51 | int num=0; |
bdarling | 13:aa200749dba6 | 52 | sscanf(string2,"%d",&num); |
bdarling | 10:47e6166f0797 | 53 | |
bdarling | 13:aa200749dba6 | 54 | if(num>0){ |
bdarling | 13:aa200749dba6 | 55 | readSamples = num; |
bdarling | 13:aa200749dba6 | 56 | pc.printf("reading <%d>\n",readSamples); |
bdarling | 6:c7220f7c1ae0 | 57 | } |
bdarling | 6:c7220f7c1ae0 | 58 | else{ |
bdarling | 10:47e6166f0797 | 59 | pc.printf("invalid value\n"); |
bdarling | 10:47e6166f0797 | 60 | } |
bdarling | 6:c7220f7c1ae0 | 61 | } |
bdarling | 6:c7220f7c1ae0 | 62 | |
bdarling | 11:573cf2feced3 | 63 | else if(strcmp(string1,"STATE")==0){ |
bdarling | 11:573cf2feced3 | 64 | if(strcmp(string2,"ON")==0){ |
bdarling | 11:573cf2feced3 | 65 | pc.printf("SAMPLING ON\n"); |
bdarling | 11:573cf2feced3 | 66 | } |
bdarling | 11:573cf2feced3 | 67 | else if(strcmp(string2,"OFF")==0){ |
bdarling | 11:573cf2feced3 | 68 | pc.printf("SAMPLING OFF\n"); |
bdarling | 11:573cf2feced3 | 69 | } |
bdarling | 11:573cf2feced3 | 70 | else{ |
bdarling | 11:573cf2feced3 | 71 | pc.printf("INVALID COMMAND\n"); |
bdarling | 11:573cf2feced3 | 72 | } |
bdarling | 11:573cf2feced3 | 73 | } |
bdarling | 11:573cf2feced3 | 74 | |
bdarling | 12:4af6b1d09c90 | 75 | else if(strcmp(string1,"LOGGING")==0){ |
bdarling | 12:4af6b1d09c90 | 76 | if(strcmp(string2,"ON")==0){ |
bdarling | 12:4af6b1d09c90 | 77 | pc.printf("LOGGING ON\n"); |
bdarling | 12:4af6b1d09c90 | 78 | } |
bdarling | 12:4af6b1d09c90 | 79 | else if(strcmp(string2,"OFF")==0){ |
bdarling | 12:4af6b1d09c90 | 80 | pc.printf("LOGGING OFF\n"); |
bdarling | 12:4af6b1d09c90 | 81 | } |
bdarling | 12:4af6b1d09c90 | 82 | else{ |
bdarling | 12:4af6b1d09c90 | 83 | pc.printf("INVALID COMMAND\n"); |
bdarling | 12:4af6b1d09c90 | 84 | } |
bdarling | 12:4af6b1d09c90 | 85 | } |
bdarling | 12:4af6b1d09c90 | 86 | |
bdarling | 10:47e6166f0797 | 87 | else if(strcmp(string1,"SETT")==0){ |
bdarling | 10:47e6166f0797 | 88 | float number=0; |
bdarling | 10:47e6166f0797 | 89 | sscanf(string2,"%f",&number); |
bdarling | 13:aa200749dba6 | 90 | if(number>=SETT_LOWER_BOUND && number<=SETT_UPPER_BOUND){ |
bdarling | 13:aa200749dba6 | 91 | sampleRate = number; |
bdarling | 13:aa200749dba6 | 92 | pc.printf("sampling period set to %f\n",sampleRate); |
bdarling | 10:47e6166f0797 | 93 | } |
bdarling | 10:47e6166f0797 | 94 | else{ |
bdarling | 10:47e6166f0797 | 95 | pc.printf("invalid value\n"); |
bdarling | 10:47e6166f0797 | 96 | } |
bdarling | 10:47e6166f0797 | 97 | } |
bdarling | 8:e3aff00ef600 | 98 | |
bdarling | 6:c7220f7c1ae0 | 99 | else if(strcmp(string1,"DELETE")==0){ |
bdarling | 13:aa200749dba6 | 100 | int num=0; |
bdarling | 13:aa200749dba6 | 101 | sscanf(string2,"%d",&num); |
bdarling | 8:e3aff00ef600 | 102 | |
bdarling | 13:aa200749dba6 | 103 | if(num>0){ |
bdarling | 13:aa200749dba6 | 104 | deleteSamples=num; |
bdarling | 13:aa200749dba6 | 105 | pc.printf("deleting <%d>\n",deleteSamples); |
bdarling | 6:c7220f7c1ae0 | 106 | } |
bdarling | 6:c7220f7c1ae0 | 107 | else{ |
bdarling | 10:47e6166f0797 | 108 | pc.printf("invalid value\n"); |
bdarling | 10:47e6166f0797 | 109 | } |
bdarling | 6:c7220f7c1ae0 | 110 | } |
bdarling | 6:c7220f7c1ae0 | 111 | |
bdarling | 8:e3aff00ef600 | 112 | else if(strcmp(string1,"SETDATE")==0) |
bdarling | 6:c7220f7c1ae0 | 113 | { |
bdarling | 8:e3aff00ef600 | 114 | int validDigit=0; |
bdarling | 8:e3aff00ef600 | 115 | int i; |
bdarling | 8:e3aff00ef600 | 116 | for(i=0;i<8;i++){ //check first 8 elements of array are digits |
bdarling | 8:e3aff00ef600 | 117 | if(isdigit(string2[i])){ |
bdarling | 8:e3aff00ef600 | 118 | validDigit++; |
bdarling | 8:e3aff00ef600 | 119 | } |
bdarling | 8:e3aff00ef600 | 120 | } |
bdarling | 8:e3aff00ef600 | 121 | if(string2[8] == NULL){ //check element 8 of array is NULL, where the string terminates |
bdarling | 8:e3aff00ef600 | 122 | validDigit++; |
bdarling | 8:e3aff00ef600 | 123 | } |
bdarling | 8:e3aff00ef600 | 124 | |
bdarling | 10:47e6166f0797 | 125 | if(validDigit==9){ |
bdarling | 6:c7220f7c1ae0 | 126 | |
bdarling | 6:c7220f7c1ae0 | 127 | dayStr[0] = string2[0]; |
bdarling | 6:c7220f7c1ae0 | 128 | dayStr[1] = string2[1]; |
bdarling | 6:c7220f7c1ae0 | 129 | dayStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 130 | monthStr[0] = string2[2]; |
bdarling | 6:c7220f7c1ae0 | 131 | monthStr[1] = string2[3]; |
bdarling | 6:c7220f7c1ae0 | 132 | monthStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 133 | yearStr[0] = string2[4]; |
bdarling | 6:c7220f7c1ae0 | 134 | yearStr[1] = string2[5]; |
bdarling | 6:c7220f7c1ae0 | 135 | yearStr[2] = string2[6]; |
bdarling | 6:c7220f7c1ae0 | 136 | yearStr[3] = string2[7]; |
bdarling | 10:47e6166f0797 | 137 | yearStr[4] = NULL; |
bdarling | 10:47e6166f0797 | 138 | //note if date is required in int form, use atoi() to convert from string to int |
bdarling | 6:c7220f7c1ae0 | 139 | |
bdarling | 10:47e6166f0797 | 140 | pc.printf("DATE UPDATED TO <%s><%s><%s>\n",dayStr,monthStr,yearStr); |
bdarling | 10:47e6166f0797 | 141 | |
bdarling | 6:c7220f7c1ae0 | 142 | } |
bdarling | 6:c7220f7c1ae0 | 143 | else{ |
bdarling | 6:c7220f7c1ae0 | 144 | pc.printf("invalid value\n"); |
bdarling | 8:e3aff00ef600 | 145 | } |
bdarling | 6:c7220f7c1ae0 | 146 | } |
bdarling | 9:6fb498cf6156 | 147 | |
bdarling | 9:6fb498cf6156 | 148 | else if(strcmp(string1,"SETTIME")==0) |
bdarling | 9:6fb498cf6156 | 149 | { |
bdarling | 9:6fb498cf6156 | 150 | int validDigit=0; |
bdarling | 9:6fb498cf6156 | 151 | int i; |
bdarling | 9:6fb498cf6156 | 152 | for(i=0;i<6;i++){ //check first 8 elements of array are digits |
bdarling | 9:6fb498cf6156 | 153 | if(isdigit(string2[i])){ |
bdarling | 9:6fb498cf6156 | 154 | validDigit++; |
bdarling | 9:6fb498cf6156 | 155 | } |
bdarling | 9:6fb498cf6156 | 156 | } |
bdarling | 9:6fb498cf6156 | 157 | if(string2[6] == NULL){ //check element 8 of array is NULL, where the string terminates |
bdarling | 9:6fb498cf6156 | 158 | validDigit++; |
bdarling | 9:6fb498cf6156 | 159 | } |
bdarling | 9:6fb498cf6156 | 160 | |
bdarling | 9:6fb498cf6156 | 161 | if(validDigit==7){ |
bdarling | 9:6fb498cf6156 | 162 | |
bdarling | 9:6fb498cf6156 | 163 | hourStr[0] = string2[0]; |
bdarling | 9:6fb498cf6156 | 164 | hourStr[1] = string2[1]; |
bdarling | 9:6fb498cf6156 | 165 | hourStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 166 | minuteStr[0] = string2[2]; |
bdarling | 9:6fb498cf6156 | 167 | minuteStr[1] = string2[3]; |
bdarling | 9:6fb498cf6156 | 168 | minuteStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 169 | secondStr[0] = string2[4]; |
bdarling | 9:6fb498cf6156 | 170 | secondStr[1] = string2[5]; |
bdarling | 10:47e6166f0797 | 171 | secondStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 172 | |
bdarling | 13:aa200749dba6 | 173 | pc.printf("TIME UPDATE TO <%s><%s><%s>\n",hourStr,minuteStr,secondStr); |
bdarling | 9:6fb498cf6156 | 174 | } |
bdarling | 9:6fb498cf6156 | 175 | else{ |
bdarling | 9:6fb498cf6156 | 176 | pc.printf("invalid value\n"); |
bdarling | 9:6fb498cf6156 | 177 | } |
bdarling | 9:6fb498cf6156 | 178 | } |
bdarling | 6:c7220f7c1ae0 | 179 | |
bdarling | 6:c7220f7c1ae0 | 180 | else{ |
bdarling | 6:c7220f7c1ae0 | 181 | pc.printf("invalid command\n"); |
bdarling | 6:c7220f7c1ae0 | 182 | } |
bdarling | 6:c7220f7c1ae0 | 183 | } |