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@9:6fb498cf6156, 2017-12-14 (annotated)
- Committer:
- bdarling
- Date:
- Thu Dec 14 16:53:18 2017 +0000
- Revision:
- 9:6fb498cf6156
- Parent:
- 8:e3aff00ef600
- Child:
- 10:47e6166f0797
Implemented SETTIME
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 | int number; |
bdarling | 6:c7220f7c1ae0 | 12 | char dayStr[3]; |
bdarling | 6:c7220f7c1ae0 | 13 | char monthStr[3]; |
bdarling | 6:c7220f7c1ae0 | 14 | char yearStr[5]; |
bdarling | 9:6fb498cf6156 | 15 | char hourStr[3]; |
bdarling | 9:6fb498cf6156 | 16 | char minuteStr[3]; |
bdarling | 9:6fb498cf6156 | 17 | char secondStr[3]; |
bdarling | 6:c7220f7c1ae0 | 18 | int day; |
bdarling | 6:c7220f7c1ae0 | 19 | int month; |
bdarling | 6:c7220f7c1ae0 | 20 | int year; |
bdarling | 6:c7220f7c1ae0 | 21 | |
bdarling | 6:c7220f7c1ae0 | 22 | |
bdarling | 8:e3aff00ef600 | 23 | |
bdarling | 6:c7220f7c1ae0 | 24 | void getData(){ |
bdarling | 6:c7220f7c1ae0 | 25 | pc.scanf("%s %s", string1, string2); |
bdarling | 8:e3aff00ef600 | 26 | //pc.printf("%s %s\n",string1, string2); \\for debugging |
bdarling | 6:c7220f7c1ae0 | 27 | } |
bdarling | 6:c7220f7c1ae0 | 28 | |
bdarling | 6:c7220f7c1ae0 | 29 | void readSerial(){ |
bdarling | 6:c7220f7c1ae0 | 30 | if((strcmp(string1,"READ")==0) && (strcmp(string2,"ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 31 | pc.printf("reading all\n"); |
bdarling | 6:c7220f7c1ae0 | 32 | } |
bdarling | 6:c7220f7c1ae0 | 33 | |
bdarling | 6:c7220f7c1ae0 | 34 | else if((strcmp(string1,"DELETE")==0) && (strcmp(string2,"ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 35 | pc.printf("deleting all\n"); |
bdarling | 6:c7220f7c1ae0 | 36 | } |
bdarling | 6:c7220f7c1ae0 | 37 | |
bdarling | 6:c7220f7c1ae0 | 38 | else if(strcmp(string1,"READ")==0){ |
bdarling | 8:e3aff00ef600 | 39 | int test=0; |
bdarling | 8:e3aff00ef600 | 40 | sscanf(string2,"%d",&test); |
bdarling | 8:e3aff00ef600 | 41 | |
bdarling | 8:e3aff00ef600 | 42 | if(test>0){ |
bdarling | 6:c7220f7c1ae0 | 43 | number = atoi(string2); |
bdarling | 6:c7220f7c1ae0 | 44 | if(number > 0){ |
bdarling | 6:c7220f7c1ae0 | 45 | pc.printf("reading <%d>\n",number); |
bdarling | 6:c7220f7c1ae0 | 46 | } |
bdarling | 6:c7220f7c1ae0 | 47 | else{ |
bdarling | 6:c7220f7c1ae0 | 48 | pc.printf("invalid value\n"); |
bdarling | 6:c7220f7c1ae0 | 49 | } |
bdarling | 6:c7220f7c1ae0 | 50 | } |
bdarling | 6:c7220f7c1ae0 | 51 | else{ |
bdarling | 6:c7220f7c1ae0 | 52 | pc.printf("invalid command\n"); |
bdarling | 6:c7220f7c1ae0 | 53 | } |
bdarling | 6:c7220f7c1ae0 | 54 | } |
bdarling | 6:c7220f7c1ae0 | 55 | |
bdarling | 8:e3aff00ef600 | 56 | |
bdarling | 8:e3aff00ef600 | 57 | |
bdarling | 6:c7220f7c1ae0 | 58 | else if(strcmp(string1,"DELETE")==0){ |
bdarling | 8:e3aff00ef600 | 59 | int test=0; |
bdarling | 8:e3aff00ef600 | 60 | sscanf(string2,"%d",&test); |
bdarling | 8:e3aff00ef600 | 61 | |
bdarling | 8:e3aff00ef600 | 62 | if(test>0){ |
bdarling | 6:c7220f7c1ae0 | 63 | number = atoi(string2); |
bdarling | 6:c7220f7c1ae0 | 64 | if(number > 0){ |
bdarling | 6:c7220f7c1ae0 | 65 | pc.printf("deleting <%d>\n",number); |
bdarling | 6:c7220f7c1ae0 | 66 | } |
bdarling | 6:c7220f7c1ae0 | 67 | else{ |
bdarling | 6:c7220f7c1ae0 | 68 | pc.printf("invalid value\n"); |
bdarling | 6:c7220f7c1ae0 | 69 | } |
bdarling | 6:c7220f7c1ae0 | 70 | } |
bdarling | 6:c7220f7c1ae0 | 71 | else{ |
bdarling | 6:c7220f7c1ae0 | 72 | pc.printf("invalid command\n"); |
bdarling | 8:e3aff00ef600 | 73 | } |
bdarling | 6:c7220f7c1ae0 | 74 | } |
bdarling | 6:c7220f7c1ae0 | 75 | |
bdarling | 8:e3aff00ef600 | 76 | else if(strcmp(string1,"SETDATE")==0) |
bdarling | 6:c7220f7c1ae0 | 77 | { |
bdarling | 8:e3aff00ef600 | 78 | int validDigit=0; |
bdarling | 8:e3aff00ef600 | 79 | int i; |
bdarling | 8:e3aff00ef600 | 80 | for(i=0;i<8;i++){ //check first 8 elements of array are digits |
bdarling | 8:e3aff00ef600 | 81 | if(isdigit(string2[i])){ |
bdarling | 8:e3aff00ef600 | 82 | validDigit++; |
bdarling | 8:e3aff00ef600 | 83 | } |
bdarling | 8:e3aff00ef600 | 84 | } |
bdarling | 8:e3aff00ef600 | 85 | if(string2[8] == NULL){ //check element 8 of array is NULL, where the string terminates |
bdarling | 8:e3aff00ef600 | 86 | validDigit++; |
bdarling | 8:e3aff00ef600 | 87 | } |
bdarling | 8:e3aff00ef600 | 88 | |
bdarling | 8:e3aff00ef600 | 89 | if(validDigit==9){ |
bdarling | 8:e3aff00ef600 | 90 | pc.printf("**********SETTING DATE**********\n"); |
bdarling | 8:e3aff00ef600 | 91 | pc.printf("date string: %s\n", string2); |
bdarling | 6:c7220f7c1ae0 | 92 | |
bdarling | 6:c7220f7c1ae0 | 93 | dayStr[0] = string2[0]; |
bdarling | 6:c7220f7c1ae0 | 94 | dayStr[1] = string2[1]; |
bdarling | 6:c7220f7c1ae0 | 95 | dayStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 96 | monthStr[0] = string2[2]; |
bdarling | 6:c7220f7c1ae0 | 97 | monthStr[1] = string2[3]; |
bdarling | 6:c7220f7c1ae0 | 98 | monthStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 99 | yearStr[0] = string2[4]; |
bdarling | 6:c7220f7c1ae0 | 100 | yearStr[1] = string2[5]; |
bdarling | 6:c7220f7c1ae0 | 101 | yearStr[2] = string2[6]; |
bdarling | 6:c7220f7c1ae0 | 102 | yearStr[3] = string2[7]; |
bdarling | 6:c7220f7c1ae0 | 103 | yearStr[4] = NULL; |
bdarling | 6:c7220f7c1ae0 | 104 | |
bdarling | 8:e3aff00ef600 | 105 | pc.printf("Day = %s\n", dayStr); |
bdarling | 8:e3aff00ef600 | 106 | pc.printf("Month = %s\n",monthStr); |
bdarling | 8:e3aff00ef600 | 107 | pc.printf("Year = %s\n",yearStr); |
bdarling | 8:e3aff00ef600 | 108 | pc.printf("**********DATE SET**********\n"); |
bdarling | 6:c7220f7c1ae0 | 109 | } |
bdarling | 6:c7220f7c1ae0 | 110 | else{ |
bdarling | 6:c7220f7c1ae0 | 111 | pc.printf("invalid value\n"); |
bdarling | 8:e3aff00ef600 | 112 | } |
bdarling | 6:c7220f7c1ae0 | 113 | } |
bdarling | 9:6fb498cf6156 | 114 | |
bdarling | 9:6fb498cf6156 | 115 | else if(strcmp(string1,"SETTIME")==0) |
bdarling | 9:6fb498cf6156 | 116 | { |
bdarling | 9:6fb498cf6156 | 117 | int validDigit=0; |
bdarling | 9:6fb498cf6156 | 118 | int i; |
bdarling | 9:6fb498cf6156 | 119 | for(i=0;i<6;i++){ //check first 8 elements of array are digits |
bdarling | 9:6fb498cf6156 | 120 | if(isdigit(string2[i])){ |
bdarling | 9:6fb498cf6156 | 121 | validDigit++; |
bdarling | 9:6fb498cf6156 | 122 | } |
bdarling | 9:6fb498cf6156 | 123 | } |
bdarling | 9:6fb498cf6156 | 124 | if(string2[6] == NULL){ //check element 8 of array is NULL, where the string terminates |
bdarling | 9:6fb498cf6156 | 125 | validDigit++; |
bdarling | 9:6fb498cf6156 | 126 | } |
bdarling | 9:6fb498cf6156 | 127 | |
bdarling | 9:6fb498cf6156 | 128 | if(validDigit==7){ |
bdarling | 9:6fb498cf6156 | 129 | pc.printf("**********SETTING TIME**********\n"); |
bdarling | 9:6fb498cf6156 | 130 | pc.printf("time string: %s\n", string2); |
bdarling | 9:6fb498cf6156 | 131 | |
bdarling | 9:6fb498cf6156 | 132 | hourStr[0] = string2[0]; |
bdarling | 9:6fb498cf6156 | 133 | hourStr[1] = string2[1]; |
bdarling | 9:6fb498cf6156 | 134 | hourStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 135 | minuteStr[0] = string2[2]; |
bdarling | 9:6fb498cf6156 | 136 | minuteStr[1] = string2[3]; |
bdarling | 9:6fb498cf6156 | 137 | minuteStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 138 | secondStr[0] = string2[4]; |
bdarling | 9:6fb498cf6156 | 139 | secondStr[1] = string2[5]; |
bdarling | 9:6fb498cf6156 | 140 | secondStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 141 | // dayStr[0] = string2[0]; |
bdarling | 9:6fb498cf6156 | 142 | // dayStr[1] = string2[1]; |
bdarling | 9:6fb498cf6156 | 143 | // dayStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 144 | // monthStr[0] = string2[2]; |
bdarling | 9:6fb498cf6156 | 145 | // monthStr[1] = string2[3]; |
bdarling | 9:6fb498cf6156 | 146 | // monthStr[2] = NULL; |
bdarling | 9:6fb498cf6156 | 147 | // yearStr[0] = string2[4]; |
bdarling | 9:6fb498cf6156 | 148 | // yearStr[1] = string2[5]; |
bdarling | 9:6fb498cf6156 | 149 | // yearStr[2] = string2[6]; |
bdarling | 9:6fb498cf6156 | 150 | // yearStr[3] = string2[7]; |
bdarling | 9:6fb498cf6156 | 151 | // yearStr[4] = NULL; |
bdarling | 9:6fb498cf6156 | 152 | |
bdarling | 9:6fb498cf6156 | 153 | |
bdarling | 9:6fb498cf6156 | 154 | pc.printf("Hour = %s\n", hourStr); |
bdarling | 9:6fb498cf6156 | 155 | pc.printf("Minute = %s\n",minuteStr); |
bdarling | 9:6fb498cf6156 | 156 | pc.printf("Second = %s\n",secondStr); |
bdarling | 9:6fb498cf6156 | 157 | pc.printf("**********TIME SET**********\n"); |
bdarling | 9:6fb498cf6156 | 158 | } |
bdarling | 9:6fb498cf6156 | 159 | else{ |
bdarling | 9:6fb498cf6156 | 160 | pc.printf("invalid value\n"); |
bdarling | 9:6fb498cf6156 | 161 | } |
bdarling | 9:6fb498cf6156 | 162 | } |
bdarling | 6:c7220f7c1ae0 | 163 | |
bdarling | 6:c7220f7c1ae0 | 164 | else{ |
bdarling | 6:c7220f7c1ae0 | 165 | pc.printf("invalid command\n"); |
bdarling | 6:c7220f7c1ae0 | 166 | } |
bdarling | 6:c7220f7c1ae0 | 167 | } |