tinkerforge_async/bindings/
ambient_light_v3_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//! Measures ambient light up to 64000lux.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/AmbientLightV3_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 AmbientLightV3BrickletFunction {
24    GetIlluminance,
25    SetIlluminanceCallbackConfiguration,
26    GetIlluminanceCallbackConfiguration,
27    SetConfiguration,
28    GetConfiguration,
29    GetSpitfpErrorCount,
30    SetBootloaderMode,
31    GetBootloaderMode,
32    SetWriteFirmwarePointer,
33    WriteFirmware,
34    SetStatusLedConfig,
35    GetStatusLedConfig,
36    GetChipTemperature,
37    Reset,
38    WriteUid,
39    ReadUid,
40    GetIdentity,
41    CallbackIlluminance,
42}
43impl From<AmbientLightV3BrickletFunction> for u8 {
44    fn from(fun: AmbientLightV3BrickletFunction) -> Self {
45        match fun {
46            AmbientLightV3BrickletFunction::GetIlluminance => 1,
47            AmbientLightV3BrickletFunction::SetIlluminanceCallbackConfiguration => 2,
48            AmbientLightV3BrickletFunction::GetIlluminanceCallbackConfiguration => 3,
49            AmbientLightV3BrickletFunction::SetConfiguration => 5,
50            AmbientLightV3BrickletFunction::GetConfiguration => 6,
51            AmbientLightV3BrickletFunction::GetSpitfpErrorCount => 234,
52            AmbientLightV3BrickletFunction::SetBootloaderMode => 235,
53            AmbientLightV3BrickletFunction::GetBootloaderMode => 236,
54            AmbientLightV3BrickletFunction::SetWriteFirmwarePointer => 237,
55            AmbientLightV3BrickletFunction::WriteFirmware => 238,
56            AmbientLightV3BrickletFunction::SetStatusLedConfig => 239,
57            AmbientLightV3BrickletFunction::GetStatusLedConfig => 240,
58            AmbientLightV3BrickletFunction::GetChipTemperature => 242,
59            AmbientLightV3BrickletFunction::Reset => 243,
60            AmbientLightV3BrickletFunction::WriteUid => 248,
61            AmbientLightV3BrickletFunction::ReadUid => 249,
62            AmbientLightV3BrickletFunction::GetIdentity => 255,
63            AmbientLightV3BrickletFunction::CallbackIlluminance => 4,
64        }
65    }
66}
67pub const AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
68pub const AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
69pub const AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
70pub const AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
71pub const AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
72pub const AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_UNLIMITED: u8 = 6;
73pub const AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_64000LUX: u8 = 0;
74pub const AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_32000LUX: u8 = 1;
75pub const AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_16000LUX: u8 = 2;
76pub const AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_8000LUX: u8 = 3;
77pub const AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_1300LUX: u8 = 4;
78pub const AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_600LUX: u8 = 5;
79pub const AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_50MS: u8 = 0;
80pub const AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_100MS: u8 = 1;
81pub const AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_150MS: u8 = 2;
82pub const AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_200MS: u8 = 3;
83pub const AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_250MS: u8 = 4;
84pub const AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_300MS: u8 = 5;
85pub const AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_350MS: u8 = 6;
86pub const AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_400MS: u8 = 7;
87pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
88pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
89pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
90pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
91pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
92pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
93pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
94pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
95pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
96pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
97pub const AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
98pub const AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
99pub const AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
100pub const AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
101pub const AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
102
103#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
104pub struct IlluminanceCallbackConfiguration {
105    pub period: u32,
106    pub value_has_to_change: bool,
107    pub option: char,
108    pub min: u32,
109    pub max: u32,
110}
111impl FromByteSlice for IlluminanceCallbackConfiguration {
112    fn bytes_expected() -> usize {
113        14
114    }
115    fn from_le_byte_slice(bytes: &[u8]) -> IlluminanceCallbackConfiguration {
116        IlluminanceCallbackConfiguration {
117            period: <u32>::from_le_byte_slice(&bytes[0..4]),
118            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
119            option: <char>::from_le_byte_slice(&bytes[5..6]),
120            min: <u32>::from_le_byte_slice(&bytes[6..10]),
121            max: <u32>::from_le_byte_slice(&bytes[10..14]),
122        }
123    }
124}
125
126#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
127pub struct Configuration {
128    pub illuminance_range: u8,
129    pub integration_time: u8,
130}
131impl FromByteSlice for Configuration {
132    fn bytes_expected() -> usize {
133        2
134    }
135    fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
136        Configuration {
137            illuminance_range: <u8>::from_le_byte_slice(&bytes[0..1]),
138            integration_time: <u8>::from_le_byte_slice(&bytes[1..2]),
139        }
140    }
141}
142
143#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
144pub struct SpitfpErrorCount {
145    pub error_count_ack_checksum: u32,
146    pub error_count_message_checksum: u32,
147    pub error_count_frame: u32,
148    pub error_count_overflow: u32,
149}
150impl FromByteSlice for SpitfpErrorCount {
151    fn bytes_expected() -> usize {
152        16
153    }
154    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
155        SpitfpErrorCount {
156            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
157            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
158            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
159            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
160        }
161    }
162}
163
164#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
165pub struct Identity {
166    pub uid: String,
167    pub connected_uid: String,
168    pub position: char,
169    pub hardware_version: [u8; 3],
170    pub firmware_version: [u8; 3],
171    pub device_identifier: u16,
172}
173impl FromByteSlice for Identity {
174    fn bytes_expected() -> usize {
175        25
176    }
177    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
178        Identity {
179            uid: <String>::from_le_byte_slice(&bytes[0..8]),
180            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
181            position: <char>::from_le_byte_slice(&bytes[16..17]),
182            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
183            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
184            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
185        }
186    }
187}
188
189/// Measures ambient light up to 64000lux
190#[derive(Clone)]
191pub struct AmbientLightV3Bricklet {
192    device: Device,
193}
194impl AmbientLightV3Bricklet {
195    pub const DEVICE_IDENTIFIER: u16 = 2131;
196    pub const DEVICE_DISPLAY_NAME: &'static str = "Ambient Light Bricklet 3.0";
197    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
198    pub fn new(uid: Uid, connection: AsyncIpConnection) -> AmbientLightV3Bricklet {
199        let mut result = AmbientLightV3Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
200        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::GetIlluminance) as usize] =
201            ResponseExpectedFlag::AlwaysTrue;
202        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::SetIlluminanceCallbackConfiguration) as usize] =
203            ResponseExpectedFlag::True;
204        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::GetIlluminanceCallbackConfiguration) as usize] =
205            ResponseExpectedFlag::AlwaysTrue;
206        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::SetConfiguration) as usize] = ResponseExpectedFlag::False;
207        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::GetConfiguration) as usize] =
208            ResponseExpectedFlag::AlwaysTrue;
209        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::GetSpitfpErrorCount) as usize] =
210            ResponseExpectedFlag::AlwaysTrue;
211        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::SetBootloaderMode) as usize] =
212            ResponseExpectedFlag::AlwaysTrue;
213        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::GetBootloaderMode) as usize] =
214            ResponseExpectedFlag::AlwaysTrue;
215        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::SetWriteFirmwarePointer) as usize] =
216            ResponseExpectedFlag::False;
217        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::WriteFirmware) as usize] =
218            ResponseExpectedFlag::AlwaysTrue;
219        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::SetStatusLedConfig) as usize] =
220            ResponseExpectedFlag::False;
221        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::GetStatusLedConfig) as usize] =
222            ResponseExpectedFlag::AlwaysTrue;
223        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::GetChipTemperature) as usize] =
224            ResponseExpectedFlag::AlwaysTrue;
225        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
226        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
227        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
228        result.device.response_expected[u8::from(AmbientLightV3BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
229        result
230    }
231
232    /// Returns the response expected flag for the function specified by the function ID parameter.
233    /// It is true if the function is expected to send a response, false otherwise.
234    ///
235    /// For getter functions this is enabled by default and cannot be disabled, because those
236    /// functions will always send a response. For callback configuration functions it is enabled
237    /// by default too, but can be disabled by [`set_response_expected`](crate::ambient_light_v3_bricklet::AmbientLightV3Bricklet::set_response_expected).
238    /// For setter functions it is disabled by default and can be enabled.
239    ///
240    /// Enabling the response expected flag for a setter function allows to detect timeouts
241    /// and other error conditions calls of this setter as well. The device will then send a response
242    /// for this purpose. If this flag is disabled for a setter function then no response is sent
243    /// and errors are silently ignored, because they cannot be detected.
244    ///
245    /// See [`set_response_expected`](crate::ambient_light_v3_bricklet::AmbientLightV3Bricklet::set_response_expected) for the list of function ID constants available for this function.
246    pub fn get_response_expected(&mut self, fun: AmbientLightV3BrickletFunction) -> Result<bool, GetResponseExpectedError> {
247        self.device.get_response_expected(u8::from(fun))
248    }
249
250    /// Changes the response expected flag of the function specified by the function ID parameter.
251    /// This flag can only be changed for setter (default value: false) and callback configuration
252    /// functions (default value: true). For getter functions it is always enabled.
253    ///
254    /// Enabling the response expected flag for a setter function allows to detect timeouts and
255    /// other error conditions calls of this setter as well. The device will then send a response
256    /// for this purpose. If this flag is disabled for a setter function then no response is sent
257    /// and errors are silently ignored, because they cannot be detected.
258    pub fn set_response_expected(
259        &mut self,
260        fun: AmbientLightV3BrickletFunction,
261        response_expected: bool,
262    ) -> Result<(), SetResponseExpectedError> {
263        self.device.set_response_expected(u8::from(fun), response_expected)
264    }
265
266    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
267    pub fn set_response_expected_all(&mut self, response_expected: bool) {
268        self.device.set_response_expected_all(response_expected)
269    }
270
271    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
272    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
273    pub fn get_api_version(&self) -> [u8; 3] {
274        self.device.api_version
275    }
276
277    /// This receiver is triggered periodically according to the configuration set by
278    /// [`set_illuminance_callback_configuration`].
279    ///
280    /// The parameter is the same as [`get_illuminance`].
281    ///
282    /// [`get_illuminance`]: #method.get_illuminance
283    /// [`set_illuminance_callback_configuration`]: #method.set_illuminance_callback_configuration
284    pub async fn get_illuminance_callback_receiver(&mut self) -> impl Stream<Item = u32> {
285        self.device
286            .get_callback_receiver(u8::from(AmbientLightV3BrickletFunction::CallbackIlluminance))
287            .await
288            .map(|p| u32::from_le_byte_slice(p.body()))
289    }
290
291    /// Returns the illuminance of the ambient light sensor. The measurement range goes
292    /// up to about 100000lux, but above 64000lux the precision starts to drop.
293    /// The illuminance is given in lux/100, i.e. a value of 450000 means that an
294    /// illuminance of 4500lux is measured.
295    ///
296    /// An illuminance of 0lux indicates an error condition where the sensor cannot
297    /// perform a reasonable measurement. This can happen with very dim or very bright
298    /// light conditions. In bright light conditions this might indicate that the sensor
299    /// is saturated and the configuration should be modified ([`set_configuration`])
300    /// to better match the conditions.
301    ///
302    ///
303    /// If you want to get the value periodically, it is recommended to use the
304    /// [`get_illuminance_callback_receiver`] receiver. You can set the receiver configuration
305    /// with [`set_illuminance_callback_configuration`].
306    pub async fn get_illuminance(&mut self) -> Result<u32, TinkerforgeError> {
307        let payload = [0; 0];
308
309        #[allow(unused_variables)]
310        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::GetIlluminance), &payload).await?;
311        Ok(u32::from_le_byte_slice(result.body()))
312    }
313
314    /// The period is the period with which the [`get_illuminance_callback_receiver`] receiver is triggered
315    /// periodically. A value of 0 turns the receiver off.
316    ///
317    /// If the `value has to change`-parameter is set to true, the receiver is only
318    /// triggered after the value has changed. If the value didn't change
319    /// within the period, the receiver is triggered immediately on change.
320    ///
321    /// If it is set to false, the receiver is continuously triggered with the period,
322    /// independent of the value.
323    ///
324    /// It is furthermore possible to constrain the receiver with thresholds.
325    ///
326    /// The `option`-parameter together with min/max sets a threshold for the [`get_illuminance_callback_receiver`] receiver.
327    ///
328    /// The following options are possible:
329    ///
330    ///  Option| Description
331    ///  --- | ---
332    ///  'x'|    Threshold is turned off
333    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
334    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
335    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
336    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
337    ///
338    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
339    ///
340    /// Associated constants:
341    /// * AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_OFF
342    ///	* AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_OUTSIDE
343    ///	* AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_INSIDE
344    ///	* AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_SMALLER
345    ///	* AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_GREATER
346    pub async fn set_illuminance_callback_configuration(
347        &mut self,
348        period: u32,
349        value_has_to_change: bool,
350        option: char,
351        min: u32,
352        max: u32,
353    ) -> Result<(), TinkerforgeError> {
354        let mut payload = [0; 14];
355        period.write_to_slice(&mut payload[0..4]);
356        value_has_to_change.write_to_slice(&mut payload[4..5]);
357        option.write_to_slice(&mut payload[5..6]);
358        min.write_to_slice(&mut payload[6..10]);
359        max.write_to_slice(&mut payload[10..14]);
360
361        #[allow(unused_variables)]
362        let result = self.device.set(u8::from(AmbientLightV3BrickletFunction::SetIlluminanceCallbackConfiguration), &payload).await?;
363        Ok(())
364    }
365
366    /// Returns the receiver configuration as set by [`set_illuminance_callback_configuration`].
367    ///
368    /// Associated constants:
369    /// * AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_OFF
370    ///	* AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_OUTSIDE
371    ///	* AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_INSIDE
372    ///	* AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_SMALLER
373    ///	* AMBIENT_LIGHT_V3_BRICKLET_THRESHOLD_OPTION_GREATER
374    pub async fn get_illuminance_callback_configuration(&mut self) -> Result<IlluminanceCallbackConfiguration, TinkerforgeError> {
375        let payload = [0; 0];
376
377        #[allow(unused_variables)]
378        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::GetIlluminanceCallbackConfiguration), &payload).await?;
379        Ok(IlluminanceCallbackConfiguration::from_le_byte_slice(result.body()))
380    }
381
382    /// Sets the configuration. It is possible to configure an illuminance range
383    /// between 0-600lux and 0-64000lux and an integration time between 50ms and 400ms.
384    ///
385    /// The unlimited illuminance range allows to measure up to about 100000lux, but
386    /// above 64000lux the precision starts to drop.
387    ///
388    /// A smaller illuminance range increases the resolution of the data. A longer
389    /// integration time will result in less noise on the data.
390    ///
391    /// If the actual measure illuminance is out-of-range then the current illuminance
392    /// range maximum +0.01lux is reported by [`get_illuminance`] and the
393    /// [`get_illuminance_callback_receiver`] receiver. For example, 800001 for the 0-8000lux range.
394    ///
395    /// With a long integration time the sensor might be saturated before the measured
396    /// value reaches the maximum of the selected illuminance range. In this case 0lux
397    /// is reported by [`get_illuminance`] and the [`get_illuminance_callback_receiver`] receiver.
398    ///
399    /// If the measurement is out-of-range or the sensor is saturated then you should
400    /// configure the next higher illuminance range. If the highest range is already
401    /// in use, then start to reduce the integration time.
402    ///
403    /// Associated constants:
404    /// * AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_UNLIMITED
405    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_64000LUX
406    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_32000LUX
407    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_16000LUX
408    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_8000LUX
409    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_1300LUX
410    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_600LUX
411    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_50MS
412    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_100MS
413    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_150MS
414    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_200MS
415    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_250MS
416    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_300MS
417    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_350MS
418    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_400MS
419    pub async fn set_configuration(&mut self, illuminance_range: u8, integration_time: u8) -> Result<(), TinkerforgeError> {
420        let mut payload = [0; 2];
421        illuminance_range.write_to_slice(&mut payload[0..1]);
422        integration_time.write_to_slice(&mut payload[1..2]);
423
424        #[allow(unused_variables)]
425        let result = self.device.set(u8::from(AmbientLightV3BrickletFunction::SetConfiguration), &payload).await?;
426        Ok(())
427    }
428
429    /// Returns the configuration as set by [`set_configuration`].
430    ///
431    /// Associated constants:
432    /// * AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_UNLIMITED
433    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_64000LUX
434    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_32000LUX
435    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_16000LUX
436    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_8000LUX
437    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_1300LUX
438    ///	* AMBIENT_LIGHT_V3_BRICKLET_ILLUMINANCE_RANGE_600LUX
439    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_50MS
440    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_100MS
441    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_150MS
442    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_200MS
443    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_250MS
444    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_300MS
445    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_350MS
446    ///	* AMBIENT_LIGHT_V3_BRICKLET_INTEGRATION_TIME_400MS
447    pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
448        let payload = [0; 0];
449
450        #[allow(unused_variables)]
451        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::GetConfiguration), &payload).await?;
452        Ok(Configuration::from_le_byte_slice(result.body()))
453    }
454
455    /// Returns the error count for the communication between Brick and Bricklet.
456    ///
457    /// The errors are divided into
458    ///
459    /// * ACK checksum errors,
460    /// * message checksum errors,
461    /// * framing errors and
462    /// * overflow errors.
463    ///
464    /// The errors counts are for errors that occur on the Bricklet side. All
465    /// Bricks have a similar function that returns the errors on the Brick side.
466    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
467        let payload = [0; 0];
468
469        #[allow(unused_variables)]
470        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::GetSpitfpErrorCount), &payload).await?;
471        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
472    }
473
474    /// Sets the bootloader mode and returns the status after the requested
475    /// mode change was instigated.
476    ///
477    /// You can change from bootloader mode to firmware mode and vice versa. A change
478    /// from bootloader mode to firmware mode will only take place if the entry function,
479    /// device identifier and CRC are present and correct.
480    ///
481    /// This function is used by Brick Viewer during flashing. It should not be
482    /// necessary to call it in a normal user program.
483    ///
484    /// Associated constants:
485    /// * AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
486    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE
487    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
488    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
489    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
490    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_OK
491    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
492    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
493    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
494    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
495    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
496    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
497        let mut payload = [0; 1];
498        mode.write_to_slice(&mut payload[0..1]);
499
500        #[allow(unused_variables)]
501        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::SetBootloaderMode), &payload).await?;
502        Ok(u8::from_le_byte_slice(result.body()))
503    }
504
505    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
506    ///
507    /// Associated constants:
508    /// * AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
509    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE
510    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
511    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
512    ///	* AMBIENT_LIGHT_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
513    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
514        let payload = [0; 0];
515
516        #[allow(unused_variables)]
517        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::GetBootloaderMode), &payload).await?;
518        Ok(u8::from_le_byte_slice(result.body()))
519    }
520
521    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
522    /// to be increased by chunks of size 64. The data is written to flash
523    /// every 4 chunks (which equals to one page of size 256).
524    ///
525    /// This function is used by Brick Viewer during flashing. It should not be
526    /// necessary to call it in a normal user program.
527    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
528        let mut payload = [0; 4];
529        pointer.write_to_slice(&mut payload[0..4]);
530
531        #[allow(unused_variables)]
532        let result = self.device.set(u8::from(AmbientLightV3BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
533        Ok(())
534    }
535
536    /// Writes 64 Bytes of firmware at the position as written by
537    /// [`set_write_firmware_pointer`] before. The firmware is written
538    /// to flash every 4 chunks.
539    ///
540    /// You can only write firmware in bootloader mode.
541    ///
542    /// This function is used by Brick Viewer during flashing. It should not be
543    /// necessary to call it in a normal user program.
544    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
545        let mut payload = [0; 64];
546        data.write_to_slice(&mut payload[0..64]);
547
548        #[allow(unused_variables)]
549        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::WriteFirmware), &payload).await?;
550        Ok(u8::from_le_byte_slice(result.body()))
551    }
552
553    /// Sets the status LED configuration. By default the LED shows
554    /// communication traffic between Brick and Bricklet, it flickers once
555    /// for every 10 received data packets.
556    ///
557    /// You can also turn the LED permanently on/off or show a heartbeat.
558    ///
559    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
560    ///
561    /// Associated constants:
562    /// * AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_OFF
563    ///	* AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_ON
564    ///	* AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
565    ///	* AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
566    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
567        let mut payload = [0; 1];
568        config.write_to_slice(&mut payload[0..1]);
569
570        #[allow(unused_variables)]
571        let result = self.device.set(u8::from(AmbientLightV3BrickletFunction::SetStatusLedConfig), &payload).await?;
572        Ok(())
573    }
574
575    /// Returns the configuration as set by [`set_status_led_config`]
576    ///
577    /// Associated constants:
578    /// * AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_OFF
579    ///	* AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_ON
580    ///	* AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
581    ///	* AMBIENT_LIGHT_V3_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
582    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
583        let payload = [0; 0];
584
585        #[allow(unused_variables)]
586        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::GetStatusLedConfig), &payload).await?;
587        Ok(u8::from_le_byte_slice(result.body()))
588    }
589
590    /// Returns the temperature as measured inside the microcontroller. The
591    /// value returned is not the ambient temperature!
592    ///
593    /// The temperature is only proportional to the real temperature and it has bad
594    /// accuracy. Practically it is only useful as an indicator for
595    /// temperature changes.
596    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
597        let payload = [0; 0];
598
599        #[allow(unused_variables)]
600        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::GetChipTemperature), &payload).await?;
601        Ok(i16::from_le_byte_slice(result.body()))
602    }
603
604    /// Calling this function will reset the Bricklet. All configurations
605    /// will be lost.
606    ///
607    /// After a reset you have to create new device objects,
608    /// calling functions on the existing ones will result in
609    /// undefined behavior!
610    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
611        let payload = [0; 0];
612
613        #[allow(unused_variables)]
614        let result = self.device.set(u8::from(AmbientLightV3BrickletFunction::Reset), &payload).await?;
615        Ok(())
616    }
617
618    /// Writes a new UID into flash. If you want to set a new UID
619    /// you have to decode the Base58 encoded UID string into an
620    /// integer first.
621    ///
622    /// We recommend that you use Brick Viewer to change the UID.
623    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
624        let mut payload = [0; 4];
625        uid.write_to_slice(&mut payload[0..4]);
626
627        #[allow(unused_variables)]
628        let result = self.device.set(u8::from(AmbientLightV3BrickletFunction::WriteUid), &payload).await?;
629        Ok(())
630    }
631
632    /// Returns the current UID as an integer. Encode as
633    /// Base58 to get the usual string version.
634    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
635        let payload = [0; 0];
636
637        #[allow(unused_variables)]
638        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::ReadUid), &payload).await?;
639        Ok(u32::from_le_byte_slice(result.body()))
640    }
641
642    /// Returns the UID, the UID where the Bricklet is connected to,
643    /// the position, the hardware and firmware version as well as the
644    /// device identifier.
645    ///
646    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
647    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
648    /// position 'z'.
649    ///
650    /// The device identifier numbers can be found [here](device_identifier).
651    /// |device_identifier_constant|
652    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
653        let payload = [0; 0];
654
655        #[allow(unused_variables)]
656        let result = self.device.get(u8::from(AmbientLightV3BrickletFunction::GetIdentity), &payload).await?;
657        Ok(Identity::from_le_byte_slice(result.body()))
658    }
659}