Skip to main content

Command

Struct Command 

Source
pub struct Command { /* private fields */ }
Expand description

An encoded command ready to write to the board’s command characteristic.

Construct commands with methods such as Command::set_leds and Command::read_battery_level.

Transport implementations use Command::bytes and Command::write_kind to perform the write.

§Examples

use chessnut_move::protocol::{
    Command, File, LedColor, LedPattern, Rank, Square, WriteKind,
};

let mut leds = LedPattern::default();
leds.set_color(Square::new(File::E, Rank::Four), LedColor::Green);

let command = Command::set_leds(&leds);
assert_eq!(command.write_kind(), WriteKind::WithoutResponse);

Implementations§

Source§

impl Command

Source

pub fn auto_move(position: Position, mode: AutoMoveMode) -> Self

Creates a command that moves the physical pieces to a target position.

The target describes all 64 squares, not only the pieces that changed. Position notifications are unavailable while the board is performing an automatic movement.

Use Command::stop_auto_move to cancel movement.

§Examples
use chessnut_move::protocol::{
    AutoMoveMode, Color, Command, File, Piece, PieceKind, Position, Rank,
    SQUARE_COUNT, Square,
};

let mut target = Position::new([None; SQUARE_COUNT]);
target.set_piece(
    Square::new(File::E, Rank::Four),
    Some(Piece {
        color: Color::White,
        kind: PieceKind::Pawn,
    }),
);

let command = Command::auto_move(target, AutoMoveMode::Normal);
assert_eq!(command.bytes().len(), 35);
Source

pub const fn stop_auto_move() -> Self

Creates a command that stops the current automatic movement.

The command is safe to send when no automatic movement is active.

§Examples

Send the stop command through an initialized async session:

use chessnut_move::protocol::Command;
use chessnut_move::transport::{AsyncBoard, AsyncTransport, BoardError};

async fn stop<T: AsyncTransport>(
    board: &mut AsyncBoard<T>,
) -> Result<(), BoardError<T::Error>> {
    board.send(&Command::stop_auto_move()).await
}
Source

pub const fn enable_realtime_updates() -> Self

Creates a command that enables realtime position notifications.

Low-level transport users must subscribe to NotificationSource::Position before writing this command so the initial position is not missed. Board sessions perform this sequence as part of their initialization procedure.

§Examples
use chessnut_move::protocol::{Command, WriteKind};

let command = Command::enable_realtime_updates();
assert_eq!(command.bytes(), [0x21, 0x01, 0x00]);
assert_eq!(command.write_kind(), WriteKind::WithResponse);

See AsyncBoard::initialize for the runtime-neutral async initialization procedure. See BlockingBoard::initialize for the blocking initialization procedure. The Tokio actor performs initialization in its spawned task.

Source

pub const fn read_battery_level() -> Self

Creates a query for the board’s battery status.

The corresponding command-response notification decodes to BoardEvent::BatteryStatus.

§Examples

Send the query through an initialized async session and wait for its response:

use chessnut_move::protocol::{BatteryStatus, BoardEvent, Command};
use chessnut_move::transport::{AsyncBoard, AsyncTransport, BoardError};

async fn read_battery<T: AsyncTransport>(
    board: &mut AsyncBoard<T>,
) -> Result<BatteryStatus, BoardError<T::Error>> {
    board.send(&Command::read_battery_level()).await?;

    loop {
        if let BoardEvent::BatteryStatus(status) = board.next_event().await? {
            return Ok(status);
        }
    }
}

Tokio actor consumers can use BoardHandle::battery_status to send and correlate this query.

Source

pub const fn read_piece_status() -> Self

Creates a query for the status of all tracked physical pieces.

The corresponding command-response notification decodes to BoardEvent::PieceStatus.

§Examples

Send the query through an initialized async session and wait for its response:

use chessnut_move::protocol::{BoardEvent, Command, PieceStatus};
use chessnut_move::transport::{AsyncBoard, AsyncTransport, BoardError};

async fn read_pieces<T: AsyncTransport>(
    board: &mut AsyncBoard<T>,
) -> Result<PieceStatus, BoardError<T::Error>> {
    board.send(&Command::read_piece_status()).await?;

    loop {
        if let BoardEvent::PieceStatus(status) = board.next_event().await? {
            return Ok(status);
        }
    }
}

Tokio actor consumers can use BoardHandle::piece_status to send and correlate this query.

Source

pub fn set_leds(pattern: &LedPattern) -> Self

Creates a command that replaces the LED color of every square.

Squares not selected in pattern are turned off. Use LedPattern::default to turn off all square LEDs.

§Examples
use chessnut_move::protocol::{
    Command, File, LedColor, LedPattern, Rank, Square,
};

let mut pattern = LedPattern::default();
pattern.set_color(Square::new(File::E, Rank::Four), LedColor::Blue);

let command = Command::set_leds(&pattern);
assert_eq!(command.bytes().len(), 34);
Source

pub fn bytes(&self) -> &[u8]

Returns the complete byte sequence to write to the board.

§Examples
use chessnut_move::protocol::Command;

assert_eq!(Command::enable_realtime_updates().bytes(), [0x21, 0x01, 0x00]);
Source

pub const fn write_kind(&self) -> WriteKind

Returns the GATT write operation required by this command.

§Examples
use chessnut_move::protocol::{Command, WriteKind};

assert_eq!(
    Command::stop_auto_move().write_kind(),
    WriteKind::WithoutResponse,
);

Trait Implementations§

Source§

impl Clone for Command

Source§

fn clone(&self) -> Command

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 PartialEq for Command

Source§

fn eq(&self, other: &Command) -> 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more