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 JoystickBrickletFunction {
24 GetPosition,
25 IsPressed,
26 GetAnalogValue,
27 Calibrate,
28 SetPositionCallbackPeriod,
29 GetPositionCallbackPeriod,
30 SetAnalogValueCallbackPeriod,
31 GetAnalogValueCallbackPeriod,
32 SetPositionCallbackThreshold,
33 GetPositionCallbackThreshold,
34 SetAnalogValueCallbackThreshold,
35 GetAnalogValueCallbackThreshold,
36 SetDebouncePeriod,
37 GetDebouncePeriod,
38 GetIdentity,
39 CallbackPosition,
40 CallbackAnalogValue,
41 CallbackPositionReached,
42 CallbackAnalogValueReached,
43 CallbackPressed,
44 CallbackReleased,
45}
46impl From<JoystickBrickletFunction> for u8 {
47 fn from(fun: JoystickBrickletFunction) -> Self {
48 match fun {
49 JoystickBrickletFunction::GetPosition => 1,
50 JoystickBrickletFunction::IsPressed => 2,
51 JoystickBrickletFunction::GetAnalogValue => 3,
52 JoystickBrickletFunction::Calibrate => 4,
53 JoystickBrickletFunction::SetPositionCallbackPeriod => 5,
54 JoystickBrickletFunction::GetPositionCallbackPeriod => 6,
55 JoystickBrickletFunction::SetAnalogValueCallbackPeriod => 7,
56 JoystickBrickletFunction::GetAnalogValueCallbackPeriod => 8,
57 JoystickBrickletFunction::SetPositionCallbackThreshold => 9,
58 JoystickBrickletFunction::GetPositionCallbackThreshold => 10,
59 JoystickBrickletFunction::SetAnalogValueCallbackThreshold => 11,
60 JoystickBrickletFunction::GetAnalogValueCallbackThreshold => 12,
61 JoystickBrickletFunction::SetDebouncePeriod => 13,
62 JoystickBrickletFunction::GetDebouncePeriod => 14,
63 JoystickBrickletFunction::GetIdentity => 255,
64 JoystickBrickletFunction::CallbackPosition => 15,
65 JoystickBrickletFunction::CallbackAnalogValue => 16,
66 JoystickBrickletFunction::CallbackPositionReached => 17,
67 JoystickBrickletFunction::CallbackAnalogValueReached => 18,
68 JoystickBrickletFunction::CallbackPressed => 19,
69 JoystickBrickletFunction::CallbackReleased => 20,
70 }
71 }
72}
73pub const JOYSTICK_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
74pub const JOYSTICK_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
75pub const JOYSTICK_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
76pub const JOYSTICK_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
77pub const JOYSTICK_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
78
79#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
80pub struct Position {
81 pub x: i16,
82 pub y: i16,
83}
84impl FromByteSlice for Position {
85 fn bytes_expected() -> usize {
86 4
87 }
88 fn from_le_byte_slice(bytes: &[u8]) -> Position {
89 Position { x: <i16>::from_le_byte_slice(&bytes[0..2]), y: <i16>::from_le_byte_slice(&bytes[2..4]) }
90 }
91}
92
93#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
94pub struct AnalogValue {
95 pub x: u16,
96 pub y: u16,
97}
98impl FromByteSlice for AnalogValue {
99 fn bytes_expected() -> usize {
100 4
101 }
102 fn from_le_byte_slice(bytes: &[u8]) -> AnalogValue {
103 AnalogValue { x: <u16>::from_le_byte_slice(&bytes[0..2]), y: <u16>::from_le_byte_slice(&bytes[2..4]) }
104 }
105}
106
107#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
108pub struct PositionCallbackThreshold {
109 pub option: char,
110 pub min_x: i16,
111 pub max_x: i16,
112 pub min_y: i16,
113 pub max_y: i16,
114}
115impl FromByteSlice for PositionCallbackThreshold {
116 fn bytes_expected() -> usize {
117 9
118 }
119 fn from_le_byte_slice(bytes: &[u8]) -> PositionCallbackThreshold {
120 PositionCallbackThreshold {
121 option: <char>::from_le_byte_slice(&bytes[0..1]),
122 min_x: <i16>::from_le_byte_slice(&bytes[1..3]),
123 max_x: <i16>::from_le_byte_slice(&bytes[3..5]),
124 min_y: <i16>::from_le_byte_slice(&bytes[5..7]),
125 max_y: <i16>::from_le_byte_slice(&bytes[7..9]),
126 }
127 }
128}
129
130#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
131pub struct AnalogValueCallbackThreshold {
132 pub option: char,
133 pub min_x: u16,
134 pub max_x: u16,
135 pub min_y: u16,
136 pub max_y: u16,
137}
138impl FromByteSlice for AnalogValueCallbackThreshold {
139 fn bytes_expected() -> usize {
140 9
141 }
142 fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueCallbackThreshold {
143 AnalogValueCallbackThreshold {
144 option: <char>::from_le_byte_slice(&bytes[0..1]),
145 min_x: <u16>::from_le_byte_slice(&bytes[1..3]),
146 max_x: <u16>::from_le_byte_slice(&bytes[3..5]),
147 min_y: <u16>::from_le_byte_slice(&bytes[5..7]),
148 max_y: <u16>::from_le_byte_slice(&bytes[7..9]),
149 }
150 }
151}
152
153#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
154pub struct PositionEvent {
155 pub x: i16,
156 pub y: i16,
157}
158impl FromByteSlice for PositionEvent {
159 fn bytes_expected() -> usize {
160 4
161 }
162 fn from_le_byte_slice(bytes: &[u8]) -> PositionEvent {
163 PositionEvent { x: <i16>::from_le_byte_slice(&bytes[0..2]), y: <i16>::from_le_byte_slice(&bytes[2..4]) }
164 }
165}
166
167#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
168pub struct AnalogValueEvent {
169 pub x: u16,
170 pub y: u16,
171}
172impl FromByteSlice for AnalogValueEvent {
173 fn bytes_expected() -> usize {
174 4
175 }
176 fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueEvent {
177 AnalogValueEvent { x: <u16>::from_le_byte_slice(&bytes[0..2]), y: <u16>::from_le_byte_slice(&bytes[2..4]) }
178 }
179}
180
181#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
182pub struct PositionReachedEvent {
183 pub x: i16,
184 pub y: i16,
185}
186impl FromByteSlice for PositionReachedEvent {
187 fn bytes_expected() -> usize {
188 4
189 }
190 fn from_le_byte_slice(bytes: &[u8]) -> PositionReachedEvent {
191 PositionReachedEvent { x: <i16>::from_le_byte_slice(&bytes[0..2]), y: <i16>::from_le_byte_slice(&bytes[2..4]) }
192 }
193}
194
195#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
196pub struct AnalogValueReachedEvent {
197 pub x: u16,
198 pub y: u16,
199}
200impl FromByteSlice for AnalogValueReachedEvent {
201 fn bytes_expected() -> usize {
202 4
203 }
204 fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueReachedEvent {
205 AnalogValueReachedEvent { x: <u16>::from_le_byte_slice(&bytes[0..2]), y: <u16>::from_le_byte_slice(&bytes[2..4]) }
206 }
207}
208
209#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
210pub struct Identity {
211 pub uid: String,
212 pub connected_uid: String,
213 pub position: char,
214 pub hardware_version: [u8; 3],
215 pub firmware_version: [u8; 3],
216 pub device_identifier: u16,
217}
218impl FromByteSlice for Identity {
219 fn bytes_expected() -> usize {
220 25
221 }
222 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
223 Identity {
224 uid: <String>::from_le_byte_slice(&bytes[0..8]),
225 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
226 position: <char>::from_le_byte_slice(&bytes[16..17]),
227 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
228 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
229 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
230 }
231 }
232}
233
234#[derive(Clone)]
236pub struct JoystickBricklet {
237 device: Device,
238}
239impl JoystickBricklet {
240 pub const DEVICE_IDENTIFIER: u16 = 210;
241 pub const DEVICE_DISPLAY_NAME: &'static str = "Joystick Bricklet";
242 pub fn new(uid: Uid, connection: AsyncIpConnection) -> JoystickBricklet {
244 let mut result = JoystickBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
245 result.device.response_expected[u8::from(JoystickBrickletFunction::GetPosition) as usize] = ResponseExpectedFlag::AlwaysTrue;
246 result.device.response_expected[u8::from(JoystickBrickletFunction::IsPressed) as usize] = ResponseExpectedFlag::AlwaysTrue;
247 result.device.response_expected[u8::from(JoystickBrickletFunction::GetAnalogValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
248 result.device.response_expected[u8::from(JoystickBrickletFunction::Calibrate) as usize] = ResponseExpectedFlag::False;
249 result.device.response_expected[u8::from(JoystickBrickletFunction::SetPositionCallbackPeriod) as usize] =
250 ResponseExpectedFlag::True;
251 result.device.response_expected[u8::from(JoystickBrickletFunction::GetPositionCallbackPeriod) as usize] =
252 ResponseExpectedFlag::AlwaysTrue;
253 result.device.response_expected[u8::from(JoystickBrickletFunction::SetAnalogValueCallbackPeriod) as usize] =
254 ResponseExpectedFlag::True;
255 result.device.response_expected[u8::from(JoystickBrickletFunction::GetAnalogValueCallbackPeriod) as usize] =
256 ResponseExpectedFlag::AlwaysTrue;
257 result.device.response_expected[u8::from(JoystickBrickletFunction::SetPositionCallbackThreshold) as usize] =
258 ResponseExpectedFlag::True;
259 result.device.response_expected[u8::from(JoystickBrickletFunction::GetPositionCallbackThreshold) as usize] =
260 ResponseExpectedFlag::AlwaysTrue;
261 result.device.response_expected[u8::from(JoystickBrickletFunction::SetAnalogValueCallbackThreshold) as usize] =
262 ResponseExpectedFlag::True;
263 result.device.response_expected[u8::from(JoystickBrickletFunction::GetAnalogValueCallbackThreshold) as usize] =
264 ResponseExpectedFlag::AlwaysTrue;
265 result.device.response_expected[u8::from(JoystickBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
266 result.device.response_expected[u8::from(JoystickBrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
267 result.device.response_expected[u8::from(JoystickBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
268 result
269 }
270
271 pub fn get_response_expected(&mut self, fun: JoystickBrickletFunction) -> Result<bool, GetResponseExpectedError> {
286 self.device.get_response_expected(u8::from(fun))
287 }
288
289 pub fn set_response_expected(
298 &mut self,
299 fun: JoystickBrickletFunction,
300 response_expected: bool,
301 ) -> Result<(), SetResponseExpectedError> {
302 self.device.set_response_expected(u8::from(fun), response_expected)
303 }
304
305 pub fn set_response_expected_all(&mut self, response_expected: bool) {
307 self.device.set_response_expected_all(response_expected)
308 }
309
310 pub fn get_api_version(&self) -> [u8; 3] {
313 self.device.api_version
314 }
315
316 pub async fn get_position_callback_receiver(&mut self) -> impl Stream<Item = PositionEvent> {
326 self.device
327 .get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackPosition))
328 .await
329 .map(|p| PositionEvent::from_le_byte_slice(p.body()))
330 }
331
332 pub async fn get_analog_value_callback_receiver(&mut self) -> impl Stream<Item = AnalogValueEvent> {
339 self.device
340 .get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackAnalogValue))
341 .await
342 .map(|p| AnalogValueEvent::from_le_byte_slice(p.body()))
343 }
344
345 pub async fn get_position_reached_callback_receiver(&mut self) -> impl Stream<Item = PositionReachedEvent> {
352 self.device
353 .get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackPositionReached))
354 .await
355 .map(|p| PositionReachedEvent::from_le_byte_slice(p.body()))
356 }
357
358 pub async fn get_analog_value_reached_callback_receiver(&mut self) -> impl Stream<Item = AnalogValueReachedEvent> {
365 self.device
366 .get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackAnalogValueReached))
367 .await
368 .map(|p| AnalogValueReachedEvent::from_le_byte_slice(p.body()))
369 }
370
371 pub async fn get_pressed_callback_receiver(&mut self) -> impl Stream<Item = ()> {
373 self.device.get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackPressed)).await.map(|_p| ())
374 }
375
376 pub async fn get_released_callback_receiver(&mut self) -> impl Stream<Item = ()> {
378 self.device.get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackReleased)).await.map(|_p| ())
379 }
380
381 pub async fn get_position(&mut self) -> Result<Position, TinkerforgeError> {
388 let payload = [0; 0];
389
390 #[allow(unused_variables)]
391 let result = self.device.get(u8::from(JoystickBrickletFunction::GetPosition), &payload).await?;
392 Ok(Position::from_le_byte_slice(result.body()))
393 }
394
395 pub async fn is_pressed(&mut self) -> Result<bool, TinkerforgeError> {
400 let payload = [0; 0];
401
402 #[allow(unused_variables)]
403 let result = self.device.get(u8::from(JoystickBrickletFunction::IsPressed), &payload).await?;
404 Ok(bool::from_le_byte_slice(result.body()))
405 }
406
407 pub async fn get_analog_value(&mut self) -> Result<AnalogValue, TinkerforgeError> {
419 let payload = [0; 0];
420
421 #[allow(unused_variables)]
422 let result = self.device.get(u8::from(JoystickBrickletFunction::GetAnalogValue), &payload).await?;
423 Ok(AnalogValue::from_le_byte_slice(result.body()))
424 }
425
426 pub async fn calibrate(&mut self) -> Result<(), TinkerforgeError> {
433 let payload = [0; 0];
434
435 #[allow(unused_variables)]
436 let result = self.device.set(u8::from(JoystickBrickletFunction::Calibrate), &payload).await?;
437 Ok(())
438 }
439
440 pub async fn set_position_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
446 let mut payload = [0; 4];
447 period.write_to_slice(&mut payload[0..4]);
448
449 #[allow(unused_variables)]
450 let result = self.device.set(u8::from(JoystickBrickletFunction::SetPositionCallbackPeriod), &payload).await?;
451 Ok(())
452 }
453
454 pub async fn get_position_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
456 let payload = [0; 0];
457
458 #[allow(unused_variables)]
459 let result = self.device.get(u8::from(JoystickBrickletFunction::GetPositionCallbackPeriod), &payload).await?;
460 Ok(u32::from_le_byte_slice(result.body()))
461 }
462
463 pub async fn set_analog_value_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
469 let mut payload = [0; 4];
470 period.write_to_slice(&mut payload[0..4]);
471
472 #[allow(unused_variables)]
473 let result = self.device.set(u8::from(JoystickBrickletFunction::SetAnalogValueCallbackPeriod), &payload).await?;
474 Ok(())
475 }
476
477 pub async fn get_analog_value_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
479 let payload = [0; 0];
480
481 #[allow(unused_variables)]
482 let result = self.device.get(u8::from(JoystickBrickletFunction::GetAnalogValueCallbackPeriod), &payload).await?;
483 Ok(u32::from_le_byte_slice(result.body()))
484 }
485
486 pub async fn set_position_callback_threshold(
505 &mut self,
506 option: char,
507 min_x: i16,
508 max_x: i16,
509 min_y: i16,
510 max_y: i16,
511 ) -> Result<(), TinkerforgeError> {
512 let mut payload = [0; 9];
513 option.write_to_slice(&mut payload[0..1]);
514 min_x.write_to_slice(&mut payload[1..3]);
515 max_x.write_to_slice(&mut payload[3..5]);
516 min_y.write_to_slice(&mut payload[5..7]);
517 max_y.write_to_slice(&mut payload[7..9]);
518
519 #[allow(unused_variables)]
520 let result = self.device.set(u8::from(JoystickBrickletFunction::SetPositionCallbackThreshold), &payload).await?;
521 Ok(())
522 }
523
524 pub async fn get_position_callback_threshold(&mut self) -> Result<PositionCallbackThreshold, TinkerforgeError> {
533 let payload = [0; 0];
534
535 #[allow(unused_variables)]
536 let result = self.device.get(u8::from(JoystickBrickletFunction::GetPositionCallbackThreshold), &payload).await?;
537 Ok(PositionCallbackThreshold::from_le_byte_slice(result.body()))
538 }
539
540 pub async fn set_analog_value_callback_threshold(
559 &mut self,
560 option: char,
561 min_x: u16,
562 max_x: u16,
563 min_y: u16,
564 max_y: u16,
565 ) -> Result<(), TinkerforgeError> {
566 let mut payload = [0; 9];
567 option.write_to_slice(&mut payload[0..1]);
568 min_x.write_to_slice(&mut payload[1..3]);
569 max_x.write_to_slice(&mut payload[3..5]);
570 min_y.write_to_slice(&mut payload[5..7]);
571 max_y.write_to_slice(&mut payload[7..9]);
572
573 #[allow(unused_variables)]
574 let result = self.device.set(u8::from(JoystickBrickletFunction::SetAnalogValueCallbackThreshold), &payload).await?;
575 Ok(())
576 }
577
578 pub async fn get_analog_value_callback_threshold(&mut self) -> Result<AnalogValueCallbackThreshold, TinkerforgeError> {
587 let payload = [0; 0];
588
589 #[allow(unused_variables)]
590 let result = self.device.get(u8::from(JoystickBrickletFunction::GetAnalogValueCallbackThreshold), &payload).await?;
591 Ok(AnalogValueCallbackThreshold::from_le_byte_slice(result.body()))
592 }
593
594 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
606 let mut payload = [0; 4];
607 debounce.write_to_slice(&mut payload[0..4]);
608
609 #[allow(unused_variables)]
610 let result = self.device.set(u8::from(JoystickBrickletFunction::SetDebouncePeriod), &payload).await?;
611 Ok(())
612 }
613
614 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
616 let payload = [0; 0];
617
618 #[allow(unused_variables)]
619 let result = self.device.get(u8::from(JoystickBrickletFunction::GetDebouncePeriod), &payload).await?;
620 Ok(u32::from_le_byte_slice(result.body()))
621 }
622
623 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
634 let payload = [0; 0];
635
636 #[allow(unused_variables)]
637 let result = self.device.get(u8::from(JoystickBrickletFunction::GetIdentity), &payload).await?;
638 Ok(Identity::from_le_byte_slice(result.body()))
639 }
640}