Skip to content

Commit

Permalink
SerialStreamer: Print signed decimal numbers correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-carlos committed Aug 19, 2024
1 parent d59f07b commit 35ffab3
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions source/samples/SerialStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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' );
}
}


}


0 comments on commit 35ffab3

Please sign in to comment.