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 LoadCellV2BrickletFunction {
24 GetWeight,
25 SetWeightCallbackConfiguration,
26 GetWeightCallbackConfiguration,
27 SetMovingAverage,
28 GetMovingAverage,
29 SetInfoLedConfig,
30 GetInfoLedConfig,
31 Calibrate,
32 Tare,
33 SetConfiguration,
34 GetConfiguration,
35 GetSpitfpErrorCount,
36 SetBootloaderMode,
37 GetBootloaderMode,
38 SetWriteFirmwarePointer,
39 WriteFirmware,
40 SetStatusLedConfig,
41 GetStatusLedConfig,
42 GetChipTemperature,
43 Reset,
44 WriteUid,
45 ReadUid,
46 GetIdentity,
47 CallbackWeight,
48}
49impl From<LoadCellV2BrickletFunction> for u8 {
50 fn from(fun: LoadCellV2BrickletFunction) -> Self {
51 match fun {
52 LoadCellV2BrickletFunction::GetWeight => 1,
53 LoadCellV2BrickletFunction::SetWeightCallbackConfiguration => 2,
54 LoadCellV2BrickletFunction::GetWeightCallbackConfiguration => 3,
55 LoadCellV2BrickletFunction::SetMovingAverage => 5,
56 LoadCellV2BrickletFunction::GetMovingAverage => 6,
57 LoadCellV2BrickletFunction::SetInfoLedConfig => 7,
58 LoadCellV2BrickletFunction::GetInfoLedConfig => 8,
59 LoadCellV2BrickletFunction::Calibrate => 9,
60 LoadCellV2BrickletFunction::Tare => 10,
61 LoadCellV2BrickletFunction::SetConfiguration => 11,
62 LoadCellV2BrickletFunction::GetConfiguration => 12,
63 LoadCellV2BrickletFunction::GetSpitfpErrorCount => 234,
64 LoadCellV2BrickletFunction::SetBootloaderMode => 235,
65 LoadCellV2BrickletFunction::GetBootloaderMode => 236,
66 LoadCellV2BrickletFunction::SetWriteFirmwarePointer => 237,
67 LoadCellV2BrickletFunction::WriteFirmware => 238,
68 LoadCellV2BrickletFunction::SetStatusLedConfig => 239,
69 LoadCellV2BrickletFunction::GetStatusLedConfig => 240,
70 LoadCellV2BrickletFunction::GetChipTemperature => 242,
71 LoadCellV2BrickletFunction::Reset => 243,
72 LoadCellV2BrickletFunction::WriteUid => 248,
73 LoadCellV2BrickletFunction::ReadUid => 249,
74 LoadCellV2BrickletFunction::GetIdentity => 255,
75 LoadCellV2BrickletFunction::CallbackWeight => 4,
76 }
77 }
78}
79pub const LOAD_CELL_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
80pub const LOAD_CELL_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
81pub const LOAD_CELL_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
82pub const LOAD_CELL_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
83pub const LOAD_CELL_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
84pub const LOAD_CELL_V2_BRICKLET_INFO_LED_CONFIG_OFF: u8 = 0;
85pub const LOAD_CELL_V2_BRICKLET_INFO_LED_CONFIG_ON: u8 = 1;
86pub const LOAD_CELL_V2_BRICKLET_INFO_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
87pub const LOAD_CELL_V2_BRICKLET_RATE_10HZ: u8 = 0;
88pub const LOAD_CELL_V2_BRICKLET_RATE_80HZ: u8 = 1;
89pub const LOAD_CELL_V2_BRICKLET_GAIN_128X: u8 = 0;
90pub const LOAD_CELL_V2_BRICKLET_GAIN_64X: u8 = 1;
91pub const LOAD_CELL_V2_BRICKLET_GAIN_32X: u8 = 2;
92pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
93pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
94pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
95pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
96pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
97pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
98pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
99pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
100pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
101pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
102pub const LOAD_CELL_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
103pub const LOAD_CELL_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
104pub const LOAD_CELL_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
105pub const LOAD_CELL_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
106pub const LOAD_CELL_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
107
108#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
109pub struct WeightCallbackConfiguration {
110 pub period: u32,
111 pub value_has_to_change: bool,
112 pub option: char,
113 pub min: i32,
114 pub max: i32,
115}
116impl FromByteSlice for WeightCallbackConfiguration {
117 fn bytes_expected() -> usize {
118 14
119 }
120 fn from_le_byte_slice(bytes: &[u8]) -> WeightCallbackConfiguration {
121 WeightCallbackConfiguration {
122 period: <u32>::from_le_byte_slice(&bytes[0..4]),
123 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
124 option: <char>::from_le_byte_slice(&bytes[5..6]),
125 min: <i32>::from_le_byte_slice(&bytes[6..10]),
126 max: <i32>::from_le_byte_slice(&bytes[10..14]),
127 }
128 }
129}
130
131#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
132pub struct Configuration {
133 pub rate: u8,
134 pub gain: u8,
135}
136impl FromByteSlice for Configuration {
137 fn bytes_expected() -> usize {
138 2
139 }
140 fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
141 Configuration { rate: <u8>::from_le_byte_slice(&bytes[0..1]), gain: <u8>::from_le_byte_slice(&bytes[1..2]) }
142 }
143}
144
145#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
146pub struct SpitfpErrorCount {
147 pub error_count_ack_checksum: u32,
148 pub error_count_message_checksum: u32,
149 pub error_count_frame: u32,
150 pub error_count_overflow: u32,
151}
152impl FromByteSlice for SpitfpErrorCount {
153 fn bytes_expected() -> usize {
154 16
155 }
156 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
157 SpitfpErrorCount {
158 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
159 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
160 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
161 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
162 }
163 }
164}
165
166#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
167pub struct Identity {
168 pub uid: String,
169 pub connected_uid: String,
170 pub position: char,
171 pub hardware_version: [u8; 3],
172 pub firmware_version: [u8; 3],
173 pub device_identifier: u16,
174}
175impl FromByteSlice for Identity {
176 fn bytes_expected() -> usize {
177 25
178 }
179 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
180 Identity {
181 uid: <String>::from_le_byte_slice(&bytes[0..8]),
182 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
183 position: <char>::from_le_byte_slice(&bytes[16..17]),
184 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
185 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
186 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
187 }
188 }
189}
190
191#[derive(Clone)]
193pub struct LoadCellV2Bricklet {
194 device: Device,
195}
196impl LoadCellV2Bricklet {
197 pub const DEVICE_IDENTIFIER: u16 = 2104;
198 pub const DEVICE_DISPLAY_NAME: &'static str = "Load Cell Bricklet 2.0";
199 pub fn new(uid: Uid, connection: AsyncIpConnection) -> LoadCellV2Bricklet {
201 let mut result = LoadCellV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
202 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::GetWeight) as usize] = ResponseExpectedFlag::AlwaysTrue;
203 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::SetWeightCallbackConfiguration) as usize] =
204 ResponseExpectedFlag::True;
205 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::GetWeightCallbackConfiguration) as usize] =
206 ResponseExpectedFlag::AlwaysTrue;
207 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::SetMovingAverage) as usize] = ResponseExpectedFlag::False;
208 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::GetMovingAverage) as usize] = ResponseExpectedFlag::AlwaysTrue;
209 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::SetInfoLedConfig) as usize] = ResponseExpectedFlag::False;
210 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::GetInfoLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
211 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::Calibrate) as usize] = ResponseExpectedFlag::False;
212 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::Tare) as usize] = ResponseExpectedFlag::False;
213 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::SetConfiguration) as usize] = ResponseExpectedFlag::False;
214 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::GetConfiguration) as usize] = ResponseExpectedFlag::AlwaysTrue;
215 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::GetSpitfpErrorCount) as usize] =
216 ResponseExpectedFlag::AlwaysTrue;
217 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::SetBootloaderMode) as usize] =
218 ResponseExpectedFlag::AlwaysTrue;
219 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::GetBootloaderMode) as usize] =
220 ResponseExpectedFlag::AlwaysTrue;
221 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
222 ResponseExpectedFlag::False;
223 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
224 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
225 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::GetStatusLedConfig) as usize] =
226 ResponseExpectedFlag::AlwaysTrue;
227 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::GetChipTemperature) as usize] =
228 ResponseExpectedFlag::AlwaysTrue;
229 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
230 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
231 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
232 result.device.response_expected[u8::from(LoadCellV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
233 result
234 }
235
236 pub fn get_response_expected(&mut self, fun: LoadCellV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
251 self.device.get_response_expected(u8::from(fun))
252 }
253
254 pub fn set_response_expected(
263 &mut self,
264 fun: LoadCellV2BrickletFunction,
265 response_expected: bool,
266 ) -> Result<(), SetResponseExpectedError> {
267 self.device.set_response_expected(u8::from(fun), response_expected)
268 }
269
270 pub fn set_response_expected_all(&mut self, response_expected: bool) {
272 self.device.set_response_expected_all(response_expected)
273 }
274
275 pub fn get_api_version(&self) -> [u8; 3] {
278 self.device.api_version
279 }
280
281 pub async fn get_weight_callback_receiver(&mut self) -> impl Stream<Item = i32> {
289 self.device
290 .get_callback_receiver(u8::from(LoadCellV2BrickletFunction::CallbackWeight))
291 .await
292 .map(|p| i32::from_le_byte_slice(p.body()))
293 }
294
295 pub async fn get_weight(&mut self) -> Result<i32, TinkerforgeError> {
302 let payload = [0; 0];
303
304 #[allow(unused_variables)]
305 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::GetWeight), &payload).await?;
306 Ok(i32::from_le_byte_slice(result.body()))
307 }
308
309 pub async fn set_weight_callback_configuration(
342 &mut self,
343 period: u32,
344 value_has_to_change: bool,
345 option: char,
346 min: i32,
347 max: i32,
348 ) -> Result<(), TinkerforgeError> {
349 let mut payload = [0; 14];
350 period.write_to_slice(&mut payload[0..4]);
351 value_has_to_change.write_to_slice(&mut payload[4..5]);
352 option.write_to_slice(&mut payload[5..6]);
353 min.write_to_slice(&mut payload[6..10]);
354 max.write_to_slice(&mut payload[10..14]);
355
356 #[allow(unused_variables)]
357 let result = self.device.set(u8::from(LoadCellV2BrickletFunction::SetWeightCallbackConfiguration), &payload).await?;
358 Ok(())
359 }
360
361 pub async fn get_weight_callback_configuration(&mut self) -> Result<WeightCallbackConfiguration, TinkerforgeError> {
370 let payload = [0; 0];
371
372 #[allow(unused_variables)]
373 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::GetWeightCallbackConfiguration), &payload).await?;
374 Ok(WeightCallbackConfiguration::from_le_byte_slice(result.body()))
375 }
376
377 pub async fn set_moving_average(&mut self, average: u16) -> Result<(), TinkerforgeError> {
383 let mut payload = [0; 2];
384 average.write_to_slice(&mut payload[0..2]);
385
386 #[allow(unused_variables)]
387 let result = self.device.set(u8::from(LoadCellV2BrickletFunction::SetMovingAverage), &payload).await?;
388 Ok(())
389 }
390
391 pub async fn get_moving_average(&mut self) -> Result<u16, TinkerforgeError> {
393 let payload = [0; 0];
394
395 #[allow(unused_variables)]
396 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::GetMovingAverage), &payload).await?;
397 Ok(u16::from_le_byte_slice(result.body()))
398 }
399
400 pub async fn set_info_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
408 let mut payload = [0; 1];
409 config.write_to_slice(&mut payload[0..1]);
410
411 #[allow(unused_variables)]
412 let result = self.device.set(u8::from(LoadCellV2BrickletFunction::SetInfoLedConfig), &payload).await?;
413 Ok(())
414 }
415
416 pub async fn get_info_led_config(&mut self) -> Result<u8, TinkerforgeError> {
423 let payload = [0; 0];
424
425 #[allow(unused_variables)]
426 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::GetInfoLedConfig), &payload).await?;
427 Ok(u8::from_le_byte_slice(result.body()))
428 }
429
430 pub async fn calibrate(&mut self, weight: u32) -> Result<(), TinkerforgeError> {
441 let mut payload = [0; 4];
442 weight.write_to_slice(&mut payload[0..4]);
443
444 #[allow(unused_variables)]
445 let result = self.device.set(u8::from(LoadCellV2BrickletFunction::Calibrate), &payload).await?;
446 Ok(())
447 }
448
449 pub async fn tare(&mut self) -> Result<(), TinkerforgeError> {
451 let payload = [0; 0];
452
453 #[allow(unused_variables)]
454 let result = self.device.set(u8::from(LoadCellV2BrickletFunction::Tare), &payload).await?;
455 Ok(())
456 }
457
458 pub async fn set_configuration(&mut self, rate: u8, gain: u8) -> Result<(), TinkerforgeError> {
478 let mut payload = [0; 2];
479 rate.write_to_slice(&mut payload[0..1]);
480 gain.write_to_slice(&mut payload[1..2]);
481
482 #[allow(unused_variables)]
483 let result = self.device.set(u8::from(LoadCellV2BrickletFunction::SetConfiguration), &payload).await?;
484 Ok(())
485 }
486
487 pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
496 let payload = [0; 0];
497
498 #[allow(unused_variables)]
499 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::GetConfiguration), &payload).await?;
500 Ok(Configuration::from_le_byte_slice(result.body()))
501 }
502
503 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
515 let payload = [0; 0];
516
517 #[allow(unused_variables)]
518 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
519 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
520 }
521
522 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
545 let mut payload = [0; 1];
546 mode.write_to_slice(&mut payload[0..1]);
547
548 #[allow(unused_variables)]
549 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::SetBootloaderMode), &payload).await?;
550 Ok(u8::from_le_byte_slice(result.body()))
551 }
552
553 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
562 let payload = [0; 0];
563
564 #[allow(unused_variables)]
565 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::GetBootloaderMode), &payload).await?;
566 Ok(u8::from_le_byte_slice(result.body()))
567 }
568
569 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
576 let mut payload = [0; 4];
577 pointer.write_to_slice(&mut payload[0..4]);
578
579 #[allow(unused_variables)]
580 let result = self.device.set(u8::from(LoadCellV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
581 Ok(())
582 }
583
584 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
593 let mut payload = [0; 64];
594 data.write_to_slice(&mut payload[0..64]);
595
596 #[allow(unused_variables)]
597 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::WriteFirmware), &payload).await?;
598 Ok(u8::from_le_byte_slice(result.body()))
599 }
600
601 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
615 let mut payload = [0; 1];
616 config.write_to_slice(&mut payload[0..1]);
617
618 #[allow(unused_variables)]
619 let result = self.device.set(u8::from(LoadCellV2BrickletFunction::SetStatusLedConfig), &payload).await?;
620 Ok(())
621 }
622
623 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
631 let payload = [0; 0];
632
633 #[allow(unused_variables)]
634 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::GetStatusLedConfig), &payload).await?;
635 Ok(u8::from_le_byte_slice(result.body()))
636 }
637
638 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
645 let payload = [0; 0];
646
647 #[allow(unused_variables)]
648 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::GetChipTemperature), &payload).await?;
649 Ok(i16::from_le_byte_slice(result.body()))
650 }
651
652 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
659 let payload = [0; 0];
660
661 #[allow(unused_variables)]
662 let result = self.device.set(u8::from(LoadCellV2BrickletFunction::Reset), &payload).await?;
663 Ok(())
664 }
665
666 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
672 let mut payload = [0; 4];
673 uid.write_to_slice(&mut payload[0..4]);
674
675 #[allow(unused_variables)]
676 let result = self.device.set(u8::from(LoadCellV2BrickletFunction::WriteUid), &payload).await?;
677 Ok(())
678 }
679
680 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
683 let payload = [0; 0];
684
685 #[allow(unused_variables)]
686 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::ReadUid), &payload).await?;
687 Ok(u32::from_le_byte_slice(result.body()))
688 }
689
690 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
701 let payload = [0; 0];
702
703 #[allow(unused_variables)]
704 let result = self.device.get(u8::from(LoadCellV2BrickletFunction::GetIdentity), &payload).await?;
705 Ok(Identity::from_le_byte_slice(result.body()))
706 }
707}