diff --git a/print.c b/print.c index 697ffb5..1487d73 100644 --- a/print.c +++ b/print.c @@ -40,25 +40,24 @@ void printPgmString(const char *s) serial_write(c); } +// Prints a single digit of any base up to 36. 0..9 prints as +// '0'..'9' while 10..35 prints as 'a'..'z' +void printDigit(uint8_t value) { + serial_write(value < 10 ? + '0' + value : + 'a' + value - 10); +} + void printIntegerInBase(unsigned long n, unsigned long base) { - unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. - unsigned long i = 0; - if (n == 0) { - serial_write('0'); + printDigit(0); return; } - while (n > 0) { - buf[i++] = n % base; + printDigit(n % base); n /= base; } - - for (; i > 0; i--) - serial_write(buf[i - 1] < 10 ? - '0' + buf[i - 1] : - 'A' + buf[i - 1] - 10); } void printInteger(long n) diff --git a/protocol.c b/protocol.c index a504d3a..b6ea2ba 100644 --- a/protocol.c +++ b/protocol.c @@ -72,7 +72,7 @@ uint8_t protocol_execute_line(char *line) { void protocol_process() { char c; - while((c = serial_read()) != 0xff) + while((c = serial_read()) != SERIAL_NO_DATA) { if((char_counter > 0) && ((c == '\n') || (c == '\r'))) { // Line is complete. Then execute! line[char_counter] = 0; // treminate string diff --git a/serial.c b/serial.c index b2786b9..2d7aaf7 100644 --- a/serial.c +++ b/serial.c @@ -23,6 +23,7 @@ #include #include +#include "serial.h" #ifdef __AVR_ATmega328P__ @@ -97,7 +98,7 @@ uint8_t serial_read() { if (rx_buffer_head != rx_buffer_tail) { // Return magic number if no data pending - return 0xff; + return SERIAL_NO_DATA; } else { uint8_t data = rx_buffer[rx_buffer_tail]; rx_buffer_tail = (rx_buffer_tail + 1) % RX_BUFFER_SIZE; diff --git a/serial.h b/serial.h index 4991d5c..910799b 100644 --- a/serial.h +++ b/serial.h @@ -24,6 +24,8 @@ #ifndef serial_h #define serial_h +#define SERIAL_NO_DATA 0xff + void serial_init(long baud); void serial_write(uint8_t data);