Skip to content

Commit 9efc929

Browse files
mikellerCopilot
andcommitted
Improve extraction of the signed int.
Co-authored-by: Copilot <[email protected]> Signed-off-by: Michael Keller <[email protected]>
1 parent 2829431 commit 9efc929

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/hw_ostc_parser.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,14 +1183,15 @@ hw_ostc_parser_internal_foreach (hw_ostc_parser_t *parser, dc_sample_callback_t
11831183
if (value & OSTC4_COMPASS_HEADING_CLEARED_FLAG) {
11841184
snprintf(buf, BUFLEN, "Cleared compass heading");
11851185
} else {
1186+
sample.event.value = heading;
1187+
11861188
if (value & OSTC4_COMPASS_HEADING_SET_FLAG) {
11871189
sample.event.type = SAMPLE_EVENT_HEADING;
1188-
snprintf(buf, BUFLEN, "Set compass heading %d degrees", heading);
1190+
snprintf(buf, BUFLEN, "Set compass heading [degrees]%s", sample.event.value ? "" : ": 0");
11891191
} else {
1190-
snprintf(buf, BUFLEN, "Logged compass heading %d degrees", heading);
1192+
snprintf(buf, BUFLEN, "Logged compass heading [degrees]%s", sample.event.value ? "" : ": 0");
11911193
}
11921194

1193-
sample.event.value = heading;
11941195
}
11951196

11961197
sample.event.name = buf;
@@ -1210,7 +1211,10 @@ hw_ostc_parser_internal_foreach (hw_ostc_parser_t *parser, dc_sample_callback_t
12101211
}
12111212

12121213
unsigned int scrubberState = array_uint16_le(data + offset);
1213-
int scrubberTimeMinutes = (scrubberState & 0x0800) ? (-1 ^ 0x0FFF) | (scrubberState & 0x0FFF) : scrubberState & 0x0FFF; // extract 12 bit signed int
1214+
int scrubberTimeMinutes = scrubberState & 0x0FFF; // Extract the 12-bit value
1215+
if (scrubberState & 0x0800) { // Check if the sign bit is set
1216+
scrubberTimeMinutes -= 0x1000; // Perform sign extension
1217+
}
12141218
if (parser->first_scrubber_time_minutes == INT_MAX) {
12151219
parser->first_scrubber_time_minutes = scrubberTimeMinutes;
12161220
}
@@ -1220,23 +1224,23 @@ hw_ostc_parser_internal_foreach (hw_ostc_parser_t *parser, dc_sample_callback_t
12201224
dc_sample_value_t sample = {
12211225
.event.type = SAMPLE_EVENT_STRING,
12221226
.event.flags = SAMPLE_FLAGS_SEVERITY_STATE,
1227+
.event.value = scrubberTimeMinutes,
12231228
};
12241229

1225-
12261230
if (scrubberState & OSTC4_SCRUBBER_STATE_ERROR_FLAG) {
12271231
if (!parser->scrubber_error_reported) {
1228-
sample.event.flags = SAMPLE_FLAGS_SEVERITY_ALARM;
1229-
parser->scrubber_error_reported = true;
1232+
sample.event.flags = SAMPLE_FLAGS_SEVERITY_ALARM;
1233+
parser->scrubber_error_reported = true;
12301234
}
1231-
snprintf(buf, BUFLEN, "Scrubber exhausted: %d minutes remaining", scrubberTimeMinutes);
1235+
snprintf(buf, BUFLEN, "Scrubber exhausted, time remaining [minutes]%s", sample.event.value ? "" : ": 0");
12321236
} else if (scrubberState & OSTC4_SCRUBBER_STATE_WARNING_FLAG) {
12331237
if (!parser->scrubber_warning_reported) {
1234-
sample.event.flags = SAMPLE_FLAGS_SEVERITY_WARN;
1235-
parser->scrubber_warning_reported = true;
1238+
sample.event.flags = SAMPLE_FLAGS_SEVERITY_WARN;
1239+
parser->scrubber_warning_reported = true;
12361240
}
1237-
snprintf(buf, BUFLEN, "Scrubber warning: %d minutes remaining", scrubberTimeMinutes);
1241+
snprintf(buf, BUFLEN, "Scrubber warning, time remaining [minutes]%s", sample.event.value ? "" : ": 0");
12381242
} else {
1239-
snprintf(buf, BUFLEN, "Scrubber: %d minutes remaining", scrubberTimeMinutes);
1243+
snprintf(buf, BUFLEN, "Scrubber time remaining [minutes]%s", sample.event.value ? "" : ": 0");
12401244
}
12411245

12421246
sample.event.name = buf;

0 commit comments

Comments
 (0)