tinkerforge_async/bindings/
rgb_led_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//! Controls one RGB LED.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/RGBLEDV2_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 RgbLedV2BrickletFunction {
24    SetRgbValue,
25    GetRgbValue,
26    GetSpitfpErrorCount,
27    SetBootloaderMode,
28    GetBootloaderMode,
29    SetWriteFirmwarePointer,
30    WriteFirmware,
31    SetStatusLedConfig,
32    GetStatusLedConfig,
33    GetChipTemperature,
34    Reset,
35    WriteUid,
36    ReadUid,
37    GetIdentity,
38}
39impl From<RgbLedV2BrickletFunction> for u8 {
40    fn from(fun: RgbLedV2BrickletFunction) -> Self {
41        match fun {
42            RgbLedV2BrickletFunction::SetRgbValue => 1,
43            RgbLedV2BrickletFunction::GetRgbValue => 2,
44            RgbLedV2BrickletFunction::GetSpitfpErrorCount => 234,
45            RgbLedV2BrickletFunction::SetBootloaderMode => 235,
46            RgbLedV2BrickletFunction::GetBootloaderMode => 236,
47            RgbLedV2BrickletFunction::SetWriteFirmwarePointer => 237,
48            RgbLedV2BrickletFunction::WriteFirmware => 238,
49            RgbLedV2BrickletFunction::SetStatusLedConfig => 239,
50            RgbLedV2BrickletFunction::GetStatusLedConfig => 240,
51            RgbLedV2BrickletFunction::GetChipTemperature => 242,
52            RgbLedV2BrickletFunction::Reset => 243,
53            RgbLedV2BrickletFunction::WriteUid => 248,
54            RgbLedV2BrickletFunction::ReadUid => 249,
55            RgbLedV2BrickletFunction::GetIdentity => 255,
56        }
57    }
58}
59pub const RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
60pub const RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
61pub const RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
62pub const RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
63pub const RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
64pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
65pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
66pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
67pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
68pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
69pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
70pub const RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
71pub const RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
72pub const RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
73pub const RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
74
75#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
76pub struct RgbValue {
77    pub r: u8,
78    pub g: u8,
79    pub b: u8,
80}
81impl FromByteSlice for RgbValue {
82    fn bytes_expected() -> usize {
83        3
84    }
85    fn from_le_byte_slice(bytes: &[u8]) -> RgbValue {
86        RgbValue {
87            r: <u8>::from_le_byte_slice(&bytes[0..1]),
88            g: <u8>::from_le_byte_slice(&bytes[1..2]),
89            b: <u8>::from_le_byte_slice(&bytes[2..3]),
90        }
91    }
92}
93
94#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
95pub struct SpitfpErrorCount {
96    pub error_count_ack_checksum: u32,
97    pub error_count_message_checksum: u32,
98    pub error_count_frame: u32,
99    pub error_count_overflow: u32,
100}
101impl FromByteSlice for SpitfpErrorCount {
102    fn bytes_expected() -> usize {
103        16
104    }
105    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
106        SpitfpErrorCount {
107            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
108            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
109            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
110            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
111        }
112    }
113}
114
115#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
116pub struct Identity {
117    pub uid: String,
118    pub connected_uid: String,
119    pub position: char,
120    pub hardware_version: [u8; 3],
121    pub firmware_version: [u8; 3],
122    pub device_identifier: u16,
123}
124impl FromByteSlice for Identity {
125    fn bytes_expected() -> usize {
126        25
127    }
128    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
129        Identity {
130            uid: <String>::from_le_byte_slice(&bytes[0..8]),
131            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
132            position: <char>::from_le_byte_slice(&bytes[16..17]),
133            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
134            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
135            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
136        }
137    }
138}
139
140/// Controls one RGB LED
141#[derive(Clone)]
142pub struct RgbLedV2Bricklet {
143    device: Device,
144}
145impl RgbLedV2Bricklet {
146    pub const DEVICE_IDENTIFIER: u16 = 2127;
147    pub const DEVICE_DISPLAY_NAME: &'static str = "RGB LED Bricklet 2.0";
148    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
149    pub fn new(uid: Uid, connection: AsyncIpConnection) -> RgbLedV2Bricklet {
150        let mut result = RgbLedV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
151        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::SetRgbValue) as usize] = ResponseExpectedFlag::False;
152        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetRgbValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
153        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetSpitfpErrorCount) as usize] =
154            ResponseExpectedFlag::AlwaysTrue;
155        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
156        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
157        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
158        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
159        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
160        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
161        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
162        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
163        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
164        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
165        result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
166        result
167    }
168
169    /// Returns the response expected flag for the function specified by the function ID parameter.
170    /// It is true if the function is expected to send a response, false otherwise.
171    ///
172    /// For getter functions this is enabled by default and cannot be disabled, because those
173    /// functions will always send a response. For callback configuration functions it is enabled
174    /// by default too, but can be disabled by [`set_response_expected`](crate::rgb_led_v2_bricklet::RgbLedV2Bricklet::set_response_expected).
175    /// For setter functions it is disabled by default and can be enabled.
176    ///
177    /// Enabling the response expected flag for a setter function allows to detect timeouts
178    /// and other error conditions calls of this setter as well. The device will then send a response
179    /// for this purpose. If this flag is disabled for a setter function then no response is sent
180    /// and errors are silently ignored, because they cannot be detected.
181    ///
182    /// See [`set_response_expected`](crate::rgb_led_v2_bricklet::RgbLedV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
183    pub fn get_response_expected(&mut self, fun: RgbLedV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
184        self.device.get_response_expected(u8::from(fun))
185    }
186
187    /// Changes the response expected flag of the function specified by the function ID parameter.
188    /// This flag can only be changed for setter (default value: false) and callback configuration
189    /// functions (default value: true). For getter functions it is always enabled.
190    ///
191    /// Enabling the response expected flag for a setter function allows to detect timeouts and
192    /// other error conditions calls of this setter as well. The device will then send a response
193    /// for this purpose. If this flag is disabled for a setter function then no response is sent
194    /// and errors are silently ignored, because they cannot be detected.
195    pub fn set_response_expected(
196        &mut self,
197        fun: RgbLedV2BrickletFunction,
198        response_expected: bool,
199    ) -> Result<(), SetResponseExpectedError> {
200        self.device.set_response_expected(u8::from(fun), response_expected)
201    }
202
203    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
204    pub fn set_response_expected_all(&mut self, response_expected: bool) {
205        self.device.set_response_expected_all(response_expected)
206    }
207
208    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
209    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
210    pub fn get_api_version(&self) -> [u8; 3] {
211        self.device.api_version
212    }
213
214    /// Sets the *r*, *g* and *b* values for the LED.
215    pub async fn set_rgb_value(&mut self, r: u8, g: u8, b: u8) -> Result<(), TinkerforgeError> {
216        let mut payload = [0; 3];
217        r.write_to_slice(&mut payload[0..1]);
218        g.write_to_slice(&mut payload[1..2]);
219        b.write_to_slice(&mut payload[2..3]);
220
221        #[allow(unused_variables)]
222        let result = self.device.set(u8::from(RgbLedV2BrickletFunction::SetRgbValue), &payload).await?;
223        Ok(())
224    }
225
226    /// Returns the *r*, *g* and *b* values of the LED as set by [`set_rgb_value`].
227    pub async fn get_rgb_value(&mut self) -> Result<RgbValue, TinkerforgeError> {
228        let payload = [0; 0];
229
230        #[allow(unused_variables)]
231        let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetRgbValue), &payload).await?;
232        Ok(RgbValue::from_le_byte_slice(result.body()))
233    }
234
235    /// Returns the error count for the communication between Brick and Bricklet.
236    ///
237    /// The errors are divided into
238    ///
239    /// * ACK checksum errors,
240    /// * message checksum errors,
241    /// * framing errors and
242    /// * overflow errors.
243    ///
244    /// The errors counts are for errors that occur on the Bricklet side. All
245    /// Bricks have a similar function that returns the errors on the Brick side.
246    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
247        let payload = [0; 0];
248
249        #[allow(unused_variables)]
250        let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
251        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
252    }
253
254    /// Sets the bootloader mode and returns the status after the requested
255    /// mode change was instigated.
256    ///
257    /// You can change from bootloader mode to firmware mode and vice versa. A change
258    /// from bootloader mode to firmware mode will only take place if the entry function,
259    /// device identifier and CRC are present and correct.
260    ///
261    /// This function is used by Brick Viewer during flashing. It should not be
262    /// necessary to call it in a normal user program.
263    ///
264    /// Associated constants:
265    /// * RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
266    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
267    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
268    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
269    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
270    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_OK
271    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
272    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
273    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
274    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
275    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
276    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
277        let mut payload = [0; 1];
278        mode.write_to_slice(&mut payload[0..1]);
279
280        #[allow(unused_variables)]
281        let result = self.device.get(u8::from(RgbLedV2BrickletFunction::SetBootloaderMode), &payload).await?;
282        Ok(u8::from_le_byte_slice(result.body()))
283    }
284
285    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
286    ///
287    /// Associated constants:
288    /// * RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
289    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
290    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
291    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
292    ///	* RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
293    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
294        let payload = [0; 0];
295
296        #[allow(unused_variables)]
297        let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetBootloaderMode), &payload).await?;
298        Ok(u8::from_le_byte_slice(result.body()))
299    }
300
301    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
302    /// to be increased by chunks of size 64. The data is written to flash
303    /// every 4 chunks (which equals to one page of size 256).
304    ///
305    /// This function is used by Brick Viewer during flashing. It should not be
306    /// necessary to call it in a normal user program.
307    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
308        let mut payload = [0; 4];
309        pointer.write_to_slice(&mut payload[0..4]);
310
311        #[allow(unused_variables)]
312        let result = self.device.set(u8::from(RgbLedV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
313        Ok(())
314    }
315
316    /// Writes 64 Bytes of firmware at the position as written by
317    /// [`set_write_firmware_pointer`] before. The firmware is written
318    /// to flash every 4 chunks.
319    ///
320    /// You can only write firmware in bootloader mode.
321    ///
322    /// This function is used by Brick Viewer during flashing. It should not be
323    /// necessary to call it in a normal user program.
324    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
325        let mut payload = [0; 64];
326        data.write_to_slice(&mut payload[0..64]);
327
328        #[allow(unused_variables)]
329        let result = self.device.get(u8::from(RgbLedV2BrickletFunction::WriteFirmware), &payload).await?;
330        Ok(u8::from_le_byte_slice(result.body()))
331    }
332
333    /// Sets the status LED configuration. By default the LED shows
334    /// communication traffic between Brick and Bricklet, it flickers once
335    /// for every 10 received data packets.
336    ///
337    /// You can also turn the LED permanently on/off or show a heartbeat.
338    ///
339    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
340    ///
341    /// Associated constants:
342    /// * RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_OFF
343    ///	* RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_ON
344    ///	* RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
345    ///	* RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
346    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
347        let mut payload = [0; 1];
348        config.write_to_slice(&mut payload[0..1]);
349
350        #[allow(unused_variables)]
351        let result = self.device.set(u8::from(RgbLedV2BrickletFunction::SetStatusLedConfig), &payload).await?;
352        Ok(())
353    }
354
355    /// Returns the configuration as set by [`set_status_led_config`]
356    ///
357    /// Associated constants:
358    /// * RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_OFF
359    ///	* RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_ON
360    ///	* RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
361    ///	* RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
362    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
363        let payload = [0; 0];
364
365        #[allow(unused_variables)]
366        let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetStatusLedConfig), &payload).await?;
367        Ok(u8::from_le_byte_slice(result.body()))
368    }
369
370    /// Returns the temperature as measured inside the microcontroller. The
371    /// value returned is not the ambient temperature!
372    ///
373    /// The temperature is only proportional to the real temperature and it has bad
374    /// accuracy. Practically it is only useful as an indicator for
375    /// temperature changes.
376    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
377        let payload = [0; 0];
378
379        #[allow(unused_variables)]
380        let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetChipTemperature), &payload).await?;
381        Ok(i16::from_le_byte_slice(result.body()))
382    }
383
384    /// Calling this function will reset the Bricklet. All configurations
385    /// will be lost.
386    ///
387    /// After a reset you have to create new device objects,
388    /// calling functions on the existing ones will result in
389    /// undefined behavior!
390    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
391        let payload = [0; 0];
392
393        #[allow(unused_variables)]
394        let result = self.device.set(u8::from(RgbLedV2BrickletFunction::Reset), &payload).await?;
395        Ok(())
396    }
397
398    /// Writes a new UID into flash. If you want to set a new UID
399    /// you have to decode the Base58 encoded UID string into an
400    /// integer first.
401    ///
402    /// We recommend that you use Brick Viewer to change the UID.
403    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
404        let mut payload = [0; 4];
405        uid.write_to_slice(&mut payload[0..4]);
406
407        #[allow(unused_variables)]
408        let result = self.device.set(u8::from(RgbLedV2BrickletFunction::WriteUid), &payload).await?;
409        Ok(())
410    }
411
412    /// Returns the current UID as an integer. Encode as
413    /// Base58 to get the usual string version.
414    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
415        let payload = [0; 0];
416
417        #[allow(unused_variables)]
418        let result = self.device.get(u8::from(RgbLedV2BrickletFunction::ReadUid), &payload).await?;
419        Ok(u32::from_le_byte_slice(result.body()))
420    }
421
422    /// Returns the UID, the UID where the Bricklet is connected to,
423    /// the position, the hardware and firmware version as well as the
424    /// device identifier.
425    ///
426    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
427    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
428    /// position 'z'.
429    ///
430    /// The device identifier numbers can be found [here](device_identifier).
431    /// |device_identifier_constant|
432    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
433        let payload = [0; 0];
434
435        #[allow(unused_variables)]
436        let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetIdentity), &payload).await?;
437        Ok(Identity::from_le_byte_slice(result.body()))
438    }
439}