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@11:573cf2feced3, 2017-12-15 (annotated)
- Committer:
- bdarling
- Date:
- Fri Dec 15 11:57:43 2017 +0000
- Revision:
- 11:573cf2feced3
- Parent:
- 10:47e6166f0797
- Child:
- 12:4af6b1d09c90
Modified serial_protocol.cpp, added STATE <x>
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bdarling | 6:c7220f7c1ae0 | 1 | #include "serial_protocol.hpp" |
bdarling | 6:c7220f7c1ae0 | 2 | #include "sample_hardware.hpp" |
bdarling | 6:c7220f7c1ae0 | 3 | #include <string> |
bdarling | 8:e3aff00ef600 | 4 | #include <ctype.h> |
bdarling | 6:c7220f7c1ae0 | 5 | Serial pc(SERIAL_TX, SERIAL_RX); |
bdarling | 6:c7220f7c1ae0 | 6 | |
bdarling | 6:c7220f7c1ae0 | 7 | |
bdarling | 6:c7220f7c1ae0 | 8 | char string1[128]; |
bdarling | 6:c7220f7c1ae0 | 9 | char string2[128]; |
bdarling | 6:c7220f7c1ae0 | 10 | |
bdarling | 6:c7220f7c1ae0 | 11 | char dayStr[3]; |
bdarling | 6:c7220f7c1ae0 | 12 | char monthStr[3]; |
bdarling | 6:c7220f7c1ae0 | 13 | char yearStr[5]; |
bdarling | 9:6fb498cf6156 | 14 | char hourStr[3]; |
bdarling | 9:6fb498cf6156 | 15 | char minuteStr[3]; |
bdarling | 9:6fb498cf6156 | 16 | char secondStr[3]; |
bdarling | 6:c7220f7c1ae0 | 17 | int day; |
bdarling | 6:c7220f7c1ae0 | 18 | int month; |
bdarling | 6:c7220f7c1ae0 | 19 | int year; |
bdarling | 6:c7220f7c1ae0 | 20 | |
bdarling | 6:c7220f7c1ae0 | 21 | |
bdarling | 8:e3aff00ef600 | 22 | |
bdarling | 6:c7220f7c1ae0 | 23 | void getData(){ |
bdarling | 6:c7220f7c1ae0 | 24 | pc.scanf("%s %s", string1, string2); |
bdarling | 8:e3aff00ef600 | 25 | //pc.printf("%s %s\n",string1, string2); \\for debugging |
bdarling | 6:c7220f7c1ae0 | 26 | } |
bdarling | 6:c7220f7c1ae0 | 27 | |
bdarling | 6:c7220f7c1ae0 | 28 | void readSerial(){ |
bdarling | 6:c7220f7c1ae0 | 29 | if((strcmp(string1,"READ")==0) && (strcmp(string2,"ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 30 | pc.printf("reading all\n"); |
bdarling | 6:c7220f7c1ae0 | 31 | } |
bdarling | 6:c7220f7c1ae0 | 32 | |
bdarling | 6:c7220f7c1ae0 | 33 | else if((strcmp(string1,"DELETE")==0) && (strcmp(string2,"ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 34 | pc.printf("deleting all\n"); |
bdarling | 6:c7220f7c1ae0 | 35 | } |
bdarling | 6:c7220f7c1ae0 | 36 | |
bdarling | 6:c7220f7c1ae0 | 37 | else if(strcmp(string1,"READ")==0){ |
bdarling | 8:e3aff00ef600 | 38 | int test=0; |
bdarling | 8:e3aff00ef600 | 39 | sscanf(string2,"%d",&test); |
bdarling | 10:47e6166f0797 | 40 | |
bdarling | 8:e3aff00ef600 | 41 | if(test>0){ |
bdarling | 10:47e6166f0797 | 42 | pc.printf("reading <%d>\n",test); |
bdarling | 6:c7220f7c1ae0 | 43 | } |
bdarling | 6:c7220f7c1ae0 | 44 | else{ |
bdarling | 10:47e6166f0797 | 45 | pc.printf("invalid value\n"); |
bdarling | 10:47e6166f0797 | 46 | } |
bdarling | 6:c7220f7c1ae0 | 47 | } |
bdarling | 6:c7220f7c1ae0 | 48 | |
bdarling | 11:573cf2feced3 | 49 | else if(strcmp(string1,"STATE")==0){ |
bdarling | 11:573cf2feced3 | 50 | if(strcmp(string2,"ON")==0){ |
bdarling | 11:573cf2feced3 | 51 | pc.printf("SAMPLING ON\n"); |
bdarling | 11:573cf2feced3 | 52 | } |
bdarling | 11:573cf2feced3 | 53 | else if(strcmp(string2,"OFF")==0){ |
bdarling | 11:573cf2feced3 | 54 | pc.printf("SAMPLING OFF\n"); |
bdarling | 11:573cf2feced3 | 55 | } |
bdarling | 11:573cf2feced3 | 56 | else{ |
bdarling | 11:573cf2feced3 | 57 | pc.printf("INVALID COMMAND\n"); |
bdarling | 11:573cf2feced3 | 58 | } |
bdarling | 11:573cf2feced3 | 59 | } |
bdarling | 11:573cf2feced3 | 60 | |
bdarling | 10:47e6166f0797 | 61 | else if(strcmp(string1,"SETT")==0){ |
bdarling | 10:47e6166f0797 | 62 | float number=0; |
bdarling | 10:47e6166f0797 | 63 | sscanf(string2,"%f",&number); |
bdarling | 10:47e6166f0797 | 64 | if(number>=0.1f && number<=60.0f){ |
bdarling | 10:47e6166f0797 | 65 | pc.printf("sampling period set to %f\n",number); |
bdarling | 10:47e6166f0797 | 66 | } |
bdarling | 10:47e6166f0797 | 67 | else{ |
bdarling | 10:47e6166f0797 | 68 | pc.printf("invalid value\n"); |
bdarling | 10:47e6166f0797 | 69 | } |
bdarling | 10:47e6166f0797 | 70 | } |
bdarling | 8:e3aff00ef600 | 71 | |
bdarling | 6:c7220f7c1ae0 | 72 | else if(strcmp(string1,"DELETE")==0){ |
bdarling | 8:e3aff00ef600 | 73 | int test=0; |
bdarling | 8:e3aff00ef600 | 74 | sscanf(string2,"%d",&test); |
bdarling | 8:e3aff00ef600 | 75 | |
bdarling | 8:e3aff00ef600 | 76 | if(test>0){ |
bdarling | 10:47e6166f0797 | 77 | pc.printf("deleting <%d>\n",test); |
bdarling | 6:c7220f7c1ae0 | 78 | } |
bdarling | 6:c7220f7c1ae0 | 79 | else{ |
bdarling | 10:47e6166f0797 | 80 | pc.printf("invalid value\n"); |
bdarling | 10:47e6166f0797 | 81 | } |
bdarling | 6:c7220f7c1ae0 | 82 | } |
bdarling | 6:c7220f7c1ae0 | 83 | |
bdarling | 8:e3aff00ef600 | 84 | else if(strcmp(string1,"SETDATE")==0) |
bdarling | 6:c7220f7c1ae0 | 85 | { |
bdarling | 8:e3aff00ef600 | 86 | int validDigit=0; |
bdarling | 8:e3aff00ef600 | 87 | int i; |
bdarling | 8:e3aff00ef600 | 88 | for(i=0;i<8;i++){ //check first 8 elements of array are digits |
bdarling | 8:e3aff00ef600 | 89 | if(isdigit(string2[i])){ |
bdarling | 8:e3aff00ef600 | 90 | validDigit++; |
bdarling | 8:e3aff00ef600 | 91 | } |
bdarling | 8:e3aff00ef600 | 92 | } |
bdarling | 8:e3aff00ef600 | 93 | if(string2[8] == NULL){ //check element 8 of array is NULL, where the string terminates |
bdarling | 8:e3aff00ef600 | 94 | validDigit++; |
bdarling | 8:e3aff00ef600 | 95 | } |
bdarling | 8:e3aff00ef600 | 96 | |
bdarling | 10:47e6166f0797 | 97 | if(validDigit==9){ |
bdarling | 6:c7220f7c1ae0 | 98 | |
bdarling | 6:c7220f7c1ae0 | 99 | dayStr[0] = string2[0]; |
bdarling | 6:c7220f7c1ae0 | 100 | dayStr[1] = string2[1]; |
bdarling | 6:c7220f7c1ae0 | 101 | dayStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 102 | monthStr[0] = string2[2]; |
bdarling | 6:c7220f7c1ae0 | 103 | monthStr[1] = string2[3]; |
bdarling | 6:c7220f7c1ae0 | 104 | monthStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 105 | yearStr[0] = string2[4]; |
bdarling | 6:c7220f7c1ae0 | 106 | yearStr[1] = string2[5]; |
bdarling | 6:c7220f7c1ae0 | 107 | yearStr[2] = string2[6]; |
bdarling | 6:c7220f7c1ae0 | 108 | yearStr[3] = string2[7]; |
bdarling | 10:47e6166f0797 | 109 | yearStr[4] = NULL; |
bdarling | 10:47e6166f0797 | 110 | //note if date is required in int form, use atoi() to convert from string to int |
bdarling | 6:c7220f7c1ae0 | 111 | |
bdarling | 10:47e6166f0797 | 112 | pc.printf("DATE UPDATED TO <%s><%s><%s>\n",dayStr,monthStr,yearStr); |
bdarling | 10:47e6166f0797 | 113 | |
bdarling | 6:c7220f7c1ae0 | 114 | } |
bdarling | 6:c7220f7c1ae0 | 115 | else{ |
bdarling | 6:c7220f7c1ae0 | 116 | pc.printf("invalid value\n"); |
bdarling | 8:e3aff00ef600 | 117 | } |
bdarling | 6:c7220f7c1ae0 | 118 | } |
bdarling | 9:6fb498cf6156 | 119 | |
bdarling | 9:6fb498cf6156 | 120 | else if(strcmp(string1,"SETTIME")==0) |
bdarling | 9:6fb498cf6156 | 121 | { |
bdarling | 9:6fb498cf6156 | 122 | int validDigit=0; |
bdarling | 9:6fb498cf6156 | 123 | int i; |
bdarling | 9:6fb498cf6156 | 124 | for(i=0;i<6;i++){ //check first 8 elements of array are digits |
bdarling | 9:6fb498cf6156 | 125 | if(isdigit(string2[i])){ |
bdarling | 9:6fb498cf6156 | 126 | validDigit++; |
bdarling | 9:6fb498cf6156 | 127 | } |
bdarling | 9:6fb498cf6156 | 128 | } |
bdarling | 9:6fb498cf6156 | 129 | if(string2[6] == NULL){ //check element 8 of array is NULL, where the string terminates |
bdarling | 9:6fb498cf6156 | 130 | validDigit++; |
bdarling | 9:6fb498cf6156 | 131 | } |
bdarling | 9:6fb498cf6156 | 132 | |
bdarling | 9:6fb498cf6156 | 133 | if(validDigit==7){ |
bdarling | 9:6fb498cf6156 | 134 | |
bdarling | 9:6fb498cf6156 | 135 | hourStr[0] = string2[0]; |
bdarling | 9:6fb498cf6156 | 136 | hourStr[1] = string2[1]; |
bdarling | 9:6fb498cf6156 | 137 | hourStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 138 | minuteStr[0] = string2[2]; |
bdarling | 9:6fb498cf6156 | 139 | minuteStr[1] = string2[3]; |
bdarling | 9:6fb498cf6156 | 140 | minuteStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 141 | secondStr[0] = string2[4]; |
bdarling | 9:6fb498cf6156 | 142 | secondStr[1] = string2[5]; |
bdarling | 10:47e6166f0797 | 143 | secondStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 144 | |
bdarling | 10:47e6166f0797 | 145 | pc.printf("TIME UPDATE TO <%s><%s><%s>",hourStr,minuteStr,secondStr); |
bdarling | 9:6fb498cf6156 | 146 | } |
bdarling | 9:6fb498cf6156 | 147 | else{ |
bdarling | 9:6fb498cf6156 | 148 | pc.printf("invalid value\n"); |
bdarling | 9:6fb498cf6156 | 149 | } |
bdarling | 9:6fb498cf6156 | 150 | } |
bdarling | 6:c7220f7c1ae0 | 151 | |
bdarling | 6:c7220f7c1ae0 | 152 | else{ |
bdarling | 6:c7220f7c1ae0 | 153 | pc.printf("invalid command\n"); |
bdarling | 6:c7220f7c1ae0 | 154 | } |
bdarling | 6:c7220f7c1ae0 | 155 | } |