more examples and documentation

This commit is contained in:
Vinzenz Schroeter 2024-06-26 17:05:07 +02:00
parent e3c418efcf
commit fc0705b826
5 changed files with 116 additions and 9 deletions

View file

@ -15,10 +15,44 @@ pub type Offset = usize;
/// The encoding is currently not enforced. /// The encoding is currently not enforced.
pub type Cp437Grid = PrimitiveGrid<u8>; pub type Cp437Grid = PrimitiveGrid<u8>;
/// A command to send to the display. /// A low-level display command.
///
/// This struct and associated functions implement the UDP protocol for the display.
///
/// To send a `Command`, use a `Connection`.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Brightness, Command, Connection, Packet};
///
/// // create command
/// let command = Command::Brightness(Brightness::MAX);
///
/// // turn command into Packet
/// let packet: Packet = command.clone().into();
///
/// // read command from packet
/// let round_tripped = Command::try_from(packet).unwrap();
///
/// // round tripping produces exact copy
/// assert_eq!(command, round_tripped);
///
/// // send command
/// # let connection = Connection::open("127.0.0.1:2342").unwrap();
/// connection.send(command).unwrap();
/// ```
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Command { pub enum Command {
/// Set all pixels to the off state. Does not affect brightness. /// Set all pixels to the off state. Does not affect brightness.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Command, Connection};
/// # let connection = Connection::open("127.0.0.1:2342").unwrap();
/// connection.send(Command::Clear).unwrap();
/// ```
Clear, Clear,
/// Show text on the screen. /// Show text on the screen.
@ -27,12 +61,31 @@ pub enum Command {
/// The library does not currently convert between UTF-8 and CP-437. /// The library does not currently convert between UTF-8 and CP-437.
/// Because Rust expects UTF-8 strings, it might be necessary to only send ASCII for now. /// Because Rust expects UTF-8 strings, it might be necessary to only send ASCII for now.
/// </div> /// </div>
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Command, Connection, Cp437Grid, Origin};
/// # let connection = Connection::open("127.0.0.1:2342").unwrap();
/// let chars = ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'].map(move |c| c as u8);
/// let grid = Cp437Grid::load(5, 2, &chars);
/// connection.send(Command::Cp437Data(Origin::new(2, 2), grid)).unwrap();
/// ```
Cp437Data(Origin<Tiles>, Cp437Grid), Cp437Data(Origin<Tiles>, Cp437Grid),
/// Sets a window of pixels to the specified values /// Sets a window of pixels to the specified values
BitmapLinearWin(Origin<Pixels>, PixelGrid, CompressionCode), BitmapLinearWin(Origin<Pixels>, PixelGrid, CompressionCode),
/// Set the brightness of all tiles to the same value. /// Set the brightness of all tiles to the same value.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Brightness, Command, Connection};
/// # let connection = Connection::open("127.0.0.1:2342").unwrap();
/// let command = Command::Brightness(Brightness::MAX);
/// connection.send(command).unwrap();
/// ```
Brightness(Brightness), Brightness(Brightness),
/// Set the brightness of individual tiles in a rectangular area of the display. /// Set the brightness of individual tiles in a rectangular area of the display.
@ -73,17 +126,43 @@ pub enum Command {
/// Kills the udp daemon on the display, which usually results in a restart. /// Kills the udp daemon on the display, which usually results in a restart.
/// ///
/// Please do not send this in your normal program flow. /// Please do not send this in your normal program flow.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Command, Connection};
/// # let connection = Connection::open("127.0.0.1:2342").unwrap();
/// connection.send(Command::HardReset).unwrap();
/// ```
HardReset, HardReset,
/// <div class="warning">Untested</div> /// <div class="warning">Untested</div>
/// ///
/// Slowly decrease brightness until off or something like that? /// Slowly decrease brightness until off or something like that?
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Command, Connection};
/// # let connection = Connection::open("127.0.0.1:2342").unwrap();
/// connection.send(Command::FadeOut).unwrap();
/// ```
FadeOut, FadeOut,
#[deprecated]
/// Legacy command code, gets ignored by the real display. /// Legacy command code, gets ignored by the real display.
/// ///
/// Might be useful as a noop package. /// Might be useful as a noop package.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Command, Connection};
/// # let connection = Connection::open("127.0.0.1:2342").unwrap();
/// // this sends a packet that does nothing
/// # #[allow(deprecated)]
/// connection.send(Command::BitmapLegacy).unwrap();
/// ```
#[deprecated]
BitmapLegacy, BitmapLegacy,
} }

View file

@ -1,4 +1,17 @@
/// Specifies the kind of compression to use. Availability depends on features. /// Specifies the kind of compression to use. Availability depends on features.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Command, CompressionCode, Origin, PixelGrid};
/// // create command without payload compression
/// # let pixels = PixelGrid::max_sized();
/// _ = Command::BitmapLinearWin(Origin::new(0, 0), pixels, CompressionCode::Uncompressed);
///
/// // create command with payload compressed with lzma and appropriate header flags
/// # let pixels = PixelGrid::max_sized();
/// _ = Command::BitmapLinearWin(Origin::new(0, 0), pixels, CompressionCode::Lzma);
/// ```
#[repr(u16)] #[repr(u16)]
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum CompressionCode { pub enum CompressionCode {

View file

@ -6,6 +6,15 @@ use log::{debug, info};
use crate::Packet; use crate::Packet;
/// A connection to the display. /// A connection to the display.
///
/// # Examples
/// ```rust
/// # use servicepoint::Command;
/// let connection = servicepoint::Connection::open("172.23.42.29:2342")
/// .expect("connection failed");
/// connection.send(Command::Clear)
/// .expect("send failed");
/// ```
pub struct Connection { pub struct Connection {
socket: UdpSocket, socket: UdpSocket,
} }

View file

@ -4,6 +4,9 @@
/// metadata needed. /// metadata needed.
pub trait DataRef<T> { pub trait DataRef<T> {
/// Get the underlying bytes writable. /// Get the underlying bytes writable.
///
/// Note that depending on the struct this is implemented on, writing invalid values here might
/// lead to panics later in the lifetime of the program or on the receiving side.
fn data_ref_mut(&mut self) -> &mut [T]; fn data_ref_mut(&mut self) -> &mut [T];
/// Get the underlying bytes read-only. /// Get the underlying bytes read-only.

View file

@ -5,13 +5,17 @@
//! ```rust //! ```rust
//! use servicepoint::{Command, CompressionCode, Grid, PixelGrid}; //! use servicepoint::{Command, CompressionCode, Grid, PixelGrid};
//! //!
//! let connection = servicepoint::Connection::open("172.23.42.29:2342") //! let connection = servicepoint::Connection::open("127.0.0.1:2342")
//! .expect("connection failed"); //! .expect("connection failed");
//! //!
//! // turn off all pixels on display //! // turn off all pixels on display
//! connection.send(Command::Clear) //! connection.send(Command::Clear)
//! .expect("send failed"); //! .expect("send failed");
//! ```
//! //!
//! ```rust
//! # use servicepoint::{Command, CompressionCode, Grid, PixelGrid};
//! # let connection = servicepoint::Connection::open("127.0.0.1:2342").expect("connection failed");
//! // turn on all pixels in a grid //! // turn on all pixels in a grid
//! let mut pixels = PixelGrid::max_sized(); //! let mut pixels = PixelGrid::max_sized();
//! pixels.fill(true); //! pixels.fill(true);
@ -24,8 +28,7 @@
//! ); //! );
//! //!
//! // send command to display //! // send command to display
//! connection.send(command) //! connection.send(command).expect("send failed");
//! .expect("send failed");
//! ``` //! ```
use std::time::Duration; use std::time::Duration;
@ -73,7 +76,7 @@ pub const TILE_SIZE: usize = 8;
pub const TILE_WIDTH: usize = 56; pub const TILE_WIDTH: usize = 56;
/// Display tile count in the y-direction /// Display tile count in the y-direction
/// ///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
@ -83,7 +86,7 @@ pub const TILE_WIDTH: usize = 56;
pub const TILE_HEIGHT: usize = 20; pub const TILE_HEIGHT: usize = 20;
/// Display width in pixels /// Display width in pixels
/// ///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
@ -121,8 +124,8 @@ pub const PIXEL_COUNT: usize = PIXEL_WIDTH * PIXEL_HEIGHT;
/// // Change pixels here /// // Change pixels here
/// ///
/// connection.send(Command::BitmapLinearWin( /// connection.send(Command::BitmapLinearWin(
/// Origin::new(0,0), /// Origin::new(0,0),
/// pixels, /// pixels,
/// CompressionCode::Lzma /// CompressionCode::Lzma
/// )) /// ))
/// .expect("send failed"); /// .expect("send failed");