tinkerforge_async/bindings/
dual_button_v2_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-16.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.20                              *
5 *                                                           *
6 * If you have a bugfix for this file and want to commit it, *
7 * please fix the bug in the generator. You can find a link  *
8 * to the generators git repository on tinkerforge.com       *
9 *************************************************************/
10
11//! Two tactile buttons with built-in blue LEDs.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/DualButtonV2_Bricklet_Rust.html).
14#[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 DualButtonV2BrickletFunction {
24    SetLedState,
25    GetLedState,
26    GetButtonState,
27    SetSelectedLedState,
28    SetStateChangedCallbackConfiguration,
29    GetStateChangedCallbackConfiguration,
30    GetSpitfpErrorCount,
31    SetBootloaderMode,
32    GetBootloaderMode,
33    SetWriteFirmwarePointer,
34    WriteFirmware,
35    SetStatusLedConfig,
36    GetStatusLedConfig,
37    GetChipTemperature,
38    Reset,
39    WriteUid,
40    ReadUid,
41    GetIdentity,
42    CallbackStateChanged,
43}
44impl From<DualButtonV2BrickletFunction> for u8 {
45    fn from(fun: DualButtonV2BrickletFunction) -> Self {
46        match fun {
47            DualButtonV2BrickletFunction::SetLedState => 1,
48            DualButtonV2BrickletFunction::GetLedState => 2,
49            DualButtonV2BrickletFunction::GetButtonState => 3,
50            DualButtonV2BrickletFunction::SetSelectedLedState => 5,
51            DualButtonV2BrickletFunction::SetStateChangedCallbackConfiguration => 6,
52            DualButtonV2BrickletFunction::GetStateChangedCallbackConfiguration => 7,
53            DualButtonV2BrickletFunction::GetSpitfpErrorCount => 234,
54            DualButtonV2BrickletFunction::SetBootloaderMode => 235,
55            DualButtonV2BrickletFunction::GetBootloaderMode => 236,
56            DualButtonV2BrickletFunction::SetWriteFirmwarePointer => 237,
57            DualButtonV2BrickletFunction::WriteFirmware => 238,
58            DualButtonV2BrickletFunction::SetStatusLedConfig => 239,
59            DualButtonV2BrickletFunction::GetStatusLedConfig => 240,
60            DualButtonV2BrickletFunction::GetChipTemperature => 242,
61            DualButtonV2BrickletFunction::Reset => 243,
62            DualButtonV2BrickletFunction::WriteUid => 248,
63            DualButtonV2BrickletFunction::ReadUid => 249,
64            DualButtonV2BrickletFunction::GetIdentity => 255,
65            DualButtonV2BrickletFunction::CallbackStateChanged => 4,
66        }
67    }
68}
69pub const DUAL_BUTTON_V2_BRICKLET_LED_STATE_AUTO_TOGGLE_ON: u8 = 0;
70pub const DUAL_BUTTON_V2_BRICKLET_LED_STATE_AUTO_TOGGLE_OFF: u8 = 1;
71pub const DUAL_BUTTON_V2_BRICKLET_LED_STATE_ON: u8 = 2;
72pub const DUAL_BUTTON_V2_BRICKLET_LED_STATE_OFF: u8 = 3;
73pub const DUAL_BUTTON_V2_BRICKLET_BUTTON_STATE_PRESSED: u8 = 0;
74pub const DUAL_BUTTON_V2_BRICKLET_BUTTON_STATE_RELEASED: u8 = 1;
75pub const DUAL_BUTTON_V2_BRICKLET_LED_LEFT: u8 = 0;
76pub const DUAL_BUTTON_V2_BRICKLET_LED_RIGHT: u8 = 1;
77pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
78pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
79pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
80pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
81pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
82pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
83pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
84pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
85pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
86pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
87pub const DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
88pub const DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
89pub const DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
90pub const DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
91pub const DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
92
93#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
94pub struct LedState {
95    pub led_l: u8,
96    pub led_r: u8,
97}
98impl FromByteSlice for LedState {
99    fn bytes_expected() -> usize {
100        2
101    }
102    fn from_le_byte_slice(bytes: &[u8]) -> LedState {
103        LedState { led_l: <u8>::from_le_byte_slice(&bytes[0..1]), led_r: <u8>::from_le_byte_slice(&bytes[1..2]) }
104    }
105}
106
107#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
108pub struct ButtonState {
109    pub button_l: u8,
110    pub button_r: u8,
111}
112impl FromByteSlice for ButtonState {
113    fn bytes_expected() -> usize {
114        2
115    }
116    fn from_le_byte_slice(bytes: &[u8]) -> ButtonState {
117        ButtonState { button_l: <u8>::from_le_byte_slice(&bytes[0..1]), button_r: <u8>::from_le_byte_slice(&bytes[1..2]) }
118    }
119}
120
121#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
122pub struct StateChangedEvent {
123    pub button_l: u8,
124    pub button_r: u8,
125    pub led_l: u8,
126    pub led_r: u8,
127}
128impl FromByteSlice for StateChangedEvent {
129    fn bytes_expected() -> usize {
130        4
131    }
132    fn from_le_byte_slice(bytes: &[u8]) -> StateChangedEvent {
133        StateChangedEvent {
134            button_l: <u8>::from_le_byte_slice(&bytes[0..1]),
135            button_r: <u8>::from_le_byte_slice(&bytes[1..2]),
136            led_l: <u8>::from_le_byte_slice(&bytes[2..3]),
137            led_r: <u8>::from_le_byte_slice(&bytes[3..4]),
138        }
139    }
140}
141
142#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
143pub struct SpitfpErrorCount {
144    pub error_count_ack_checksum: u32,
145    pub error_count_message_checksum: u32,
146    pub error_count_frame: u32,
147    pub error_count_overflow: u32,
148}
149impl FromByteSlice for SpitfpErrorCount {
150    fn bytes_expected() -> usize {
151        16
152    }
153    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
154        SpitfpErrorCount {
155            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
156            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
157            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
158            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
159        }
160    }
161}
162
163#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
164pub struct Identity {
165    pub uid: String,
166    pub connected_uid: String,
167    pub position: char,
168    pub hardware_version: [u8; 3],
169    pub firmware_version: [u8; 3],
170    pub device_identifier: u16,
171}
172impl FromByteSlice for Identity {
173    fn bytes_expected() -> usize {
174        25
175    }
176    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
177        Identity {
178            uid: <String>::from_le_byte_slice(&bytes[0..8]),
179            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
180            position: <char>::from_le_byte_slice(&bytes[16..17]),
181            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
182            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
183            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
184        }
185    }
186}
187
188/// Two tactile buttons with built-in blue LEDs
189#[derive(Clone)]
190pub struct DualButtonV2Bricklet {
191    device: Device,
192}
193impl DualButtonV2Bricklet {
194    pub const DEVICE_IDENTIFIER: u16 = 2119;
195    pub const DEVICE_DISPLAY_NAME: &'static str = "Dual Button Bricklet 2.0";
196    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
197    pub fn new(uid: Uid, connection: AsyncIpConnection) -> DualButtonV2Bricklet {
198        let mut result = DualButtonV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
199        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::SetLedState) as usize] = ResponseExpectedFlag::False;
200        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::GetLedState) as usize] = ResponseExpectedFlag::AlwaysTrue;
201        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::GetButtonState) as usize] = ResponseExpectedFlag::AlwaysTrue;
202        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::SetSelectedLedState) as usize] = ResponseExpectedFlag::False;
203        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::SetStateChangedCallbackConfiguration) as usize] =
204            ResponseExpectedFlag::True;
205        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::GetStateChangedCallbackConfiguration) as usize] =
206            ResponseExpectedFlag::AlwaysTrue;
207        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::GetSpitfpErrorCount) as usize] =
208            ResponseExpectedFlag::AlwaysTrue;
209        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::SetBootloaderMode) as usize] =
210            ResponseExpectedFlag::AlwaysTrue;
211        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::GetBootloaderMode) as usize] =
212            ResponseExpectedFlag::AlwaysTrue;
213        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
214            ResponseExpectedFlag::False;
215        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
216        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
217        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::GetStatusLedConfig) as usize] =
218            ResponseExpectedFlag::AlwaysTrue;
219        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::GetChipTemperature) as usize] =
220            ResponseExpectedFlag::AlwaysTrue;
221        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
222        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
223        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
224        result.device.response_expected[u8::from(DualButtonV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
225        result
226    }
227
228    /// Returns the response expected flag for the function specified by the function ID parameter.
229    /// It is true if the function is expected to send a response, false otherwise.
230    ///
231    /// For getter functions this is enabled by default and cannot be disabled, because those
232    /// functions will always send a response. For callback configuration functions it is enabled
233    /// by default too, but can be disabled by [`set_response_expected`](crate::dual_button_v2_bricklet::DualButtonV2Bricklet::set_response_expected).
234    /// For setter functions it is disabled by default and can be enabled.
235    ///
236    /// Enabling the response expected flag for a setter function allows to detect timeouts
237    /// and other error conditions calls of this setter as well. The device will then send a response
238    /// for this purpose. If this flag is disabled for a setter function then no response is sent
239    /// and errors are silently ignored, because they cannot be detected.
240    ///
241    /// See [`set_response_expected`](crate::dual_button_v2_bricklet::DualButtonV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
242    pub fn get_response_expected(&mut self, fun: DualButtonV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
243        self.device.get_response_expected(u8::from(fun))
244    }
245
246    /// Changes the response expected flag of the function specified by the function ID parameter.
247    /// This flag can only be changed for setter (default value: false) and callback configuration
248    /// functions (default value: true). For getter functions it is always enabled.
249    ///
250    /// Enabling the response expected flag for a setter function allows to detect timeouts and
251    /// other error conditions calls of this setter as well. The device will then send a response
252    /// for this purpose. If this flag is disabled for a setter function then no response is sent
253    /// and errors are silently ignored, because they cannot be detected.
254    pub fn set_response_expected(
255        &mut self,
256        fun: DualButtonV2BrickletFunction,
257        response_expected: bool,
258    ) -> Result<(), SetResponseExpectedError> {
259        self.device.set_response_expected(u8::from(fun), response_expected)
260    }
261
262    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
263    pub fn set_response_expected_all(&mut self, response_expected: bool) {
264        self.device.set_response_expected_all(response_expected)
265    }
266
267    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
268    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
269    pub fn get_api_version(&self) -> [u8; 3] {
270        self.device.api_version
271    }
272
273    /// This receiver is called whenever a button is pressed.
274    ///
275    /// Possible states for buttons are:
276    ///
277    /// * 0 = pressed
278    /// * 1 = released
279    ///
280    /// Possible states for LEDs are:
281    ///
282    /// * 0 = AutoToggleOn: Auto toggle enabled and LED on.
283    /// * 1 = AutoToggleOff: Auto toggle enabled and LED off.
284    /// * 2 = On: LED on (auto toggle is disabled).
285    /// * 3 = Off: LED off (auto toggle is disabled).
286    ///
287    /// This receiver can be enabled with [`set_state_changed_callback_configuration`].
288    ///
289    /// [`set_state_changed_callback_configuration`]: #method.set_state_changed_callback_configuration
290    pub async fn get_state_changed_callback_receiver(&mut self) -> impl Stream<Item = StateChangedEvent> {
291        self.device
292            .get_callback_receiver(u8::from(DualButtonV2BrickletFunction::CallbackStateChanged))
293            .await
294            .map(|p| StateChangedEvent::from_le_byte_slice(p.body()))
295    }
296
297    /// Sets the state of the LEDs. Possible states are:
298    ///
299    /// * 0 = AutoToggleOn: Enables auto toggle with initially enabled LED.
300    /// * 1 = AutoToggleOff: Activates auto toggle with initially disabled LED.
301    /// * 2 = On: Enables LED (auto toggle is disabled).
302    /// * 3 = Off: Disables LED (auto toggle is disabled).
303    ///
304    /// In auto toggle mode the LED is toggled automatically at each press of a button.
305    ///
306    /// If you just want to set one of the LEDs and don't know the current state
307    /// of the other LED, you can get the state with [`get_led_state`] or you
308    /// can use [`set_selected_led_state`].
309    ///
310    /// Associated constants:
311    /// * DUAL_BUTTON_V2_BRICKLET_LED_STATE_AUTO_TOGGLE_ON
312    ///	* DUAL_BUTTON_V2_BRICKLET_LED_STATE_AUTO_TOGGLE_OFF
313    ///	* DUAL_BUTTON_V2_BRICKLET_LED_STATE_ON
314    ///	* DUAL_BUTTON_V2_BRICKLET_LED_STATE_OFF
315    pub async fn set_led_state(&mut self, led_l: u8, led_r: u8) -> Result<(), TinkerforgeError> {
316        let mut payload = [0; 2];
317        led_l.write_to_slice(&mut payload[0..1]);
318        led_r.write_to_slice(&mut payload[1..2]);
319
320        #[allow(unused_variables)]
321        let result = self.device.set(u8::from(DualButtonV2BrickletFunction::SetLedState), &payload).await?;
322        Ok(())
323    }
324
325    /// Returns the current state of the LEDs, as set by [`set_led_state`].
326    ///
327    /// Associated constants:
328    /// * DUAL_BUTTON_V2_BRICKLET_LED_STATE_AUTO_TOGGLE_ON
329    ///	* DUAL_BUTTON_V2_BRICKLET_LED_STATE_AUTO_TOGGLE_OFF
330    ///	* DUAL_BUTTON_V2_BRICKLET_LED_STATE_ON
331    ///	* DUAL_BUTTON_V2_BRICKLET_LED_STATE_OFF
332    pub async fn get_led_state(&mut self) -> Result<LedState, TinkerforgeError> {
333        let payload = [0; 0];
334
335        #[allow(unused_variables)]
336        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::GetLedState), &payload).await?;
337        Ok(LedState::from_le_byte_slice(result.body()))
338    }
339
340    /// Returns the current state for both buttons. Possible states are:
341    ///
342    /// * 0 = pressed
343    /// * 1 = released
344    ///
345    /// Associated constants:
346    /// * DUAL_BUTTON_V2_BRICKLET_BUTTON_STATE_PRESSED
347    ///	* DUAL_BUTTON_V2_BRICKLET_BUTTON_STATE_RELEASED
348    pub async fn get_button_state(&mut self) -> Result<ButtonState, TinkerforgeError> {
349        let payload = [0; 0];
350
351        #[allow(unused_variables)]
352        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::GetButtonState), &payload).await?;
353        Ok(ButtonState::from_le_byte_slice(result.body()))
354    }
355
356    /// Sets the state of the selected LED.
357    ///
358    /// The other LED remains untouched.
359    ///
360    /// Associated constants:
361    /// * DUAL_BUTTON_V2_BRICKLET_LED_LEFT
362    ///	* DUAL_BUTTON_V2_BRICKLET_LED_RIGHT
363    ///	* DUAL_BUTTON_V2_BRICKLET_LED_STATE_AUTO_TOGGLE_ON
364    ///	* DUAL_BUTTON_V2_BRICKLET_LED_STATE_AUTO_TOGGLE_OFF
365    ///	* DUAL_BUTTON_V2_BRICKLET_LED_STATE_ON
366    ///	* DUAL_BUTTON_V2_BRICKLET_LED_STATE_OFF
367    pub async fn set_selected_led_state(&mut self, led: u8, state: u8) -> Result<(), TinkerforgeError> {
368        let mut payload = [0; 2];
369        led.write_to_slice(&mut payload[0..1]);
370        state.write_to_slice(&mut payload[1..2]);
371
372        #[allow(unused_variables)]
373        let result = self.device.set(u8::from(DualButtonV2BrickletFunction::SetSelectedLedState), &payload).await?;
374        Ok(())
375    }
376
377    /// If you enable this receiver, the [`get_state_changed_callback_receiver`] receiver is triggered
378    /// every time a button is pressed/released
379    pub async fn set_state_changed_callback_configuration(&mut self, enabled: bool) -> Result<(), TinkerforgeError> {
380        let mut payload = [0; 1];
381        enabled.write_to_slice(&mut payload[0..1]);
382
383        #[allow(unused_variables)]
384        let result = self.device.set(u8::from(DualButtonV2BrickletFunction::SetStateChangedCallbackConfiguration), &payload).await?;
385        Ok(())
386    }
387
388    /// Returns the configuration as set by [`set_state_changed_callback_configuration`].
389    pub async fn get_state_changed_callback_configuration(&mut self) -> Result<bool, TinkerforgeError> {
390        let payload = [0; 0];
391
392        #[allow(unused_variables)]
393        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::GetStateChangedCallbackConfiguration), &payload).await?;
394        Ok(bool::from_le_byte_slice(result.body()))
395    }
396
397    /// Returns the error count for the communication between Brick and Bricklet.
398    ///
399    /// The errors are divided into
400    ///
401    /// * ACK checksum errors,
402    /// * message checksum errors,
403    /// * framing errors and
404    /// * overflow errors.
405    ///
406    /// The errors counts are for errors that occur on the Bricklet side. All
407    /// Bricks have a similar function that returns the errors on the Brick side.
408    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
409        let payload = [0; 0];
410
411        #[allow(unused_variables)]
412        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
413        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
414    }
415
416    /// Sets the bootloader mode and returns the status after the requested
417    /// mode change was instigated.
418    ///
419    /// You can change from bootloader mode to firmware mode and vice versa. A change
420    /// from bootloader mode to firmware mode will only take place if the entry function,
421    /// device identifier and CRC are present and correct.
422    ///
423    /// This function is used by Brick Viewer during flashing. It should not be
424    /// necessary to call it in a normal user program.
425    ///
426    /// Associated constants:
427    /// * DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
428    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
429    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
430    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
431    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
432    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_OK
433    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
434    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
435    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
436    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
437    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
438    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
439        let mut payload = [0; 1];
440        mode.write_to_slice(&mut payload[0..1]);
441
442        #[allow(unused_variables)]
443        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::SetBootloaderMode), &payload).await?;
444        Ok(u8::from_le_byte_slice(result.body()))
445    }
446
447    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
448    ///
449    /// Associated constants:
450    /// * DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
451    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
452    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
453    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
454    ///	* DUAL_BUTTON_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
455    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
456        let payload = [0; 0];
457
458        #[allow(unused_variables)]
459        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::GetBootloaderMode), &payload).await?;
460        Ok(u8::from_le_byte_slice(result.body()))
461    }
462
463    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
464    /// to be increased by chunks of size 64. The data is written to flash
465    /// every 4 chunks (which equals to one page of size 256).
466    ///
467    /// This function is used by Brick Viewer during flashing. It should not be
468    /// necessary to call it in a normal user program.
469    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
470        let mut payload = [0; 4];
471        pointer.write_to_slice(&mut payload[0..4]);
472
473        #[allow(unused_variables)]
474        let result = self.device.set(u8::from(DualButtonV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
475        Ok(())
476    }
477
478    /// Writes 64 Bytes of firmware at the position as written by
479    /// [`set_write_firmware_pointer`] before. The firmware is written
480    /// to flash every 4 chunks.
481    ///
482    /// You can only write firmware in bootloader mode.
483    ///
484    /// This function is used by Brick Viewer during flashing. It should not be
485    /// necessary to call it in a normal user program.
486    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
487        let mut payload = [0; 64];
488        data.write_to_slice(&mut payload[0..64]);
489
490        #[allow(unused_variables)]
491        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::WriteFirmware), &payload).await?;
492        Ok(u8::from_le_byte_slice(result.body()))
493    }
494
495    /// Sets the status LED configuration. By default the LED shows
496    /// communication traffic between Brick and Bricklet, it flickers once
497    /// for every 10 received data packets.
498    ///
499    /// You can also turn the LED permanently on/off or show a heartbeat.
500    ///
501    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
502    ///
503    /// Associated constants:
504    /// * DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_OFF
505    ///	* DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_ON
506    ///	* DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
507    ///	* DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
508    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
509        let mut payload = [0; 1];
510        config.write_to_slice(&mut payload[0..1]);
511
512        #[allow(unused_variables)]
513        let result = self.device.set(u8::from(DualButtonV2BrickletFunction::SetStatusLedConfig), &payload).await?;
514        Ok(())
515    }
516
517    /// Returns the configuration as set by [`set_status_led_config`]
518    ///
519    /// Associated constants:
520    /// * DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_OFF
521    ///	* DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_ON
522    ///	* DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
523    ///	* DUAL_BUTTON_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
524    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
525        let payload = [0; 0];
526
527        #[allow(unused_variables)]
528        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::GetStatusLedConfig), &payload).await?;
529        Ok(u8::from_le_byte_slice(result.body()))
530    }
531
532    /// Returns the temperature as measured inside the microcontroller. The
533    /// value returned is not the ambient temperature!
534    ///
535    /// The temperature is only proportional to the real temperature and it has bad
536    /// accuracy. Practically it is only useful as an indicator for
537    /// temperature changes.
538    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
539        let payload = [0; 0];
540
541        #[allow(unused_variables)]
542        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::GetChipTemperature), &payload).await?;
543        Ok(i16::from_le_byte_slice(result.body()))
544    }
545
546    /// Calling this function will reset the Bricklet. All configurations
547    /// will be lost.
548    ///
549    /// After a reset you have to create new device objects,
550    /// calling functions on the existing ones will result in
551    /// undefined behavior!
552    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
553        let payload = [0; 0];
554
555        #[allow(unused_variables)]
556        let result = self.device.set(u8::from(DualButtonV2BrickletFunction::Reset), &payload).await?;
557        Ok(())
558    }
559
560    /// Writes a new UID into flash. If you want to set a new UID
561    /// you have to decode the Base58 encoded UID string into an
562    /// integer first.
563    ///
564    /// We recommend that you use Brick Viewer to change the UID.
565    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
566        let mut payload = [0; 4];
567        uid.write_to_slice(&mut payload[0..4]);
568
569        #[allow(unused_variables)]
570        let result = self.device.set(u8::from(DualButtonV2BrickletFunction::WriteUid), &payload).await?;
571        Ok(())
572    }
573
574    /// Returns the current UID as an integer. Encode as
575    /// Base58 to get the usual string version.
576    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
577        let payload = [0; 0];
578
579        #[allow(unused_variables)]
580        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::ReadUid), &payload).await?;
581        Ok(u32::from_le_byte_slice(result.body()))
582    }
583
584    /// Returns the UID, the UID where the Bricklet is connected to,
585    /// the position, the hardware and firmware version as well as the
586    /// device identifier.
587    ///
588    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
589    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
590    /// position 'z'.
591    ///
592    /// The device identifier numbers can be found [here](device_identifier).
593    /// |device_identifier_constant|
594    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
595        let payload = [0; 0];
596
597        #[allow(unused_variables)]
598        let result = self.device.get(u8::from(DualButtonV2BrickletFunction::GetIdentity), &payload).await?;
599        Ok(Identity::from_le_byte_slice(result.body()))
600    }
601}