tinkerforge_async/bindings/
dual_button_bricklet.rs1#[allow(unused_imports)]
15use crate::{
16 base58::Uid, byte_converter::*, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection,
17 low_level_traits::LowLevelRead,
18};
19#[allow(unused_imports)]
20use futures_core::Stream;
21#[allow(unused_imports)]
22use tokio_stream::StreamExt;
23pub enum DualButtonBrickletFunction {
24 SetLedState,
25 GetLedState,
26 GetButtonState,
27 SetSelectedLedState,
28 GetIdentity,
29 CallbackStateChanged,
30}
31impl From<DualButtonBrickletFunction> for u8 {
32 fn from(fun: DualButtonBrickletFunction) -> Self {
33 match fun {
34 DualButtonBrickletFunction::SetLedState => 1,
35 DualButtonBrickletFunction::GetLedState => 2,
36 DualButtonBrickletFunction::GetButtonState => 3,
37 DualButtonBrickletFunction::SetSelectedLedState => 5,
38 DualButtonBrickletFunction::GetIdentity => 255,
39 DualButtonBrickletFunction::CallbackStateChanged => 4,
40 }
41 }
42}
43pub const DUAL_BUTTON_BRICKLET_LED_STATE_AUTO_TOGGLE_ON: u8 = 0;
44pub const DUAL_BUTTON_BRICKLET_LED_STATE_AUTO_TOGGLE_OFF: u8 = 1;
45pub const DUAL_BUTTON_BRICKLET_LED_STATE_ON: u8 = 2;
46pub const DUAL_BUTTON_BRICKLET_LED_STATE_OFF: u8 = 3;
47pub const DUAL_BUTTON_BRICKLET_BUTTON_STATE_PRESSED: u8 = 0;
48pub const DUAL_BUTTON_BRICKLET_BUTTON_STATE_RELEASED: u8 = 1;
49pub const DUAL_BUTTON_BRICKLET_LED_LEFT: u8 = 0;
50pub const DUAL_BUTTON_BRICKLET_LED_RIGHT: u8 = 1;
51
52#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
53pub struct LedState {
54 pub led_l: u8,
55 pub led_r: u8,
56}
57impl FromByteSlice for LedState {
58 fn bytes_expected() -> usize {
59 2
60 }
61 fn from_le_byte_slice(bytes: &[u8]) -> LedState {
62 LedState { led_l: <u8>::from_le_byte_slice(&bytes[0..1]), led_r: <u8>::from_le_byte_slice(&bytes[1..2]) }
63 }
64}
65
66#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
67pub struct ButtonState {
68 pub button_l: u8,
69 pub button_r: u8,
70}
71impl FromByteSlice for ButtonState {
72 fn bytes_expected() -> usize {
73 2
74 }
75 fn from_le_byte_slice(bytes: &[u8]) -> ButtonState {
76 ButtonState { button_l: <u8>::from_le_byte_slice(&bytes[0..1]), button_r: <u8>::from_le_byte_slice(&bytes[1..2]) }
77 }
78}
79
80#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
81pub struct StateChangedEvent {
82 pub button_l: u8,
83 pub button_r: u8,
84 pub led_l: u8,
85 pub led_r: u8,
86}
87impl FromByteSlice for StateChangedEvent {
88 fn bytes_expected() -> usize {
89 4
90 }
91 fn from_le_byte_slice(bytes: &[u8]) -> StateChangedEvent {
92 StateChangedEvent {
93 button_l: <u8>::from_le_byte_slice(&bytes[0..1]),
94 button_r: <u8>::from_le_byte_slice(&bytes[1..2]),
95 led_l: <u8>::from_le_byte_slice(&bytes[2..3]),
96 led_r: <u8>::from_le_byte_slice(&bytes[3..4]),
97 }
98 }
99}
100
101#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
102pub struct Identity {
103 pub uid: String,
104 pub connected_uid: String,
105 pub position: char,
106 pub hardware_version: [u8; 3],
107 pub firmware_version: [u8; 3],
108 pub device_identifier: u16,
109}
110impl FromByteSlice for Identity {
111 fn bytes_expected() -> usize {
112 25
113 }
114 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
115 Identity {
116 uid: <String>::from_le_byte_slice(&bytes[0..8]),
117 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
118 position: <char>::from_le_byte_slice(&bytes[16..17]),
119 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
120 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
121 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
122 }
123 }
124}
125
126#[derive(Clone)]
128pub struct DualButtonBricklet {
129 device: Device,
130}
131impl DualButtonBricklet {
132 pub const DEVICE_IDENTIFIER: u16 = 230;
133 pub const DEVICE_DISPLAY_NAME: &'static str = "Dual Button Bricklet";
134 pub fn new(uid: Uid, connection: AsyncIpConnection) -> DualButtonBricklet {
136 let mut result = DualButtonBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
137 result.device.response_expected[u8::from(DualButtonBrickletFunction::SetLedState) as usize] = ResponseExpectedFlag::False;
138 result.device.response_expected[u8::from(DualButtonBrickletFunction::GetLedState) as usize] = ResponseExpectedFlag::AlwaysTrue;
139 result.device.response_expected[u8::from(DualButtonBrickletFunction::GetButtonState) as usize] = ResponseExpectedFlag::AlwaysTrue;
140 result.device.response_expected[u8::from(DualButtonBrickletFunction::SetSelectedLedState) as usize] = ResponseExpectedFlag::False;
141 result.device.response_expected[u8::from(DualButtonBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
142 result
143 }
144
145 pub fn get_response_expected(&mut self, fun: DualButtonBrickletFunction) -> Result<bool, GetResponseExpectedError> {
160 self.device.get_response_expected(u8::from(fun))
161 }
162
163 pub fn set_response_expected(
172 &mut self,
173 fun: DualButtonBrickletFunction,
174 response_expected: bool,
175 ) -> Result<(), SetResponseExpectedError> {
176 self.device.set_response_expected(u8::from(fun), response_expected)
177 }
178
179 pub fn set_response_expected_all(&mut self, response_expected: bool) {
181 self.device.set_response_expected_all(response_expected)
182 }
183
184 pub fn get_api_version(&self) -> [u8; 3] {
187 self.device.api_version
188 }
189
190 pub async fn get_state_changed_callback_receiver(&mut self) -> impl Stream<Item = StateChangedEvent> {
204 self.device
205 .get_callback_receiver(u8::from(DualButtonBrickletFunction::CallbackStateChanged))
206 .await
207 .map(|p| StateChangedEvent::from_le_byte_slice(p.body()))
208 }
209
210 pub async fn set_led_state(&mut self, led_l: u8, led_r: u8) -> Result<(), TinkerforgeError> {
229 let mut payload = [0; 2];
230 led_l.write_to_slice(&mut payload[0..1]);
231 led_r.write_to_slice(&mut payload[1..2]);
232
233 #[allow(unused_variables)]
234 let result = self.device.set(u8::from(DualButtonBrickletFunction::SetLedState), &payload).await?;
235 Ok(())
236 }
237
238 pub async fn get_led_state(&mut self) -> Result<LedState, TinkerforgeError> {
246 let payload = [0; 0];
247
248 #[allow(unused_variables)]
249 let result = self.device.get(u8::from(DualButtonBrickletFunction::GetLedState), &payload).await?;
250 Ok(LedState::from_le_byte_slice(result.body()))
251 }
252
253 pub async fn get_button_state(&mut self) -> Result<ButtonState, TinkerforgeError> {
262 let payload = [0; 0];
263
264 #[allow(unused_variables)]
265 let result = self.device.get(u8::from(DualButtonBrickletFunction::GetButtonState), &payload).await?;
266 Ok(ButtonState::from_le_byte_slice(result.body()))
267 }
268
269 pub async fn set_selected_led_state(&mut self, led: u8, state: u8) -> Result<(), TinkerforgeError> {
281 let mut payload = [0; 2];
282 led.write_to_slice(&mut payload[0..1]);
283 state.write_to_slice(&mut payload[1..2]);
284
285 #[allow(unused_variables)]
286 let result = self.device.set(u8::from(DualButtonBrickletFunction::SetSelectedLedState), &payload).await?;
287 Ok(())
288 }
289
290 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
301 let payload = [0; 0];
302
303 #[allow(unused_variables)]
304 let result = self.device.get(u8::from(DualButtonBrickletFunction::GetIdentity), &payload).await?;
305 Ok(Identity::from_le_byte_slice(result.body()))
306 }
307}