1use crate::lego::consts::{
6 EndState,
7 Profile
8};
9
10use super::message_types::SubcommandType;
11
12
13pub trait Serialized {
17 fn serialize(&self) -> Vec<u8>;
18}
19
20pub struct HubPropertiesParams {
25 pub property: HubPropertiesProperties,
26 pub operation: HubPropertiesOperations,
27}
28
29#[derive(Clone, Copy)]
30#[repr(u8)]
31pub enum HubPropertiesProperties {
32 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, }
48
49#[derive(Clone, Copy)]
50#[repr(u8)]
51pub enum HubPropertiesOperations {
52 Set = 0x01, EnableUpdates = 0x02, DisableUpdates = 0x03, Reset = 0x04, RequestUpdate = 0x05, Update = 0x06, }
59
60impl Serialized for HubPropertiesParams {
61 fn serialize(&self) -> Vec<u8> {
62 vec![self.property as u8, self.operation as u8]
63 }
64}
65
66
67pub struct HubActionsParams {
72 pub action_type: HubActionsTypes,
73}
74
75#[derive(Clone, Copy)]
76pub enum HubActionsTypes {
77 SwitchOffHub = 0x01, Disconnect = 0x02, VCCPortControlOn = 0x03, VCCPortControlOff = 0x04, ActivateBUSYIndication = 0x05, ResetBUSYIndication = 0x06, Shutdown = 0x2f, HubWillSwitchOff = 0x30, HubWillDisconnect = 0x31, HubWillGoIntoBootMode = 0x32, }
90
91impl Serialized for HubActionsParams {
92 fn serialize(&self) -> Vec<u8> {
93 vec![self.action_type as u8]
94 }
95}
96
97
98
99pub struct PortInformationRequestParams {
104 pub port_id: u8,
105 pub information_type: PortInformationType,
106}
107
108#[derive(Clone, Copy)]
109pub enum PortInformationType {
110 PortValue = 0x00, ModeInfo = 0x01, PossibleModeCombinations = 0x02, }
114
115impl Serialized for PortInformationRequestParams {
116 fn serialize(&self) -> Vec<u8> {
117 vec![self.port_id, self.information_type as u8]
118 }
119}
120
121
122pub struct PortModeInformationRequestParams {
127 pub port_id: u8,
128 pub mode_id: u8,
129 pub information_type: PortModeInformationType,
130}
131
132#[derive(Clone, Copy)]
133pub enum PortModeInformationType {
134 Name = 0x00, Raw = 0x01, Pct = 0x02, Si = 0x03, Symbol = 0x04, Mapping = 0x05, Internal = 0x06, MotorBias = 0x07, CapabilityBits = 0x08, ValueFormat = 0x80, }
145
146impl Serialized for PortModeInformationRequestParams {
147 fn serialize(&self) -> Vec<u8> {
148 vec![self.port_id, self.mode_id, self.information_type as u8]
149 }
150}
151
152
153pub struct PortInputFormatSetupSingleParams {
158 pub port_id: u8,
159 pub mode_id: u8,
160 pub delta: u32,
161 pub enable_notifications: bool,
162}
163
164impl Serialized for PortInputFormatSetupSingleParams {
165 fn serialize(&self) -> Vec<u8> {
166 let mut data = vec![self.port_id, self.mode_id];
167 data.append(&mut Vec::from(self.delta.to_le_bytes()));
168 data.push(if self.enable_notifications {1} else {0});
169 data
170 }
171}
172
173
174pub struct PortOutputCommandParams {
179 pub port_id: u8,
180 pub start_up_info: StartupAndCompletionInfo,
181 pub subcommand_id: SubcommandType,
182 pub payload: SubcommandPayload,
183}
184
185impl Serialized for PortOutputCommandParams {
186 fn serialize(&self) -> Vec<u8> {
187 let mut res = vec![self.port_id, self.start_up_info as u8, self.subcommand_id as u8];
188 res.append(self.payload.serialize().as_mut());
189 res
190 }
191}
192
193#[derive(Clone, Copy)]
194pub enum StartupAndCompletionInfo {
195 BufferAndNoAction = 0b00000000,
196 BufferAndFeedback = 0b00000001,
197 ExecuteImmediatelyAndNoAction = 0b00010000,
198 ExecuteImmediatelyAndFeedback = 0b00010001,
199}
200
201
202
203
204
205pub enum SubcommandPayload {
216 SetAccTime(SetAccTimePayload),
217 SetDecTime(SetDecTimePayload),
218 StartSpeed(StartSpeedPayload),
219 StartSpeedForDegrees(StartSpeedForDegreesPayload),
220 GotoAbsolutePosition(GotoAbsolutePositionPayload),
221 WriteDirectModeData(WriteDirectModeDataPayload),
222}
223
224impl Serialized for SubcommandPayload {
225 fn serialize(&self) -> Vec<u8> {
226 match self {
227 SubcommandPayload::SetAccTime(payload) => {
228 payload.serialize()
229 },
230 SubcommandPayload::SetDecTime(payload) => {
231 payload.serialize()
232 },
233 SubcommandPayload::StartSpeed(payload) => {
234 payload.serialize()
235 },
236 SubcommandPayload::StartSpeedForDegrees(payload) => {
237 payload.serialize()
238 },
239 SubcommandPayload::GotoAbsolutePosition(payload) => {
240 payload.serialize()
241 },
242 SubcommandPayload::WriteDirectModeData(payload) => {
243 payload.serialize()
244 },
245 }
246 }
247}
248
249
250pub struct SetAccTimePayload {
255 pub time: i16,
256}
257
258impl Serialized for SetAccTimePayload {
259 fn serialize(&self) -> Vec<u8> {
260 let mut data = Vec::from(self.time.to_le_bytes());
261 data.append(vec![
262 Profile::Acc as u8,
263 ].as_mut());
264 data
265 }
266}
267
268
269pub struct SetDecTimePayload {
274 pub time: i16,
275}
276
277impl Serialized for SetDecTimePayload {
278 fn serialize(&self) -> Vec<u8> {
279 let mut data = Vec::from(self.time.to_le_bytes());
280 data.append(vec![
281 Profile::Dec as u8,
282 ].as_mut());
283 data
284 }
285}
286
287
288pub struct StartSpeedPayload {
293 pub speed: i8,
294 pub max_power: i8,
295 pub use_profile: Profile,
296}
297
298impl Serialized for StartSpeedPayload {
299 fn serialize(&self) -> Vec<u8> {
300 vec![
301 self.speed.to_le_bytes()[0],
302 self.max_power.to_le_bytes()[0],
303 self.use_profile as u8
304 ]
305 }
306}
307
308
309pub struct StartSpeedForDegreesPayload {
314 pub degrees: i32,
315 pub speed: i8,
316 pub max_power: i8,
317 pub end_state: EndState,
318 pub use_profile: Profile,
319}
320
321impl Serialized for StartSpeedForDegreesPayload {
322 fn serialize(&self) -> Vec<u8> {
323 let mut data = Vec::from(self.degrees.to_le_bytes());
324 data.append(vec![
325 self.speed.to_le_bytes()[0],
326 self.max_power.to_le_bytes()[0],
327 (self.end_state as u8).to_le_bytes()[0],
328 self.use_profile as u8,
329 ].as_mut());
330 data
331 }
332}
333
334
335pub struct GotoAbsolutePositionPayload {
340 pub abs_pos: i32, pub speed: i8,
342 pub max_power: i8,
343 pub end_state: EndState,
344 pub use_profile: Profile,
345}
346
347impl Serialized for GotoAbsolutePositionPayload {
348 fn serialize(&self) -> Vec<u8> {
349 let mut data = Vec::from(self.abs_pos.to_le_bytes());
350 data.append(vec![
351 self.speed.to_le_bytes()[0],
352 self.max_power.to_le_bytes()[0],
353 (self.end_state as u8).to_le_bytes()[0],
354 self.use_profile as u8,
355 ].as_mut());
356 data
357 }
358}
359
360
361pub struct WriteDirectModeDataPayload {
366 pub mode: u8,
367 pub payload: WriteDirectModeDataCommands,
368}
369
370impl Serialized for WriteDirectModeDataPayload {
371 fn serialize(&self) -> Vec<u8> {
372 let mut data = vec![self.mode];
373 data.append(self.payload.serialize().as_mut());
374 data
375 }
376}
377
378pub enum WriteDirectModeDataCommands {
379 StartPower(StartPowerPayload),
380 SetAbsolutePosition(SetAbsolutePositionPayload),
381}
382
383impl Serialized for WriteDirectModeDataCommands {
384 fn serialize(&self) -> Vec<u8> {
385 match self {
386 WriteDirectModeDataCommands::StartPower(payload) => {
387 payload.serialize()
388 },
389 WriteDirectModeDataCommands::SetAbsolutePosition(payload) => {
390 payload.serialize()
391 },
392 }
393 }
394}
395
396
397pub struct StartPowerPayload {
405 pub power: i8,
406}
407
408impl Serialized for StartPowerPayload {
409 fn serialize(&self) -> Vec<u8> {
410 Vec::from(self.power.to_le_bytes())
411 }
412}
413
414
415pub struct SetAbsolutePositionPayload {
420 pub position: i32,
421}
422
423impl Serialized for SetAbsolutePositionPayload {
424 fn serialize(&self) -> Vec<u8> {
425 Vec::from(self.position.to_le_bytes())
426 }
427}