application: use terminal prompt

This commit is contained in:
King Kévin 2018-01-14 13:49:16 +01:00
parent f759017192
commit c3a5aa6540
1 changed files with 21 additions and 36 deletions

View File

@ -43,6 +43,7 @@
#include "print.h" // printing utilities
#include "uart.h" // USART utilities
#include "usb_cdcacm.h" // USB CDC ACM utilities
#include "terminal.h" // handle the terminal interface
//#include "rs485.h" // RS-485 utilities
//#include "rs232.h" // RS-232 utilities
@ -137,26 +138,18 @@ static bool busvoodoo_full = false;
size_t putc(char c)
{
size_t length = 0; // number of characters printed
static char newline = 0; // to remember on which character we sent the newline
if (0==c) {
length = 0; // don't print string termination character
} else if ('\r' == c || '\n' == c) { // send CR+LF newline for most carriage return and line feed combination
if (0==newline || c==newline) { // send newline only if not already send (and only once on \r\n or \n\r)
static char last_c = 0; // to remember on which character we last sent
if ('\n' == c) { // send carriage return (CR) + line feed (LF) newline for each LF
if ('\r' != last_c) { // CR has not already been sent
uart_putchar_nonblocking('\r'); // send CR over USART
usb_cdcacm_putchar('\r'); // send CR over USB
uart_putchar_nonblocking('\n'); // send LF over USART
usb_cdcacm_putchar('\n'); // send LF over USB
length += 2; // remember we printed 2 characters
newline = c; // remember on which character we sent the newline
} else {
length = 0; // the \r or \n of \n\r or \r\n has already been printed
length++; // remember we printed 1 character
}
} else {
uart_putchar_nonblocking(c); // send byte over USART
usb_cdcacm_putchar(c); // send byte over USB
newline = 0; // clear new line
length++; // remember we printed 1 character
}
uart_putchar_nonblocking(c); // send byte over USART
usb_cdcacm_putchar(c); // send byte over USB
length++; // remember we printed 1 character
last_c = c; // remember last character
return length; // return number of characters printed
}
@ -868,16 +861,15 @@ error:
return to_return;
}
/** 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)
{
// don't handle empty lines
if (!str || 0==strlen(str)) {
return;
}
// split command
const char* delimiter = " ";
char* word = strtok(str,delimiter);
@ -886,7 +878,6 @@ static void process_command(char* str)
}
// parse command
if (0==strcmp(word,"h") || 0==strcmp(word,"help") || 0==strcmp(word,"?")) {
printf("unit ID: 0x%08x%08x%08x\n", DESIG_UNIQUE_ID0, DESIG_UNIQUE_ID1, DESIG_UNIQUE_ID2);
printf("available commands:\n");
printf("led [on|off|toggle]\n");
} else if (0==strcmp(word,"l") || 0==strcmp(word,"led")) {
@ -1070,6 +1061,7 @@ void main(void)
i2c_master_write(0x3c, oled_data, LENGTH(oled_data), NULL, 0);
*/
if (false) {
// perform tests
printf("performing self-test, please follow instructions\n");
if (!test_self()) { // perform self-test
@ -1086,6 +1078,7 @@ void main(void)
led_blink(0, 1.0); // show blue OK LED
printf("pin test succeeded\n"); // notify user
}
}
/*
printf("testing RS-485 port\n");
@ -1115,6 +1108,9 @@ void main(void)
// main loop
printf("command input: ready\n");
terminal_prefix = "BV: "; // set terminal prefix
terminal_process = &process_command;
terminal_setup(); // start terminal
bool action = false; // if an action has been performed don't go to sleep
button_flag = false; // reset button flag
char c = '\0'; // to store received character
@ -1136,19 +1132,8 @@ void main(void)
while (char_flag) { // user data received
char_flag = false; // reset flag
action = true; // action has been performed
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
}
}
// printf("%02x\n", c);
terminal_send(c); // send received character to terminal
}
while (button_flag) { // user pressed button
action = true; // action has been performed