rgb_led/
messages.rs

1// SPDX-FileCopyrightText: 2025 Foundation Devices, Inc. <hello@foundation.xyz>
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4use server::{AsScalar, FromScalar};
5
6use crate::{RgbAnimation, RgbColor};
7
8#[derive(Debug, server::Message, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
9#[event(RgbColor)]
10pub struct SubscribeColorUpdates;
11
12/// Set a single LED to a color by index
13#[derive(Debug, server::Message)]
14pub struct SetTo(pub u32, pub RgbColor);
15
16impl FromScalar<2> for SetTo {
17    fn from_scalar(value: [u32; 2]) -> Self { SetTo(value[0], value[1].into()) }
18}
19
20impl AsScalar<2> for SetTo {
21    fn as_scalar(&self) -> [u32; 2] { [self.0, self.1.into()] }
22}
23
24#[derive(Debug, server::Message)]
25pub struct SetAllTo(pub RgbColor);
26
27impl FromScalar<1> for RgbColor {
28    fn from_scalar(value: [u32; 1]) -> Self { value[0].into() }
29}
30
31impl AsScalar<1> for RgbColor {
32    fn as_scalar(&self) -> [u32; 1] { [(*self).into()] }
33}
34
35#[derive(Debug, server::Message)]
36pub struct AnimateAllTo(pub RgbAnimation);
37
38impl FromScalar<4> for RgbAnimation {
39    fn from_scalar(value: [u32; 4]) -> Self {
40        RgbAnimation {
41            from: value[0].into(),
42            to: value[1].into(),
43            duration_ms: value[2] as usize,
44            reset: value[3] != 0,
45        }
46    }
47}
48
49impl AsScalar<4> for RgbAnimation {
50    fn as_scalar(&self) -> [u32; 4] {
51        [self.from.into(), self.to.into(), self.duration_ms as u32, self.reset as u32]
52    }
53}