Added newline conversion for stdin

stdin converts the following character sequences:

\n   -> \n
\r\n -> \n
\r   -> \n
\n\r -> \n

For original behaviour, a serial object can be instantiated explicitly
pull/1876/head
Christopher Haster 2016-06-07 10:08:07 -05:00
parent 69c33625eb
commit 2330dcc49a
1 changed files with 22 additions and 4 deletions

View File

@ -88,7 +88,8 @@ FileHandle::~FileHandle() {
#if DEVICE_SERIAL
extern int stdio_uart_inited;
extern serial_t stdio_uart;
static char stdio_prev;
static char stdio_in_prev;
static char stdio_out_prev;
#endif
static void init_serial() {
@ -228,11 +229,11 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
#if DEVICE_SERIAL
if (!stdio_uart_inited) init_serial();
for (unsigned int i = 0; i < length; i++) {
if (buffer[i] == '\n' && stdio_prev != '\r') {
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
serial_putc(&stdio_uart, '\r');
}
serial_putc(&stdio_uart, buffer[i]);
stdio_prev = buffer[i];
stdio_out_prev = buffer[i];
}
#endif
n = length;
@ -259,7 +260,24 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
// only read a character at a time from stdin
#if DEVICE_SERIAL
if (!stdio_uart_inited) init_serial();
*buffer = serial_getc(&stdio_uart);
while (true) {
char c = serial_getc(&stdio_uart);
if ((c == '\r' && stdio_in_prev != '\n') ||
(c == '\n' && stdio_in_prev != '\r')) {
stdio_in_prev = c;
*buffer = '\n';
break;
} else if ((c == '\r' && stdio_in_prev == '\n') ||
(c == '\n' && stdio_in_prev == '\r')) {
stdio_in_prev = c;
// onto next character
continue;
} else {
stdio_in_prev = c;
*buffer = c;
break;
}
}
#endif
n = 1;
} else {