Skip to main content

ri_esp_tiered/
lib.rs

1#![no_std]
2
3pub const MAGIC: [u8; 4] = *b"TEAI";
4
5#[derive(Debug, Clone, Copy, PartialEq)]
6pub struct SensorContext {
7    pub temperature_c: Option<f32>,
8    pub humidity_pct: Option<f32>,
9    pub battery_v: Option<f32>,
10    pub wifi_rssi: Option<i32>,
11}
12
13#[derive(Debug, Clone, PartialEq)]
14pub struct FeatureTransfer<const N: usize> {
15    pub sentinel_prediction: f32,
16    pub sentinel_confidence: f32,
17    pub features: heapless::Vec<f32, N>,
18    pub timestamp_ms: u64,
19}
20
21impl<const N: usize> FeatureTransfer<N> {
22    pub fn new(prediction: f32, confidence: f32, timestamp_ms: u64) -> Self {
23        Self {
24            sentinel_prediction: prediction,
25            sentinel_confidence: confidence,
26            features: heapless::Vec::new(),
27            timestamp_ms,
28        }
29    }
30    pub fn should_wake(&self, threshold: f32) -> bool {
31        self.sentinel_confidence < threshold
32    }
33    pub fn encode(&self, out: &mut [u8]) -> Option<usize> {
34        let need = 4 + 4 + 4 + 4 + self.features.len() * 4 + 8;
35        if out.len() < need {
36            return None;
37        }
38        let mut p = 0;
39        out[p..p + 4].copy_from_slice(&MAGIC);
40        p += 4;
41        out[p..p + 4].copy_from_slice(&self.sentinel_prediction.to_le_bytes());
42        p += 4;
43        out[p..p + 4].copy_from_slice(&self.sentinel_confidence.to_le_bytes());
44        p += 4;
45        out[p..p + 4].copy_from_slice(&(self.features.len() as u32).to_le_bytes());
46        p += 4;
47        for f in self.features.iter() {
48            out[p..p + 4].copy_from_slice(&f.to_le_bytes());
49            p += 4;
50        }
51        out[p..p + 8].copy_from_slice(&self.timestamp_ms.to_le_bytes());
52        p += 8;
53        Some(p)
54    }
55    pub fn decode(data: &[u8]) -> Option<Self> {
56        if data.len() < 24 || data[0..4] != MAGIC {
57            return None;
58        }
59        let mut p = 4;
60        let pred = f32::from_le_bytes(data[p..p + 4].try_into().ok()?);
61        p += 4;
62        let conf = f32::from_le_bytes(data[p..p + 4].try_into().ok()?);
63        p += 4;
64        let len = u32::from_le_bytes(data[p..p + 4].try_into().ok()?) as usize;
65        p += 4;
66        if len > N || data.len() < 4 + 4 + 4 + 4 + len * 4 + 8 {
67            return None;
68        }
69        let mut features = heapless::Vec::new();
70        for _ in 0..len {
71            features
72                .push(f32::from_le_bytes(data[p..p + 4].try_into().ok()?))
73                .ok()?;
74            p += 4;
75        }
76        let ts = u64::from_le_bytes(data[p..p + 8].try_into().ok()?);
77        Some(Self {
78            sentinel_prediction: pred,
79            sentinel_confidence: conf,
80            features,
81            timestamp_ms: ts,
82        })
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    #[test]
90    fn roundtrip() {
91        let mut f = FeatureTransfer::<4>::new(0.5, 0.7, 42);
92        f.features.push(1.0).unwrap();
93        let mut buf = [0u8; 64];
94        let n = f.encode(&mut buf).unwrap();
95        let d = FeatureTransfer::<4>::decode(&buf[..n]).unwrap();
96        assert_eq!(d.features[0], 1.0);
97        assert!(d.should_wake(0.75));
98    }
99}