use crate::lego::consts::{
EndState,
Profile
};
use super::message_types::SubcommandType;
pub trait Serialized {
fn serialize(&self) -> Vec<u8>;
}
pub struct HubPropertiesParams {
pub property: HubPropertiesProperties,
pub operation: HubPropertiesOperations,
}
#[derive(Clone, Copy)]
#[repr(u8)]
pub enum HubPropertiesProperties {
AdvertisingName = 0x01, Button = 0x02, FWVersion = 0x03, HWVersion = 0x04, RSSI = 0x05, BatteryVoltage = 0x06, BatteryType = 0x07, ManufacturerName = 0x08, RadioFirmwareVersion = 0x09, LEGOWirelessProtocolVersion = 0x0A, SystemTypeID = 0x0B, HWNetworkID = 0x0C, PrimaryMACAddress = 0x0D, SecondaryMACAddress = 0x0E, HardwareNetworkFamily = 0x0F, }
#[derive(Clone, Copy)]
#[repr(u8)]
pub enum HubPropertiesOperations {
Set = 0x01, EnableUpdates = 0x02, DisableUpdates = 0x03, Reset = 0x04, RequestUpdate = 0x05, Update = 0x06, }
impl Serialized for HubPropertiesParams {
fn serialize(&self) -> Vec<u8> {
vec![self.property as u8, self.operation as u8]
}
}
pub struct HubActionsParams {
pub action_type: HubActionsTypes,
}
#[derive(Clone, Copy)]
pub enum HubActionsTypes {
SwitchOffHub = 0x01, Disconnect = 0x02, VCCPortControlOn = 0x03, VCCPortControlOff = 0x04, ActivateBUSYIndication = 0x05, ResetBUSYIndication = 0x06, Shutdown = 0x2f,
HubWillSwitchOff = 0x30, HubWillDisconnect = 0x31, HubWillGoIntoBootMode = 0x32, }
impl Serialized for HubActionsParams {
fn serialize(&self) -> Vec<u8> {
vec![self.action_type as u8]
}
}
pub struct PortInformationRequestParams {
pub port_id: u8,
pub information_type: PortInformationType,
}
#[derive(Clone, Copy)]
pub enum PortInformationType {
PortValue = 0x00, ModeInfo = 0x01, PossibleModeCombinations = 0x02, }
impl Serialized for PortInformationRequestParams {
fn serialize(&self) -> Vec<u8> {
vec![self.port_id, self.information_type as u8]
}
}
pub struct PortModeInformationRequestParams {
pub port_id: u8,
pub mode_id: u8,
pub information_type: PortModeInformationType,
}
#[derive(Clone, Copy)]
pub enum PortModeInformationType {
Name = 0x00, Raw = 0x01, Pct = 0x02, Si = 0x03, Symbol = 0x04, Mapping = 0x05, Internal = 0x06, MotorBias = 0x07, CapabilityBits = 0x08, ValueFormat = 0x80, }
impl Serialized for PortModeInformationRequestParams {
fn serialize(&self) -> Vec<u8> {
vec![self.port_id, self.mode_id, self.information_type as u8]
}
}
pub struct PortInputFormatSetupSingleParams {
pub port_id: u8,
pub mode_id: u8,
pub delta: u32,
pub enable_notifications: bool,
}
impl Serialized for PortInputFormatSetupSingleParams {
fn serialize(&self) -> Vec<u8> {
let mut data = vec![self.port_id, self.mode_id];
data.append(&mut Vec::from(self.delta.to_le_bytes()));
data.push(if self.enable_notifications {1} else {0});
data
}
}
pub struct PortOutputCommandParams {
pub port_id: u8,
pub start_up_info: StartupAndCompletionInfo,
pub subcommand_id: SubcommandType,
pub payload: SubcommandPayload,
}
impl Serialized for PortOutputCommandParams {
fn serialize(&self) -> Vec<u8> {
let mut res = vec![self.port_id, self.start_up_info as u8, self.subcommand_id as u8];
res.append(self.payload.serialize().as_mut());
res
}
}
#[derive(Clone, Copy)]
pub enum StartupAndCompletionInfo {
BufferAndNoAction = 0b00000000,
BufferAndFeedback = 0b00000001,
ExecuteImmediatelyAndNoAction = 0b00010000,
ExecuteImmediatelyAndFeedback = 0b00010001,
}
pub enum SubcommandPayload {
SetAccTime(SetAccTimePayload),
SetDecTime(SetDecTimePayload),
StartSpeed(StartSpeedPayload),
StartSpeedForDegrees(StartSpeedForDegreesPayload),
GotoAbsolutePosition(GotoAbsolutePositionPayload),
WriteDirectModeData(WriteDirectModeDataPayload),
}
impl Serialized for SubcommandPayload {
fn serialize(&self) -> Vec<u8> {
match self {
SubcommandPayload::SetAccTime(payload) => {
payload.serialize()
},
SubcommandPayload::SetDecTime(payload) => {
payload.serialize()
},
SubcommandPayload::StartSpeed(payload) => {
payload.serialize()
},
SubcommandPayload::StartSpeedForDegrees(payload) => {
payload.serialize()
},
SubcommandPayload::GotoAbsolutePosition(payload) => {
payload.serialize()
},
SubcommandPayload::WriteDirectModeData(payload) => {
payload.serialize()
},
}
}
}
pub struct SetAccTimePayload {
pub time: i16,
}
impl Serialized for SetAccTimePayload {
fn serialize(&self) -> Vec<u8> {
let mut data = Vec::from(self.time.to_le_bytes());
data.append(vec![
Profile::Acc as u8,
].as_mut());
data
}
}
pub struct SetDecTimePayload {
pub time: i16,
}
impl Serialized for SetDecTimePayload {
fn serialize(&self) -> Vec<u8> {
let mut data = Vec::from(self.time.to_le_bytes());
data.append(vec![
Profile::Dec as u8,
].as_mut());
data
}
}
pub struct StartSpeedPayload {
pub speed: i8,
pub max_power: i8,
pub use_profile: Profile,
}
impl Serialized for StartSpeedPayload {
fn serialize(&self) -> Vec<u8> {
vec![
self.speed.to_le_bytes()[0],
self.max_power.to_le_bytes()[0],
self.use_profile as u8
]
}
}
pub struct StartSpeedForDegreesPayload {
pub degrees: i32,
pub speed: i8,
pub max_power: i8,
pub end_state: EndState,
pub use_profile: Profile,
}
impl Serialized for StartSpeedForDegreesPayload {
fn serialize(&self) -> Vec<u8> {
let mut data = Vec::from(self.degrees.to_le_bytes());
data.append(vec![
self.speed.to_le_bytes()[0],
self.max_power.to_le_bytes()[0],
(self.end_state as u8).to_le_bytes()[0],
self.use_profile as u8,
].as_mut());
data
}
}
pub struct GotoAbsolutePositionPayload {
pub abs_pos: i32, pub speed: i8,
pub max_power: i8,
pub end_state: EndState,
pub use_profile: Profile,
}
impl Serialized for GotoAbsolutePositionPayload {
fn serialize(&self) -> Vec<u8> {
let mut data = Vec::from(self.abs_pos.to_le_bytes());
data.append(vec![
self.speed.to_le_bytes()[0],
self.max_power.to_le_bytes()[0],
(self.end_state as u8).to_le_bytes()[0],
self.use_profile as u8,
].as_mut());
data
}
}
pub struct WriteDirectModeDataPayload {
pub mode: u8,
pub payload: WriteDirectModeDataCommands,
}
impl Serialized for WriteDirectModeDataPayload {
fn serialize(&self) -> Vec<u8> {
let mut data = vec![self.mode];
data.append(self.payload.serialize().as_mut());
data
}
}
pub enum WriteDirectModeDataCommands {
StartPower(StartPowerPayload),
SetAbsolutePosition(SetAbsolutePositionPayload),
}
impl Serialized for WriteDirectModeDataCommands {
fn serialize(&self) -> Vec<u8> {
match self {
WriteDirectModeDataCommands::StartPower(payload) => {
payload.serialize()
},
WriteDirectModeDataCommands::SetAbsolutePosition(payload) => {
payload.serialize()
},
}
}
}
pub struct StartPowerPayload {
pub power: i8,
}
impl Serialized for StartPowerPayload {
fn serialize(&self) -> Vec<u8> {
Vec::from(self.power.to_le_bytes())
}
}
pub struct SetAbsolutePositionPayload {
pub position: i32,
}
impl Serialized for SetAbsolutePositionPayload {
fn serialize(&self) -> Vec<u8> {
Vec::from(self.position.to_le_bytes())
}
}