tinkerforge_async/bindings/
load_cell_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 LoadCellBrickletFunction {
24 GetWeight,
25 SetWeightCallbackPeriod,
26 GetWeightCallbackPeriod,
27 SetWeightCallbackThreshold,
28 GetWeightCallbackThreshold,
29 SetDebouncePeriod,
30 GetDebouncePeriod,
31 SetMovingAverage,
32 GetMovingAverage,
33 LedOn,
34 LedOff,
35 IsLedOn,
36 Calibrate,
37 Tare,
38 SetConfiguration,
39 GetConfiguration,
40 GetIdentity,
41 CallbackWeight,
42 CallbackWeightReached,
43}
44impl From<LoadCellBrickletFunction> for u8 {
45 fn from(fun: LoadCellBrickletFunction) -> Self {
46 match fun {
47 LoadCellBrickletFunction::GetWeight => 1,
48 LoadCellBrickletFunction::SetWeightCallbackPeriod => 2,
49 LoadCellBrickletFunction::GetWeightCallbackPeriod => 3,
50 LoadCellBrickletFunction::SetWeightCallbackThreshold => 4,
51 LoadCellBrickletFunction::GetWeightCallbackThreshold => 5,
52 LoadCellBrickletFunction::SetDebouncePeriod => 6,
53 LoadCellBrickletFunction::GetDebouncePeriod => 7,
54 LoadCellBrickletFunction::SetMovingAverage => 8,
55 LoadCellBrickletFunction::GetMovingAverage => 9,
56 LoadCellBrickletFunction::LedOn => 10,
57 LoadCellBrickletFunction::LedOff => 11,
58 LoadCellBrickletFunction::IsLedOn => 12,
59 LoadCellBrickletFunction::Calibrate => 13,
60 LoadCellBrickletFunction::Tare => 14,
61 LoadCellBrickletFunction::SetConfiguration => 15,
62 LoadCellBrickletFunction::GetConfiguration => 16,
63 LoadCellBrickletFunction::GetIdentity => 255,
64 LoadCellBrickletFunction::CallbackWeight => 17,
65 LoadCellBrickletFunction::CallbackWeightReached => 18,
66 }
67 }
68}
69pub const LOAD_CELL_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
70pub const LOAD_CELL_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
71pub const LOAD_CELL_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
72pub const LOAD_CELL_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
73pub const LOAD_CELL_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
74pub const LOAD_CELL_BRICKLET_RATE_10HZ: u8 = 0;
75pub const LOAD_CELL_BRICKLET_RATE_80HZ: u8 = 1;
76pub const LOAD_CELL_BRICKLET_GAIN_128X: u8 = 0;
77pub const LOAD_CELL_BRICKLET_GAIN_64X: u8 = 1;
78pub const LOAD_CELL_BRICKLET_GAIN_32X: u8 = 2;
79
80#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
81pub struct WeightCallbackThreshold {
82 pub option: char,
83 pub min: i32,
84 pub max: i32,
85}
86impl FromByteSlice for WeightCallbackThreshold {
87 fn bytes_expected() -> usize {
88 9
89 }
90 fn from_le_byte_slice(bytes: &[u8]) -> WeightCallbackThreshold {
91 WeightCallbackThreshold {
92 option: <char>::from_le_byte_slice(&bytes[0..1]),
93 min: <i32>::from_le_byte_slice(&bytes[1..5]),
94 max: <i32>::from_le_byte_slice(&bytes[5..9]),
95 }
96 }
97}
98
99#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
100pub struct Configuration {
101 pub rate: u8,
102 pub gain: u8,
103}
104impl FromByteSlice for Configuration {
105 fn bytes_expected() -> usize {
106 2
107 }
108 fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
109 Configuration { rate: <u8>::from_le_byte_slice(&bytes[0..1]), gain: <u8>::from_le_byte_slice(&bytes[1..2]) }
110 }
111}
112
113#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
114pub struct Identity {
115 pub uid: String,
116 pub connected_uid: String,
117 pub position: char,
118 pub hardware_version: [u8; 3],
119 pub firmware_version: [u8; 3],
120 pub device_identifier: u16,
121}
122impl FromByteSlice for Identity {
123 fn bytes_expected() -> usize {
124 25
125 }
126 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
127 Identity {
128 uid: <String>::from_le_byte_slice(&bytes[0..8]),
129 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
130 position: <char>::from_le_byte_slice(&bytes[16..17]),
131 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
132 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
133 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
134 }
135 }
136}
137
138#[derive(Clone)]
140pub struct LoadCellBricklet {
141 device: Device,
142}
143impl LoadCellBricklet {
144 pub const DEVICE_IDENTIFIER: u16 = 253;
145 pub const DEVICE_DISPLAY_NAME: &'static str = "Load Cell Bricklet";
146 pub fn new(uid: Uid, connection: AsyncIpConnection) -> LoadCellBricklet {
148 let mut result = LoadCellBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
149 result.device.response_expected[u8::from(LoadCellBrickletFunction::GetWeight) as usize] = ResponseExpectedFlag::AlwaysTrue;
150 result.device.response_expected[u8::from(LoadCellBrickletFunction::SetWeightCallbackPeriod) as usize] = ResponseExpectedFlag::True;
151 result.device.response_expected[u8::from(LoadCellBrickletFunction::GetWeightCallbackPeriod) as usize] =
152 ResponseExpectedFlag::AlwaysTrue;
153 result.device.response_expected[u8::from(LoadCellBrickletFunction::SetWeightCallbackThreshold) as usize] =
154 ResponseExpectedFlag::True;
155 result.device.response_expected[u8::from(LoadCellBrickletFunction::GetWeightCallbackThreshold) as usize] =
156 ResponseExpectedFlag::AlwaysTrue;
157 result.device.response_expected[u8::from(LoadCellBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
158 result.device.response_expected[u8::from(LoadCellBrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
159 result.device.response_expected[u8::from(LoadCellBrickletFunction::SetMovingAverage) as usize] = ResponseExpectedFlag::False;
160 result.device.response_expected[u8::from(LoadCellBrickletFunction::GetMovingAverage) as usize] = ResponseExpectedFlag::AlwaysTrue;
161 result.device.response_expected[u8::from(LoadCellBrickletFunction::LedOn) as usize] = ResponseExpectedFlag::False;
162 result.device.response_expected[u8::from(LoadCellBrickletFunction::LedOff) as usize] = ResponseExpectedFlag::False;
163 result.device.response_expected[u8::from(LoadCellBrickletFunction::IsLedOn) as usize] = ResponseExpectedFlag::AlwaysTrue;
164 result.device.response_expected[u8::from(LoadCellBrickletFunction::Calibrate) as usize] = ResponseExpectedFlag::False;
165 result.device.response_expected[u8::from(LoadCellBrickletFunction::Tare) as usize] = ResponseExpectedFlag::False;
166 result.device.response_expected[u8::from(LoadCellBrickletFunction::SetConfiguration) as usize] = ResponseExpectedFlag::False;
167 result.device.response_expected[u8::from(LoadCellBrickletFunction::GetConfiguration) as usize] = ResponseExpectedFlag::AlwaysTrue;
168 result.device.response_expected[u8::from(LoadCellBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
169 result
170 }
171
172 pub fn get_response_expected(&mut self, fun: LoadCellBrickletFunction) -> Result<bool, GetResponseExpectedError> {
187 self.device.get_response_expected(u8::from(fun))
188 }
189
190 pub fn set_response_expected(
199 &mut self,
200 fun: LoadCellBrickletFunction,
201 response_expected: bool,
202 ) -> Result<(), SetResponseExpectedError> {
203 self.device.set_response_expected(u8::from(fun), response_expected)
204 }
205
206 pub fn set_response_expected_all(&mut self, response_expected: bool) {
208 self.device.set_response_expected_all(response_expected)
209 }
210
211 pub fn get_api_version(&self) -> [u8; 3] {
214 self.device.api_version
215 }
216
217 pub async fn get_weight_callback_receiver(&mut self) -> impl Stream<Item = i32> {
227 self.device
228 .get_callback_receiver(u8::from(LoadCellBrickletFunction::CallbackWeight))
229 .await
230 .map(|p| i32::from_le_byte_slice(p.body()))
231 }
232
233 pub async fn get_weight_reached_callback_receiver(&mut self) -> impl Stream<Item = i32> {
240 self.device
241 .get_callback_receiver(u8::from(LoadCellBrickletFunction::CallbackWeightReached))
242 .await
243 .map(|p| i32::from_le_byte_slice(p.body()))
244 }
245
246 pub async fn get_weight(&mut self) -> Result<i32, TinkerforgeError> {
252 let payload = [0; 0];
253
254 #[allow(unused_variables)]
255 let result = self.device.get(u8::from(LoadCellBrickletFunction::GetWeight), &payload).await?;
256 Ok(i32::from_le_byte_slice(result.body()))
257 }
258
259 pub async fn set_weight_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
265 let mut payload = [0; 4];
266 period.write_to_slice(&mut payload[0..4]);
267
268 #[allow(unused_variables)]
269 let result = self.device.set(u8::from(LoadCellBrickletFunction::SetWeightCallbackPeriod), &payload).await?;
270 Ok(())
271 }
272
273 pub async fn get_weight_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
275 let payload = [0; 0];
276
277 #[allow(unused_variables)]
278 let result = self.device.get(u8::from(LoadCellBrickletFunction::GetWeightCallbackPeriod), &payload).await?;
279 Ok(u32::from_le_byte_slice(result.body()))
280 }
281
282 pub async fn set_weight_callback_threshold(&mut self, option: char, min: i32, max: i32) -> Result<(), TinkerforgeError> {
301 let mut payload = [0; 9];
302 option.write_to_slice(&mut payload[0..1]);
303 min.write_to_slice(&mut payload[1..5]);
304 max.write_to_slice(&mut payload[5..9]);
305
306 #[allow(unused_variables)]
307 let result = self.device.set(u8::from(LoadCellBrickletFunction::SetWeightCallbackThreshold), &payload).await?;
308 Ok(())
309 }
310
311 pub async fn get_weight_callback_threshold(&mut self) -> Result<WeightCallbackThreshold, TinkerforgeError> {
320 let payload = [0; 0];
321
322 #[allow(unused_variables)]
323 let result = self.device.get(u8::from(LoadCellBrickletFunction::GetWeightCallbackThreshold), &payload).await?;
324 Ok(WeightCallbackThreshold::from_le_byte_slice(result.body()))
325 }
326
327 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
337 let mut payload = [0; 4];
338 debounce.write_to_slice(&mut payload[0..4]);
339
340 #[allow(unused_variables)]
341 let result = self.device.set(u8::from(LoadCellBrickletFunction::SetDebouncePeriod), &payload).await?;
342 Ok(())
343 }
344
345 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
347 let payload = [0; 0];
348
349 #[allow(unused_variables)]
350 let result = self.device.get(u8::from(LoadCellBrickletFunction::GetDebouncePeriod), &payload).await?;
351 Ok(u32::from_le_byte_slice(result.body()))
352 }
353
354 pub async fn set_moving_average(&mut self, average: u8) -> Result<(), TinkerforgeError> {
360 let mut payload = [0; 1];
361 average.write_to_slice(&mut payload[0..1]);
362
363 #[allow(unused_variables)]
364 let result = self.device.set(u8::from(LoadCellBrickletFunction::SetMovingAverage), &payload).await?;
365 Ok(())
366 }
367
368 pub async fn get_moving_average(&mut self) -> Result<u8, TinkerforgeError> {
370 let payload = [0; 0];
371
372 #[allow(unused_variables)]
373 let result = self.device.get(u8::from(LoadCellBrickletFunction::GetMovingAverage), &payload).await?;
374 Ok(u8::from_le_byte_slice(result.body()))
375 }
376
377 pub async fn led_on(&mut self) -> Result<(), TinkerforgeError> {
379 let payload = [0; 0];
380
381 #[allow(unused_variables)]
382 let result = self.device.set(u8::from(LoadCellBrickletFunction::LedOn), &payload).await?;
383 Ok(())
384 }
385
386 pub async fn led_off(&mut self) -> Result<(), TinkerforgeError> {
388 let payload = [0; 0];
389
390 #[allow(unused_variables)]
391 let result = self.device.set(u8::from(LoadCellBrickletFunction::LedOff), &payload).await?;
392 Ok(())
393 }
394
395 pub async fn is_led_on(&mut self) -> Result<bool, TinkerforgeError> {
397 let payload = [0; 0];
398
399 #[allow(unused_variables)]
400 let result = self.device.get(u8::from(LoadCellBrickletFunction::IsLedOn), &payload).await?;
401 Ok(bool::from_le_byte_slice(result.body()))
402 }
403
404 pub async fn calibrate(&mut self, weight: u32) -> Result<(), TinkerforgeError> {
415 let mut payload = [0; 4];
416 weight.write_to_slice(&mut payload[0..4]);
417
418 #[allow(unused_variables)]
419 let result = self.device.set(u8::from(LoadCellBrickletFunction::Calibrate), &payload).await?;
420 Ok(())
421 }
422
423 pub async fn tare(&mut self) -> Result<(), TinkerforgeError> {
425 let payload = [0; 0];
426
427 #[allow(unused_variables)]
428 let result = self.device.set(u8::from(LoadCellBrickletFunction::Tare), &payload).await?;
429 Ok(())
430 }
431
432 pub async fn set_configuration(&mut self, rate: u8, gain: u8) -> Result<(), TinkerforgeError> {
458 let mut payload = [0; 2];
459 rate.write_to_slice(&mut payload[0..1]);
460 gain.write_to_slice(&mut payload[1..2]);
461
462 #[allow(unused_variables)]
463 let result = self.device.set(u8::from(LoadCellBrickletFunction::SetConfiguration), &payload).await?;
464 Ok(())
465 }
466
467 pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
476 let payload = [0; 0];
477
478 #[allow(unused_variables)]
479 let result = self.device.get(u8::from(LoadCellBrickletFunction::GetConfiguration), &payload).await?;
480 Ok(Configuration::from_le_byte_slice(result.body()))
481 }
482
483 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
494 let payload = [0; 0];
495
496 #[allow(unused_variables)]
497 let result = self.device.get(u8::from(LoadCellBrickletFunction::GetIdentity), &payload).await?;
498 Ok(Identity::from_le_byte_slice(result.body()))
499 }
500}