tinkerforge_async/bindings/
ambient_light_bricklet.rs1#[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 AmbientLightBrickletFunction {
24 GetIlluminance,
25 GetAnalogValue,
26 SetIlluminanceCallbackPeriod,
27 GetIlluminanceCallbackPeriod,
28 SetAnalogValueCallbackPeriod,
29 GetAnalogValueCallbackPeriod,
30 SetIlluminanceCallbackThreshold,
31 GetIlluminanceCallbackThreshold,
32 SetAnalogValueCallbackThreshold,
33 GetAnalogValueCallbackThreshold,
34 SetDebouncePeriod,
35 GetDebouncePeriod,
36 GetIdentity,
37 CallbackIlluminance,
38 CallbackAnalogValue,
39 CallbackIlluminanceReached,
40 CallbackAnalogValueReached,
41}
42impl From<AmbientLightBrickletFunction> for u8 {
43 fn from(fun: AmbientLightBrickletFunction) -> Self {
44 match fun {
45 AmbientLightBrickletFunction::GetIlluminance => 1,
46 AmbientLightBrickletFunction::GetAnalogValue => 2,
47 AmbientLightBrickletFunction::SetIlluminanceCallbackPeriod => 3,
48 AmbientLightBrickletFunction::GetIlluminanceCallbackPeriod => 4,
49 AmbientLightBrickletFunction::SetAnalogValueCallbackPeriod => 5,
50 AmbientLightBrickletFunction::GetAnalogValueCallbackPeriod => 6,
51 AmbientLightBrickletFunction::SetIlluminanceCallbackThreshold => 7,
52 AmbientLightBrickletFunction::GetIlluminanceCallbackThreshold => 8,
53 AmbientLightBrickletFunction::SetAnalogValueCallbackThreshold => 9,
54 AmbientLightBrickletFunction::GetAnalogValueCallbackThreshold => 10,
55 AmbientLightBrickletFunction::SetDebouncePeriod => 11,
56 AmbientLightBrickletFunction::GetDebouncePeriod => 12,
57 AmbientLightBrickletFunction::GetIdentity => 255,
58 AmbientLightBrickletFunction::CallbackIlluminance => 13,
59 AmbientLightBrickletFunction::CallbackAnalogValue => 14,
60 AmbientLightBrickletFunction::CallbackIlluminanceReached => 15,
61 AmbientLightBrickletFunction::CallbackAnalogValueReached => 16,
62 }
63 }
64}
65pub const AMBIENT_LIGHT_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
66pub const AMBIENT_LIGHT_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
67pub const AMBIENT_LIGHT_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
68pub const AMBIENT_LIGHT_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
69pub const AMBIENT_LIGHT_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
70
71#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
72pub struct IlluminanceCallbackThreshold {
73 pub option: char,
74 pub min: u16,
75 pub max: u16,
76}
77impl FromByteSlice for IlluminanceCallbackThreshold {
78 fn bytes_expected() -> usize {
79 5
80 }
81 fn from_le_byte_slice(bytes: &[u8]) -> IlluminanceCallbackThreshold {
82 IlluminanceCallbackThreshold {
83 option: <char>::from_le_byte_slice(&bytes[0..1]),
84 min: <u16>::from_le_byte_slice(&bytes[1..3]),
85 max: <u16>::from_le_byte_slice(&bytes[3..5]),
86 }
87 }
88}
89
90#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
91pub struct AnalogValueCallbackThreshold {
92 pub option: char,
93 pub min: u16,
94 pub max: u16,
95}
96impl FromByteSlice for AnalogValueCallbackThreshold {
97 fn bytes_expected() -> usize {
98 5
99 }
100 fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueCallbackThreshold {
101 AnalogValueCallbackThreshold {
102 option: <char>::from_le_byte_slice(&bytes[0..1]),
103 min: <u16>::from_le_byte_slice(&bytes[1..3]),
104 max: <u16>::from_le_byte_slice(&bytes[3..5]),
105 }
106 }
107}
108
109#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
110pub struct Identity {
111 pub uid: String,
112 pub connected_uid: String,
113 pub position: char,
114 pub hardware_version: [u8; 3],
115 pub firmware_version: [u8; 3],
116 pub device_identifier: u16,
117}
118impl FromByteSlice for Identity {
119 fn bytes_expected() -> usize {
120 25
121 }
122 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
123 Identity {
124 uid: <String>::from_le_byte_slice(&bytes[0..8]),
125 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
126 position: <char>::from_le_byte_slice(&bytes[16..17]),
127 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
128 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
129 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
130 }
131 }
132}
133
134#[derive(Clone)]
136pub struct AmbientLightBricklet {
137 device: Device,
138}
139impl AmbientLightBricklet {
140 pub const DEVICE_IDENTIFIER: u16 = 21;
141 pub const DEVICE_DISPLAY_NAME: &'static str = "Ambient Light Bricklet";
142 pub fn new(uid: Uid, connection: AsyncIpConnection) -> AmbientLightBricklet {
144 let mut result = AmbientLightBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
145 result.device.response_expected[u8::from(AmbientLightBrickletFunction::GetIlluminance) as usize] = ResponseExpectedFlag::AlwaysTrue;
146 result.device.response_expected[u8::from(AmbientLightBrickletFunction::GetAnalogValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
147 result.device.response_expected[u8::from(AmbientLightBrickletFunction::SetIlluminanceCallbackPeriod) as usize] =
148 ResponseExpectedFlag::True;
149 result.device.response_expected[u8::from(AmbientLightBrickletFunction::GetIlluminanceCallbackPeriod) as usize] =
150 ResponseExpectedFlag::AlwaysTrue;
151 result.device.response_expected[u8::from(AmbientLightBrickletFunction::SetAnalogValueCallbackPeriod) as usize] =
152 ResponseExpectedFlag::True;
153 result.device.response_expected[u8::from(AmbientLightBrickletFunction::GetAnalogValueCallbackPeriod) as usize] =
154 ResponseExpectedFlag::AlwaysTrue;
155 result.device.response_expected[u8::from(AmbientLightBrickletFunction::SetIlluminanceCallbackThreshold) as usize] =
156 ResponseExpectedFlag::True;
157 result.device.response_expected[u8::from(AmbientLightBrickletFunction::GetIlluminanceCallbackThreshold) as usize] =
158 ResponseExpectedFlag::AlwaysTrue;
159 result.device.response_expected[u8::from(AmbientLightBrickletFunction::SetAnalogValueCallbackThreshold) as usize] =
160 ResponseExpectedFlag::True;
161 result.device.response_expected[u8::from(AmbientLightBrickletFunction::GetAnalogValueCallbackThreshold) as usize] =
162 ResponseExpectedFlag::AlwaysTrue;
163 result.device.response_expected[u8::from(AmbientLightBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
164 result.device.response_expected[u8::from(AmbientLightBrickletFunction::GetDebouncePeriod) as usize] =
165 ResponseExpectedFlag::AlwaysTrue;
166 result.device.response_expected[u8::from(AmbientLightBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
167 result
168 }
169
170 pub fn get_response_expected(&mut self, fun: AmbientLightBrickletFunction) -> Result<bool, GetResponseExpectedError> {
185 self.device.get_response_expected(u8::from(fun))
186 }
187
188 pub fn set_response_expected(
197 &mut self,
198 fun: AmbientLightBrickletFunction,
199 response_expected: bool,
200 ) -> Result<(), SetResponseExpectedError> {
201 self.device.set_response_expected(u8::from(fun), response_expected)
202 }
203
204 pub fn set_response_expected_all(&mut self, response_expected: bool) {
206 self.device.set_response_expected_all(response_expected)
207 }
208
209 pub fn get_api_version(&self) -> [u8; 3] {
212 self.device.api_version
213 }
214
215 pub async fn get_illuminance_callback_receiver(&mut self) -> impl Stream<Item = u16> {
225 self.device
226 .get_callback_receiver(u8::from(AmbientLightBrickletFunction::CallbackIlluminance))
227 .await
228 .map(|p| u16::from_le_byte_slice(p.body()))
229 }
230
231 pub async fn get_analog_value_callback_receiver(&mut self) -> impl Stream<Item = u16> {
238 self.device
239 .get_callback_receiver(u8::from(AmbientLightBrickletFunction::CallbackAnalogValue))
240 .await
241 .map(|p| u16::from_le_byte_slice(p.body()))
242 }
243
244 pub async fn get_illuminance_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
251 self.device
252 .get_callback_receiver(u8::from(AmbientLightBrickletFunction::CallbackIlluminanceReached))
253 .await
254 .map(|p| u16::from_le_byte_slice(p.body()))
255 }
256
257 pub async fn get_analog_value_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
264 self.device
265 .get_callback_receiver(u8::from(AmbientLightBrickletFunction::CallbackAnalogValueReached))
266 .await
267 .map(|p| u16::from_le_byte_slice(p.body()))
268 }
269
270 pub async fn get_illuminance(&mut self) -> Result<u16, TinkerforgeError> {
276 let payload = [0; 0];
277
278 #[allow(unused_variables)]
279 let result = self.device.get(u8::from(AmbientLightBrickletFunction::GetIlluminance), &payload).await?;
280 Ok(u16::from_le_byte_slice(result.body()))
281 }
282
283 pub async fn get_analog_value(&mut self) -> Result<u16, TinkerforgeError> {
299 let payload = [0; 0];
300
301 #[allow(unused_variables)]
302 let result = self.device.get(u8::from(AmbientLightBrickletFunction::GetAnalogValue), &payload).await?;
303 Ok(u16::from_le_byte_slice(result.body()))
304 }
305
306 pub async fn set_illuminance_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
312 let mut payload = [0; 4];
313 period.write_to_slice(&mut payload[0..4]);
314
315 #[allow(unused_variables)]
316 let result = self.device.set(u8::from(AmbientLightBrickletFunction::SetIlluminanceCallbackPeriod), &payload).await?;
317 Ok(())
318 }
319
320 pub async fn get_illuminance_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
322 let payload = [0; 0];
323
324 #[allow(unused_variables)]
325 let result = self.device.get(u8::from(AmbientLightBrickletFunction::GetIlluminanceCallbackPeriod), &payload).await?;
326 Ok(u32::from_le_byte_slice(result.body()))
327 }
328
329 pub async fn set_analog_value_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
335 let mut payload = [0; 4];
336 period.write_to_slice(&mut payload[0..4]);
337
338 #[allow(unused_variables)]
339 let result = self.device.set(u8::from(AmbientLightBrickletFunction::SetAnalogValueCallbackPeriod), &payload).await?;
340 Ok(())
341 }
342
343 pub async fn get_analog_value_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
345 let payload = [0; 0];
346
347 #[allow(unused_variables)]
348 let result = self.device.get(u8::from(AmbientLightBrickletFunction::GetAnalogValueCallbackPeriod), &payload).await?;
349 Ok(u32::from_le_byte_slice(result.body()))
350 }
351
352 pub async fn set_illuminance_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
371 let mut payload = [0; 5];
372 option.write_to_slice(&mut payload[0..1]);
373 min.write_to_slice(&mut payload[1..3]);
374 max.write_to_slice(&mut payload[3..5]);
375
376 #[allow(unused_variables)]
377 let result = self.device.set(u8::from(AmbientLightBrickletFunction::SetIlluminanceCallbackThreshold), &payload).await?;
378 Ok(())
379 }
380
381 pub async fn get_illuminance_callback_threshold(&mut self) -> Result<IlluminanceCallbackThreshold, TinkerforgeError> {
390 let payload = [0; 0];
391
392 #[allow(unused_variables)]
393 let result = self.device.get(u8::from(AmbientLightBrickletFunction::GetIlluminanceCallbackThreshold), &payload).await?;
394 Ok(IlluminanceCallbackThreshold::from_le_byte_slice(result.body()))
395 }
396
397 pub async fn set_analog_value_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
416 let mut payload = [0; 5];
417 option.write_to_slice(&mut payload[0..1]);
418 min.write_to_slice(&mut payload[1..3]);
419 max.write_to_slice(&mut payload[3..5]);
420
421 #[allow(unused_variables)]
422 let result = self.device.set(u8::from(AmbientLightBrickletFunction::SetAnalogValueCallbackThreshold), &payload).await?;
423 Ok(())
424 }
425
426 pub async fn get_analog_value_callback_threshold(&mut self) -> Result<AnalogValueCallbackThreshold, TinkerforgeError> {
435 let payload = [0; 0];
436
437 #[allow(unused_variables)]
438 let result = self.device.get(u8::from(AmbientLightBrickletFunction::GetAnalogValueCallbackThreshold), &payload).await?;
439 Ok(AnalogValueCallbackThreshold::from_le_byte_slice(result.body()))
440 }
441
442 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
454 let mut payload = [0; 4];
455 debounce.write_to_slice(&mut payload[0..4]);
456
457 #[allow(unused_variables)]
458 let result = self.device.set(u8::from(AmbientLightBrickletFunction::SetDebouncePeriod), &payload).await?;
459 Ok(())
460 }
461
462 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
464 let payload = [0; 0];
465
466 #[allow(unused_variables)]
467 let result = self.device.get(u8::from(AmbientLightBrickletFunction::GetDebouncePeriod), &payload).await?;
468 Ok(u32::from_le_byte_slice(result.body()))
469 }
470
471 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
482 let payload = [0; 0];
483
484 #[allow(unused_variables)]
485 let result = self.device.get(u8::from(AmbientLightBrickletFunction::GetIdentity), &payload).await?;
486 Ok(Identity::from_le_byte_slice(result.body()))
487 }
488}