add setting time with user command

This commit is contained in:
King Kévin 2016-03-26 19:48:25 +01:00
parent 6edccc845d
commit 5879eeea71
1 changed files with 78 additions and 3 deletions

81
main.c
View File

@ -27,6 +27,7 @@ The time is read from a DS1307 RTC module.
#include <stdlib.h> // standard utilities
#include <unistd.h> // standard streams
#include <errno.h> // error number utilities
#include <string.h> // string utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/stm32/rcc.h> // real-time control clock library
@ -71,6 +72,10 @@ const uint32_t ticks_midday = 12*60*60*TICKS_PER_SECOND;
uint8_t clock_leds[WS2812B_LEDS*3] = {0};
/** current time in tick */
volatile uint32_t current_time = 0;
/** user input command */
char command[32] = {0};
/** user input command index */
uint8_t command_i = 0;
int _write(int file, char *ptr, int len)
{
@ -267,6 +272,67 @@ static void clock_animate_time(uint32_t time)
}
}
/** @brief process user command
* @param[in] str user command string (\0 ended)
*/
static void process_command(char* str)
{
/* split command */
const char* delimiter = " ";
char* word = strtok(str,delimiter);
if (!word) {
goto error;
}
/* parse command */
if (0==strcmp(word,"help")) {
printf("available commands:\n");
printf("date [YYYY-MM-DD]\n");
printf("time [HH:MM:SS]\n");
} else if (0==strcmp(word,"date")) {
word = strtok(NULL,delimiter);
if (!word) {
printf("current date: %04d-%02d-%02d\n", rtc_read_year(), rtc_read_month(), rtc_read_date());
} else if (strlen(word)!=10 || word[0]!='2' || word[1]!='0' || word[2]<'0' || word[2]>'9' || word[3]<'0' || word[3]>'9' || word[5]<'0' || word[5]>'1' || word[6]<'0' || word[6]>'9' || word[8]<'0' || word[8]>'3' || word[9]<'0' || word[9]>'9') {
goto error;
} else {
if (!rtc_write_year((word[0]-'0')*1000+(word[1]-'0')*100+(word[2]-'0')*10+(word[3]-'0')*1)) {
printf("setting year failed\n");
} else if (!rtc_write_month((word[5]-'0')*10+(word[6]-'0')*1)) {
printf("setting month failed\n");
} else if (!rtc_write_date((word[8]-'0')*10+(word[9]-'0')*1)) {
printf("setting day failed\n");
} else {
printf("date set\n");
}
}
} else if (0==strcmp(word,"time")) {
word = strtok(NULL,delimiter);
if (!word) {
printf("current time: %02d:%02d:%02d\n", rtc_read_hours(), rtc_read_minutes(), rtc_read_seconds());
} else if (strlen(word)!=8 || word[0]<'0' || word[0]>'2' || word[1]<'0' || word[1]>'9' || word[3]<'0' || word[3]>'5' || word[4]<'0' || word[4]>'9' || word[6]<'0' || word[6]>'5' || word[7]<'0' || word[7]>'9') {
goto error;
} else {
if (!rtc_write_hours((word[0]-'0')*10+(word[1]-'0')*1)) {
printf("setting hours failed\n");
} else if (!rtc_write_minutes((word[3]-'0')*10+(word[4]-'0')*1)) {
printf("setting minutes failed\n");
} else if (!rtc_write_seconds((word[6]-'0')*10+(word[7]-'0')*1)) {
printf("setting seconds failed\n");
} else {
current_time = rtc_read_hours()*ticks_hour+rtc_read_minutes()*ticks_minute+rtc_read_seconds()*ticks_second; // set also internal time
rtc_oscillator_enable(); // be sure the oscillation is enabled
printf("time set\n");
}
}
} else {
goto error;
}
return; // command successfully processed
error:
puts("command not recognized. enter help to list commands");
}
/** @brief program entry point
* this is the firmware function started by the micro-controller
*/
@ -321,9 +387,6 @@ int main(void)
current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // the current time
clock_animate_time(current_time); // set time with animation
//rtc_write_time(0,52,9,4,23,3,2016);
//rtc_oscillator_enable();
printf("input commands\n");
bool action = false; // if an action has been performed don't go to sleep
button_flag = false; // reset button flag
@ -346,6 +409,18 @@ int main(void)
char_flag = false; // reset flag
action = true; // action has been performed
printf("%c",c); // echo receive character
if (c=='\n') { // end of command received
if (command_i>0) { // there is a command to process
command[command_i] = 0; // end string
command_i = 0; // prepare for next command
process_command(command); // process user command
}
} else if (c!='\r') { // user command input
command[command_i] = c; // save command input
if (command_i<LENGTH(command)-2) { // verify if there is place to save next character
command_i++; // save next character
}
}
}
while (button_flag) { // user pressed button
button_flag = false; // reset flag