#[allow(unused_imports)]
use crate::{
byte_converter::*, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection, low_level_traits::LowLevelRead,
};
#[allow(unused_imports)]
use futures_core::Stream;
#[allow(unused_imports)]
use tokio_stream::StreamExt;
pub enum JoystickBrickletFunction {
GetPosition,
IsPressed,
GetAnalogValue,
Calibrate,
SetPositionCallbackPeriod,
GetPositionCallbackPeriod,
SetAnalogValueCallbackPeriod,
GetAnalogValueCallbackPeriod,
SetPositionCallbackThreshold,
GetPositionCallbackThreshold,
SetAnalogValueCallbackThreshold,
GetAnalogValueCallbackThreshold,
SetDebouncePeriod,
GetDebouncePeriod,
GetIdentity,
CallbackPosition,
CallbackAnalogValue,
CallbackPositionReached,
CallbackAnalogValueReached,
CallbackPressed,
CallbackReleased,
}
impl From<JoystickBrickletFunction> for u8 {
fn from(fun: JoystickBrickletFunction) -> Self {
match fun {
JoystickBrickletFunction::GetPosition => 1,
JoystickBrickletFunction::IsPressed => 2,
JoystickBrickletFunction::GetAnalogValue => 3,
JoystickBrickletFunction::Calibrate => 4,
JoystickBrickletFunction::SetPositionCallbackPeriod => 5,
JoystickBrickletFunction::GetPositionCallbackPeriod => 6,
JoystickBrickletFunction::SetAnalogValueCallbackPeriod => 7,
JoystickBrickletFunction::GetAnalogValueCallbackPeriod => 8,
JoystickBrickletFunction::SetPositionCallbackThreshold => 9,
JoystickBrickletFunction::GetPositionCallbackThreshold => 10,
JoystickBrickletFunction::SetAnalogValueCallbackThreshold => 11,
JoystickBrickletFunction::GetAnalogValueCallbackThreshold => 12,
JoystickBrickletFunction::SetDebouncePeriod => 13,
JoystickBrickletFunction::GetDebouncePeriod => 14,
JoystickBrickletFunction::GetIdentity => 255,
JoystickBrickletFunction::CallbackPosition => 15,
JoystickBrickletFunction::CallbackAnalogValue => 16,
JoystickBrickletFunction::CallbackPositionReached => 17,
JoystickBrickletFunction::CallbackAnalogValueReached => 18,
JoystickBrickletFunction::CallbackPressed => 19,
JoystickBrickletFunction::CallbackReleased => 20,
}
}
}
pub const JOYSTICK_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
pub const JOYSTICK_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
pub const JOYSTICK_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
pub const JOYSTICK_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
pub const JOYSTICK_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Position {
pub x: i16,
pub y: i16,
}
impl FromByteSlice for Position {
fn bytes_expected() -> usize {
4
}
fn from_le_byte_slice(bytes: &[u8]) -> Position {
Position { x: <i16>::from_le_byte_slice(&bytes[0..2]), y: <i16>::from_le_byte_slice(&bytes[2..4]) }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AnalogValue {
pub x: u16,
pub y: u16,
}
impl FromByteSlice for AnalogValue {
fn bytes_expected() -> usize {
4
}
fn from_le_byte_slice(bytes: &[u8]) -> AnalogValue {
AnalogValue { x: <u16>::from_le_byte_slice(&bytes[0..2]), y: <u16>::from_le_byte_slice(&bytes[2..4]) }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PositionCallbackThreshold {
pub option: char,
pub min_x: i16,
pub max_x: i16,
pub min_y: i16,
pub max_y: i16,
}
impl FromByteSlice for PositionCallbackThreshold {
fn bytes_expected() -> usize {
9
}
fn from_le_byte_slice(bytes: &[u8]) -> PositionCallbackThreshold {
PositionCallbackThreshold {
option: <char>::from_le_byte_slice(&bytes[0..1]),
min_x: <i16>::from_le_byte_slice(&bytes[1..3]),
max_x: <i16>::from_le_byte_slice(&bytes[3..5]),
min_y: <i16>::from_le_byte_slice(&bytes[5..7]),
max_y: <i16>::from_le_byte_slice(&bytes[7..9]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AnalogValueCallbackThreshold {
pub option: char,
pub min_x: u16,
pub max_x: u16,
pub min_y: u16,
pub max_y: u16,
}
impl FromByteSlice for AnalogValueCallbackThreshold {
fn bytes_expected() -> usize {
9
}
fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueCallbackThreshold {
AnalogValueCallbackThreshold {
option: <char>::from_le_byte_slice(&bytes[0..1]),
min_x: <u16>::from_le_byte_slice(&bytes[1..3]),
max_x: <u16>::from_le_byte_slice(&bytes[3..5]),
min_y: <u16>::from_le_byte_slice(&bytes[5..7]),
max_y: <u16>::from_le_byte_slice(&bytes[7..9]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PositionEvent {
pub x: i16,
pub y: i16,
}
impl FromByteSlice for PositionEvent {
fn bytes_expected() -> usize {
4
}
fn from_le_byte_slice(bytes: &[u8]) -> PositionEvent {
PositionEvent { x: <i16>::from_le_byte_slice(&bytes[0..2]), y: <i16>::from_le_byte_slice(&bytes[2..4]) }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AnalogValueEvent {
pub x: u16,
pub y: u16,
}
impl FromByteSlice for AnalogValueEvent {
fn bytes_expected() -> usize {
4
}
fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueEvent {
AnalogValueEvent { x: <u16>::from_le_byte_slice(&bytes[0..2]), y: <u16>::from_le_byte_slice(&bytes[2..4]) }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PositionReachedEvent {
pub x: i16,
pub y: i16,
}
impl FromByteSlice for PositionReachedEvent {
fn bytes_expected() -> usize {
4
}
fn from_le_byte_slice(bytes: &[u8]) -> PositionReachedEvent {
PositionReachedEvent { x: <i16>::from_le_byte_slice(&bytes[0..2]), y: <i16>::from_le_byte_slice(&bytes[2..4]) }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AnalogValueReachedEvent {
pub x: u16,
pub y: u16,
}
impl FromByteSlice for AnalogValueReachedEvent {
fn bytes_expected() -> usize {
4
}
fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueReachedEvent {
AnalogValueReachedEvent { x: <u16>::from_le_byte_slice(&bytes[0..2]), y: <u16>::from_le_byte_slice(&bytes[2..4]) }
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Identity {
pub uid: String,
pub connected_uid: String,
pub position: char,
pub hardware_version: [u8; 3],
pub firmware_version: [u8; 3],
pub device_identifier: u16,
}
impl FromByteSlice for Identity {
fn bytes_expected() -> usize {
25
}
fn from_le_byte_slice(bytes: &[u8]) -> Identity {
Identity {
uid: <String>::from_le_byte_slice(&bytes[0..8]),
connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
position: <char>::from_le_byte_slice(&bytes[16..17]),
hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
}
}
}
#[derive(Clone)]
pub struct JoystickBricklet {
device: Device,
}
impl JoystickBricklet {
pub const DEVICE_IDENTIFIER: u16 = 210;
pub const DEVICE_DISPLAY_NAME: &'static str = "Joystick Bricklet";
pub fn new(uid: u32, connection: AsyncIpConnection) -> JoystickBricklet {
let mut result = JoystickBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
result.device.response_expected[u8::from(JoystickBrickletFunction::GetPosition) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(JoystickBrickletFunction::IsPressed) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(JoystickBrickletFunction::GetAnalogValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(JoystickBrickletFunction::Calibrate) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(JoystickBrickletFunction::SetPositionCallbackPeriod) as usize] =
ResponseExpectedFlag::True;
result.device.response_expected[u8::from(JoystickBrickletFunction::GetPositionCallbackPeriod) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(JoystickBrickletFunction::SetAnalogValueCallbackPeriod) as usize] =
ResponseExpectedFlag::True;
result.device.response_expected[u8::from(JoystickBrickletFunction::GetAnalogValueCallbackPeriod) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(JoystickBrickletFunction::SetPositionCallbackThreshold) as usize] =
ResponseExpectedFlag::True;
result.device.response_expected[u8::from(JoystickBrickletFunction::GetPositionCallbackThreshold) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(JoystickBrickletFunction::SetAnalogValueCallbackThreshold) as usize] =
ResponseExpectedFlag::True;
result.device.response_expected[u8::from(JoystickBrickletFunction::GetAnalogValueCallbackThreshold) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(JoystickBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
result.device.response_expected[u8::from(JoystickBrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(JoystickBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
result
}
pub fn get_response_expected(&mut self, fun: JoystickBrickletFunction) -> Result<bool, GetResponseExpectedError> {
self.device.get_response_expected(u8::from(fun))
}
pub fn set_response_expected(
&mut self,
fun: JoystickBrickletFunction,
response_expected: bool,
) -> Result<(), SetResponseExpectedError> {
self.device.set_response_expected(u8::from(fun), response_expected)
}
pub fn set_response_expected_all(&mut self, response_expected: bool) {
self.device.set_response_expected_all(response_expected)
}
pub fn get_api_version(&self) -> [u8; 3] {
self.device.api_version
}
pub async fn get_position_callback_receiver(&mut self) -> impl Stream<Item = PositionEvent> {
self.device
.get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackPosition))
.await
.map(|p| PositionEvent::from_le_byte_slice(p.body()))
}
pub async fn get_analog_value_callback_receiver(&mut self) -> impl Stream<Item = AnalogValueEvent> {
self.device
.get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackAnalogValue))
.await
.map(|p| AnalogValueEvent::from_le_byte_slice(p.body()))
}
pub async fn get_position_reached_callback_receiver(&mut self) -> impl Stream<Item = PositionReachedEvent> {
self.device
.get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackPositionReached))
.await
.map(|p| PositionReachedEvent::from_le_byte_slice(p.body()))
}
pub async fn get_analog_value_reached_callback_receiver(&mut self) -> impl Stream<Item = AnalogValueReachedEvent> {
self.device
.get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackAnalogValueReached))
.await
.map(|p| AnalogValueReachedEvent::from_le_byte_slice(p.body()))
}
pub async fn get_pressed_callback_receiver(&mut self) -> impl Stream<Item = ()> {
self.device.get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackPressed)).await.map(|_p| ())
}
pub async fn get_released_callback_receiver(&mut self) -> impl Stream<Item = ()> {
self.device.get_callback_receiver(u8::from(JoystickBrickletFunction::CallbackReleased)).await.map(|_p| ())
}
pub async fn get_position(&mut self) -> Result<Position, TinkerforgeError> {
let payload = [0; 0];
#[allow(unused_variables)]
let result = self.device.get(u8::from(JoystickBrickletFunction::GetPosition), &payload).await?;
Ok(Position::from_le_byte_slice(result.body()))
}
pub async fn is_pressed(&mut self) -> Result<bool, TinkerforgeError> {
let payload = [0; 0];
#[allow(unused_variables)]
let result = self.device.get(u8::from(JoystickBrickletFunction::IsPressed), &payload).await?;
Ok(bool::from_le_byte_slice(result.body()))
}
pub async fn get_analog_value(&mut self) -> Result<AnalogValue, TinkerforgeError> {
let payload = [0; 0];
#[allow(unused_variables)]
let result = self.device.get(u8::from(JoystickBrickletFunction::GetAnalogValue), &payload).await?;
Ok(AnalogValue::from_le_byte_slice(result.body()))
}
pub async fn calibrate(&mut self) -> Result<(), TinkerforgeError> {
let payload = [0; 0];
#[allow(unused_variables)]
let result = self.device.set(u8::from(JoystickBrickletFunction::Calibrate), &payload).await?;
Ok(())
}
pub async fn set_position_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
let mut payload = [0; 4];
period.write_to_slice(&mut payload[0..4]);
#[allow(unused_variables)]
let result = self.device.set(u8::from(JoystickBrickletFunction::SetPositionCallbackPeriod), &payload).await?;
Ok(())
}
pub async fn get_position_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
let payload = [0; 0];
#[allow(unused_variables)]
let result = self.device.get(u8::from(JoystickBrickletFunction::GetPositionCallbackPeriod), &payload).await?;
Ok(u32::from_le_byte_slice(result.body()))
}
pub async fn set_analog_value_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
let mut payload = [0; 4];
period.write_to_slice(&mut payload[0..4]);
#[allow(unused_variables)]
let result = self.device.set(u8::from(JoystickBrickletFunction::SetAnalogValueCallbackPeriod), &payload).await?;
Ok(())
}
pub async fn get_analog_value_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
let payload = [0; 0];
#[allow(unused_variables)]
let result = self.device.get(u8::from(JoystickBrickletFunction::GetAnalogValueCallbackPeriod), &payload).await?;
Ok(u32::from_le_byte_slice(result.body()))
}
pub async fn set_position_callback_threshold(
&mut self,
option: char,
min_x: i16,
max_x: i16,
min_y: i16,
max_y: i16,
) -> Result<(), TinkerforgeError> {
let mut payload = [0; 9];
option.write_to_slice(&mut payload[0..1]);
min_x.write_to_slice(&mut payload[1..3]);
max_x.write_to_slice(&mut payload[3..5]);
min_y.write_to_slice(&mut payload[5..7]);
max_y.write_to_slice(&mut payload[7..9]);
#[allow(unused_variables)]
let result = self.device.set(u8::from(JoystickBrickletFunction::SetPositionCallbackThreshold), &payload).await?;
Ok(())
}
pub async fn get_position_callback_threshold(&mut self) -> Result<PositionCallbackThreshold, TinkerforgeError> {
let payload = [0; 0];
#[allow(unused_variables)]
let result = self.device.get(u8::from(JoystickBrickletFunction::GetPositionCallbackThreshold), &payload).await?;
Ok(PositionCallbackThreshold::from_le_byte_slice(result.body()))
}
pub async fn set_analog_value_callback_threshold(
&mut self,
option: char,
min_x: u16,
max_x: u16,
min_y: u16,
max_y: u16,
) -> Result<(), TinkerforgeError> {
let mut payload = [0; 9];
option.write_to_slice(&mut payload[0..1]);
min_x.write_to_slice(&mut payload[1..3]);
max_x.write_to_slice(&mut payload[3..5]);
min_y.write_to_slice(&mut payload[5..7]);
max_y.write_to_slice(&mut payload[7..9]);
#[allow(unused_variables)]
let result = self.device.set(u8::from(JoystickBrickletFunction::SetAnalogValueCallbackThreshold), &payload).await?;
Ok(())
}
pub async fn get_analog_value_callback_threshold(&mut self) -> Result<AnalogValueCallbackThreshold, TinkerforgeError> {
let payload = [0; 0];
#[allow(unused_variables)]
let result = self.device.get(u8::from(JoystickBrickletFunction::GetAnalogValueCallbackThreshold), &payload).await?;
Ok(AnalogValueCallbackThreshold::from_le_byte_slice(result.body()))
}
pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
let mut payload = [0; 4];
debounce.write_to_slice(&mut payload[0..4]);
#[allow(unused_variables)]
let result = self.device.set(u8::from(JoystickBrickletFunction::SetDebouncePeriod), &payload).await?;
Ok(())
}
pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
let payload = [0; 0];
#[allow(unused_variables)]
let result = self.device.get(u8::from(JoystickBrickletFunction::GetDebouncePeriod), &payload).await?;
Ok(u32::from_le_byte_slice(result.body()))
}
pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
let payload = [0; 0];
#[allow(unused_variables)]
let result = self.device.get(u8::from(JoystickBrickletFunction::GetIdentity), &payload).await?;
Ok(Identity::from_le_byte_slice(result.body()))
}
}