Skip to content

Commit

Permalink
examples, format
Browse files Browse the repository at this point in the history
  • Loading branch information
kaesaecracker committed Jun 23, 2024
1 parent 70b0393 commit 3dc5dae
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 33 deletions.
16 changes: 0 additions & 16 deletions crates/servicepoint/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,9 @@ impl Connection {
/// # use servicepoint::{Command, CompressionCode, Grid, PixelGrid};
/// # let connection = servicepoint::Connection::open("172.23.42.29:2342")
/// # .expect("connection failed");
///
/// // turn off all pixels on display
/// connection.send(Command::Clear)
/// .expect("send failed");
///
/// // turn on all pixels in a grid
/// let mut pixels = PixelGrid::max_sized();
/// pixels.fill(true);
///
/// // create command to send pixels
/// let command = Command::BitmapLinearWin(
/// servicepoint::Origin::new(0, 0),
/// pixels,
/// CompressionCode::Uncompressed
/// );
///
/// // send command to display
/// connection.send(command)
/// .expect("send failed");
/// ```
pub fn send(
&self,
Expand Down
90 changes: 86 additions & 4 deletions crates/servicepoint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
//! Abstractions for the UDP protocol of the CCCB servicepoint display.
//!
//! # Examples
//!
//! ```rust
//! use servicepoint::{Command, CompressionCode, Grid, PixelGrid};
//!
//! let connection = servicepoint::Connection::open("172.23.42.29:2342")
//! .expect("connection failed");
//!
//! // turn off all pixels on display
//! connection.send(Command::Clear)
//! .expect("send failed");
//!
//! // turn on all pixels in a grid
//! let mut pixels = PixelGrid::max_sized();
//! pixels.fill(true);
//!
//! // create command to send pixels
//! let command = Command::BitmapLinearWin(
//! servicepoint::Origin::new(0, 0),
//! pixels,
//! CompressionCode::Uncompressed
//! );
//!
//! // send command to display
//! connection.send(command)
//! .expect("send failed");
//! ```

use std::time::Duration;

Expand Down Expand Up @@ -34,22 +62,76 @@ mod primitive_grid;
/// size of a single tile in one dimension
pub const TILE_SIZE: usize = 8;

/// tile count in the x-direction
/// Display tile count in the x-direction
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Cp437Grid, TILE_HEIGHT, TILE_WIDTH};
/// let grid = Cp437Grid::new(TILE_WIDTH, TILE_HEIGHT);
/// ```
pub const TILE_WIDTH: usize = 56;

/// tile count in the y-direction
/// Display tile count in the y-direction
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Cp437Grid, TILE_HEIGHT, TILE_WIDTH};
/// let grid = Cp437Grid::new(TILE_WIDTH, TILE_HEIGHT);
/// ```
pub const TILE_HEIGHT: usize = 20;

/// screen width in pixels
/// Display width in pixels
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid};
/// let grid = PixelGrid::new(PIXEL_WIDTH, PIXEL_HEIGHT);
/// ```
pub const PIXEL_WIDTH: usize = TILE_WIDTH * TILE_SIZE;

/// screen height in pixels
/// Display height in pixels
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid};
/// let grid = PixelGrid::new(PIXEL_WIDTH, PIXEL_HEIGHT);
/// ```
pub const PIXEL_HEIGHT: usize = TILE_HEIGHT * TILE_SIZE;

/// pixel count on whole screen
pub const PIXEL_COUNT: usize = PIXEL_WIDTH * PIXEL_HEIGHT;

/// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets.
///
/// # Examples
///
/// ```rust
/// # use std::time::Instant;
/// # use servicepoint::{Command, CompressionCode, FRAME_PACING, Origin, PixelGrid};
/// # let connection = servicepoint::Connection::open("172.23.42.29:2342")
/// # .expect("connection failed");
/// # let pixels = PixelGrid::max_sized();
/// loop {
/// let start = Instant::now();
///
/// // Change pixels here
///
/// connection.send(Command::BitmapLinearWin(
/// Origin::new(0,0),
/// pixels,
/// CompressionCode::Lzma
/// ))
/// .expect("send failed");
///
/// // warning: will crash if resulting duration is negative, e.g. when resuming from standby
/// std::thread::sleep(FRAME_PACING - start.elapsed());
/// # break; // prevent doctest from hanging
/// }
/// ```
pub const FRAME_PACING: Duration = Duration::from_millis(30);

// include README.md in doctest
Expand Down
35 changes: 23 additions & 12 deletions crates/servicepoint_binding_c/src/brightness_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//!
//! prefix `sp_brightness_grid_`

use servicepoint::{Brightness, BrightnessGrid, DataRef, Grid, PrimitiveGrid};
use std::intrinsics::transmute;
use servicepoint::{BrightnessGrid, DataRef, Grid, PrimitiveGrid, Brightness};

use crate::c_slice::CByteSlice;

Expand All @@ -25,7 +25,9 @@ pub unsafe extern "C" fn sp_brightness_grid_new(
width: usize,
height: usize,
) -> *mut CBrightnessGrid {
Box::into_raw(Box::new(CBrightnessGrid(BrightnessGrid::new(width, height))))
Box::into_raw(Box::new(CBrightnessGrid(BrightnessGrid::new(
width, height,
))))
}

/// Loads a `BrightnessGrid` with the specified dimensions from the provided data.
Expand All @@ -51,8 +53,8 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
) -> *mut CBrightnessGrid {
let data = std::slice::from_raw_parts(data, data_length);
let grid = PrimitiveGrid::load(width, height, data);
let grid = BrightnessGrid::try_from(grid)
.expect("invalid brightness value");
let grid =
BrightnessGrid::try_from(grid).expect("invalid brightness value");
Box::into_raw(Box::new(CBrightnessGrid(grid)))
}

Expand Down Expand Up @@ -83,7 +85,9 @@ pub unsafe extern "C" fn sp_brightness_grid_clone(
/// - `this` is not used concurrently or after this call
/// - `this` was not passed to another consuming function, e.g. to create a `Command`
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_dealloc(this: *mut CBrightnessGrid) {
pub unsafe extern "C" fn sp_brightness_grid_dealloc(
this: *mut CBrightnessGrid,
) {
_ = Box::from_raw(this);
}

Expand Down Expand Up @@ -140,8 +144,8 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
y: usize,
value: u8,
) {
let brightness = Brightness::try_from(value)
.expect("invalid brightness value");
let brightness =
Brightness::try_from(value).expect("invalid brightness value");
(*this).0.set(x, y, brightness);
}

Expand All @@ -159,9 +163,12 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
/// - `this` points to a valid `BrightnessGrid`
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_fill(this: *mut CBrightnessGrid, value: u8) {
let brightness = Brightness::try_from(value)
.expect("invalid brightness value");
pub unsafe extern "C" fn sp_brightness_grid_fill(
this: *mut CBrightnessGrid,
value: u8,
) {
let brightness =
Brightness::try_from(value).expect("invalid brightness value");
(*this).0.fill(brightness);
}

Expand All @@ -177,7 +184,9 @@ pub unsafe extern "C" fn sp_brightness_grid_fill(this: *mut CBrightnessGrid, val
///
/// - `this` points to a valid `BrightnessGrid`
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_width(this: *const CBrightnessGrid) -> usize {
pub unsafe extern "C" fn sp_brightness_grid_width(
this: *const CBrightnessGrid,
) -> usize {
(*this).0.width()
}

Expand All @@ -193,7 +202,9 @@ pub unsafe extern "C" fn sp_brightness_grid_width(this: *const CBrightnessGrid)
///
/// - `this` points to a valid `BrightnessGrid`
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_height(this: *const CBrightnessGrid) -> usize {
pub unsafe extern "C" fn sp_brightness_grid_height(
this: *const CBrightnessGrid,
) -> usize {
(*this).0.height()
}

Expand Down
4 changes: 3 additions & 1 deletion crates/servicepoint_binding_c/src/cp437_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ pub unsafe extern "C" fn sp_cp437_grid_width(this: *const CCp437Grid) -> usize {
///
/// - `this` points to a valid `Cp437Grid`
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_height(this: *const CCp437Grid) -> usize {
pub unsafe extern "C" fn sp_cp437_grid_height(
this: *const CCp437Grid,
) -> usize {
(*this).0.height()
}

Expand Down

0 comments on commit 3dc5dae

Please sign in to comment.