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@6:c7220f7c1ae0, 2017-12-14 (annotated)
- Committer:
- bdarling
- Date:
- Thu Dec 14 12:30:30 2017 +0000
- Revision:
- 6:c7220f7c1ae0
- Child:
- 8:e3aff00ef600
Added serial_protocol.cpp and serial_protocol.hpp
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 <iostream> |
bdarling | 6:c7220f7c1ae0 | 4 | #include <string> |
bdarling | 6:c7220f7c1ae0 | 5 | Serial pc(SERIAL_TX, SERIAL_RX); |
bdarling | 6:c7220f7c1ae0 | 6 | |
bdarling | 6:c7220f7c1ae0 | 7 | using namespace std; |
bdarling | 6:c7220f7c1ae0 | 8 | |
bdarling | 6:c7220f7c1ae0 | 9 | |
bdarling | 6:c7220f7c1ae0 | 10 | char string1[128]; |
bdarling | 6:c7220f7c1ae0 | 11 | char string2[128]; |
bdarling | 6:c7220f7c1ae0 | 12 | |
bdarling | 6:c7220f7c1ae0 | 13 | |
bdarling | 6:c7220f7c1ae0 | 14 | //std::string string1; |
bdarling | 6:c7220f7c1ae0 | 15 | //std::string string2; |
bdarling | 6:c7220f7c1ae0 | 16 | int number; |
bdarling | 6:c7220f7c1ae0 | 17 | //string dayStr; |
bdarling | 6:c7220f7c1ae0 | 18 | char dayStr[3]; |
bdarling | 6:c7220f7c1ae0 | 19 | char monthStr[3]; |
bdarling | 6:c7220f7c1ae0 | 20 | char yearStr[5]; |
bdarling | 6:c7220f7c1ae0 | 21 | int day; |
bdarling | 6:c7220f7c1ae0 | 22 | int month; |
bdarling | 6:c7220f7c1ae0 | 23 | int year; |
bdarling | 6:c7220f7c1ae0 | 24 | |
bdarling | 6:c7220f7c1ae0 | 25 | |
bdarling | 6:c7220f7c1ae0 | 26 | void getData(){ |
bdarling | 6:c7220f7c1ae0 | 27 | pc.scanf("%s %s", string1, string2); |
bdarling | 6:c7220f7c1ae0 | 28 | //pc.printf("%s %s", string1, string2); |
bdarling | 6:c7220f7c1ae0 | 29 | |
bdarling | 6:c7220f7c1ae0 | 30 | |
bdarling | 6:c7220f7c1ae0 | 31 | } |
bdarling | 6:c7220f7c1ae0 | 32 | |
bdarling | 6:c7220f7c1ae0 | 33 | void readSerial(){ |
bdarling | 6:c7220f7c1ae0 | 34 | if((strcmp(string1,"READ")==0) && (strcmp(string2,"ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 35 | //if((string1.compare("READ") == 0) && (string2.compare("ALL") == 0)){ |
bdarling | 6:c7220f7c1ae0 | 36 | pc.printf("reading all\n"); |
bdarling | 6:c7220f7c1ae0 | 37 | } |
bdarling | 6:c7220f7c1ae0 | 38 | |
bdarling | 6:c7220f7c1ae0 | 39 | else if((strcmp(string1,"DELETE")==0) && (strcmp(string2,"ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 40 | //else if((string1.compare("DELETE")==0) && (string2.compare("ALL")==0)){ |
bdarling | 6:c7220f7c1ae0 | 41 | pc.printf("deleting all\n"); |
bdarling | 6:c7220f7c1ae0 | 42 | } |
bdarling | 6:c7220f7c1ae0 | 43 | |
bdarling | 6:c7220f7c1ae0 | 44 | else if(strcmp(string1,"READ")==0){ |
bdarling | 6:c7220f7c1ae0 | 45 | //else if(string1.compare("READ")==0){ |
bdarling | 6:c7220f7c1ae0 | 46 | if(sscanf(string2,"%d")>0){ |
bdarling | 6:c7220f7c1ae0 | 47 | number = atoi(string2); |
bdarling | 6:c7220f7c1ae0 | 48 | if(number > 0){ |
bdarling | 6:c7220f7c1ae0 | 49 | pc.printf("reading <%d>\n",number); |
bdarling | 6:c7220f7c1ae0 | 50 | } |
bdarling | 6:c7220f7c1ae0 | 51 | else{ |
bdarling | 6:c7220f7c1ae0 | 52 | pc.printf("invalid value\n"); |
bdarling | 6:c7220f7c1ae0 | 53 | } |
bdarling | 6:c7220f7c1ae0 | 54 | } |
bdarling | 6:c7220f7c1ae0 | 55 | else{ |
bdarling | 6:c7220f7c1ae0 | 56 | pc.printf("invalid command\n"); |
bdarling | 6:c7220f7c1ae0 | 57 | } |
bdarling | 6:c7220f7c1ae0 | 58 | } |
bdarling | 6:c7220f7c1ae0 | 59 | |
bdarling | 6:c7220f7c1ae0 | 60 | else if(strcmp(string1,"DELETE")==0){ |
bdarling | 6:c7220f7c1ae0 | 61 | if(sscanf(string2,"%d")>0){ |
bdarling | 6:c7220f7c1ae0 | 62 | number = atoi(string2); |
bdarling | 6:c7220f7c1ae0 | 63 | if(number > 0){ |
bdarling | 6:c7220f7c1ae0 | 64 | pc.printf("deleting <%d>\n",number); |
bdarling | 6:c7220f7c1ae0 | 65 | } |
bdarling | 6:c7220f7c1ae0 | 66 | else{ |
bdarling | 6:c7220f7c1ae0 | 67 | pc.printf("invalid value\n"); |
bdarling | 6:c7220f7c1ae0 | 68 | } |
bdarling | 6:c7220f7c1ae0 | 69 | } |
bdarling | 6:c7220f7c1ae0 | 70 | else{ |
bdarling | 6:c7220f7c1ae0 | 71 | pc.printf("invalid command\n"); |
bdarling | 6:c7220f7c1ae0 | 72 | } |
bdarling | 6:c7220f7c1ae0 | 73 | |
bdarling | 6:c7220f7c1ae0 | 74 | } |
bdarling | 6:c7220f7c1ae0 | 75 | |
bdarling | 6:c7220f7c1ae0 | 76 | else if(strcmp(string1,"SETDATE")==0) |
bdarling | 6:c7220f7c1ae0 | 77 | { |
bdarling | 6:c7220f7c1ae0 | 78 | pc.printf("Setdate started\n\r"); |
bdarling | 6:c7220f7c1ae0 | 79 | if(sscanf(string2,"%8s") > 0) |
bdarling | 6:c7220f7c1ae0 | 80 | { |
bdarling | 6:c7220f7c1ae0 | 81 | pc.printf("date string is %s\n\r", string2); |
bdarling | 6:c7220f7c1ae0 | 82 | |
bdarling | 6:c7220f7c1ae0 | 83 | dayStr[0] = string2[0]; |
bdarling | 6:c7220f7c1ae0 | 84 | dayStr[1] = string2[1]; |
bdarling | 6:c7220f7c1ae0 | 85 | dayStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 86 | monthStr[0] = string2[2]; |
bdarling | 6:c7220f7c1ae0 | 87 | monthStr[1] = string2[3]; |
bdarling | 6:c7220f7c1ae0 | 88 | monthStr[2] = NULL; |
bdarling | 6:c7220f7c1ae0 | 89 | yearStr[0] = string2[4]; |
bdarling | 6:c7220f7c1ae0 | 90 | yearStr[1] = string2[5]; |
bdarling | 6:c7220f7c1ae0 | 91 | yearStr[2] = string2[6]; |
bdarling | 6:c7220f7c1ae0 | 92 | yearStr[3] = string2[7]; |
bdarling | 6:c7220f7c1ae0 | 93 | yearStr[4] = NULL; |
bdarling | 6:c7220f7c1ae0 | 94 | |
bdarling | 6:c7220f7c1ae0 | 95 | pc.printf("Day = %s Month = %s Year = %s\n\r", dayStr, monthStr, yearStr); |
bdarling | 6:c7220f7c1ae0 | 96 | } |
bdarling | 6:c7220f7c1ae0 | 97 | else{ |
bdarling | 6:c7220f7c1ae0 | 98 | pc.printf("invalid value\n"); |
bdarling | 6:c7220f7c1ae0 | 99 | } |
bdarling | 6:c7220f7c1ae0 | 100 | } |
bdarling | 6:c7220f7c1ae0 | 101 | |
bdarling | 6:c7220f7c1ae0 | 102 | else{ |
bdarling | 6:c7220f7c1ae0 | 103 | pc.printf("invalid command\n"); |
bdarling | 6:c7220f7c1ae0 | 104 | } |
bdarling | 6:c7220f7c1ae0 | 105 | } |