rillrate_protocol/live_data/pulse/
state.rs

1use crate::base::frame_flow::{FrameFlowSpec, FrameFlowState};
2use serde::{Deserialize, Serialize};
3
4pub type PulseFrameState = FrameFlowState<PulseFrameSpec>;
5
6#[derive(Debug, Clone, Serialize, Deserialize, Default)]
7pub struct Range {
8    pub min: Option<f32>,
9    pub max: Option<f32>,
10}
11
12impl Range {
13    pub fn new(mut min: f32, mut max: f32) -> Self {
14        if min > max {
15            std::mem::swap(&mut min, &mut max);
16        }
17        Self {
18            min: Some(min),
19            max: Some(max),
20        }
21    }
22
23    pub fn min(min: f32) -> Self {
24        Self {
25            min: Some(min),
26            max: None,
27        }
28    }
29
30    pub fn max(max: f32) -> Self {
31        Self {
32            min: None,
33            max: Some(max),
34        }
35    }
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct Label {
40    pub caption: String,
41    pub divisor: f32,
42}
43
44impl Default for Label {
45    fn default() -> Self {
46        Self {
47            caption: String::new(),
48            divisor: 1.0,
49        }
50    }
51}
52
53impl Label {
54    pub fn new(caption: impl Into<String>, divisor: f32) -> Self {
55        Self {
56            caption: caption.into(),
57            divisor,
58        }
59    }
60
61    pub fn pct_100() -> Self {
62        Self::new("%", 1.0)
63    }
64
65    pub fn pct_1() -> Self {
66        Self::new("%", 1.0 / 100.0)
67    }
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct PulseFrameSpec {
72    /// Retain interval in seconds.
73    // TODO: Make `retain` optional
74    pub retain: u32,
75    pub range: Range,
76    pub label: Label,
77}
78
79impl Default for PulseFrameSpec {
80    fn default() -> Self {
81        Self {
82            retain: 30,
83            range: Range::default(),
84            label: Label::default(),
85        }
86    }
87}
88
89impl FrameFlowSpec for PulseFrameSpec {
90    type Frame = f32;
91
92    fn retain_secs(&self) -> u32 {
93        self.retain
94    }
95}