-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.rs
More file actions
174 lines (167 loc) · 6.01 KB
/
event.rs
File metadata and controls
174 lines (167 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! Event frame parsing helper module
//!
//! Contains functions for parsing E-frames (Event data) from blackbox log data.
//! These helpers are used by both the library parser and CLI binary.
use crate::parser::stream::BBLDataStream;
use crate::types::EventFrame;
use anyhow::Result;
/// Helper function to parse inflight adjustment events (types 4 and 13)
/// Returns the event description string
fn parse_inflight_adjustment(
stream: &mut BBLDataStream,
event_data: &mut Vec<u8>,
) -> Result<String> {
let adjustment_function = stream.read_byte()?;
event_data.extend_from_slice(&[adjustment_function]);
if adjustment_function > 127 {
let new_value = stream.read_unsigned_vb()? as f32;
Ok(format!(
"Inflight adjustment - Function: {}, New value: {:.3}",
adjustment_function, new_value
))
} else {
let new_value = stream.read_signed_vb()?;
Ok(format!(
"Inflight adjustment - Function: {}, New value: {}",
adjustment_function, new_value
))
}
}
/// Parse E-frame (Event frame) data from the stream
///
/// E-frames contain various event types such as sync beeps, autotune cycles,
/// inflight adjustments, logging resume, disarm, flight mode changes, and log end.
/// Each event type has its own data format that this function decodes.
pub fn parse_e_frame(stream: &mut BBLDataStream, debug: bool) -> Result<EventFrame> {
if debug {
println!("Parsing E frame (Event frame)");
}
// Read event type (1 byte)
let event_type = stream.read_byte()?;
// Read event data - the length depends on the event type
let mut event_data = Vec::new();
let event_name = match event_type {
0 => {
// FLIGHT_LOG_EVENT_SYNC_BEEP
"Sync beep".to_string()
}
1 => {
// FLIGHT_LOG_EVENT_AUTOTUNE_CYCLE_START
"Autotune cycle start".to_string()
}
2 => {
// FLIGHT_LOG_EVENT_AUTOTUNE_CYCLE_RESULT
let axis = stream.read_byte()?;
let p_gain = stream.read_signed_vb()? as f32 / 1000.0;
let i_gain = stream.read_signed_vb()? as f32 / 1000.0;
let d_gain = stream.read_signed_vb()? as f32 / 1000.0;
event_data.extend_from_slice(&[axis]);
format!(
"Autotune cycle result - Axis: {}, P: {:.3}, I: {:.3}, D: {:.3}",
axis, p_gain, i_gain, d_gain
)
}
3 => {
// FLIGHT_LOG_EVENT_AUTOTUNE_TARGETS
let current_angle = stream.read_signed_vb()?;
let target_angle = stream.read_signed_vb()?;
let target_angle_at_peak = stream.read_signed_vb()?;
let first_peak_angle = stream.read_signed_vb()?;
let second_peak_angle = stream.read_signed_vb()?;
format!(
"Autotune targets - Current: {}, Target: {}, Peak target: {}, First peak: {}, Second peak: {}",
current_angle, target_angle, target_angle_at_peak, first_peak_angle, second_peak_angle
)
}
4 => {
// FLIGHT_LOG_EVENT_INFLIGHT_ADJUSTMENT
parse_inflight_adjustment(stream, &mut event_data)?
}
5 => {
// FLIGHT_LOG_EVENT_LOGGING_RESUME
// Note: Event type 14 has identical implementation - both use this newer numbering
let log_iteration = stream.read_unsigned_vb()?;
let current_time = stream.read_unsigned_vb()?;
format!(
"Logging resume - Iteration: {}, Time: {}",
log_iteration, current_time
)
}
6 => {
// FLIGHT_LOG_EVENT_LOG_END (old numbering)
// Read end message bytes
for _ in 0..4 {
if !stream.eof {
event_data.push(stream.read_byte()?);
}
}
"Log end".to_string()
}
10 => {
// FLIGHT_LOG_EVENT_AUTOTUNE_CYCLE_START (UNUSED)
"Autotune cycle start (unused)".to_string()
}
11 => {
// FLIGHT_LOG_EVENT_AUTOTUNE_CYCLE_RESULT (UNUSED)
"Autotune cycle result (unused)".to_string()
}
12 => {
// FLIGHT_LOG_EVENT_AUTOTUNE_TARGETS (UNUSED)
"Autotune targets (unused)".to_string()
}
13 => {
// FLIGHT_LOG_EVENT_INFLIGHT_ADJUSTMENT
parse_inflight_adjustment(stream, &mut event_data)?
}
14 => {
// FLIGHT_LOG_EVENT_LOGGING_RESUME (newer numbering, same as type 5)
// Both event type numbers are used in different firmware versions
let log_iteration = stream.read_unsigned_vb()?;
let current_time = stream.read_unsigned_vb()?;
format!(
"Logging resume - Iteration: {}, Time: {}",
log_iteration, current_time
)
}
15 => {
// FLIGHT_LOG_EVENT_DISARM
"Disarm".to_string()
}
30 => {
// FLIGHT_LOG_EVENT_FLIGHTMODE - flight mode status event
// Read flight mode data
for _ in 0..4 {
if !stream.eof {
event_data.push(stream.read_byte()?);
}
}
"Flight mode change".to_string()
}
255 => {
// FLIGHT_LOG_EVENT_LOG_END
"Log end".to_string()
}
_ => {
// Unknown event type - read a few bytes as data
for _ in 0..8 {
if stream.eof {
break;
}
event_data.push(stream.read_byte()?);
}
format!("Unknown event type: {}", event_type)
}
};
if debug {
println!(
"DEBUG: Event - Type: {}, Description: {}",
event_type, event_name
);
}
Ok(EventFrame {
timestamp_us: 0, // Will be set later from context
event_type,
event_data,
event_name,
})
}