Skip to main content

rodecaster_protocol/
trigger.rs

1//! Momentary two-boolean virtual button and trigger pulses.
2//!
3//! Many action and request properties on RØDECaster devices (such as
4//! `mixLinkRequest`, `mixUnlinkRequest`, `padProgressRequestSignal`, and
5//! `sipSlotCallDisconnect`) follow a momentary button-pulse pattern.
6//!
7//! On the wire, these properties encode two serialized booleans:
8//! - Press (asserted): `(true, true)` -> `[0x01, 0x01, 0x02, 0x01, 0x01, 0x02]`
9//! - Release (idle): `(false, false)` -> `[0x01, 0x01, 0x03, 0x01, 0x01, 0x03]`
10//!
11//! Physical interactions emit [`TriggerPhase::Press`] on touch-down and
12//! [`TriggerPhase::Release`] on touch-up. Commands assert `Press`, and the
13//! device resets the property with a `Release` acknowledgment.
14
15use crate::juce_var::{marker, Value};
16
17/// Encode two booleans into the canonical 6-byte JUCE payload.
18pub const fn bool_pair(a: bool, b: bool) -> [u8; 6] {
19    let byte_a = if a {
20        marker::BOOL_TRUE
21    } else {
22        marker::BOOL_FALSE
23    };
24    let byte_b = if b {
25        marker::BOOL_TRUE
26    } else {
27        marker::BOOL_FALSE
28    };
29    [0x01, 0x01, byte_a, 0x01, 0x01, byte_b]
30}
31
32/// The 6-byte trigger payload when asserting a press: `(true, true)`.
33pub const PRESS_BYTES: [u8; 6] = bool_pair(true, true);
34
35/// The 6-byte trigger payload when releasing / idle: `(false, false)`.
36pub const RELEASE_BYTES: [u8; 6] = bool_pair(false, false);
37
38/// Build a `Value::Binary` containing the `(true, true)` press payload.
39pub fn press_value() -> Value {
40    Value::Binary(PRESS_BYTES.to_vec())
41}
42
43/// Build a `Value::Binary` containing the `(false, false)` release payload.
44pub fn release_value() -> Value {
45    Value::Binary(RELEASE_BYTES.to_vec())
46}
47
48/// Decode a 6-byte payload into its two boolean values.
49///
50/// Returns `None` if the payload does not match the 6-byte two-boolean JUCE format.
51pub fn decode_bool_pair(bytes: &[u8]) -> Option<(bool, bool)> {
52    if bytes.len() != 6 {
53        return None;
54    }
55    if bytes[0] != 0x01 || bytes[1] != 0x01 || bytes[3] != 0x01 || bytes[4] != 0x01 {
56        return None;
57    }
58    let a = match bytes[2] {
59        marker::BOOL_TRUE => true,
60        marker::BOOL_FALSE => false,
61        _ => return None,
62    };
63    let b = match bytes[5] {
64        marker::BOOL_TRUE => true,
65        marker::BOOL_FALSE => false,
66        _ => return None,
67    };
68    Some((a, b))
69}
70
71/// Phase of a momentary button press or trigger pulse.
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub enum TriggerPhase {
74    /// Button pressed down / trigger asserted (`(true, true)`).
75    Press,
76    /// Button released / trigger idle (`(false, false)`).
77    Release,
78}
79
80/// Backward-compatible type alias for [`TriggerPhase`].
81pub type TriggerOrigin = TriggerPhase;
82
83/// Decode the trigger phase from a `Value`.
84///
85/// Returns `Some(TriggerPhase)` if the value is a valid 6-byte two-boolean payload.
86pub fn decode_phase(value: &Value) -> Option<TriggerPhase> {
87    let bytes = match value {
88        Value::Binary(b) => b,
89        _ => return None,
90    };
91    let (a, _b) = decode_bool_pair(bytes)?;
92    if a {
93        Some(TriggerPhase::Press)
94    } else {
95        Some(TriggerPhase::Release)
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn bool_pair_encoding() {
105        assert_eq!(PRESS_BYTES, [0x01, 0x01, 0x02, 0x01, 0x01, 0x02]);
106        assert_eq!(RELEASE_BYTES, [0x01, 0x01, 0x03, 0x01, 0x01, 0x03]);
107    }
108
109    #[test]
110    fn bool_pair_round_trips() {
111        assert_eq!(decode_bool_pair(&PRESS_BYTES), Some((true, true)));
112        assert_eq!(decode_bool_pair(&RELEASE_BYTES), Some((false, false)));
113        assert_eq!(
114            decode_bool_pair(&bool_pair(true, false)),
115            Some((true, false))
116        );
117        assert_eq!(
118            decode_bool_pair(&bool_pair(false, true)),
119            Some((false, true))
120        );
121    }
122
123    #[test]
124    fn decode_phase_semantics() {
125        assert_eq!(decode_phase(&press_value()), Some(TriggerPhase::Press));
126        assert_eq!(decode_phase(&release_value()), Some(TriggerPhase::Release));
127        assert_eq!(decode_phase(&Value::Binary(vec![0x01, 0x01, 0x02])), None);
128        assert_eq!(decode_phase(&Value::Bool(true)), None);
129    }
130}