Jamie Satchell
/
SOFT253_Weather_Sensor_JSatchell
MAIN
Fork of SOFT253_Template_Weather_OS_54 by
prompt.cpp@37:13f74964a045, 2017-05-15 (annotated)
- Committer:
- J_Satchell
- Date:
- Mon May 15 11:38:07 2017 +0000
- Revision:
- 37:13f74964a045
- Parent:
- 36:af6abc6f7590
fff
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
J_Satchell | 36:af6abc6f7590 | 1 | #include "mbed.h" |
J_Satchell | 36:af6abc6f7590 | 2 | #include "log.hpp" |
J_Satchell | 36:af6abc6f7590 | 3 | #include "Data.hpp" |
J_Satchell | 36:af6abc6f7590 | 4 | |
J_Satchell | 37:13f74964a045 | 5 | /* prompt deals with all user inputs and what functions should run in response */ |
J_Satchell | 36:af6abc6f7590 | 6 | Serial serial(USBTX, USBRX); |
J_Satchell | 36:af6abc6f7590 | 7 | Mutex mutex_p; |
J_Satchell | 36:af6abc6f7590 | 8 | |
J_Satchell | 37:13f74964a045 | 9 | /*function for writing out a record with time struct to format time*/ |
J_Satchell | 37:13f74964a045 | 10 | void prompt_write_entry(Data entry){ |
J_Satchell | 37:13f74964a045 | 11 | ; |
J_Satchell | 37:13f74964a045 | 12 | time_t currenttime = entry.logtime; |
J_Satchell | 37:13f74964a045 | 13 | tm* timenew = localtime(¤ttime); |
J_Satchell | 37:13f74964a045 | 14 | |
J_Satchell | 37:13f74964a045 | 15 | serial.printf("%4.2fC %3.1f%% %6.1f %i %i %i \r\n", entry.tempCelsius, entry.humi, entry.pressure, timenew->tm_hour, timenew->tm_min, timenew->tm_sec); |
J_Satchell | 37:13f74964a045 | 16 | |
J_Satchell | 37:13f74964a045 | 17 | } |
J_Satchell | 36:af6abc6f7590 | 18 | |
J_Satchell | 36:af6abc6f7590 | 19 | void readline(char* buffer) |
J_Satchell | 36:af6abc6f7590 | 20 | { |
J_Satchell | 36:af6abc6f7590 | 21 | int i = 0; |
J_Satchell | 37:13f74964a045 | 22 | while (1) { |
J_Satchell | 36:af6abc6f7590 | 23 | char c = serial.getc(); |
J_Satchell | 36:af6abc6f7590 | 24 | serial.putc(c); |
J_Satchell | 36:af6abc6f7590 | 25 | buffer[i] = c; |
J_Satchell | 36:af6abc6f7590 | 26 | |
J_Satchell | 36:af6abc6f7590 | 27 | if (c == '\n' || c == '\r') |
J_Satchell | 36:af6abc6f7590 | 28 | break; |
J_Satchell | 36:af6abc6f7590 | 29 | else |
J_Satchell | 36:af6abc6f7590 | 30 | i ++; |
J_Satchell | 36:af6abc6f7590 | 31 | } |
J_Satchell | 36:af6abc6f7590 | 32 | buffer[i] = '\0'; |
J_Satchell | 36:af6abc6f7590 | 33 | } |
J_Satchell | 36:af6abc6f7590 | 34 | |
J_Satchell | 36:af6abc6f7590 | 35 | void prompt_init() |
J_Satchell | 36:af6abc6f7590 | 36 | { |
J_Satchell | 36:af6abc6f7590 | 37 | serial.printf("Welcome to the nucleo board low level environmental sensor, type 'help' for commands\r\n"); |
J_Satchell | 36:af6abc6f7590 | 38 | } |
J_Satchell | 36:af6abc6f7590 | 39 | |
J_Satchell | 36:af6abc6f7590 | 40 | |
J_Satchell | 36:af6abc6f7590 | 41 | |
J_Satchell | 36:af6abc6f7590 | 42 | void prompt_interpret(char* cmd) |
J_Satchell | 37:13f74964a045 | 43 | { |
J_Satchell | 37:13f74964a045 | 44 | mutex_p.lock(); |
J_Satchell | 37:13f74964a045 | 45 | int input; |
J_Satchell | 36:af6abc6f7590 | 46 | int n = log_length(); |
J_Satchell | 37:13f74964a045 | 47 | int hours; |
J_Satchell | 37:13f74964a045 | 48 | int minutes; |
J_Satchell | 37:13f74964a045 | 49 | int seconds; |
J_Satchell | 37:13f74964a045 | 50 | n = (n - 1); |
J_Satchell | 37:13f74964a045 | 51 | if (strcmp(cmd, "help") == 0) { |
J_Satchell | 37:13f74964a045 | 52 | serial.printf("Commands:\r\n help -\r\n READ ALL - Show all records \r\n READ <n> = Show specified amount of records \r\n SETTIME <hh> <mm> <ss> = Set the time \r\n DELETE ALL = Deletes all records \r\n DELETE <n> = deletes the oldest record \r\n "); |
J_Satchell | 37:13f74964a045 | 53 | } else if (strcmp(cmd, "READ ALL") == 0) { |
J_Satchell | 37:13f74964a045 | 54 | serial.printf("\r\n"); |
J_Satchell | 37:13f74964a045 | 55 | for (int i = n - 1; i >= 1; i--) { |
J_Satchell | 37:13f74964a045 | 56 | Data entry = log_get(i); |
J_Satchell | 37:13f74964a045 | 57 | prompt_write_entry(entry); |
J_Satchell | 36:af6abc6f7590 | 58 | } |
J_Satchell | 37:13f74964a045 | 59 | } else if (sscanf(cmd, "READ %i", &input) == 1) { |
J_Satchell | 37:13f74964a045 | 60 | serial.printf("\r\n"); |
J_Satchell | 37:13f74964a045 | 61 | |
J_Satchell | 37:13f74964a045 | 62 | int n = log_length(); |
J_Satchell | 37:13f74964a045 | 63 | if (input > n){ |
J_Satchell | 37:13f74964a045 | 64 | input = n; |
J_Satchell | 37:13f74964a045 | 65 | } |
J_Satchell | 37:13f74964a045 | 66 | for (int i = input - 1; i >= 1; i--) { |
J_Satchell | 37:13f74964a045 | 67 | Data entry = log_get(i); |
J_Satchell | 37:13f74964a045 | 68 | prompt_write_entry(entry); |
J_Satchell | 37:13f74964a045 | 69 | } |
J_Satchell | 37:13f74964a045 | 70 | } |
J_Satchell | 37:13f74964a045 | 71 | else if (sscanf(cmd, "SETTIME %i %i %i", &hours, &minutes, &seconds) == 3){ |
J_Satchell | 37:13f74964a045 | 72 | serial.printf("\r\n"); |
J_Satchell | 37:13f74964a045 | 73 | time_t currenttime = time(0); |
J_Satchell | 37:13f74964a045 | 74 | tm* timenew = localtime(¤ttime); |
J_Satchell | 37:13f74964a045 | 75 | |
J_Satchell | 37:13f74964a045 | 76 | timenew->tm_hour = hours; |
J_Satchell | 37:13f74964a045 | 77 | timenew->tm_min = minutes; |
J_Satchell | 37:13f74964a045 | 78 | timenew->tm_sec = seconds; |
J_Satchell | 37:13f74964a045 | 79 | |
J_Satchell | 37:13f74964a045 | 80 | set_time(mktime(timenew)); |
J_Satchell | 37:13f74964a045 | 81 | } |
J_Satchell | 37:13f74964a045 | 82 | else if (strcmp(cmd, "DELETE_ALL") == 0) { |
J_Satchell | 37:13f74964a045 | 83 | serial.printf("\r\n"); |
J_Satchell | 37:13f74964a045 | 84 | for (int i = n - 1; i >= 1; i--) { |
J_Satchell | 37:13f74964a045 | 85 | log_pop(); |
J_Satchell | 37:13f74964a045 | 86 | |
J_Satchell | 36:af6abc6f7590 | 87 | } |
J_Satchell | 36:af6abc6f7590 | 88 | } |
J_Satchell | 37:13f74964a045 | 89 | else if (sscanf(cmd, "DELETE %i", &input) == 1) { |
J_Satchell | 37:13f74964a045 | 90 | serial.printf("\r\n"); |
J_Satchell | 37:13f74964a045 | 91 | |
J_Satchell | 37:13f74964a045 | 92 | int n = log_length(); |
J_Satchell | 37:13f74964a045 | 93 | if (input > n){ |
J_Satchell | 37:13f74964a045 | 94 | input = n; |
J_Satchell | 37:13f74964a045 | 95 | } |
J_Satchell | 37:13f74964a045 | 96 | for (int i = input - 1; i >= 1; i--) { |
J_Satchell | 37:13f74964a045 | 97 | log_pop(); |
J_Satchell | 37:13f74964a045 | 98 | } |
J_Satchell | 37:13f74964a045 | 99 | } |
J_Satchell | 37:13f74964a045 | 100 | |
J_Satchell | 37:13f74964a045 | 101 | else { |
J_Satchell | 36:af6abc6f7590 | 102 | serial.printf("Unknown command!\r\n"); |
J_Satchell | 37:13f74964a045 | 103 | } |
J_Satchell | 37:13f74964a045 | 104 | mutex_p.unlock(); |
J_Satchell | 36:af6abc6f7590 | 105 | } |
J_Satchell | 36:af6abc6f7590 | 106 | void prompt_run() |
J_Satchell | 36:af6abc6f7590 | 107 | { |
J_Satchell | 37:13f74964a045 | 108 | while (1) { |
J_Satchell | 36:af6abc6f7590 | 109 | char buffer[64]; |
J_Satchell | 36:af6abc6f7590 | 110 | readline(buffer); |
J_Satchell | 36:af6abc6f7590 | 111 | |
J_Satchell | 36:af6abc6f7590 | 112 | prompt_interpret(buffer); |
J_Satchell | 36:af6abc6f7590 | 113 | } |
J_Satchell | 36:af6abc6f7590 | 114 | } |