From c59e11fd6be9503e2d996a54150b23050e97ef82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 28 Apr 2016 11:18:10 +0200 Subject: [PATCH] better newline printing --- main.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index 1e88b8b..099bf13 100644 --- a/main.c +++ b/main.c @@ -112,16 +112,24 @@ float clock_brightness = 1; int _write(int file, char *ptr, int len) { - int i; + int i; // how much data has been sent + static char newline = 0; // what newline has been sent if (file == STDOUT_FILENO || file == STDERR_FILENO) { for (i = 0; i < len; i++) { - if (ptr[i] == '\n') { // add carrier return before line feed. this is recommended for most UART terminals - usart_putchar_nonblocking('\r'); // a second line feed doesn't break the display - cdcacm_putchar('\r'); // a second line feed doesn't break the display + if (ptr[i] == '\r' || ptr[i] == '\n') { // send CR+LF newline for any carriage return and line feed combination + if (newline==0 || (newline==ptr[i])) { // newline has already been detected + usart_putchar_nonblocking('\r'); // send newline over USART + usart_putchar_nonblocking('\n'); // send newline over USART + cdcacm_putchar('\r'); // send newline over USB + cdcacm_putchar('\n'); // send newline over USB + newline = ptr[i]; // remember the newline + } + } else { // non-newline character + usart_putchar_nonblocking(ptr[i]); // send byte over USART + cdcacm_putchar(ptr[i]); // send byte over USB + newline = 0; // clear new line } - usart_putchar_nonblocking(ptr[i]); // send byte over USART - cdcacm_putchar(ptr[i]); // send byte over USB } return i; } @@ -338,7 +346,7 @@ static void process_command(char* str) return; // command successfully processed error: - puts("command not recognized. enter help to list commands"); + printf("command not recognized. enter help to list commands\n"); } /** @brief program entry point @@ -475,13 +483,13 @@ 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 (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 if (c!='\r') { // user command input + } else { // user command input command[command_i] = c; // save command input if (command_i