Implement uartPrintf()

This commit is contained in:
Lexi / Zoe 2021-04-16 22:37:14 +02:00
parent 5d5e452136
commit 9e0ffd394b
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
3 changed files with 32 additions and 32 deletions

View File

@ -6,13 +6,9 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <util/delay.h> #include <util/delay.h>
// Internal function prototypes
void _uartPutAsciiBlock(DataBuffer buffer);
// Mode: ASCII (false) or binary (true) // Mode: ASCII (false) or binary (true)
bool binary_mode = false; bool binary_mode = false;
@ -127,25 +123,17 @@ void commandRead(char* arg) {
} }
} else { } else {
// Fancy ASCII output // 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); } 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) { void commandWrite(char* arg) {
if (arg == NULL) { if (arg == NULL) {
uartPutLine("ERR WRITE needs a start address"); 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. // TESTREAD command: for testing purposes, reads a few bytes and returns them in a human readable format.
void commandTestRead() { void commandTestRead() {
uint8_t byte;
char outBuffer[20];
eepromSetReadMode(); eepromSetReadMode();
for (int i = 0x00; i < 0x20; i++) { for (int i = 0x00; i < 0x20; i++) {
itoa(i, outBuffer, 16); uartPrintf("TESTREAD 0x%02X: ", i);
uartPutString("TESTREAD 0x");
uartPutString(outBuffer);
uartPutString(": ");
byte = eepromReadByte(i); uint8_t byte = eepromReadByte(i);
itoa(byte, outBuffer, 16);
if (byte >= 32 && byte <= 126) {
uartPutChar(byte); uartPutChar(byte);
uartPutString(" (0x"); } else {
uartPutString(outBuffer); uartPutChar('?');
uartPutString(")"); }
uartPutLine(NULL); uartPrintf(" (0x%02X)\n");
} }
} }

View File

@ -2,6 +2,8 @@
#include "uart.h" #include "uart.h"
#include <avr/io.h> #include <avr/io.h>
#include <stdarg.h>
#include <stdio.h>
#include <util/setbaud.h> #include <util/setbaud.h>
// Initialize UART // Initialize UART
@ -54,6 +56,19 @@ void uartPutLine(char* data) {
uartPutChar('\n'); 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) // Receive a single character (blocking)
unsigned char uartGetChar() { unsigned char uartGetChar() {
// Block until a character has been received // Block until a character has been received

View File

@ -16,6 +16,9 @@ void uartPutString(char* data);
// Transmit a string followed by a line break // Transmit a string followed by a line break
void uartPutLine(char* data); void uartPutLine(char* data);
// Transmit a sprintf formatted string (maximum buffer: 256 bytes!)
void uartPrintf(const char* format, ...);
// Read a single character (blocking) // Read a single character (blocking)
unsigned char uartGetChar(); unsigned char uartGetChar();