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