tinkerforge_async/bindings/
rotary_encoder_v2_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 RotaryEncoderV2BrickletFunction {
24 GetCount,
25 SetCountCallbackConfiguration,
26 GetCountCallbackConfiguration,
27 IsPressed,
28 GetSpitfpErrorCount,
29 SetBootloaderMode,
30 GetBootloaderMode,
31 SetWriteFirmwarePointer,
32 WriteFirmware,
33 SetStatusLedConfig,
34 GetStatusLedConfig,
35 GetChipTemperature,
36 Reset,
37 WriteUid,
38 ReadUid,
39 GetIdentity,
40 CallbackCount,
41 CallbackPressed,
42 CallbackReleased,
43}
44impl From<RotaryEncoderV2BrickletFunction> for u8 {
45 fn from(fun: RotaryEncoderV2BrickletFunction) -> Self {
46 match fun {
47 RotaryEncoderV2BrickletFunction::GetCount => 1,
48 RotaryEncoderV2BrickletFunction::SetCountCallbackConfiguration => 2,
49 RotaryEncoderV2BrickletFunction::GetCountCallbackConfiguration => 3,
50 RotaryEncoderV2BrickletFunction::IsPressed => 5,
51 RotaryEncoderV2BrickletFunction::GetSpitfpErrorCount => 234,
52 RotaryEncoderV2BrickletFunction::SetBootloaderMode => 235,
53 RotaryEncoderV2BrickletFunction::GetBootloaderMode => 236,
54 RotaryEncoderV2BrickletFunction::SetWriteFirmwarePointer => 237,
55 RotaryEncoderV2BrickletFunction::WriteFirmware => 238,
56 RotaryEncoderV2BrickletFunction::SetStatusLedConfig => 239,
57 RotaryEncoderV2BrickletFunction::GetStatusLedConfig => 240,
58 RotaryEncoderV2BrickletFunction::GetChipTemperature => 242,
59 RotaryEncoderV2BrickletFunction::Reset => 243,
60 RotaryEncoderV2BrickletFunction::WriteUid => 248,
61 RotaryEncoderV2BrickletFunction::ReadUid => 249,
62 RotaryEncoderV2BrickletFunction::GetIdentity => 255,
63 RotaryEncoderV2BrickletFunction::CallbackCount => 4,
64 RotaryEncoderV2BrickletFunction::CallbackPressed => 6,
65 RotaryEncoderV2BrickletFunction::CallbackReleased => 7,
66 }
67 }
68}
69pub const ROTARY_ENCODER_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
70pub const ROTARY_ENCODER_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
71pub const ROTARY_ENCODER_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
72pub const ROTARY_ENCODER_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
73pub const ROTARY_ENCODER_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
74pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
75pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
76pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
77pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
78pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
79pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
80pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
81pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
82pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
83pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
84pub const ROTARY_ENCODER_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
85pub const ROTARY_ENCODER_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
86pub const ROTARY_ENCODER_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
87pub const ROTARY_ENCODER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
88pub const ROTARY_ENCODER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
89
90#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
91pub struct CountCallbackConfiguration {
92 pub period: u32,
93 pub value_has_to_change: bool,
94 pub option: char,
95 pub min: i32,
96 pub max: i32,
97}
98impl FromByteSlice for CountCallbackConfiguration {
99 fn bytes_expected() -> usize {
100 14
101 }
102 fn from_le_byte_slice(bytes: &[u8]) -> CountCallbackConfiguration {
103 CountCallbackConfiguration {
104 period: <u32>::from_le_byte_slice(&bytes[0..4]),
105 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
106 option: <char>::from_le_byte_slice(&bytes[5..6]),
107 min: <i32>::from_le_byte_slice(&bytes[6..10]),
108 max: <i32>::from_le_byte_slice(&bytes[10..14]),
109 }
110 }
111}
112
113#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
114pub struct SpitfpErrorCount {
115 pub error_count_ack_checksum: u32,
116 pub error_count_message_checksum: u32,
117 pub error_count_frame: u32,
118 pub error_count_overflow: u32,
119}
120impl FromByteSlice for SpitfpErrorCount {
121 fn bytes_expected() -> usize {
122 16
123 }
124 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
125 SpitfpErrorCount {
126 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
127 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
128 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
129 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
130 }
131 }
132}
133
134#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
135pub struct Identity {
136 pub uid: String,
137 pub connected_uid: String,
138 pub position: char,
139 pub hardware_version: [u8; 3],
140 pub firmware_version: [u8; 3],
141 pub device_identifier: u16,
142}
143impl FromByteSlice for Identity {
144 fn bytes_expected() -> usize {
145 25
146 }
147 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
148 Identity {
149 uid: <String>::from_le_byte_slice(&bytes[0..8]),
150 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
151 position: <char>::from_le_byte_slice(&bytes[16..17]),
152 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
153 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
154 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
155 }
156 }
157}
158
159#[derive(Clone)]
161pub struct RotaryEncoderV2Bricklet {
162 device: Device,
163}
164impl RotaryEncoderV2Bricklet {
165 pub const DEVICE_IDENTIFIER: u16 = 294;
166 pub const DEVICE_DISPLAY_NAME: &'static str = "Rotary Encoder Bricklet 2.0";
167 pub fn new(uid: Uid, connection: AsyncIpConnection) -> RotaryEncoderV2Bricklet {
169 let mut result = RotaryEncoderV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
170 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::GetCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
171 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::SetCountCallbackConfiguration) as usize] =
172 ResponseExpectedFlag::True;
173 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::GetCountCallbackConfiguration) as usize] =
174 ResponseExpectedFlag::AlwaysTrue;
175 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::IsPressed) as usize] = ResponseExpectedFlag::AlwaysTrue;
176 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::GetSpitfpErrorCount) as usize] =
177 ResponseExpectedFlag::AlwaysTrue;
178 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::SetBootloaderMode) as usize] =
179 ResponseExpectedFlag::AlwaysTrue;
180 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::GetBootloaderMode) as usize] =
181 ResponseExpectedFlag::AlwaysTrue;
182 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
183 ResponseExpectedFlag::False;
184 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::WriteFirmware) as usize] =
185 ResponseExpectedFlag::AlwaysTrue;
186 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::SetStatusLedConfig) as usize] =
187 ResponseExpectedFlag::False;
188 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::GetStatusLedConfig) as usize] =
189 ResponseExpectedFlag::AlwaysTrue;
190 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::GetChipTemperature) as usize] =
191 ResponseExpectedFlag::AlwaysTrue;
192 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
193 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
194 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
195 result.device.response_expected[u8::from(RotaryEncoderV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
196 result
197 }
198
199 pub fn get_response_expected(&mut self, fun: RotaryEncoderV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
214 self.device.get_response_expected(u8::from(fun))
215 }
216
217 pub fn set_response_expected(
226 &mut self,
227 fun: RotaryEncoderV2BrickletFunction,
228 response_expected: bool,
229 ) -> Result<(), SetResponseExpectedError> {
230 self.device.set_response_expected(u8::from(fun), response_expected)
231 }
232
233 pub fn set_response_expected_all(&mut self, response_expected: bool) {
235 self.device.set_response_expected_all(response_expected)
236 }
237
238 pub fn get_api_version(&self) -> [u8; 3] {
241 self.device.api_version
242 }
243
244 pub async fn get_count_callback_receiver(&mut self) -> impl Stream<Item = i32> {
252 self.device
253 .get_callback_receiver(u8::from(RotaryEncoderV2BrickletFunction::CallbackCount))
254 .await
255 .map(|p| i32::from_le_byte_slice(p.body()))
256 }
257
258 pub async fn get_pressed_callback_receiver(&mut self) -> impl Stream<Item = ()> {
260 self.device.get_callback_receiver(u8::from(RotaryEncoderV2BrickletFunction::CallbackPressed)).await.map(|_p| ())
261 }
262
263 pub async fn get_released_callback_receiver(&mut self) -> impl Stream<Item = ()> {
265 self.device.get_callback_receiver(u8::from(RotaryEncoderV2BrickletFunction::CallbackReleased)).await.map(|_p| ())
266 }
267
268 pub async fn get_count(&mut self, reset: bool) -> Result<i32, TinkerforgeError> {
282 let mut payload = [0; 1];
283 reset.write_to_slice(&mut payload[0..1]);
284
285 #[allow(unused_variables)]
286 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::GetCount), &payload).await?;
287 Ok(i32::from_le_byte_slice(result.body()))
288 }
289
290 pub async fn set_count_callback_configuration(
323 &mut self,
324 period: u32,
325 value_has_to_change: bool,
326 option: char,
327 min: i32,
328 max: i32,
329 ) -> Result<(), TinkerforgeError> {
330 let mut payload = [0; 14];
331 period.write_to_slice(&mut payload[0..4]);
332 value_has_to_change.write_to_slice(&mut payload[4..5]);
333 option.write_to_slice(&mut payload[5..6]);
334 min.write_to_slice(&mut payload[6..10]);
335 max.write_to_slice(&mut payload[10..14]);
336
337 #[allow(unused_variables)]
338 let result = self.device.set(u8::from(RotaryEncoderV2BrickletFunction::SetCountCallbackConfiguration), &payload).await?;
339 Ok(())
340 }
341
342 pub async fn get_count_callback_configuration(&mut self) -> Result<CountCallbackConfiguration, TinkerforgeError> {
351 let payload = [0; 0];
352
353 #[allow(unused_variables)]
354 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::GetCountCallbackConfiguration), &payload).await?;
355 Ok(CountCallbackConfiguration::from_le_byte_slice(result.body()))
356 }
357
358 pub async fn is_pressed(&mut self) -> Result<bool, TinkerforgeError> {
363 let payload = [0; 0];
364
365 #[allow(unused_variables)]
366 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::IsPressed), &payload).await?;
367 Ok(bool::from_le_byte_slice(result.body()))
368 }
369
370 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
382 let payload = [0; 0];
383
384 #[allow(unused_variables)]
385 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
386 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
387 }
388
389 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
412 let mut payload = [0; 1];
413 mode.write_to_slice(&mut payload[0..1]);
414
415 #[allow(unused_variables)]
416 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::SetBootloaderMode), &payload).await?;
417 Ok(u8::from_le_byte_slice(result.body()))
418 }
419
420 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
429 let payload = [0; 0];
430
431 #[allow(unused_variables)]
432 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::GetBootloaderMode), &payload).await?;
433 Ok(u8::from_le_byte_slice(result.body()))
434 }
435
436 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
443 let mut payload = [0; 4];
444 pointer.write_to_slice(&mut payload[0..4]);
445
446 #[allow(unused_variables)]
447 let result = self.device.set(u8::from(RotaryEncoderV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
448 Ok(())
449 }
450
451 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
460 let mut payload = [0; 64];
461 data.write_to_slice(&mut payload[0..64]);
462
463 #[allow(unused_variables)]
464 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::WriteFirmware), &payload).await?;
465 Ok(u8::from_le_byte_slice(result.body()))
466 }
467
468 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
482 let mut payload = [0; 1];
483 config.write_to_slice(&mut payload[0..1]);
484
485 #[allow(unused_variables)]
486 let result = self.device.set(u8::from(RotaryEncoderV2BrickletFunction::SetStatusLedConfig), &payload).await?;
487 Ok(())
488 }
489
490 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
498 let payload = [0; 0];
499
500 #[allow(unused_variables)]
501 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::GetStatusLedConfig), &payload).await?;
502 Ok(u8::from_le_byte_slice(result.body()))
503 }
504
505 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
512 let payload = [0; 0];
513
514 #[allow(unused_variables)]
515 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::GetChipTemperature), &payload).await?;
516 Ok(i16::from_le_byte_slice(result.body()))
517 }
518
519 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
526 let payload = [0; 0];
527
528 #[allow(unused_variables)]
529 let result = self.device.set(u8::from(RotaryEncoderV2BrickletFunction::Reset), &payload).await?;
530 Ok(())
531 }
532
533 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
539 let mut payload = [0; 4];
540 uid.write_to_slice(&mut payload[0..4]);
541
542 #[allow(unused_variables)]
543 let result = self.device.set(u8::from(RotaryEncoderV2BrickletFunction::WriteUid), &payload).await?;
544 Ok(())
545 }
546
547 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
550 let payload = [0; 0];
551
552 #[allow(unused_variables)]
553 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::ReadUid), &payload).await?;
554 Ok(u32::from_le_byte_slice(result.body()))
555 }
556
557 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
568 let payload = [0; 0];
569
570 #[allow(unused_variables)]
571 let result = self.device.get(u8::from(RotaryEncoderV2BrickletFunction::GetIdentity), &payload).await?;
572 Ok(Identity::from_le_byte_slice(result.body()))
573 }
574}