Jamie Satchell
/
SOFT253_Weather_Sensor_JSatchell
MAIN
Fork of SOFT253_Template_Weather_OS_54 by
Diff: prompt.cpp
- Revision:
- 37:13f74964a045
- Parent:
- 36:af6abc6f7590
--- a/prompt.cpp Mon May 15 08:58:08 2017 +0000 +++ b/prompt.cpp Mon May 15 11:38:07 2017 +0000 @@ -2,15 +2,24 @@ #include "log.hpp" #include "Data.hpp" +/* prompt deals with all user inputs and what functions should run in response */ Serial serial(USBTX, USBRX); Mutex mutex_p; +/*function for writing out a record with time struct to format time*/ +void prompt_write_entry(Data entry){ + ; + time_t currenttime = entry.logtime; + tm* timenew = localtime(¤ttime); + + 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); + + } void readline(char* buffer) { int i = 0; - while (1) - { + while (1) { char c = serial.getc(); serial.putc(c); buffer[i] = c; @@ -31,28 +40,72 @@ void prompt_interpret(char* cmd) -{ mutex_p.lock(); +{ + mutex_p.lock(); + int input; int n = log_length(); - n = (n - 1); - if (strcmp(cmd, "help") == 0){ - serial.printf("Commands:\r\n help -\r\n READ ALL - Show all records \r\n"); + int hours; + int minutes; + int seconds; + n = (n - 1); + if (strcmp(cmd, "help") == 0) { + 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 "); + } else if (strcmp(cmd, "READ ALL") == 0) { + serial.printf("\r\n"); + for (int i = n - 1; i >= 1; i--) { + Data entry = log_get(i); + prompt_write_entry(entry); } - else if (strcmp(cmd, "READ ALL") == 0) - {serial.printf("\r\n"); - for (int i = n - 1; i >= 1; i--){ - Data entry = log_get(i); - serial.printf("%4.2fC %3.1f%% %6.1f %i %i %i \r\n", entry.tempCelsius, entry.humi, entry.pressure, entry.seconds, entry.minutes, entry.hours); + } else if (sscanf(cmd, "READ %i", &input) == 1) { + serial.printf("\r\n"); + + int n = log_length(); + if (input > n){ + input = n; + } + for (int i = input - 1; i >= 1; i--) { + Data entry = log_get(i); + prompt_write_entry(entry); + } + } + else if (sscanf(cmd, "SETTIME %i %i %i", &hours, &minutes, &seconds) == 3){ + serial.printf("\r\n"); + time_t currenttime = time(0); + tm* timenew = localtime(¤ttime); + + timenew->tm_hour = hours; + timenew->tm_min = minutes; + timenew->tm_sec = seconds; + + set_time(mktime(timenew)); + } + else if (strcmp(cmd, "DELETE_ALL") == 0) { + serial.printf("\r\n"); + for (int i = n - 1; i >= 1; i--) { + log_pop(); + } } - else{ + else if (sscanf(cmd, "DELETE %i", &input) == 1) { + serial.printf("\r\n"); + + int n = log_length(); + if (input > n){ + input = n; + } + for (int i = input - 1; i >= 1; i--) { + log_pop(); + } + } + + else { serial.printf("Unknown command!\r\n"); - } - mutex_p.unlock(); + } + mutex_p.unlock(); } void prompt_run() -{ - while (1) { + while (1) { char buffer[64]; readline(buffer);