1#[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 AnalogInV3BrickletFunction {
24 GetVoltage,
25 SetVoltageCallbackConfiguration,
26 GetVoltageCallbackConfiguration,
27 SetOversampling,
28 GetOversampling,
29 SetCalibration,
30 GetCalibration,
31 GetSpitfpErrorCount,
32 SetBootloaderMode,
33 GetBootloaderMode,
34 SetWriteFirmwarePointer,
35 WriteFirmware,
36 SetStatusLedConfig,
37 GetStatusLedConfig,
38 GetChipTemperature,
39 Reset,
40 WriteUid,
41 ReadUid,
42 GetIdentity,
43 CallbackVoltage,
44}
45impl From<AnalogInV3BrickletFunction> for u8 {
46 fn from(fun: AnalogInV3BrickletFunction) -> Self {
47 match fun {
48 AnalogInV3BrickletFunction::GetVoltage => 1,
49 AnalogInV3BrickletFunction::SetVoltageCallbackConfiguration => 2,
50 AnalogInV3BrickletFunction::GetVoltageCallbackConfiguration => 3,
51 AnalogInV3BrickletFunction::SetOversampling => 5,
52 AnalogInV3BrickletFunction::GetOversampling => 6,
53 AnalogInV3BrickletFunction::SetCalibration => 7,
54 AnalogInV3BrickletFunction::GetCalibration => 8,
55 AnalogInV3BrickletFunction::GetSpitfpErrorCount => 234,
56 AnalogInV3BrickletFunction::SetBootloaderMode => 235,
57 AnalogInV3BrickletFunction::GetBootloaderMode => 236,
58 AnalogInV3BrickletFunction::SetWriteFirmwarePointer => 237,
59 AnalogInV3BrickletFunction::WriteFirmware => 238,
60 AnalogInV3BrickletFunction::SetStatusLedConfig => 239,
61 AnalogInV3BrickletFunction::GetStatusLedConfig => 240,
62 AnalogInV3BrickletFunction::GetChipTemperature => 242,
63 AnalogInV3BrickletFunction::Reset => 243,
64 AnalogInV3BrickletFunction::WriteUid => 248,
65 AnalogInV3BrickletFunction::ReadUid => 249,
66 AnalogInV3BrickletFunction::GetIdentity => 255,
67 AnalogInV3BrickletFunction::CallbackVoltage => 4,
68 }
69 }
70}
71pub const ANALOG_IN_V3_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
72pub const ANALOG_IN_V3_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
73pub const ANALOG_IN_V3_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
74pub const ANALOG_IN_V3_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
75pub const ANALOG_IN_V3_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
76pub const ANALOG_IN_V3_BRICKLET_OVERSAMPLING_32: u8 = 0;
77pub const ANALOG_IN_V3_BRICKLET_OVERSAMPLING_64: u8 = 1;
78pub const ANALOG_IN_V3_BRICKLET_OVERSAMPLING_128: u8 = 2;
79pub const ANALOG_IN_V3_BRICKLET_OVERSAMPLING_256: u8 = 3;
80pub const ANALOG_IN_V3_BRICKLET_OVERSAMPLING_512: u8 = 4;
81pub const ANALOG_IN_V3_BRICKLET_OVERSAMPLING_1024: u8 = 5;
82pub const ANALOG_IN_V3_BRICKLET_OVERSAMPLING_2048: u8 = 6;
83pub const ANALOG_IN_V3_BRICKLET_OVERSAMPLING_4096: u8 = 7;
84pub const ANALOG_IN_V3_BRICKLET_OVERSAMPLING_8192: u8 = 8;
85pub const ANALOG_IN_V3_BRICKLET_OVERSAMPLING_16384: u8 = 9;
86pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
87pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
88pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
89pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
90pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
91pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
92pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
93pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
94pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
95pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
96pub const ANALOG_IN_V3_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
97pub const ANALOG_IN_V3_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
98pub const ANALOG_IN_V3_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
99pub const ANALOG_IN_V3_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
100pub const ANALOG_IN_V3_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
101
102#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
103pub struct VoltageCallbackConfiguration {
104 pub period: u32,
105 pub value_has_to_change: bool,
106 pub option: char,
107 pub min: u16,
108 pub max: u16,
109}
110impl FromByteSlice for VoltageCallbackConfiguration {
111 fn bytes_expected() -> usize {
112 10
113 }
114 fn from_le_byte_slice(bytes: &[u8]) -> VoltageCallbackConfiguration {
115 VoltageCallbackConfiguration {
116 period: <u32>::from_le_byte_slice(&bytes[0..4]),
117 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
118 option: <char>::from_le_byte_slice(&bytes[5..6]),
119 min: <u16>::from_le_byte_slice(&bytes[6..8]),
120 max: <u16>::from_le_byte_slice(&bytes[8..10]),
121 }
122 }
123}
124
125#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
126pub struct Calibration {
127 pub offset: i16,
128 pub multiplier: u16,
129 pub divisor: u16,
130}
131impl FromByteSlice for Calibration {
132 fn bytes_expected() -> usize {
133 6
134 }
135 fn from_le_byte_slice(bytes: &[u8]) -> Calibration {
136 Calibration {
137 offset: <i16>::from_le_byte_slice(&bytes[0..2]),
138 multiplier: <u16>::from_le_byte_slice(&bytes[2..4]),
139 divisor: <u16>::from_le_byte_slice(&bytes[4..6]),
140 }
141 }
142}
143
144#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
145pub struct SpitfpErrorCount {
146 pub error_count_ack_checksum: u32,
147 pub error_count_message_checksum: u32,
148 pub error_count_frame: u32,
149 pub error_count_overflow: u32,
150}
151impl FromByteSlice for SpitfpErrorCount {
152 fn bytes_expected() -> usize {
153 16
154 }
155 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
156 SpitfpErrorCount {
157 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
158 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
159 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
160 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
161 }
162 }
163}
164
165#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
166pub struct Identity {
167 pub uid: String,
168 pub connected_uid: String,
169 pub position: char,
170 pub hardware_version: [u8; 3],
171 pub firmware_version: [u8; 3],
172 pub device_identifier: u16,
173}
174impl FromByteSlice for Identity {
175 fn bytes_expected() -> usize {
176 25
177 }
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#[derive(Clone)]
192pub struct AnalogInV3Bricklet {
193 device: Device,
194}
195impl AnalogInV3Bricklet {
196 pub const DEVICE_IDENTIFIER: u16 = 295;
197 pub const DEVICE_DISPLAY_NAME: &'static str = "Analog In Bricklet 3.0";
198 pub fn new(uid: Uid, connection: AsyncIpConnection) -> AnalogInV3Bricklet {
200 let mut result = AnalogInV3Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
201 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::GetVoltage) as usize] = ResponseExpectedFlag::AlwaysTrue;
202 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::SetVoltageCallbackConfiguration) as usize] =
203 ResponseExpectedFlag::True;
204 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::GetVoltageCallbackConfiguration) as usize] =
205 ResponseExpectedFlag::AlwaysTrue;
206 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::SetOversampling) as usize] = ResponseExpectedFlag::False;
207 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::GetOversampling) as usize] = ResponseExpectedFlag::AlwaysTrue;
208 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::SetCalibration) as usize] = ResponseExpectedFlag::False;
209 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::GetCalibration) as usize] = ResponseExpectedFlag::AlwaysTrue;
210 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::GetSpitfpErrorCount) as usize] =
211 ResponseExpectedFlag::AlwaysTrue;
212 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::SetBootloaderMode) as usize] =
213 ResponseExpectedFlag::AlwaysTrue;
214 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::GetBootloaderMode) as usize] =
215 ResponseExpectedFlag::AlwaysTrue;
216 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::SetWriteFirmwarePointer) as usize] =
217 ResponseExpectedFlag::False;
218 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
219 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
220 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::GetStatusLedConfig) as usize] =
221 ResponseExpectedFlag::AlwaysTrue;
222 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::GetChipTemperature) as usize] =
223 ResponseExpectedFlag::AlwaysTrue;
224 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
225 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
226 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
227 result.device.response_expected[u8::from(AnalogInV3BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
228 result
229 }
230
231 pub fn get_response_expected(&mut self, fun: AnalogInV3BrickletFunction) -> Result<bool, GetResponseExpectedError> {
246 self.device.get_response_expected(u8::from(fun))
247 }
248
249 pub fn set_response_expected(
258 &mut self,
259 fun: AnalogInV3BrickletFunction,
260 response_expected: bool,
261 ) -> Result<(), SetResponseExpectedError> {
262 self.device.set_response_expected(u8::from(fun), response_expected)
263 }
264
265 pub fn set_response_expected_all(&mut self, response_expected: bool) {
267 self.device.set_response_expected_all(response_expected)
268 }
269
270 pub fn get_api_version(&self) -> [u8; 3] {
273 self.device.api_version
274 }
275
276 pub async fn get_voltage_callback_receiver(&mut self) -> impl Stream<Item = u16> {
284 self.device
285 .get_callback_receiver(u8::from(AnalogInV3BrickletFunction::CallbackVoltage))
286 .await
287 .map(|p| u16::from_le_byte_slice(p.body()))
288 }
289
290 pub async fn get_voltage(&mut self) -> Result<u16, TinkerforgeError> {
298 let payload = [0; 0];
299
300 #[allow(unused_variables)]
301 let result = self.device.get(u8::from(AnalogInV3BrickletFunction::GetVoltage), &payload).await?;
302 Ok(u16::from_le_byte_slice(result.body()))
303 }
304
305 pub async fn set_voltage_callback_configuration(
338 &mut self,
339 period: u32,
340 value_has_to_change: bool,
341 option: char,
342 min: u16,
343 max: u16,
344 ) -> Result<(), TinkerforgeError> {
345 let mut payload = [0; 10];
346 period.write_to_slice(&mut payload[0..4]);
347 value_has_to_change.write_to_slice(&mut payload[4..5]);
348 option.write_to_slice(&mut payload[5..6]);
349 min.write_to_slice(&mut payload[6..8]);
350 max.write_to_slice(&mut payload[8..10]);
351
352 #[allow(unused_variables)]
353 let result = self.device.set(u8::from(AnalogInV3BrickletFunction::SetVoltageCallbackConfiguration), &payload).await?;
354 Ok(())
355 }
356
357 pub async fn get_voltage_callback_configuration(&mut self) -> Result<VoltageCallbackConfiguration, TinkerforgeError> {
366 let payload = [0; 0];
367
368 #[allow(unused_variables)]
369 let result = self.device.get(u8::from(AnalogInV3BrickletFunction::GetVoltageCallbackConfiguration), &payload).await?;
370 Ok(VoltageCallbackConfiguration::from_le_byte_slice(result.body()))
371 }
372
373 pub async fn set_oversampling(&mut self, oversampling: u8) -> Result<(), TinkerforgeError> {
398 let mut payload = [0; 1];
399 oversampling.write_to_slice(&mut payload[0..1]);
400
401 #[allow(unused_variables)]
402 let result = self.device.set(u8::from(AnalogInV3BrickletFunction::SetOversampling), &payload).await?;
403 Ok(())
404 }
405
406 pub async fn get_oversampling(&mut self) -> Result<u8, TinkerforgeError> {
420 let payload = [0; 0];
421
422 #[allow(unused_variables)]
423 let result = self.device.get(u8::from(AnalogInV3BrickletFunction::GetOversampling), &payload).await?;
424 Ok(u8::from_le_byte_slice(result.body()))
425 }
426
427 pub async fn set_calibration(&mut self, offset: i16, multiplier: u16, divisor: u16) -> Result<(), TinkerforgeError> {
436 let mut payload = [0; 6];
437 offset.write_to_slice(&mut payload[0..2]);
438 multiplier.write_to_slice(&mut payload[2..4]);
439 divisor.write_to_slice(&mut payload[4..6]);
440
441 #[allow(unused_variables)]
442 let result = self.device.set(u8::from(AnalogInV3BrickletFunction::SetCalibration), &payload).await?;
443 Ok(())
444 }
445
446 pub async fn get_calibration(&mut self) -> Result<Calibration, TinkerforgeError> {
448 let payload = [0; 0];
449
450 #[allow(unused_variables)]
451 let result = self.device.get(u8::from(AnalogInV3BrickletFunction::GetCalibration), &payload).await?;
452 Ok(Calibration::from_le_byte_slice(result.body()))
453 }
454
455 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(AnalogInV3BrickletFunction::GetSpitfpErrorCount), &payload).await?;
471 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
472 }
473
474 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(AnalogInV3BrickletFunction::SetBootloaderMode), &payload).await?;
502 Ok(u8::from_le_byte_slice(result.body()))
503 }
504
505 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(AnalogInV3BrickletFunction::GetBootloaderMode), &payload).await?;
518 Ok(u8::from_le_byte_slice(result.body()))
519 }
520
521 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(AnalogInV3BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
533 Ok(())
534 }
535
536 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(AnalogInV3BrickletFunction::WriteFirmware), &payload).await?;
550 Ok(u8::from_le_byte_slice(result.body()))
551 }
552
553 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(AnalogInV3BrickletFunction::SetStatusLedConfig), &payload).await?;
572 Ok(())
573 }
574
575 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(AnalogInV3BrickletFunction::GetStatusLedConfig), &payload).await?;
587 Ok(u8::from_le_byte_slice(result.body()))
588 }
589
590 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(AnalogInV3BrickletFunction::GetChipTemperature), &payload).await?;
601 Ok(i16::from_le_byte_slice(result.body()))
602 }
603
604 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(AnalogInV3BrickletFunction::Reset), &payload).await?;
615 Ok(())
616 }
617
618 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(AnalogInV3BrickletFunction::WriteUid), &payload).await?;
629 Ok(())
630 }
631
632 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(AnalogInV3BrickletFunction::ReadUid), &payload).await?;
639 Ok(u32::from_le_byte_slice(result.body()))
640 }
641
642 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(AnalogInV3BrickletFunction::GetIdentity), &payload).await?;
657 Ok(Identity::from_le_byte_slice(result.body()))
658 }
659}