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@8:e3aff00ef600, 2017-12-14 (annotated)
- Committer:
- bdarling
- Date:
- Thu Dec 14 16:44:07 2017 +0000
- Revision:
- 8:e3aff00ef600
- Parent:
- 6:c7220f7c1ae0
- Child:
- 9:6fb498cf6156
modified serial_protocol.cpp, SETDATE is now functioning correctly
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 | 6:c7220f7c1ae0 | 15 | int day; |
bdarling | 6:c7220f7c1ae0 | 16 | int month; |
bdarling | 6:c7220f7c1ae0 | 17 | int year; |
bdarling | 6:c7220f7c1ae0 | 18 | |
bdarling | 6:c7220f7c1ae0 | 19 | |
bdarling | 8:e3aff00ef600 | 20 | |
bdarling | 6:c7220f7c1ae0 | 21 | void getData(){ |
bdarling | 6:c7220f7c1ae0 | 22 | pc.scanf("%s %s", string1, string2); |
bdarling | 8:e3aff00ef600 | 23 | //pc.printf("%s %s\n",string1, string2); \\for debugging |
bdarling | 6:c7220f7c1ae0 | 24 | } |
bdarling | 6:c7220f7c1ae0 | 25 | |
bdarling | 6:c7220f7c1ae0 | 26 | void readSerial(){ |
bdarling | 6:c7220f7c1ae0 | 27 | if((strcmp(string1,"READ")==0) && (strcmp(string2,"ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 28 | pc.printf("reading all\n"); |
bdarling | 6:c7220f7c1ae0 | 29 | } |
bdarling | 6:c7220f7c1ae0 | 30 | |
bdarling | 6:c7220f7c1ae0 | 31 | else if((strcmp(string1,"DELETE")==0) && (strcmp(string2,"ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 32 | pc.printf("deleting all\n"); |
bdarling | 6:c7220f7c1ae0 | 33 | } |
bdarling | 6:c7220f7c1ae0 | 34 | |
bdarling | 6:c7220f7c1ae0 | 35 | else if(strcmp(string1,"READ")==0){ |
bdarling | 8:e3aff00ef600 | 36 | int test=0; |
bdarling | 8:e3aff00ef600 | 37 | sscanf(string2,"%d",&test); |
bdarling | 8:e3aff00ef600 | 38 | |
bdarling | 8:e3aff00ef600 | 39 | if(test>0){ |
bdarling | 6:c7220f7c1ae0 | 40 | number = atoi(string2); |
bdarling | 6:c7220f7c1ae0 | 41 | if(number > 0){ |
bdarling | 6:c7220f7c1ae0 | 42 | pc.printf("reading <%d>\n",number); |
bdarling | 6:c7220f7c1ae0 | 43 | } |
bdarling | 6:c7220f7c1ae0 | 44 | else{ |
bdarling | 6:c7220f7c1ae0 | 45 | pc.printf("invalid value\n"); |
bdarling | 6:c7220f7c1ae0 | 46 | } |
bdarling | 6:c7220f7c1ae0 | 47 | } |
bdarling | 6:c7220f7c1ae0 | 48 | else{ |
bdarling | 6:c7220f7c1ae0 | 49 | pc.printf("invalid command\n"); |
bdarling | 6:c7220f7c1ae0 | 50 | } |
bdarling | 6:c7220f7c1ae0 | 51 | } |
bdarling | 6:c7220f7c1ae0 | 52 | |
bdarling | 8:e3aff00ef600 | 53 | |
bdarling | 8:e3aff00ef600 | 54 | |
bdarling | 6:c7220f7c1ae0 | 55 | else if(strcmp(string1,"DELETE")==0){ |
bdarling | 8:e3aff00ef600 | 56 | int test=0; |
bdarling | 8:e3aff00ef600 | 57 | sscanf(string2,"%d",&test); |
bdarling | 8:e3aff00ef600 | 58 | |
bdarling | 8:e3aff00ef600 | 59 | if(test>0){ |
bdarling | 6:c7220f7c1ae0 | 60 | number = atoi(string2); |
bdarling | 6:c7220f7c1ae0 | 61 | if(number > 0){ |
bdarling | 6:c7220f7c1ae0 | 62 | pc.printf("deleting <%d>\n",number); |
bdarling | 6:c7220f7c1ae0 | 63 | } |
bdarling | 6:c7220f7c1ae0 | 64 | else{ |
bdarling | 6:c7220f7c1ae0 | 65 | pc.printf("invalid value\n"); |
bdarling | 6:c7220f7c1ae0 | 66 | } |
bdarling | 6:c7220f7c1ae0 | 67 | } |
bdarling | 6:c7220f7c1ae0 | 68 | else{ |
bdarling | 6:c7220f7c1ae0 | 69 | pc.printf("invalid command\n"); |
bdarling | 8:e3aff00ef600 | 70 | } |
bdarling | 6:c7220f7c1ae0 | 71 | } |
bdarling | 6:c7220f7c1ae0 | 72 | |
bdarling | 8:e3aff00ef600 | 73 | else if(strcmp(string1,"SETDATE")==0) |
bdarling | 6:c7220f7c1ae0 | 74 | { |
bdarling | 8:e3aff00ef600 | 75 | int validDigit=0; |
bdarling | 8:e3aff00ef600 | 76 | int i; |
bdarling | 8:e3aff00ef600 | 77 | for(i=0;i<8;i++){ //check first 8 elements of array are digits |
bdarling | 8:e3aff00ef600 | 78 | if(isdigit(string2[i])){ |
bdarling | 8:e3aff00ef600 | 79 | validDigit++; |
bdarling | 8:e3aff00ef600 | 80 | } |
bdarling | 8:e3aff00ef600 | 81 | } |
bdarling | 8:e3aff00ef600 | 82 | if(string2[8] == NULL){ //check element 8 of array is NULL, where the string terminates |
bdarling | 8:e3aff00ef600 | 83 | validDigit++; |
bdarling | 8:e3aff00ef600 | 84 | } |
bdarling | 8:e3aff00ef600 | 85 | |
bdarling | 8:e3aff00ef600 | 86 | if(validDigit==9){ |
bdarling | 8:e3aff00ef600 | 87 | pc.printf("**********SETTING DATE**********\n"); |
bdarling | 8:e3aff00ef600 | 88 | pc.printf("date string: %s\n", string2); |
bdarling | 6:c7220f7c1ae0 | 89 | |
bdarling | 6:c7220f7c1ae0 | 90 | dayStr[0] = string2[0]; |
bdarling | 6:c7220f7c1ae0 | 91 | dayStr[1] = string2[1]; |
bdarling | 6:c7220f7c1ae0 | 92 | dayStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 93 | monthStr[0] = string2[2]; |
bdarling | 6:c7220f7c1ae0 | 94 | monthStr[1] = string2[3]; |
bdarling | 6:c7220f7c1ae0 | 95 | monthStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 96 | yearStr[0] = string2[4]; |
bdarling | 6:c7220f7c1ae0 | 97 | yearStr[1] = string2[5]; |
bdarling | 6:c7220f7c1ae0 | 98 | yearStr[2] = string2[6]; |
bdarling | 6:c7220f7c1ae0 | 99 | yearStr[3] = string2[7]; |
bdarling | 6:c7220f7c1ae0 | 100 | yearStr[4] = NULL; |
bdarling | 6:c7220f7c1ae0 | 101 | |
bdarling | 8:e3aff00ef600 | 102 | pc.printf("Day = %s\n", dayStr); |
bdarling | 8:e3aff00ef600 | 103 | pc.printf("Month = %s\n",monthStr); |
bdarling | 8:e3aff00ef600 | 104 | pc.printf("Year = %s\n",yearStr); |
bdarling | 8:e3aff00ef600 | 105 | pc.printf("**********DATE SET**********\n"); |
bdarling | 6:c7220f7c1ae0 | 106 | } |
bdarling | 6:c7220f7c1ae0 | 107 | else{ |
bdarling | 6:c7220f7c1ae0 | 108 | pc.printf("invalid value\n"); |
bdarling | 8:e3aff00ef600 | 109 | } |
bdarling | 6:c7220f7c1ae0 | 110 | } |
bdarling | 6:c7220f7c1ae0 | 111 | |
bdarling | 6:c7220f7c1ae0 | 112 | else{ |
bdarling | 6:c7220f7c1ae0 | 113 | pc.printf("invalid command\n"); |
bdarling | 6:c7220f7c1ae0 | 114 | } |
bdarling | 6:c7220f7c1ae0 | 115 | } |