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@10:47e6166f0797, 2017-12-15 (annotated)
- Committer:
- bdarling
- Date:
- Fri Dec 15 11:50:57 2017 +0000
- Revision:
- 10:47e6166f0797
- Parent:
- 9:6fb498cf6156
- Child:
- 11:573cf2feced3
Modified serial_protocol.cpp, SETT now uses floats.
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 | 10:47e6166f0797 | 49 | else if(strcmp(string1,"SETT")==0){ |
bdarling | 10:47e6166f0797 | 50 | float number=0; |
bdarling | 10:47e6166f0797 | 51 | sscanf(string2,"%f",&number); |
bdarling | 10:47e6166f0797 | 52 | if(number>=0.1f && number<=60.0f){ |
bdarling | 10:47e6166f0797 | 53 | pc.printf("sampling period set to %f\n",number); |
bdarling | 10:47e6166f0797 | 54 | } |
bdarling | 10:47e6166f0797 | 55 | else{ |
bdarling | 10:47e6166f0797 | 56 | pc.printf("invalid value\n"); |
bdarling | 10:47e6166f0797 | 57 | } |
bdarling | 10:47e6166f0797 | 58 | } |
bdarling | 8:e3aff00ef600 | 59 | |
bdarling | 6:c7220f7c1ae0 | 60 | else if(strcmp(string1,"DELETE")==0){ |
bdarling | 8:e3aff00ef600 | 61 | int test=0; |
bdarling | 8:e3aff00ef600 | 62 | sscanf(string2,"%d",&test); |
bdarling | 8:e3aff00ef600 | 63 | |
bdarling | 8:e3aff00ef600 | 64 | if(test>0){ |
bdarling | 10:47e6166f0797 | 65 | pc.printf("deleting <%d>\n",test); |
bdarling | 6:c7220f7c1ae0 | 66 | } |
bdarling | 6:c7220f7c1ae0 | 67 | else{ |
bdarling | 10:47e6166f0797 | 68 | pc.printf("invalid value\n"); |
bdarling | 10:47e6166f0797 | 69 | } |
bdarling | 6:c7220f7c1ae0 | 70 | } |
bdarling | 6:c7220f7c1ae0 | 71 | |
bdarling | 8:e3aff00ef600 | 72 | else if(strcmp(string1,"SETDATE")==0) |
bdarling | 6:c7220f7c1ae0 | 73 | { |
bdarling | 8:e3aff00ef600 | 74 | int validDigit=0; |
bdarling | 8:e3aff00ef600 | 75 | int i; |
bdarling | 8:e3aff00ef600 | 76 | for(i=0;i<8;i++){ //check first 8 elements of array are digits |
bdarling | 8:e3aff00ef600 | 77 | if(isdigit(string2[i])){ |
bdarling | 8:e3aff00ef600 | 78 | validDigit++; |
bdarling | 8:e3aff00ef600 | 79 | } |
bdarling | 8:e3aff00ef600 | 80 | } |
bdarling | 8:e3aff00ef600 | 81 | if(string2[8] == NULL){ //check element 8 of array is NULL, where the string terminates |
bdarling | 8:e3aff00ef600 | 82 | validDigit++; |
bdarling | 8:e3aff00ef600 | 83 | } |
bdarling | 8:e3aff00ef600 | 84 | |
bdarling | 10:47e6166f0797 | 85 | if(validDigit==9){ |
bdarling | 6:c7220f7c1ae0 | 86 | |
bdarling | 6:c7220f7c1ae0 | 87 | dayStr[0] = string2[0]; |
bdarling | 6:c7220f7c1ae0 | 88 | dayStr[1] = string2[1]; |
bdarling | 6:c7220f7c1ae0 | 89 | dayStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 90 | monthStr[0] = string2[2]; |
bdarling | 6:c7220f7c1ae0 | 91 | monthStr[1] = string2[3]; |
bdarling | 6:c7220f7c1ae0 | 92 | monthStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 93 | yearStr[0] = string2[4]; |
bdarling | 6:c7220f7c1ae0 | 94 | yearStr[1] = string2[5]; |
bdarling | 6:c7220f7c1ae0 | 95 | yearStr[2] = string2[6]; |
bdarling | 6:c7220f7c1ae0 | 96 | yearStr[3] = string2[7]; |
bdarling | 10:47e6166f0797 | 97 | yearStr[4] = NULL; |
bdarling | 10:47e6166f0797 | 98 | //note if date is required in int form, use atoi() to convert from string to int |
bdarling | 6:c7220f7c1ae0 | 99 | |
bdarling | 10:47e6166f0797 | 100 | pc.printf("DATE UPDATED TO <%s><%s><%s>\n",dayStr,monthStr,yearStr); |
bdarling | 10:47e6166f0797 | 101 | |
bdarling | 6:c7220f7c1ae0 | 102 | } |
bdarling | 6:c7220f7c1ae0 | 103 | else{ |
bdarling | 6:c7220f7c1ae0 | 104 | pc.printf("invalid value\n"); |
bdarling | 8:e3aff00ef600 | 105 | } |
bdarling | 6:c7220f7c1ae0 | 106 | } |
bdarling | 9:6fb498cf6156 | 107 | |
bdarling | 9:6fb498cf6156 | 108 | else if(strcmp(string1,"SETTIME")==0) |
bdarling | 9:6fb498cf6156 | 109 | { |
bdarling | 9:6fb498cf6156 | 110 | int validDigit=0; |
bdarling | 9:6fb498cf6156 | 111 | int i; |
bdarling | 9:6fb498cf6156 | 112 | for(i=0;i<6;i++){ //check first 8 elements of array are digits |
bdarling | 9:6fb498cf6156 | 113 | if(isdigit(string2[i])){ |
bdarling | 9:6fb498cf6156 | 114 | validDigit++; |
bdarling | 9:6fb498cf6156 | 115 | } |
bdarling | 9:6fb498cf6156 | 116 | } |
bdarling | 9:6fb498cf6156 | 117 | if(string2[6] == NULL){ //check element 8 of array is NULL, where the string terminates |
bdarling | 9:6fb498cf6156 | 118 | validDigit++; |
bdarling | 9:6fb498cf6156 | 119 | } |
bdarling | 9:6fb498cf6156 | 120 | |
bdarling | 9:6fb498cf6156 | 121 | if(validDigit==7){ |
bdarling | 9:6fb498cf6156 | 122 | |
bdarling | 9:6fb498cf6156 | 123 | hourStr[0] = string2[0]; |
bdarling | 9:6fb498cf6156 | 124 | hourStr[1] = string2[1]; |
bdarling | 9:6fb498cf6156 | 125 | hourStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 126 | minuteStr[0] = string2[2]; |
bdarling | 9:6fb498cf6156 | 127 | minuteStr[1] = string2[3]; |
bdarling | 9:6fb498cf6156 | 128 | minuteStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 129 | secondStr[0] = string2[4]; |
bdarling | 9:6fb498cf6156 | 130 | secondStr[1] = string2[5]; |
bdarling | 10:47e6166f0797 | 131 | secondStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 132 | |
bdarling | 10:47e6166f0797 | 133 | pc.printf("TIME UPDATE TO <%s><%s><%s>",hourStr,minuteStr,secondStr); |
bdarling | 9:6fb498cf6156 | 134 | } |
bdarling | 9:6fb498cf6156 | 135 | else{ |
bdarling | 9:6fb498cf6156 | 136 | pc.printf("invalid value\n"); |
bdarling | 9:6fb498cf6156 | 137 | } |
bdarling | 9:6fb498cf6156 | 138 | } |
bdarling | 6:c7220f7c1ae0 | 139 | |
bdarling | 6:c7220f7c1ae0 | 140 | else{ |
bdarling | 6:c7220f7c1ae0 | 141 | pc.printf("invalid command\n"); |
bdarling | 6:c7220f7c1ae0 | 142 | } |
bdarling | 6:c7220f7c1ae0 | 143 | } |