Skip to main content

Command

Enum Command 

Source
#[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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

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

§addr: u32

24-bit function address.

§value: u16

New 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.

Fields

§addr: u32

24-bit function address.

§

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

§flags: u8

Flags byte (0 for a normal throttle).

§controller: NodeId

The controlling throttle’s Node ID.

§

ReleaseController

Stop being this train’s controller (0x20 0x02).

Fields

§flags: u8

Flags byte (normally 0).

§controller: NodeId

The Node ID that was assigned.

§

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

§flags: u8

Flags byte (normally 0).

§new_controller: NodeId

The node that wants to become the controller.

§

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

Source

pub fn encode(&self) -> Vec<u8> ⓘ

Serialize to the command payload bytes.

Source

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.

Source

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]));

Trait Implementations§

Source§

impl Clone for Command

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Command

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Command

Source§

impl Hash for Command

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Command

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Command

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.