Skip to content

Commit ed5bc26

Browse files
committed
refactor: address CodeRabbit nitpick feedback
- Extract magic number 1000 to MAX_REASONABLE_VBAT_RAW constant - Add debug logging for PREDICT_MOTOR_0 hardcoded fallback - Remove redundant function-scoped import (decoder::* glob already includes it) All tests pass, no clippy warnings.
1 parent 34f0bb9 commit ed5bc26

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/parser/decoder.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub const ENCODING_TAG2_3SVARIABLE: u8 = 10;
1313

1414
// Predictor constants - directly from JavaScript reference
1515
pub const PREDICT_0: u8 = 0;
16+
17+
// Maximum reasonable raw vbatLatest value before considering it corrupted
18+
const MAX_REASONABLE_VBAT_RAW: i32 = 1000;
1619
pub const PREDICT_PREVIOUS: u8 = 1;
1720
pub const PREDICT_STRAIGHT_LINE: u8 = 2;
1821
pub const PREDICT_AVERAGE_2: u8 = 3;
@@ -109,7 +112,7 @@ pub fn apply_predictor_with_debug(
109112
.unwrap_or(false)
110113
{
111114
// Check if previous value is corrupted (way too high for voltage)
112-
if prev[field_index] > 1000 {
115+
if prev[field_index] > MAX_REASONABLE_VBAT_RAW {
113116
if debug {
114117
eprintln!("DEBUG: Fixed corrupted vbatLatest previous value {} replaced with reasonable estimate", prev[field_index]);
115118
}
@@ -169,6 +172,12 @@ pub fn apply_predictor_with_debug(
169172
// Fallback: use hardcoded position (typically field 39 in I-frame)
170173
let motor0_index = 39;
171174
if motor0_index < current_frame.len() {
175+
if debug {
176+
eprintln!(
177+
"DEBUG: PREDICT_MOTOR_0 using hardcoded fallback index {}",
178+
motor0_index
179+
);
180+
}
172181
current_frame[motor0_index] + raw_value
173182
} else {
174183
raw_value

src/parser/frame.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::parser::{decoder::*, event::parse_e_frame, gps::*, stream::BBLDataStream};
1+
use crate::parser::{
2+
decoder::apply_predictor_with_debug, decoder::*, event::parse_e_frame, gps::*,
3+
stream::BBLDataStream,
4+
};
25
use crate::types::{
36
DecodedFrame, EventFrame, FrameDefinition, FrameHistory, FrameStats, GpsCoordinate,
47
GpsHomeCoordinate,
@@ -446,8 +449,6 @@ pub fn parse_frame_data(
446449
sysconfig: &HashMap<String, i32>,
447450
debug: bool,
448451
) -> Result<()> {
449-
use crate::parser::decoder::apply_predictor_with_debug;
450-
451452
let mut i = 0;
452453
let mut values = [0i32; 8];
453454

0 commit comments

Comments
 (0)