better newline printing

This commit is contained in:
King Kévin 2016-04-28 11:18:10 +02:00
parent 910be1996d
commit c59e11fd6b
1 changed files with 17 additions and 9 deletions

26
main.c
View File

@ -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<LENGTH(command)-2) { // verify if there is place to save next character
command_i++; // save next character