sparkbler/
lightstick.rs

1use std::marker::PhantomData;
2
3use bluest;
4use bluest::Adapter;
5use bluest::Device;
6
7use crate::color::ColorChar;
8
9/// A thin wrapper around the bluest device type to limit the API
10/// to things we can do with the Ado hibana lightstick
11#[derive(Debug, Clone)]
12pub struct LightStick<T> {
13    pub(crate) device: Device,
14    pub(crate) adapter: Adapter,
15    pub(crate) _marker: PhantomData<T>,
16}
17
18#[derive(Debug, Clone)]
19pub struct Discovered;
20#[derive(Debug, Clone)]
21pub struct Connected;
22
23impl LightStick<Discovered> {
24    /// Connects to the discovered lightstick
25    ///
26    /// On error, raises the underlying bluest Error type
27    pub async fn connect(self) -> Result<LightStick<Connected>, bluest::Error> {
28        self.adapter.connect_device(&self.device).await?;
29        Ok(LightStick {
30            device: self.device,
31            adapter: self.adapter,
32            _marker: PhantomData,
33        })
34    }
35}
36
37impl LightStick<Connected> {
38    // disconnects from the lightstick,
39    // consuming it and returning the disconnected version.
40    //
41    // Note that doing so turns off the light stick, preventing it
42    // from reconnecting.
43    pub async fn disconnect(self) -> Result<(), bluest::Error> {
44        self.adapter.disconnect_device(&self.device).await
45    }
46
47    /// Returns the characterstic that the lightstick uses for color
48    pub async fn get_characteristic<'connected>(&'connected self) -> ColorChar<'connected> {
49        ColorChar::new(&self).await
50    }
51
52    pub fn get_id(&self) -> bluest::DeviceId {
53        self.device.id()
54    }
55}