servicepoint/crates/servicepoint_binding_c/src/command.rs

392 lines
12 KiB
Rust
Raw Normal View History

//! C functions for interacting with `Command`s
//!
//! prefix `sp_command_`
use std::ptr::null_mut;
2024-09-05 21:15:53 +02:00
use servicepoint::{Brightness, Origin};
2024-09-05 21:15:53 +02:00
use crate::bit_vec::SPBitVec;
use crate::brightness_grid::SPBrightnessGrid;
use crate::constants::SPCompressionCode;
use crate::cp437_grid::SPCp437Grid;
use crate::packet::SPPacket;
use crate::pixel_grid::SPPixelGrid;
use crate::SPOffset;
2024-08-29 22:02:53 +02:00
/// A low-level display command.
///
/// This struct and associated functions implement the UDP protocol for the display.
///
2024-09-06 20:10:14 +02:00
/// To send a `SPCommand`, use a `SPConnection`.
2024-09-05 21:15:53 +02:00
///
/// # Examples
///
/// ```C
/// sp_connection_send(connection, sp_command_clear());
/// sp_connection_send(connection, sp_command_brightness(5));
/// ```
pub struct SPCommand(pub(crate) servicepoint::Command);
2024-09-05 21:15:53 +02:00
impl Clone for SPCommand {
fn clone(&self) -> Self {
2024-09-05 21:15:53 +02:00
SPCommand(self.0.clone())
}
}
2024-09-06 20:10:14 +02:00
/// Tries to turn a `SPPacket` into a `SPCommand`. The packet is deallocated in the process.
///
2024-09-06 20:10:14 +02:00
/// Returns: pointer to new `SPCommand` instance or NULL
2024-05-28 21:25:59 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-09-06 20:10:14 +02:00
/// - `packet` points to a valid instance of `SPPacket`
2024-05-28 21:25:59 +02:00
/// - `packet` is not used concurrently or after this call
/// - the result is checked for NULL
2024-09-06 20:10:14 +02:00
/// - the returned `SPCommand` instance is freed in some way, either by using a consuming function or
2024-05-28 21:25:59 +02:00
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_try_from_packet(
2024-09-05 21:15:53 +02:00
packet: *mut SPPacket,
) -> *mut SPCommand {
let packet = *Box::from_raw(packet);
2024-09-05 21:15:53 +02:00
match servicepoint::Command::try_from(packet.0) {
Err(_) => null_mut(),
2024-09-05 21:15:53 +02:00
Ok(command) => Box::into_raw(Box::new(SPCommand(command))),
}
}
2024-09-06 20:10:14 +02:00
/// Clones a `SPCommand` instance.
2024-05-28 21:25:59 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `this` points to a valid instance of `Command`
/// - `this` is not written to concurrently
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_clone(
2024-09-05 21:15:53 +02:00
original: *const SPCommand,
) -> *mut SPCommand {
Box::into_raw(Box::new((*original).clone()))
}
2024-05-28 21:25:59 +02:00
/// Allocates a new `Command::Clear` instance.
///
2024-09-06 20:10:14 +02:00
/// Set all pixels to the off state. Does not affect brightness.
///
/// # Examples
///
/// ```C
/// sp_connection_send(connection, sp_command_clear());
/// ```
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
2024-09-05 21:15:53 +02:00
pub unsafe extern "C" fn sp_command_clear() -> *mut SPCommand {
Box::into_raw(Box::new(SPCommand(servicepoint::Command::Clear)))
}
2024-05-28 21:25:59 +02:00
/// Allocates a new `Command::HardReset` instance.
///
2024-09-06 20:10:14 +02:00
/// Kills the udp daemon on the display, which usually results in a restart.
/// Please do not send this in your normal program flow.
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
2024-09-05 21:15:53 +02:00
pub unsafe extern "C" fn sp_command_hard_reset() -> *mut SPCommand {
Box::into_raw(Box::new(SPCommand(servicepoint::Command::HardReset)))
}
2024-05-28 21:25:59 +02:00
/// Allocates a new `Command::FadeOut` instance.
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
2024-09-05 21:15:53 +02:00
pub unsafe extern "C" fn sp_command_fade_out() -> *mut SPCommand {
Box::into_raw(Box::new(SPCommand(servicepoint::Command::FadeOut)))
}
2024-06-23 13:00:13 +02:00
/// Allocates a new `Command::Brightness` instance for setting the brightness of all tiles to the
/// same value.
///
/// # Panics
///
/// - When the provided brightness value is out of range (0-11).
2024-05-28 21:25:59 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-09-05 21:15:53 +02:00
/// - the returned `SPCommand` instance is freed in some way, either by using a consuming function or
2024-05-28 21:25:59 +02:00
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_brightness(
brightness: u8,
2024-09-05 21:15:53 +02:00
) -> *mut SPCommand {
2024-06-23 13:38:46 +02:00
let brightness =
Brightness::try_from(brightness).expect("invalid brightness");
2024-09-05 21:15:53 +02:00
Box::into_raw(Box::new(SPCommand(servicepoint::Command::Brightness(
brightness,
))))
}
/// Allocates a new `Command::CharBrightness` instance.
2024-09-05 21:15:53 +02:00
/// The passed `SPBrightnessGrid` gets consumed.
2024-05-28 21:25:59 +02:00
///
2024-09-06 20:10:14 +02:00
/// Set the brightness of individual tiles in a rectangular area of the display.
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
2024-09-05 21:15:53 +02:00
/// - `grid` points to a valid instance of `SPBrightnessGrid`
/// - `grid` is not used concurrently or after this call
2024-05-28 21:25:59 +02:00
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_char_brightness(
x: usize,
y: usize,
2024-09-05 21:15:53 +02:00
grid: *mut SPBrightnessGrid,
) -> *mut SPCommand {
let byte_grid = *Box::from_raw(grid);
Box::into_raw(Box::new(SPCommand(servicepoint::Command::CharBrightness(
2024-06-23 13:38:46 +02:00
Origin::new(x, y),
2024-08-29 22:21:21 +02:00
byte_grid.actual,
))))
}
/// Allocates a new `Command::BitmapLinear` instance.
2024-05-28 21:25:59 +02:00
/// The passed `BitVec` gets consumed.
///
2024-09-06 20:10:14 +02:00
/// Set pixel data starting at the pixel offset on screen.
///
/// The screen will continuously overwrite more pixel data without regarding the offset, meaning
/// once the starting row is full, overwriting will continue on column 0.
///
/// The contained `BitVec` is always uncompressed.
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bit_vec` points to a valid instance of `BitVec`
/// - `bit_vec` is not used concurrently or after this call
/// - `compression` matches one of the allowed enum values
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear(
2024-09-05 21:15:53 +02:00
offset: SPOffset,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
let bit_vec = *Box::from_raw(bit_vec);
2024-09-05 21:15:53 +02:00
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinear(
offset,
bit_vec.into(),
2024-09-05 21:15:53 +02:00
compression.try_into().expect("invalid compression code"),
))))
}
/// Allocates a new `Command::BitmapLinearAnd` instance.
2024-05-28 21:25:59 +02:00
/// The passed `BitVec` gets consumed.
///
2024-09-06 20:10:14 +02:00
/// Set pixel data according to an and-mask starting at the offset.
///
/// The screen will continuously overwrite more pixel data without regarding the offset, meaning
/// once the starting row is full, overwriting will continue on column 0.
///
/// The contained `BitVec` is always uncompressed.
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bit_vec` points to a valid instance of `BitVec`
/// - `bit_vec` is not used concurrently or after this call
/// - `compression` matches one of the allowed enum values
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_and(
2024-09-05 21:15:53 +02:00
offset: SPOffset,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
let bit_vec = *Box::from_raw(bit_vec);
2024-09-05 21:15:53 +02:00
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearAnd(
offset,
bit_vec.into(),
2024-09-05 21:15:53 +02:00
compression.try_into().expect("invalid compression code"),
))))
}
/// Allocates a new `Command::BitmapLinearOr` instance.
2024-05-28 21:25:59 +02:00
/// The passed `BitVec` gets consumed.
///
2024-09-06 20:10:14 +02:00
/// Set pixel data according to an or-mask starting at the offset.
///
/// The screen will continuously overwrite more pixel data without regarding the offset, meaning
/// once the starting row is full, overwriting will continue on column 0.
///
/// The contained `BitVec` is always uncompressed.
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bit_vec` points to a valid instance of `BitVec`
/// - `bit_vec` is not used concurrently or after this call
/// - `compression` matches one of the allowed enum values
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_or(
2024-09-05 21:15:53 +02:00
offset: SPOffset,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
let bit_vec = *Box::from_raw(bit_vec);
2024-09-05 21:15:53 +02:00
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearOr(
offset,
bit_vec.into(),
2024-09-05 21:15:53 +02:00
compression.try_into().expect("invalid compression code"),
))))
}
/// Allocates a new `Command::BitmapLinearXor` instance.
2024-05-28 21:25:59 +02:00
/// The passed `BitVec` gets consumed.
///
2024-09-06 20:10:14 +02:00
/// Set pixel data according to a xor-mask starting at the offset.
///
/// The screen will continuously overwrite more pixel data without regarding the offset, meaning
/// once the starting row is full, overwriting will continue on column 0.
///
/// The contained `BitVec` is always uncompressed.
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bit_vec` points to a valid instance of `BitVec`
/// - `bit_vec` is not used concurrently or after this call
/// - `compression` matches one of the allowed enum values
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
2024-09-05 21:15:53 +02:00
offset: SPOffset,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
let bit_vec = *Box::from_raw(bit_vec);
2024-09-05 21:15:53 +02:00
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearXor(
offset,
bit_vec.into(),
2024-09-05 21:15:53 +02:00
compression.try_into().expect("invalid compression code"),
))))
}
/// Allocates a new `Command::Cp437Data` instance.
2024-05-28 21:25:59 +02:00
/// The passed `ByteGrid` gets consumed.
///
2024-09-06 20:10:14 +02:00
/// Show text on the screen.
///
/// <div class="warning">
/// 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.
/// </div>
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - `byte_grid` points to a valid instance of `ByteGrid`
/// - `byte_grid` is not used concurrently or after this call
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_cp437_data(
x: usize,
y: usize,
2024-09-05 21:15:53 +02:00
byte_grid: *mut SPCp437Grid,
) -> *mut SPCommand {
let byte_grid = *Box::from_raw(byte_grid);
2024-09-05 21:15:53 +02:00
Box::into_raw(Box::new(SPCommand(servicepoint::Command::Cp437Data(
Origin::new(x, y),
2024-08-29 22:21:21 +02:00
byte_grid.actual,
))))
}
/// Allocates a new `Command::BitmapLinearWin` instance.
2024-05-28 21:25:59 +02:00
/// The passed `PixelGrid` gets consumed.
///
2024-09-06 20:10:14 +02:00
/// Sets a window of pixels to the specified values
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - `pixel_grid` points to a valid instance of `PixelGrid`
/// - `pixel_grid` is not used concurrently or after this call
/// - `compression` matches one of the allowed enum values
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_win(
x: usize,
y: usize,
2024-09-05 21:15:53 +02:00
pixel_grid: *mut SPPixelGrid,
compression_code: SPCompressionCode,
) -> *mut SPCommand {
let byte_grid = (*Box::from_raw(pixel_grid)).0;
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearWin(
2024-06-23 13:38:46 +02:00
Origin::new(x, y),
byte_grid,
2024-09-05 21:15:53 +02:00
compression_code
.try_into()
.expect("invalid compression code"),
))))
}
2024-05-28 21:25:59 +02:00
/// Deallocates a `Command`.
///
2024-09-06 20:10:14 +02:00
/// # Examples
///
/// ```C
/// SPCommand c = sp_command_clear();
/// sp_command_dealloc(c);
/// ```
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
/// - `this` points to a valid `Command`
/// - `this` is not used concurrently or after this call
/// - `this` was not passed to another consuming function, e.g. to create a `Packet`
#[no_mangle]
2024-09-05 21:15:53 +02:00
pub unsafe extern "C" fn sp_command_dealloc(ptr: *mut SPCommand) {
_ = Box::from_raw(ptr);
}