use crate::{
byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
ip_connection::GetRequestSender,
};
pub enum AccelerometerBrickletFunction {
GetAcceleration,
SetAccelerationCallbackPeriod,
GetAccelerationCallbackPeriod,
SetAccelerationCallbackThreshold,
GetAccelerationCallbackThreshold,
SetDebouncePeriod,
GetDebouncePeriod,
GetTemperature,
SetConfiguration,
GetConfiguration,
LedOn,
LedOff,
IsLedOn,
GetIdentity,
CallbackAcceleration,
CallbackAccelerationReached,
}
impl From<AccelerometerBrickletFunction> for u8 {
fn from(fun: AccelerometerBrickletFunction) -> Self {
match fun {
AccelerometerBrickletFunction::GetAcceleration => 1,
AccelerometerBrickletFunction::SetAccelerationCallbackPeriod => 2,
AccelerometerBrickletFunction::GetAccelerationCallbackPeriod => 3,
AccelerometerBrickletFunction::SetAccelerationCallbackThreshold => 4,
AccelerometerBrickletFunction::GetAccelerationCallbackThreshold => 5,
AccelerometerBrickletFunction::SetDebouncePeriod => 6,
AccelerometerBrickletFunction::GetDebouncePeriod => 7,
AccelerometerBrickletFunction::GetTemperature => 8,
AccelerometerBrickletFunction::SetConfiguration => 9,
AccelerometerBrickletFunction::GetConfiguration => 10,
AccelerometerBrickletFunction::LedOn => 11,
AccelerometerBrickletFunction::LedOff => 12,
AccelerometerBrickletFunction::IsLedOn => 13,
AccelerometerBrickletFunction::GetIdentity => 255,
AccelerometerBrickletFunction::CallbackAcceleration => 14,
AccelerometerBrickletFunction::CallbackAccelerationReached => 15,
}
}
}
pub const ACCELEROMETER_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
pub const ACCELEROMETER_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
pub const ACCELEROMETER_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
pub const ACCELEROMETER_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
pub const ACCELEROMETER_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
pub const ACCELEROMETER_BRICKLET_DATA_RATE_OFF: u8 = 0;
pub const ACCELEROMETER_BRICKLET_DATA_RATE_3HZ: u8 = 1;
pub const ACCELEROMETER_BRICKLET_DATA_RATE_6HZ: u8 = 2;
pub const ACCELEROMETER_BRICKLET_DATA_RATE_12HZ: u8 = 3;
pub const ACCELEROMETER_BRICKLET_DATA_RATE_25HZ: u8 = 4;
pub const ACCELEROMETER_BRICKLET_DATA_RATE_50HZ: u8 = 5;
pub const ACCELEROMETER_BRICKLET_DATA_RATE_100HZ: u8 = 6;
pub const ACCELEROMETER_BRICKLET_DATA_RATE_400HZ: u8 = 7;
pub const ACCELEROMETER_BRICKLET_DATA_RATE_800HZ: u8 = 8;
pub const ACCELEROMETER_BRICKLET_DATA_RATE_1600HZ: u8 = 9;
pub const ACCELEROMETER_BRICKLET_FULL_SCALE_2G: u8 = 0;
pub const ACCELEROMETER_BRICKLET_FULL_SCALE_4G: u8 = 1;
pub const ACCELEROMETER_BRICKLET_FULL_SCALE_6G: u8 = 2;
pub const ACCELEROMETER_BRICKLET_FULL_SCALE_8G: u8 = 3;
pub const ACCELEROMETER_BRICKLET_FULL_SCALE_16G: u8 = 4;
pub const ACCELEROMETER_BRICKLET_FILTER_BANDWIDTH_800HZ: u8 = 0;
pub const ACCELEROMETER_BRICKLET_FILTER_BANDWIDTH_400HZ: u8 = 1;
pub const ACCELEROMETER_BRICKLET_FILTER_BANDWIDTH_200HZ: u8 = 2;
pub const ACCELEROMETER_BRICKLET_FILTER_BANDWIDTH_50HZ: u8 = 3;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Acceleration {
pub x: i16,
pub y: i16,
pub z: i16,
}
impl FromByteSlice for Acceleration {
fn bytes_expected() -> usize { 6 }
fn from_le_byte_slice(bytes: &[u8]) -> Acceleration {
Acceleration {
x: <i16>::from_le_byte_slice(&bytes[0..2]),
y: <i16>::from_le_byte_slice(&bytes[2..4]),
z: <i16>::from_le_byte_slice(&bytes[4..6]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AccelerationCallbackThreshold {
pub option: char,
pub min_x: i16,
pub max_x: i16,
pub min_y: i16,
pub max_y: i16,
pub min_z: i16,
pub max_z: i16,
}
impl FromByteSlice for AccelerationCallbackThreshold {
fn bytes_expected() -> usize { 13 }
fn from_le_byte_slice(bytes: &[u8]) -> AccelerationCallbackThreshold {
AccelerationCallbackThreshold {
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]),
min_z: <i16>::from_le_byte_slice(&bytes[9..11]),
max_z: <i16>::from_le_byte_slice(&bytes[11..13]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Configuration {
pub data_rate: u8,
pub full_scale: u8,
pub filter_bandwidth: u8,
}
impl FromByteSlice for Configuration {
fn bytes_expected() -> usize { 3 }
fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
Configuration {
data_rate: <u8>::from_le_byte_slice(&bytes[0..1]),
full_scale: <u8>::from_le_byte_slice(&bytes[1..2]),
filter_bandwidth: <u8>::from_le_byte_slice(&bytes[2..3]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AccelerationEvent {
pub x: i16,
pub y: i16,
pub z: i16,
}
impl FromByteSlice for AccelerationEvent {
fn bytes_expected() -> usize { 6 }
fn from_le_byte_slice(bytes: &[u8]) -> AccelerationEvent {
AccelerationEvent {
x: <i16>::from_le_byte_slice(&bytes[0..2]),
y: <i16>::from_le_byte_slice(&bytes[2..4]),
z: <i16>::from_le_byte_slice(&bytes[4..6]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AccelerationReachedEvent {
pub x: i16,
pub y: i16,
pub z: i16,
}
impl FromByteSlice for AccelerationReachedEvent {
fn bytes_expected() -> usize { 6 }
fn from_le_byte_slice(bytes: &[u8]) -> AccelerationReachedEvent {
AccelerationReachedEvent {
x: <i16>::from_le_byte_slice(&bytes[0..2]),
y: <i16>::from_le_byte_slice(&bytes[2..4]),
z: <i16>::from_le_byte_slice(&bytes[4..6]),
}
}
}
#[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 AccelerometerBricklet {
device: Device,
}
impl AccelerometerBricklet {
pub const DEVICE_IDENTIFIER: u16 = 250;
pub const DEVICE_DISPLAY_NAME: &'static str = "Accelerometer Bricklet";
pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> AccelerometerBricklet {
let mut result = AccelerometerBricklet { device: Device::new([2, 0, 1], uid, req_sender, 0) };
result.device.response_expected[u8::from(AccelerometerBrickletFunction::GetAcceleration) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::SetAccelerationCallbackPeriod) as usize] =
ResponseExpectedFlag::True;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::GetAccelerationCallbackPeriod) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::SetAccelerationCallbackThreshold) as usize] =
ResponseExpectedFlag::True;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::GetAccelerationCallbackThreshold) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::GetDebouncePeriod) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::GetTemperature) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::SetConfiguration) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::GetConfiguration) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::LedOn) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::LedOff) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::IsLedOn) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(AccelerometerBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
result
}
pub fn get_response_expected(&mut self, fun: AccelerometerBrickletFunction) -> Result<bool, GetResponseExpectedError> {
self.device.get_response_expected(u8::from(fun))
}
pub fn set_response_expected(
&mut self,
fun: AccelerometerBrickletFunction,
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 fn get_acceleration_callback_receiver(&self) -> ConvertingCallbackReceiver<AccelerationEvent> {
self.device.get_callback_receiver(u8::from(AccelerometerBrickletFunction::CallbackAcceleration))
}
pub fn get_acceleration_reached_callback_receiver(&self) -> ConvertingCallbackReceiver<AccelerationReachedEvent> {
self.device.get_callback_receiver(u8::from(AccelerometerBrickletFunction::CallbackAccelerationReached))
}
pub fn get_acceleration(&self) -> ConvertingReceiver<Acceleration> {
let payload = vec![0; 0];
self.device.get(u8::from(AccelerometerBrickletFunction::GetAcceleration), payload)
}
pub fn set_acceleration_callback_period(&self, period: u32) -> ConvertingReceiver<()> {
let mut payload = vec![0; 4];
payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
self.device.set(u8::from(AccelerometerBrickletFunction::SetAccelerationCallbackPeriod), payload)
}
pub fn get_acceleration_callback_period(&self) -> ConvertingReceiver<u32> {
let payload = vec![0; 0];
self.device.get(u8::from(AccelerometerBrickletFunction::GetAccelerationCallbackPeriod), payload)
}
pub fn set_acceleration_callback_threshold(
&self,
option: char,
min_x: i16,
max_x: i16,
min_y: i16,
max_y: i16,
min_z: i16,
max_z: i16,
) -> ConvertingReceiver<()> {
let mut payload = vec![0; 13];
payload[0..1].copy_from_slice(&<char>::to_le_byte_vec(option));
payload[1..3].copy_from_slice(&<i16>::to_le_byte_vec(min_x));
payload[3..5].copy_from_slice(&<i16>::to_le_byte_vec(max_x));
payload[5..7].copy_from_slice(&<i16>::to_le_byte_vec(min_y));
payload[7..9].copy_from_slice(&<i16>::to_le_byte_vec(max_y));
payload[9..11].copy_from_slice(&<i16>::to_le_byte_vec(min_z));
payload[11..13].copy_from_slice(&<i16>::to_le_byte_vec(max_z));
self.device.set(u8::from(AccelerometerBrickletFunction::SetAccelerationCallbackThreshold), payload)
}
pub fn get_acceleration_callback_threshold(&self) -> ConvertingReceiver<AccelerationCallbackThreshold> {
let payload = vec![0; 0];
self.device.get(u8::from(AccelerometerBrickletFunction::GetAccelerationCallbackThreshold), payload)
}
pub fn set_debounce_period(&self, debounce: u32) -> ConvertingReceiver<()> {
let mut payload = vec![0; 4];
payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(debounce));
self.device.set(u8::from(AccelerometerBrickletFunction::SetDebouncePeriod), payload)
}
pub fn get_debounce_period(&self) -> ConvertingReceiver<u32> {
let payload = vec![0; 0];
self.device.get(u8::from(AccelerometerBrickletFunction::GetDebouncePeriod), payload)
}
pub fn get_temperature(&self) -> ConvertingReceiver<i16> {
let payload = vec![0; 0];
self.device.get(u8::from(AccelerometerBrickletFunction::GetTemperature), payload)
}
pub fn set_configuration(&self, data_rate: u8, full_scale: u8, filter_bandwidth: u8) -> ConvertingReceiver<()> {
let mut payload = vec![0; 3];
payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(data_rate));
payload[1..2].copy_from_slice(&<u8>::to_le_byte_vec(full_scale));
payload[2..3].copy_from_slice(&<u8>::to_le_byte_vec(filter_bandwidth));
self.device.set(u8::from(AccelerometerBrickletFunction::SetConfiguration), payload)
}
pub fn get_configuration(&self) -> ConvertingReceiver<Configuration> {
let payload = vec![0; 0];
self.device.get(u8::from(AccelerometerBrickletFunction::GetConfiguration), payload)
}
pub fn led_on(&self) -> ConvertingReceiver<()> {
let payload = vec![0; 0];
self.device.set(u8::from(AccelerometerBrickletFunction::LedOn), payload)
}
pub fn led_off(&self) -> ConvertingReceiver<()> {
let payload = vec![0; 0];
self.device.set(u8::from(AccelerometerBrickletFunction::LedOff), payload)
}
pub fn is_led_on(&self) -> ConvertingReceiver<bool> {
let payload = vec![0; 0];
self.device.get(u8::from(AccelerometerBrickletFunction::IsLedOn), payload)
}
pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
let payload = vec![0; 0];
self.device.get(u8::from(AccelerometerBrickletFunction::GetIdentity), payload)
}
}