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 JoystickV2BrickletFunction {
24 GetPosition,
25 IsPressed,
26 Calibrate,
27 SetPositionCallbackConfiguration,
28 GetPositionCallbackConfiguration,
29 SetPressedCallbackConfiguration,
30 GetPressedCallbackConfiguration,
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 CallbackPosition,
44 CallbackPressed,
45}
46impl From<JoystickV2BrickletFunction> for u8 {
47 fn from(fun: JoystickV2BrickletFunction) -> Self {
48 match fun {
49 JoystickV2BrickletFunction::GetPosition => 1,
50 JoystickV2BrickletFunction::IsPressed => 2,
51 JoystickV2BrickletFunction::Calibrate => 3,
52 JoystickV2BrickletFunction::SetPositionCallbackConfiguration => 4,
53 JoystickV2BrickletFunction::GetPositionCallbackConfiguration => 5,
54 JoystickV2BrickletFunction::SetPressedCallbackConfiguration => 7,
55 JoystickV2BrickletFunction::GetPressedCallbackConfiguration => 8,
56 JoystickV2BrickletFunction::GetSpitfpErrorCount => 234,
57 JoystickV2BrickletFunction::SetBootloaderMode => 235,
58 JoystickV2BrickletFunction::GetBootloaderMode => 236,
59 JoystickV2BrickletFunction::SetWriteFirmwarePointer => 237,
60 JoystickV2BrickletFunction::WriteFirmware => 238,
61 JoystickV2BrickletFunction::SetStatusLedConfig => 239,
62 JoystickV2BrickletFunction::GetStatusLedConfig => 240,
63 JoystickV2BrickletFunction::GetChipTemperature => 242,
64 JoystickV2BrickletFunction::Reset => 243,
65 JoystickV2BrickletFunction::WriteUid => 248,
66 JoystickV2BrickletFunction::ReadUid => 249,
67 JoystickV2BrickletFunction::GetIdentity => 255,
68 JoystickV2BrickletFunction::CallbackPosition => 6,
69 JoystickV2BrickletFunction::CallbackPressed => 9,
70 }
71 }
72}
73pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
74pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
75pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
76pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
77pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
78pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
79pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
80pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
81pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
82pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
83pub const JOYSTICK_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
84pub const JOYSTICK_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
85pub const JOYSTICK_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
86pub const JOYSTICK_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
87pub const JOYSTICK_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
88
89#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
90pub struct Position {
91 pub x: i16,
92 pub y: i16,
93}
94impl FromByteSlice for Position {
95 fn bytes_expected() -> usize {
96 4
97 }
98 fn from_le_byte_slice(bytes: &[u8]) -> Position {
99 Position { x: <i16>::from_le_byte_slice(&bytes[0..2]), y: <i16>::from_le_byte_slice(&bytes[2..4]) }
100 }
101}
102
103#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
104pub struct PositionCallbackConfiguration {
105 pub period: u32,
106 pub value_has_to_change: bool,
107}
108impl FromByteSlice for PositionCallbackConfiguration {
109 fn bytes_expected() -> usize {
110 5
111 }
112 fn from_le_byte_slice(bytes: &[u8]) -> PositionCallbackConfiguration {
113 PositionCallbackConfiguration {
114 period: <u32>::from_le_byte_slice(&bytes[0..4]),
115 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
116 }
117 }
118}
119
120#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
121pub struct PositionEvent {
122 pub x: i16,
123 pub y: i16,
124}
125impl FromByteSlice for PositionEvent {
126 fn bytes_expected() -> usize {
127 4
128 }
129 fn from_le_byte_slice(bytes: &[u8]) -> PositionEvent {
130 PositionEvent { x: <i16>::from_le_byte_slice(&bytes[0..2]), y: <i16>::from_le_byte_slice(&bytes[2..4]) }
131 }
132}
133
134#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
135pub struct PressedCallbackConfiguration {
136 pub period: u32,
137 pub value_has_to_change: bool,
138}
139impl FromByteSlice for PressedCallbackConfiguration {
140 fn bytes_expected() -> usize {
141 5
142 }
143 fn from_le_byte_slice(bytes: &[u8]) -> PressedCallbackConfiguration {
144 PressedCallbackConfiguration {
145 period: <u32>::from_le_byte_slice(&bytes[0..4]),
146 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
147 }
148 }
149}
150
151#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
152pub struct SpitfpErrorCount {
153 pub error_count_ack_checksum: u32,
154 pub error_count_message_checksum: u32,
155 pub error_count_frame: u32,
156 pub error_count_overflow: u32,
157}
158impl FromByteSlice for SpitfpErrorCount {
159 fn bytes_expected() -> usize {
160 16
161 }
162 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
163 SpitfpErrorCount {
164 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
165 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
166 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
167 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
168 }
169 }
170}
171
172#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
173pub struct Identity {
174 pub uid: String,
175 pub connected_uid: String,
176 pub position: char,
177 pub hardware_version: [u8; 3],
178 pub firmware_version: [u8; 3],
179 pub device_identifier: u16,
180}
181impl FromByteSlice for Identity {
182 fn bytes_expected() -> usize {
183 25
184 }
185 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
186 Identity {
187 uid: <String>::from_le_byte_slice(&bytes[0..8]),
188 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
189 position: <char>::from_le_byte_slice(&bytes[16..17]),
190 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
191 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
192 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
193 }
194 }
195}
196
197#[derive(Clone)]
199pub struct JoystickV2Bricklet {
200 device: Device,
201}
202impl JoystickV2Bricklet {
203 pub const DEVICE_IDENTIFIER: u16 = 2138;
204 pub const DEVICE_DISPLAY_NAME: &'static str = "Joystick Bricklet 2.0";
205 pub fn new(uid: Uid, connection: AsyncIpConnection) -> JoystickV2Bricklet {
207 let mut result = JoystickV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
208 result.device.response_expected[u8::from(JoystickV2BrickletFunction::GetPosition) as usize] = ResponseExpectedFlag::AlwaysTrue;
209 result.device.response_expected[u8::from(JoystickV2BrickletFunction::IsPressed) as usize] = ResponseExpectedFlag::AlwaysTrue;
210 result.device.response_expected[u8::from(JoystickV2BrickletFunction::Calibrate) as usize] = ResponseExpectedFlag::False;
211 result.device.response_expected[u8::from(JoystickV2BrickletFunction::SetPositionCallbackConfiguration) as usize] =
212 ResponseExpectedFlag::True;
213 result.device.response_expected[u8::from(JoystickV2BrickletFunction::GetPositionCallbackConfiguration) as usize] =
214 ResponseExpectedFlag::AlwaysTrue;
215 result.device.response_expected[u8::from(JoystickV2BrickletFunction::SetPressedCallbackConfiguration) as usize] =
216 ResponseExpectedFlag::True;
217 result.device.response_expected[u8::from(JoystickV2BrickletFunction::GetPressedCallbackConfiguration) as usize] =
218 ResponseExpectedFlag::AlwaysTrue;
219 result.device.response_expected[u8::from(JoystickV2BrickletFunction::GetSpitfpErrorCount) as usize] =
220 ResponseExpectedFlag::AlwaysTrue;
221 result.device.response_expected[u8::from(JoystickV2BrickletFunction::SetBootloaderMode) as usize] =
222 ResponseExpectedFlag::AlwaysTrue;
223 result.device.response_expected[u8::from(JoystickV2BrickletFunction::GetBootloaderMode) as usize] =
224 ResponseExpectedFlag::AlwaysTrue;
225 result.device.response_expected[u8::from(JoystickV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
226 ResponseExpectedFlag::False;
227 result.device.response_expected[u8::from(JoystickV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
228 result.device.response_expected[u8::from(JoystickV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
229 result.device.response_expected[u8::from(JoystickV2BrickletFunction::GetStatusLedConfig) as usize] =
230 ResponseExpectedFlag::AlwaysTrue;
231 result.device.response_expected[u8::from(JoystickV2BrickletFunction::GetChipTemperature) as usize] =
232 ResponseExpectedFlag::AlwaysTrue;
233 result.device.response_expected[u8::from(JoystickV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
234 result.device.response_expected[u8::from(JoystickV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
235 result.device.response_expected[u8::from(JoystickV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
236 result.device.response_expected[u8::from(JoystickV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
237 result
238 }
239
240 pub fn get_response_expected(&mut self, fun: JoystickV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
255 self.device.get_response_expected(u8::from(fun))
256 }
257
258 pub fn set_response_expected(
267 &mut self,
268 fun: JoystickV2BrickletFunction,
269 response_expected: bool,
270 ) -> Result<(), SetResponseExpectedError> {
271 self.device.set_response_expected(u8::from(fun), response_expected)
272 }
273
274 pub fn set_response_expected_all(&mut self, response_expected: bool) {
276 self.device.set_response_expected_all(response_expected)
277 }
278
279 pub fn get_api_version(&self) -> [u8; 3] {
282 self.device.api_version
283 }
284
285 pub async fn get_position_callback_receiver(&mut self) -> impl Stream<Item = PositionEvent> {
293 self.device
294 .get_callback_receiver(u8::from(JoystickV2BrickletFunction::CallbackPosition))
295 .await
296 .map(|p| PositionEvent::from_le_byte_slice(p.body()))
297 }
298
299 pub async fn get_pressed_callback_receiver(&mut self) -> impl Stream<Item = bool> {
304 self.device
305 .get_callback_receiver(u8::from(JoystickV2BrickletFunction::CallbackPressed))
306 .await
307 .map(|p| bool::from_le_byte_slice(p.body()))
308 }
309
310 pub async fn get_position(&mut self) -> Result<Position, TinkerforgeError> {
317 let payload = [0; 0];
318
319 #[allow(unused_variables)]
320 let result = self.device.get(u8::from(JoystickV2BrickletFunction::GetPosition), &payload).await?;
321 Ok(Position::from_le_byte_slice(result.body()))
322 }
323
324 pub async fn is_pressed(&mut self) -> Result<bool, TinkerforgeError> {
330 let payload = [0; 0];
331
332 #[allow(unused_variables)]
333 let result = self.device.get(u8::from(JoystickV2BrickletFunction::IsPressed), &payload).await?;
334 Ok(bool::from_le_byte_slice(result.body()))
335 }
336
337 pub async fn calibrate(&mut self) -> Result<(), TinkerforgeError> {
344 let payload = [0; 0];
345
346 #[allow(unused_variables)]
347 let result = self.device.set(u8::from(JoystickV2BrickletFunction::Calibrate), &payload).await?;
348 Ok(())
349 }
350
351 pub async fn set_position_callback_configuration(&mut self, period: u32, value_has_to_change: bool) -> Result<(), TinkerforgeError> {
361 let mut payload = [0; 5];
362 period.write_to_slice(&mut payload[0..4]);
363 value_has_to_change.write_to_slice(&mut payload[4..5]);
364
365 #[allow(unused_variables)]
366 let result = self.device.set(u8::from(JoystickV2BrickletFunction::SetPositionCallbackConfiguration), &payload).await?;
367 Ok(())
368 }
369
370 pub async fn get_position_callback_configuration(&mut self) -> Result<PositionCallbackConfiguration, TinkerforgeError> {
373 let payload = [0; 0];
374
375 #[allow(unused_variables)]
376 let result = self.device.get(u8::from(JoystickV2BrickletFunction::GetPositionCallbackConfiguration), &payload).await?;
377 Ok(PositionCallbackConfiguration::from_le_byte_slice(result.body()))
378 }
379
380 pub async fn set_pressed_callback_configuration(&mut self, period: u32, value_has_to_change: bool) -> Result<(), TinkerforgeError> {
390 let mut payload = [0; 5];
391 period.write_to_slice(&mut payload[0..4]);
392 value_has_to_change.write_to_slice(&mut payload[4..5]);
393
394 #[allow(unused_variables)]
395 let result = self.device.set(u8::from(JoystickV2BrickletFunction::SetPressedCallbackConfiguration), &payload).await?;
396 Ok(())
397 }
398
399 pub async fn get_pressed_callback_configuration(&mut self) -> Result<PressedCallbackConfiguration, TinkerforgeError> {
402 let payload = [0; 0];
403
404 #[allow(unused_variables)]
405 let result = self.device.get(u8::from(JoystickV2BrickletFunction::GetPressedCallbackConfiguration), &payload).await?;
406 Ok(PressedCallbackConfiguration::from_le_byte_slice(result.body()))
407 }
408
409 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
421 let payload = [0; 0];
422
423 #[allow(unused_variables)]
424 let result = self.device.get(u8::from(JoystickV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
425 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
426 }
427
428 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
451 let mut payload = [0; 1];
452 mode.write_to_slice(&mut payload[0..1]);
453
454 #[allow(unused_variables)]
455 let result = self.device.get(u8::from(JoystickV2BrickletFunction::SetBootloaderMode), &payload).await?;
456 Ok(u8::from_le_byte_slice(result.body()))
457 }
458
459 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
468 let payload = [0; 0];
469
470 #[allow(unused_variables)]
471 let result = self.device.get(u8::from(JoystickV2BrickletFunction::GetBootloaderMode), &payload).await?;
472 Ok(u8::from_le_byte_slice(result.body()))
473 }
474
475 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
482 let mut payload = [0; 4];
483 pointer.write_to_slice(&mut payload[0..4]);
484
485 #[allow(unused_variables)]
486 let result = self.device.set(u8::from(JoystickV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
487 Ok(())
488 }
489
490 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
499 let mut payload = [0; 64];
500 data.write_to_slice(&mut payload[0..64]);
501
502 #[allow(unused_variables)]
503 let result = self.device.get(u8::from(JoystickV2BrickletFunction::WriteFirmware), &payload).await?;
504 Ok(u8::from_le_byte_slice(result.body()))
505 }
506
507 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
521 let mut payload = [0; 1];
522 config.write_to_slice(&mut payload[0..1]);
523
524 #[allow(unused_variables)]
525 let result = self.device.set(u8::from(JoystickV2BrickletFunction::SetStatusLedConfig), &payload).await?;
526 Ok(())
527 }
528
529 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
537 let payload = [0; 0];
538
539 #[allow(unused_variables)]
540 let result = self.device.get(u8::from(JoystickV2BrickletFunction::GetStatusLedConfig), &payload).await?;
541 Ok(u8::from_le_byte_slice(result.body()))
542 }
543
544 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
551 let payload = [0; 0];
552
553 #[allow(unused_variables)]
554 let result = self.device.get(u8::from(JoystickV2BrickletFunction::GetChipTemperature), &payload).await?;
555 Ok(i16::from_le_byte_slice(result.body()))
556 }
557
558 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
565 let payload = [0; 0];
566
567 #[allow(unused_variables)]
568 let result = self.device.set(u8::from(JoystickV2BrickletFunction::Reset), &payload).await?;
569 Ok(())
570 }
571
572 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
578 let mut payload = [0; 4];
579 uid.write_to_slice(&mut payload[0..4]);
580
581 #[allow(unused_variables)]
582 let result = self.device.set(u8::from(JoystickV2BrickletFunction::WriteUid), &payload).await?;
583 Ok(())
584 }
585
586 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
589 let payload = [0; 0];
590
591 #[allow(unused_variables)]
592 let result = self.device.get(u8::from(JoystickV2BrickletFunction::ReadUid), &payload).await?;
593 Ok(u32::from_le_byte_slice(result.body()))
594 }
595
596 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
607 let payload = [0; 0];
608
609 #[allow(unused_variables)]
610 let result = self.device.get(u8::from(JoystickV2BrickletFunction::GetIdentity), &payload).await?;
611 Ok(Identity::from_le_byte_slice(result.body()))
612 }
613}