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
impl Command
Sourcepub fn auto_move(position: Position, mode: AutoMoveMode) -> Self
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);Sourcepub const fn stop_auto_move() -> Self
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
}Sourcepub const fn enable_realtime_updates() -> Self
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.
Sourcepub const fn read_battery_level() -> Self
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.
Sourcepub const fn read_piece_status() -> Self
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.
Sourcepub fn set_leds(pattern: &LedPattern) -> Self
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);Sourcepub fn bytes(&self) -> &[u8] ⓘ
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]);Sourcepub const fn write_kind(&self) -> WriteKind
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,
);