#[non_exhaustive]pub enum Command {
Show 13 variants
SetSpeed(Speed),
SetFunction {
addr: u32,
value: u16,
},
EmergencyStop,
QuerySpeed,
QueryFunction {
addr: u32,
},
AssignController {
flags: u8,
controller: NodeId,
},
ReleaseController {
flags: u8,
controller: NodeId,
},
QueryController,
ControllerChangingNotify {
flags: u8,
new_controller: NodeId,
},
Reserve,
Release,
Noop,
Unknown(Vec<u8>),
}Expand description
A Traction Control Command (throttle → train).
Encode with Command::encode and send as the payload of
Message::TractionCommand. A train
implementation decodes incoming payloads with Command::decode.
use rolcb::traction::{Command, Speed};
use rolcb::NodeId;
let me = NodeId::new(0x0501_0101_2299);
let bytes = Command::AssignController { flags: 0, controller: me }.encode();
assert_eq!(bytes, [0x20, 0x01, 0x00, 0x05, 0x01, 0x01, 0x01, 0x22, 0x99]);
assert_eq!(Command::decode(&bytes), Command::AssignController { flags: 0, controller: me });
assert_eq!(Command::Noop.encode(), [0x40, 0x03]);
assert_eq!(Command::decode(&[0x7F]), Command::Unknown(vec![0x7F]));The enum is #[non_exhaustive]: match it with a _ arm.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
SetSpeed(Speed)
Set speed and direction (0x00). Also clears an emergency stop.
SetFunction
Set a function (0x01). For DCC trains, addresses 0..=28 are F0..F28.
Fields
value: u16New value; for on/off functions 0 = off, 1 = on (see function_value).
EmergencyStop
Emergency stop (0x02): stop immediately, ignoring momentum.
QuerySpeed
Ask for the current speed (0x10); answered by Reply::QuerySpeed.
QueryFunction
Ask for a function value (0x11); answered by Reply::QueryFunction.
AssignController
Become this train’s controller (0x20 0x01). Answered by
Reply::AssignController; if another throttle was in control, the
train first asks it with Command::ControllerChangingNotify.
Fields
ReleaseController
Stop being this train’s controller (0x20 0x02).
QueryController
Ask which node controls the train (0x20 0x03).
ControllerChangingNotify
Controller Changing Notify (0x20 0x04 flags NodeID), train →
current controller: another node (new_controller) asked to take
over. The controller answers with Reply::ControllerChanged
(result 0 = accept). This is the one command a throttle receives;
Node never auto-rejects it.
Fields
Reserve
Reserve the train for a multi-step operation (0x40 0x01).
Release
Release a reservation (0x40 0x02).
Noop
Keep-alive, also the answer to a heartbeat request.
Encoded as 0x40 0x03.
Unknown(Vec<u8>)
Anything not recognised (or too short to decode), raw bytes kept.
Implementations§
Source§impl Command
impl Command
Sourcepub fn decode(p: &[u8]) -> Command
pub fn decode(p: &[u8]) -> Command
Parse command payload bytes. Never fails; unknown or truncated
commands become Command::Unknown. The consist
LISTENER_FLAG bit in byte 0 is ignored.
Sourcepub fn is_controller_changing_notify(payload: &[u8]) -> bool
pub fn is_controller_changing_notify(payload: &[u8]) -> bool
True if payload is a Controller Changing Notify command
(20 04 …), the one Traction Control Command a throttle receives.
Cheaper than a full Command::decode.
use rolcb::traction::Command;
assert!(Command::is_controller_changing_notify(&[0x20, 0x04, 0, 5, 1, 1, 1, 0x22, 0x77]));
assert!(!Command::is_controller_changing_notify(&[0x20, 0x01]));