From 35ffab3e09eccdd50bbaaf2c47242941abe33369 Mon Sep 17 00:00:00 2001 From: Carlos Pereira Atencio Date: Mon, 19 Aug 2024 16:16:54 +0100 Subject: [PATCH] SerialStreamer: Print signed decimal numbers correctly. --- source/samples/SerialStreamer.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/source/samples/SerialStreamer.cpp b/source/samples/SerialStreamer.cpp index 4c3c521b..fa6876f2 100644 --- a/source/samples/SerialStreamer.cpp +++ b/source/samples/SerialStreamer.cpp @@ -96,8 +96,6 @@ void SerialStreamer::streamBuffer(ManagedBuffer buffer) uint8_t *end = d+buffer.length(); uint32_t data; - uint32_t sum = 0; - while(d < end) { data = *d++; @@ -109,29 +107,35 @@ void SerialStreamer::streamBuffer(ManagedBuffer buffer) if (bps > DATASTREAM_FORMAT_24BIT_SIGNED) data |= (*d++) << 24; - if (mode == SERIAL_STREAM_MODE_HEX) + if (mode == SERIAL_STREAM_MODE_HEX) { uBit.serial.printf("%x ", data); - else - uBit.serial.printf("%d ", data); + } else { + // SERIAL_STREAM_MODE_DECIMAL + if (bps == DATASTREAM_FORMAT_8BIT_SIGNED) { + uBit.serial.printf("%d ", (int8_t)(data & 0xFF)); + } else if (bps == DATASTREAM_FORMAT_16BIT_SIGNED) { + uBit.serial.printf("%d ", (int16_t)(data & 0xFFFF)); + } else if (bps == DATASTREAM_FORMAT_24BIT_SIGNED) { + uBit.serial.printf("%d ", (int32_t)(data & 0xFFFFFF)); + } else { + // Note we cannot print a uint32_t as the printf + // implementation does not support "%u" + uBit.serial.printf("%d ", data); + } + } CRLF++; - sum += data; if (CRLF >= 16){ uBit.serial.putc('\r'); uBit.serial.putc('\n'); - sum = 0; CRLF = 0; } } - + if (CRLF > 0) { uBit.serial.putc( '\r' ); uBit.serial.putc( '\n' ); } } - - } - -