Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SerialStreamer: Print signed decimal numbers correctly. #92

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 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,40 @@ 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) {
// Move the sign bit to the most significant bit
int32_t signed_data = data & 0x7FFFFF;
if (data & (1 << 23)) {
signed_data |= 1 << 31;
}
uBit.serial.printf("%d ", signed_data);
} else {
// Cannot print uint32_t correctly as serial.printf
// 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' );
}
}


}


Loading