diff --git a/firmware/src/commands.c b/firmware/src/commands.c index 4a71afc..51b8a8f 100644 --- a/firmware/src/commands.c +++ b/firmware/src/commands.c @@ -6,13 +6,9 @@ #include #include -#include #include #include -// Internal function prototypes -void _uartPutAsciiBlock(DataBuffer buffer); - // Mode: ASCII (false) or binary (true) bool binary_mode = false; @@ -127,25 +123,17 @@ void commandRead(char* arg) { } } else { // Fancy ASCII output - _uartPutAsciiBlock(buffer); + uartPrintf("<%d>", buffer.bytes); + + for (int i = 0; i < buffer.bytes; i++) { + uartPrintf(" %02X", buffer.data[i]); + } + uartPutLine(NULL); } } while (buffer.bytes > 0); } -void _uartPutAsciiBlock(DataBuffer buffer) { - char outBuffer[20]; - sprintf(outBuffer, "<%d>", buffer.bytes); - uartPutString(outBuffer); - - for (int i = 0; i < buffer.bytes; i++) { - sprintf(outBuffer, " %02X", buffer.data[i]); - uartPutString(outBuffer); - } - - uartPutLine(NULL); -} - void commandWrite(char* arg) { if (arg == NULL) { uartPutLine("ERR WRITE needs a start address"); @@ -176,26 +164,20 @@ void commandErase() { // TESTREAD command: for testing purposes, reads a few bytes and returns them in a human readable format. void commandTestRead() { - uint8_t byte; - char outBuffer[20]; - eepromSetReadMode(); for (int i = 0x00; i < 0x20; i++) { - itoa(i, outBuffer, 16); - uartPutString("TESTREAD 0x"); - uartPutString(outBuffer); - uartPutString(": "); + uartPrintf("TESTREAD 0x%02X: ", i); - byte = eepromReadByte(i); - itoa(byte, outBuffer, 16); + uint8_t byte = eepromReadByte(i); - uartPutChar(byte); - uartPutString(" (0x"); - uartPutString(outBuffer); - uartPutString(")"); + if (byte >= 32 && byte <= 126) { + uartPutChar(byte); + } else { + uartPutChar('?'); + } - uartPutLine(NULL); + uartPrintf(" (0x%02X)\n"); } } diff --git a/firmware/src/uart.c b/firmware/src/uart.c index b68bab2..1d6fcba 100644 --- a/firmware/src/uart.c +++ b/firmware/src/uart.c @@ -2,6 +2,8 @@ #include "uart.h" #include +#include +#include #include // Initialize UART @@ -54,6 +56,19 @@ void uartPutLine(char* data) { uartPutChar('\n'); } +// Transmit a sprintf formatted string (maximum buffer: 256 bytes!) +void uartPrintf(const char* format, ...) { + char outBuffer[256]; + + va_list args; + va_start(args, format); + + vsprintf(outBuffer, format, args); + uartPutString(outBuffer); + + va_end(args); +} + // Receive a single character (blocking) unsigned char uartGetChar() { // Block until a character has been received diff --git a/firmware/src/uart.h b/firmware/src/uart.h index b47c953..7421af4 100644 --- a/firmware/src/uart.h +++ b/firmware/src/uart.h @@ -16,6 +16,9 @@ void uartPutString(char* data); // Transmit a string followed by a line break void uartPutLine(char* data); +// Transmit a sprintf formatted string (maximum buffer: 256 bytes!) +void uartPrintf(const char* format, ...); + // Read a single character (blocking) unsigned char uartGetChar();