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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
mod def;

use alloc::vec::Vec;

pub use self::def::*;
use super::{DataFrameKind, FrameKind, Unit};
use crate::data::MainFrameHistory;
use crate::filter::AppliedFilter;
use crate::parser::InternalResult;
use crate::units::prelude::*;
use crate::utils::as_i32;
use crate::{units, Headers, Reader};

/// Data parsed from a main frame.
#[derive(Debug)]
pub struct MainFrame<'data, 'headers, 'parser> {
    headers: &'headers Headers<'data>,
    raw: &'parser RawMainFrame,
    filter: &'parser AppliedFilter,
}

impl super::seal::Sealed for MainFrame<'_, '_, '_> {}

impl super::Frame for MainFrame<'_, '_, '_> {
    type Value = MainValue;

    #[inline]
    fn len(&self) -> usize {
        self.filter.len()
    }

    fn get_raw(&self, index: usize) -> Option<u32> {
        let index = self.filter.get(index)?;

        let value = if index == 0 {
            self.raw.iteration
        } else {
            self.raw.values[index - 1]
        };

        Some(value)
    }

    fn get(&self, index: usize) -> Option<MainValue> {
        let frame_def = self.headers.main_frame_def();
        let index = self.filter.get(index)?;

        if index == 0 {
            return Some(MainValue::Unsigned(self.raw.iteration));
        }
        let index = index - 1;

        let def = &frame_def.fields[index];
        let raw = self.raw.values[index];

        let value = match def.unit {
            MainUnit::Amperage => {
                debug_assert!(def.signed);
                let raw = as_i32(raw);
                MainValue::Amperage(units::new::current(raw))
            }
            MainUnit::Voltage => {
                debug_assert!(!def.signed);
                MainValue::Voltage(units::new::vbat(raw))
            }
            MainUnit::Acceleration => {
                debug_assert!(def.signed);
                let raw = as_i32(raw);
                MainValue::Acceleration(units::new::acceleration(raw, self.headers))
            }
            MainUnit::Rotation => {
                debug_assert!(def.signed);
                let raw = as_i32(raw);
                MainValue::Rotation(units::new::angular_velocity(raw, self.headers))
            }
            MainUnit::Unitless => MainValue::new_unitless(raw, def.signed),
        };

        Some(value)
    }
}

impl<'data, 'headers, 'parser> MainFrame<'data, 'headers, 'parser> {
    pub(crate) fn new(
        headers: &'headers Headers<'data>,
        raw: &'parser RawMainFrame,
        filter: &'parser AppliedFilter,
    ) -> Self {
        Self {
            headers,
            raw,
            filter,
        }
    }

    /// Returns the parsed time since power on.
    #[inline]
    pub fn time(&self) -> Time {
        units::new::time(self.raw.time)
    }

    /// Returns the raw microsecond counter since power on.
    ///
    /// **Note:** This does not currently handle overflow of the transmitted
    /// 32bit counter. See [#54](https://github.com/blackbox-log/blackbox-log/issues/54).
    #[inline]
    pub fn time_raw(&self) -> u64 {
        self.raw.time
    }
}

#[derive(Debug, Clone)]
pub(crate) struct RawMainFrame {
    intra: bool,
    pub(crate) iteration: u32,
    pub(crate) time: u64,
    pub(crate) values: Vec<u32>,
}

impl RawMainFrame {
    pub(crate) fn parse(
        data: &mut Reader,
        headers: &Headers,
        kind: FrameKind,
        history: &MainFrameHistory,
    ) -> InternalResult<Self> {
        let last = history.last();
        let def = headers.main_frame_def();

        if kind == FrameKind::Data(DataFrameKind::Intra) {
            def.parse_intra(data, headers, last)
        } else {
            let skipped = 0; // FIXME

            def.parse_inter(data, headers, last, history.last_last(), skipped)
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MainValue {
    Amperage(ElectricCurrent),
    Voltage(ElectricPotential),
    Acceleration(Acceleration),
    Rotation(AngularVelocity),
    Unsigned(u32),
    Signed(i32),
}

impl MainValue {
    const fn new_unitless(value: u32, signed: bool) -> Self {
        if signed {
            Self::Signed(as_i32(value))
        } else {
            Self::Unsigned(value)
        }
    }
}

impl From<MainValue> for super::Value {
    fn from(value: MainValue) -> Self {
        match value {
            MainValue::Amperage(a) => Self::Amperage(a),
            MainValue::Voltage(v) => Self::Voltage(v),
            MainValue::Acceleration(a) => Self::Acceleration(a),
            MainValue::Rotation(r) => Self::Rotation(r),
            MainValue::Unsigned(x) => Self::Unsigned(x),
            MainValue::Signed(x) => Self::Signed(x),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MainUnit {
    Amperage,
    Voltage,
    Acceleration,
    Rotation,
    Unitless,
}

impl From<MainUnit> for Unit {
    fn from(unit: MainUnit) -> Self {
        match unit {
            MainUnit::Amperage => Self::Amperage,
            MainUnit::Voltage => Self::Voltage,
            MainUnit::Acceleration => Self::Acceleration,
            MainUnit::Rotation => Self::Rotation,
            MainUnit::Unitless => Self::Unitless,
        }
    }
}