application: remove user command handling
This commit is contained in:
parent
938ab3f3b7
commit
76079d178b
|
@ -83,93 +83,6 @@ size_t putc(char c)
|
||||||
return length; // return number of characters printed
|
return length; // return number of characters printed
|
||||||
}
|
}
|
||||||
|
|
||||||
/** user input command */
|
|
||||||
static char command[32] = {0};
|
|
||||||
/** user input command index */
|
|
||||||
uint8_t command_i = 0;
|
|
||||||
|
|
||||||
/** 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,"h") || 0==strcmp(word,"help") || 0==strcmp(word,"?")) {
|
|
||||||
printf("available commands:\n");
|
|
||||||
printf("led [on|off|toggle]\n");
|
|
||||||
} else if (0==strcmp(word,"l") || 0==strcmp(word,"led")) {
|
|
||||||
word = strtok(NULL,delimiter);
|
|
||||||
if (!word) {
|
|
||||||
printf("LED is ");
|
|
||||||
if (gpio_get(GPIO(LED_PORT), GPIO(LED_PIN))) {
|
|
||||||
printf("on\n");
|
|
||||||
} else {
|
|
||||||
printf("off\n");
|
|
||||||
}
|
|
||||||
} else if (0==strcmp(word,"on")) {
|
|
||||||
led_on(); // switch LED on
|
|
||||||
printf("LED switched on\n"); // notify user
|
|
||||||
} else if (0==strcmp(word,"off")) {
|
|
||||||
led_off(); // switch LED off
|
|
||||||
printf("LED switched off\n"); // notify user
|
|
||||||
} else if (0==strcmp(word,"toggle")) {
|
|
||||||
led_toggle(); // toggle LED
|
|
||||||
printf("LED toggled\n"); // notify user
|
|
||||||
} else {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
} else if (0==strcmp(word,"time")) {
|
|
||||||
word = strtok(NULL,delimiter);
|
|
||||||
if (!word) {
|
|
||||||
time_rtc = rtc_get_counter_val(); // get time from internal RTC
|
|
||||||
time_tm = localtime(&time_rtc); // convert time
|
|
||||||
printf("time: %02d:%02d:%02d\n", time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec);
|
|
||||||
} 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') { // time format is incorrect
|
|
||||||
goto error;
|
|
||||||
} else {
|
|
||||||
time_rtc = rtc_get_counter_val(); // get time from internal RTC
|
|
||||||
time_tm = localtime(&time_rtc); // convert time
|
|
||||||
time_tm->tm_hour = (word[0]-'0')*10+(word[1]-'0')*1; // set hours
|
|
||||||
time_tm->tm_min = (word[3]-'0')*10+(word[4]-'0')*1; // set minutes
|
|
||||||
time_tm->tm_sec = (word[6]-'0')*10+(word[7]-'0')*1; // set seconds
|
|
||||||
time_rtc = mktime(time_tm); // get back seconds
|
|
||||||
rtc_set_counter_val(time_rtc); // save time to internal RTC
|
|
||||||
printf("time set\n");
|
|
||||||
}
|
|
||||||
} else if (0==strcmp(word,"date")) {
|
|
||||||
word = strtok(NULL,delimiter);
|
|
||||||
if (!word) {
|
|
||||||
time_rtc = rtc_get_counter_val(); // get time from internal RTC
|
|
||||||
time_tm = localtime(&time_rtc); // convert time
|
|
||||||
printf("date: %d-%02d-%02d\n", 1900+time_tm->tm_year, time_tm->tm_mon, time_tm->tm_mday);
|
|
||||||
} 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 {
|
|
||||||
time_rtc = rtc_get_counter_val(); // get time from internal RTC
|
|
||||||
time_tm = localtime(&time_rtc); // convert time
|
|
||||||
time_tm->tm_year = ((word[0]-'0')*1000+(word[1]-'0')*100+(word[2]-'0')*10+(word[3]-'0')*1)-1900; // set year
|
|
||||||
time_tm->tm_mon = (word[5]-'0')*10+(word[6]-'0')*1; // set month
|
|
||||||
time_tm->tm_mday = (word[8]-'0')*10+(word[9]-'0')*1; // set day
|
|
||||||
time_rtc = mktime(time_tm); // get back seconds
|
|
||||||
rtc_set_counter_val(time_rtc); // save time to internal RTC
|
|
||||||
printf("date set\n");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
return; // command successfully processed
|
|
||||||
error:
|
|
||||||
printf("command not recognized. enter help to list commands\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if !FF_FS_NORTC && !FF_FS_READONLY
|
#if !FF_FS_NORTC && !FF_FS_READONLY
|
||||||
/** get the current time
|
/** get the current time
|
||||||
* @return current local time shall be returned as bit-fields packed into a DWORD value
|
* @return current local time shall be returned as bit-fields packed into a DWORD value
|
||||||
|
@ -451,18 +364,6 @@ void main(void)
|
||||||
char_flag = false; // reset flag
|
char_flag = false; // reset flag
|
||||||
action = true; // action has been performed
|
action = true; // action has been performed
|
||||||
printf("%c",c); // echo receive character
|
printf("%c",c); // echo receive character
|
||||||
if (c=='\r' || 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 { // 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
|
while (button_flag) { // user pressed button
|
||||||
action = true; // action has been performed
|
action = true; // action has been performed
|
||||||
|
|
Loading…
Reference in New Issue