tinkerforge/bindings/
hat_brick.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-27.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.21                              *
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//! HAT for Raspberry Pi with 8 Bricklets ports and real-time clock.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricks/HAT_Brick_Rust.html).
14use crate::{
15    byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
16    ip_connection::GetRequestSender,
17};
18pub enum HatBrickFunction {
19    SetSleepMode,
20    GetSleepMode,
21    SetBrickletPower,
22    GetBrickletPower,
23    GetVoltages,
24    SetVoltagesCallbackConfiguration,
25    GetVoltagesCallbackConfiguration,
26    SetRtcDriver,
27    GetRtcDriver,
28    GetSpitfpErrorCount,
29    SetBootloaderMode,
30    GetBootloaderMode,
31    SetWriteFirmwarePointer,
32    WriteFirmware,
33    SetStatusLedConfig,
34    GetStatusLedConfig,
35    GetChipTemperature,
36    Reset,
37    WriteUid,
38    ReadUid,
39    GetIdentity,
40    CallbackVoltages,
41}
42impl From<HatBrickFunction> for u8 {
43    fn from(fun: HatBrickFunction) -> Self {
44        match fun {
45            HatBrickFunction::SetSleepMode => 1,
46            HatBrickFunction::GetSleepMode => 2,
47            HatBrickFunction::SetBrickletPower => 3,
48            HatBrickFunction::GetBrickletPower => 4,
49            HatBrickFunction::GetVoltages => 5,
50            HatBrickFunction::SetVoltagesCallbackConfiguration => 6,
51            HatBrickFunction::GetVoltagesCallbackConfiguration => 7,
52            HatBrickFunction::SetRtcDriver => 9,
53            HatBrickFunction::GetRtcDriver => 10,
54            HatBrickFunction::GetSpitfpErrorCount => 234,
55            HatBrickFunction::SetBootloaderMode => 235,
56            HatBrickFunction::GetBootloaderMode => 236,
57            HatBrickFunction::SetWriteFirmwarePointer => 237,
58            HatBrickFunction::WriteFirmware => 238,
59            HatBrickFunction::SetStatusLedConfig => 239,
60            HatBrickFunction::GetStatusLedConfig => 240,
61            HatBrickFunction::GetChipTemperature => 242,
62            HatBrickFunction::Reset => 243,
63            HatBrickFunction::WriteUid => 248,
64            HatBrickFunction::ReadUid => 249,
65            HatBrickFunction::GetIdentity => 255,
66            HatBrickFunction::CallbackVoltages => 8,
67        }
68    }
69}
70pub const HAT_BRICK_RTC_DRIVER_PCF8523: u8 = 0;
71pub const HAT_BRICK_RTC_DRIVER_DS1338: u8 = 1;
72pub const HAT_BRICK_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
73pub const HAT_BRICK_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
74pub const HAT_BRICK_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
75pub const HAT_BRICK_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
76pub const HAT_BRICK_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
77pub const HAT_BRICK_BOOTLOADER_STATUS_OK: u8 = 0;
78pub const HAT_BRICK_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
79pub const HAT_BRICK_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
80pub const HAT_BRICK_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
81pub const HAT_BRICK_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
82pub const HAT_BRICK_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
83pub const HAT_BRICK_STATUS_LED_CONFIG_OFF: u8 = 0;
84pub const HAT_BRICK_STATUS_LED_CONFIG_ON: u8 = 1;
85pub const HAT_BRICK_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
86pub const HAT_BRICK_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
87
88#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
89pub struct SleepMode {
90    pub power_off_delay: u32,
91    pub power_off_duration: u32,
92    pub raspberry_pi_off: bool,
93    pub bricklets_off: bool,
94    pub enable_sleep_indicator: bool,
95}
96impl FromByteSlice for SleepMode {
97    fn bytes_expected() -> usize { 11 }
98    fn from_le_byte_slice(bytes: &[u8]) -> SleepMode {
99        SleepMode {
100            power_off_delay: <u32>::from_le_byte_slice(&bytes[0..4]),
101            power_off_duration: <u32>::from_le_byte_slice(&bytes[4..8]),
102            raspberry_pi_off: <bool>::from_le_byte_slice(&bytes[8..9]),
103            bricklets_off: <bool>::from_le_byte_slice(&bytes[9..10]),
104            enable_sleep_indicator: <bool>::from_le_byte_slice(&bytes[10..11]),
105        }
106    }
107}
108
109#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
110pub struct Voltages {
111    pub voltage_usb: u16,
112    pub voltage_dc: u16,
113}
114impl FromByteSlice for Voltages {
115    fn bytes_expected() -> usize { 4 }
116    fn from_le_byte_slice(bytes: &[u8]) -> Voltages {
117        Voltages { voltage_usb: <u16>::from_le_byte_slice(&bytes[0..2]), voltage_dc: <u16>::from_le_byte_slice(&bytes[2..4]) }
118    }
119}
120
121#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
122pub struct VoltagesCallbackConfiguration {
123    pub period: u32,
124    pub value_has_to_change: bool,
125}
126impl FromByteSlice for VoltagesCallbackConfiguration {
127    fn bytes_expected() -> usize { 5 }
128    fn from_le_byte_slice(bytes: &[u8]) -> VoltagesCallbackConfiguration {
129        VoltagesCallbackConfiguration {
130            period: <u32>::from_le_byte_slice(&bytes[0..4]),
131            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
132        }
133    }
134}
135
136#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
137pub struct VoltagesEvent {
138    pub voltage_usb: u16,
139    pub voltage_dc: u16,
140}
141impl FromByteSlice for VoltagesEvent {
142    fn bytes_expected() -> usize { 4 }
143    fn from_le_byte_slice(bytes: &[u8]) -> VoltagesEvent {
144        VoltagesEvent { voltage_usb: <u16>::from_le_byte_slice(&bytes[0..2]), voltage_dc: <u16>::from_le_byte_slice(&bytes[2..4]) }
145    }
146}
147
148#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
149pub struct SpitfpErrorCount {
150    pub error_count_ack_checksum: u32,
151    pub error_count_message_checksum: u32,
152    pub error_count_frame: u32,
153    pub error_count_overflow: u32,
154}
155impl FromByteSlice for SpitfpErrorCount {
156    fn bytes_expected() -> usize { 16 }
157    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
158        SpitfpErrorCount {
159            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
160            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
161            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
162            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
163        }
164    }
165}
166
167#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
168pub struct Identity {
169    pub uid: String,
170    pub connected_uid: String,
171    pub position: char,
172    pub hardware_version: [u8; 3],
173    pub firmware_version: [u8; 3],
174    pub device_identifier: u16,
175}
176impl FromByteSlice for Identity {
177    fn bytes_expected() -> usize { 25 }
178    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
179        Identity {
180            uid: <String>::from_le_byte_slice(&bytes[0..8]),
181            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
182            position: <char>::from_le_byte_slice(&bytes[16..17]),
183            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
184            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
185            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
186        }
187    }
188}
189
190/// HAT for Raspberry Pi with 8 Bricklets ports and real-time clock
191#[derive(Clone)]
192pub struct HatBrick {
193    device: Device,
194}
195impl HatBrick {
196    pub const DEVICE_IDENTIFIER: u16 = 111;
197    pub const DEVICE_DISPLAY_NAME: &'static str = "HAT Brick";
198    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
199    pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> HatBrick {
200        let mut result = HatBrick { device: Device::new([2, 0, 2], uid, req_sender, 0) };
201        result.device.response_expected[u8::from(HatBrickFunction::SetSleepMode) as usize] = ResponseExpectedFlag::False;
202        result.device.response_expected[u8::from(HatBrickFunction::GetSleepMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
203        result.device.response_expected[u8::from(HatBrickFunction::SetBrickletPower) as usize] = ResponseExpectedFlag::False;
204        result.device.response_expected[u8::from(HatBrickFunction::GetBrickletPower) as usize] = ResponseExpectedFlag::AlwaysTrue;
205        result.device.response_expected[u8::from(HatBrickFunction::GetVoltages) as usize] = ResponseExpectedFlag::AlwaysTrue;
206        result.device.response_expected[u8::from(HatBrickFunction::SetVoltagesCallbackConfiguration) as usize] = ResponseExpectedFlag::True;
207        result.device.response_expected[u8::from(HatBrickFunction::GetVoltagesCallbackConfiguration) as usize] =
208            ResponseExpectedFlag::AlwaysTrue;
209        result.device.response_expected[u8::from(HatBrickFunction::SetRtcDriver) as usize] = ResponseExpectedFlag::False;
210        result.device.response_expected[u8::from(HatBrickFunction::GetRtcDriver) as usize] = ResponseExpectedFlag::AlwaysTrue;
211        result.device.response_expected[u8::from(HatBrickFunction::GetSpitfpErrorCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
212        result.device.response_expected[u8::from(HatBrickFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
213        result.device.response_expected[u8::from(HatBrickFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
214        result.device.response_expected[u8::from(HatBrickFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
215        result.device.response_expected[u8::from(HatBrickFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
216        result.device.response_expected[u8::from(HatBrickFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
217        result.device.response_expected[u8::from(HatBrickFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
218        result.device.response_expected[u8::from(HatBrickFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
219        result.device.response_expected[u8::from(HatBrickFunction::Reset) as usize] = ResponseExpectedFlag::False;
220        result.device.response_expected[u8::from(HatBrickFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
221        result.device.response_expected[u8::from(HatBrickFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
222        result.device.response_expected[u8::from(HatBrickFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
223        result
224    }
225
226    /// Returns the response expected flag for the function specified by the function ID parameter.
227    /// It is true if the function is expected to send a response, false otherwise.
228    ///
229    /// For getter functions this is enabled by default and cannot be disabled, because those
230    /// functions will always send a response. For callback configuration functions it is enabled
231    /// by default too, but can be disabled by [`set_response_expected`](crate::hat_brick::HatBrick::set_response_expected).
232    /// For setter functions it is disabled by default and can be enabled.
233    ///
234    /// Enabling the response expected flag for a setter function allows to detect timeouts
235    /// and other error conditions calls of this setter as well. The device will then send a response
236    /// for this purpose. If this flag is disabled for a setter function then no response is sent
237    /// and errors are silently ignored, because they cannot be detected.
238    ///
239    /// See [`set_response_expected`](crate::hat_brick::HatBrick::set_response_expected) for the list of function ID constants available for this function.
240    pub fn get_response_expected(&mut self, fun: HatBrickFunction) -> Result<bool, GetResponseExpectedError> {
241        self.device.get_response_expected(u8::from(fun))
242    }
243
244    /// Changes the response expected flag of the function specified by the function ID parameter.
245    /// This flag can only be changed for setter (default value: false) and callback configuration
246    /// functions (default value: true). For getter functions it is always enabled.
247    ///
248    /// Enabling the response expected flag for a setter function allows to detect timeouts and
249    /// 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    pub fn set_response_expected(&mut self, fun: HatBrickFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
253        self.device.set_response_expected(u8::from(fun), response_expected)
254    }
255
256    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
257    pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
258
259    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
260    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
261    pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
262
263    /// This receiver is triggered periodically according to the configuration set by
264    /// [`set_voltages_callback_configuration`].
265    ///
266    /// The parameters are the same as [`get_voltages`].
267    ///
268    /// [`get_voltages`]: #method.get_voltages
269    /// [`set_voltages_callback_configuration`]: #method.set_voltages_callback_configuration
270    /// .. versionadded:: 2.0.1$nbsp;(Firmware)
271    pub fn get_voltages_callback_receiver(&self) -> ConvertingCallbackReceiver<VoltagesEvent> {
272        self.device.get_callback_receiver(u8::from(HatBrickFunction::CallbackVoltages))
273    }
274
275    /// Sets the sleep mode.
276    ///
277    /// # Note
278    ///  Calling this function will cut the Raspberry Pi's power after Power Off Delay seconds.
279    ///  You have to shut down the operating system yourself, e.g. by calling 'sudo shutdown -h now'.
280    ///
281    /// Parameters:
282    ///
283    /// * Power Off Delay: Time before the RPi/Bricklets are powered off.
284    /// * Power Off Duration: Duration that the RPi/Bricklets stay powered off.
285    /// * Raspberry Pi Off: RPi is powered off if set to true.
286    /// * Bricklets Off: Bricklets are powered off if set to true.
287    /// * Enable Sleep Indicator: If set to true, the status LED will blink in a 1s interval
288    ///   during the whole power off duration. This will draw additional 0.3mA.
289    ///
290    /// Example: To turn RPi and Bricklets off in 5 seconds for 10 minutes with sleep
291    /// indicator enabled, call (5, 60*10, true, true, true).
292    ///
293    /// This function can also be used to implement a watchdog. To do this you can
294    /// write a program that calls this function once per second in a loop with
295    /// (10, 2, true, false, false). If the RPi crashes or gets stuck
296    /// the HAT will reset the RPi after 10 seconds.
297    pub fn set_sleep_mode(
298        &self,
299        power_off_delay: u32,
300        power_off_duration: u32,
301        raspberry_pi_off: bool,
302        bricklets_off: bool,
303        enable_sleep_indicator: bool,
304    ) -> ConvertingReceiver<()> {
305        let mut payload = vec![0; 11];
306        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(power_off_delay));
307        payload[4..8].copy_from_slice(&<u32>::to_le_byte_vec(power_off_duration));
308        payload[8..9].copy_from_slice(&<bool>::to_le_byte_vec(raspberry_pi_off));
309        payload[9..10].copy_from_slice(&<bool>::to_le_byte_vec(bricklets_off));
310        payload[10..11].copy_from_slice(&<bool>::to_le_byte_vec(enable_sleep_indicator));
311
312        self.device.set(u8::from(HatBrickFunction::SetSleepMode), payload)
313    }
314
315    /// Returns the sleep mode settings as set by [`set_sleep_mode`].
316    pub fn get_sleep_mode(&self) -> ConvertingReceiver<SleepMode> {
317        let payload = vec![0; 0];
318
319        self.device.get(u8::from(HatBrickFunction::GetSleepMode), payload)
320    }
321
322    /// Set to true/false to turn the power supply of the connected Bricklets on/off.
323    pub fn set_bricklet_power(&self, bricklet_power: bool) -> ConvertingReceiver<()> {
324        let mut payload = vec![0; 1];
325        payload[0..1].copy_from_slice(&<bool>::to_le_byte_vec(bricklet_power));
326
327        self.device.set(u8::from(HatBrickFunction::SetBrickletPower), payload)
328    }
329
330    /// Returns the power status of the connected Bricklets as set by [`set_bricklet_power`].
331    pub fn get_bricklet_power(&self) -> ConvertingReceiver<bool> {
332        let payload = vec![0; 0];
333
334        self.device.get(u8::from(HatBrickFunction::GetBrickletPower), payload)
335    }
336
337    /// Returns the USB supply voltage and the DC input supply voltage.
338    ///
339    /// There are three possible combinations:
340    ///
341    /// * Only USB connected: The USB supply voltage will be fed back to the
342    ///   DC input connector. You will read the USB voltage and a slightly lower
343    ///   voltage on the DC input.
344    /// * Only DC input connected: The DC voltage will not be fed back to the
345    ///   USB connector. You will read the DC input voltage and the USB voltage
346    ///   will be 0.
347    /// * USB and DC input connected: You will read both voltages. In this case
348    ///   the USB supply will be without load, but it will work as backup if you
349    ///   disconnect the DC input (or if the DC input voltage falls below the
350    ///   USB voltage).
351    pub fn get_voltages(&self) -> ConvertingReceiver<Voltages> {
352        let payload = vec![0; 0];
353
354        self.device.get(u8::from(HatBrickFunction::GetVoltages), payload)
355    }
356
357    /// The period is the period with which the [`get_voltages_callback_receiver`]
358    /// receiver is triggered periodically. A value of 0 turns the receiver off.
359    ///
360    /// If the `value has to change`-parameter is set to true, the receiver is only
361    /// triggered after the value has changed. If the value didn't change within the
362    /// period, the receiver is triggered immediately on change.
363    ///
364    /// If it is set to false, the receiver is continuously triggered with the period,
365    /// independent of the value.
366    ///
367    ///
368    /// .. versionadded:: 2.0.1$nbsp;(Firmware)
369    pub fn set_voltages_callback_configuration(&self, period: u32, value_has_to_change: bool) -> ConvertingReceiver<()> {
370        let mut payload = vec![0; 5];
371        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
372        payload[4..5].copy_from_slice(&<bool>::to_le_byte_vec(value_has_to_change));
373
374        self.device.set(u8::from(HatBrickFunction::SetVoltagesCallbackConfiguration), payload)
375    }
376
377    /// Returns the receiver configuration as set by
378    /// [`set_voltages_callback_configuration`].
379    ///
380    ///
381    /// .. versionadded:: 2.0.1$nbsp;(Firmware)
382    pub fn get_voltages_callback_configuration(&self) -> ConvertingReceiver<VoltagesCallbackConfiguration> {
383        let payload = vec![0; 0];
384
385        self.device.get(u8::from(HatBrickFunction::GetVoltagesCallbackConfiguration), payload)
386    }
387
388    /// Configures the RTC driver that is given to the Raspberry Pi to be used.
389    /// Currently there are two different RTCs used:
390    ///
391    /// * Hardware version <= 1.5: PCF8523
392    /// * Hardware version 1.6: DS1338
393    ///
394    /// The correct driver will be set during factory flashing by Tinkerforge.
395    ///
396    ///
397    /// .. versionadded:: 2.0.3$nbsp;(Firmware)
398    ///
399    /// Associated constants:
400    /// * HAT_BRICK_RTC_DRIVER_PCF8523
401    ///	* HAT_BRICK_RTC_DRIVER_DS1338
402    pub fn set_rtc_driver(&self, rtc_driver: u8) -> ConvertingReceiver<()> {
403        let mut payload = vec![0; 1];
404        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(rtc_driver));
405
406        self.device.set(u8::from(HatBrickFunction::SetRtcDriver), payload)
407    }
408
409    /// Returns the RTC driver as set by [`set_rtc_driver`].
410    ///
411    ///
412    /// .. versionadded:: 2.0.3$nbsp;(Firmware)
413    ///
414    /// Associated constants:
415    /// * HAT_BRICK_RTC_DRIVER_PCF8523
416    ///	* HAT_BRICK_RTC_DRIVER_DS1338
417    pub fn get_rtc_driver(&self) -> ConvertingReceiver<u8> {
418        let payload = vec![0; 0];
419
420        self.device.get(u8::from(HatBrickFunction::GetRtcDriver), payload)
421    }
422
423    /// Returns the error count for the communication between Brick and Bricklet.
424    ///
425    /// The errors are divided into
426    ///
427    /// * ACK checksum errors,
428    /// * message checksum errors,
429    /// * framing errors and
430    /// * overflow errors.
431    ///
432    /// The errors counts are for errors that occur on the Bricklet side. All
433    /// Bricks have a similar function that returns the errors on the Brick side.
434    pub fn get_spitfp_error_count(&self) -> ConvertingReceiver<SpitfpErrorCount> {
435        let payload = vec![0; 0];
436
437        self.device.get(u8::from(HatBrickFunction::GetSpitfpErrorCount), payload)
438    }
439
440    /// Sets the bootloader mode and returns the status after the requested
441    /// mode change was instigated.
442    ///
443    /// You can change from bootloader mode to firmware mode and vice versa. A change
444    /// from bootloader mode to firmware mode will only take place if the entry function,
445    /// device identifier and CRC are present and correct.
446    ///
447    /// This function is used by Brick Viewer during flashing. It should not be
448    /// necessary to call it in a normal user program.
449    ///
450    /// Associated constants:
451    /// * HAT_BRICK_BOOTLOADER_MODE_BOOTLOADER
452    ///	* HAT_BRICK_BOOTLOADER_MODE_FIRMWARE
453    ///	* HAT_BRICK_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
454    ///	* HAT_BRICK_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
455    ///	* HAT_BRICK_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
456    ///	* HAT_BRICK_BOOTLOADER_STATUS_OK
457    ///	* HAT_BRICK_BOOTLOADER_STATUS_INVALID_MODE
458    ///	* HAT_BRICK_BOOTLOADER_STATUS_NO_CHANGE
459    ///	* HAT_BRICK_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
460    ///	* HAT_BRICK_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
461    ///	* HAT_BRICK_BOOTLOADER_STATUS_CRC_MISMATCH
462    pub fn set_bootloader_mode(&self, mode: u8) -> ConvertingReceiver<u8> {
463        let mut payload = vec![0; 1];
464        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(mode));
465
466        self.device.get(u8::from(HatBrickFunction::SetBootloaderMode), payload)
467    }
468
469    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
470    ///
471    /// Associated constants:
472    /// * HAT_BRICK_BOOTLOADER_MODE_BOOTLOADER
473    ///	* HAT_BRICK_BOOTLOADER_MODE_FIRMWARE
474    ///	* HAT_BRICK_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
475    ///	* HAT_BRICK_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
476    ///	* HAT_BRICK_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
477    pub fn get_bootloader_mode(&self) -> ConvertingReceiver<u8> {
478        let payload = vec![0; 0];
479
480        self.device.get(u8::from(HatBrickFunction::GetBootloaderMode), payload)
481    }
482
483    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
484    /// to be increased by chunks of size 64. The data is written to flash
485    /// every 4 chunks (which equals to one page of size 256).
486    ///
487    /// This function is used by Brick Viewer during flashing. It should not be
488    /// necessary to call it in a normal user program.
489    pub fn set_write_firmware_pointer(&self, pointer: u32) -> ConvertingReceiver<()> {
490        let mut payload = vec![0; 4];
491        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(pointer));
492
493        self.device.set(u8::from(HatBrickFunction::SetWriteFirmwarePointer), payload)
494    }
495
496    /// Writes 64 Bytes of firmware at the position as written by
497    /// [`set_write_firmware_pointer`] before. The firmware is written
498    /// to flash every 4 chunks.
499    ///
500    /// You can only write firmware in bootloader mode.
501    ///
502    /// This function is used by Brick Viewer during flashing. It should not be
503    /// necessary to call it in a normal user program.
504    pub fn write_firmware(&self, data: [u8; 64]) -> ConvertingReceiver<u8> {
505        let mut payload = vec![0; 64];
506        payload[0..64].copy_from_slice(&<[u8; 64]>::to_le_byte_vec(data));
507
508        self.device.get(u8::from(HatBrickFunction::WriteFirmware), payload)
509    }
510
511    /// Sets the status LED configuration. By default the LED shows
512    /// communication traffic between Brick and Bricklet, it flickers once
513    /// for every 10 received data packets.
514    ///
515    /// You can also turn the LED permanently on/off or show a heartbeat.
516    ///
517    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
518    ///
519    /// Associated constants:
520    /// * HAT_BRICK_STATUS_LED_CONFIG_OFF
521    ///	* HAT_BRICK_STATUS_LED_CONFIG_ON
522    ///	* HAT_BRICK_STATUS_LED_CONFIG_SHOW_HEARTBEAT
523    ///	* HAT_BRICK_STATUS_LED_CONFIG_SHOW_STATUS
524    pub fn set_status_led_config(&self, config: u8) -> ConvertingReceiver<()> {
525        let mut payload = vec![0; 1];
526        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(config));
527
528        self.device.set(u8::from(HatBrickFunction::SetStatusLedConfig), payload)
529    }
530
531    /// Returns the configuration as set by [`set_status_led_config`]
532    ///
533    /// Associated constants:
534    /// * HAT_BRICK_STATUS_LED_CONFIG_OFF
535    ///	* HAT_BRICK_STATUS_LED_CONFIG_ON
536    ///	* HAT_BRICK_STATUS_LED_CONFIG_SHOW_HEARTBEAT
537    ///	* HAT_BRICK_STATUS_LED_CONFIG_SHOW_STATUS
538    pub fn get_status_led_config(&self) -> ConvertingReceiver<u8> {
539        let payload = vec![0; 0];
540
541        self.device.get(u8::from(HatBrickFunction::GetStatusLedConfig), payload)
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 fn get_chip_temperature(&self) -> ConvertingReceiver<i16> {
551        let payload = vec![0; 0];
552
553        self.device.get(u8::from(HatBrickFunction::GetChipTemperature), payload)
554    }
555
556    /// Calling this function will reset the Bricklet. All configurations
557    /// will be lost.
558    ///
559    /// After a reset you have to create new device objects,
560    /// calling functions on the existing ones will result in
561    /// undefined behavior!
562    pub fn reset(&self) -> ConvertingReceiver<()> {
563        let payload = vec![0; 0];
564
565        self.device.set(u8::from(HatBrickFunction::Reset), payload)
566    }
567
568    /// Writes a new UID into flash. If you want to set a new UID
569    /// you have to decode the Base58 encoded UID string into an
570    /// integer first.
571    ///
572    /// We recommend that you use Brick Viewer to change the UID.
573    pub fn write_uid(&self, uid: u32) -> ConvertingReceiver<()> {
574        let mut payload = vec![0; 4];
575        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(uid));
576
577        self.device.set(u8::from(HatBrickFunction::WriteUid), payload)
578    }
579
580    /// Returns the current UID as an integer. Encode as
581    /// Base58 to get the usual string version.
582    pub fn read_uid(&self) -> ConvertingReceiver<u32> {
583        let payload = vec![0; 0];
584
585        self.device.get(u8::from(HatBrickFunction::ReadUid), payload)
586    }
587
588    /// Returns the UID, the UID where the HAT is connected to
589    /// (typically '0' as the HAT is the root device in the topology),
590    /// the position, the hardware and firmware version as well as the
591    /// device identifier.
592    ///
593    /// The HAT (Zero) Brick is always at position 'i'.
594    ///
595    /// The device identifier numbers can be found [here](device_identifier).
596    /// |device_identifier_constant|
597    pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
598        let payload = vec![0; 0];
599
600        self.device.get(u8::from(HatBrickFunction::GetIdentity), payload)
601    }
602}