wrap and rename ALL the types

This commit is contained in:
Vinzenz Schroeter 2024-09-05 21:15:53 +02:00
parent b9fc06117e
commit 051dbfabea
18 changed files with 577 additions and 480 deletions

View file

@ -2,34 +2,34 @@
//!
//! prefix `sp_bit_vec_`
use crate::c_slice::CByteSlice;
use crate::c_slice::SPByteSlice;
use servicepoint::bitvec::prelude::{BitVec, Msb0};
/// cbindgen:no-export
type SpBitVec = BitVec<u8, Msb0>;
/// A vector of bits
pub struct CBitVec {
actual: SpBitVec,
}
///
/// # Examples
/// ```C
/// SPBitVec vec = sp_bit_vec_new(8);
/// sp_bit_vec_set(vec, 5, true);
/// sp_bit_vec_dealloc(vec);
/// ```
pub struct SPBitVec(BitVec<u8, Msb0>);
impl From<SpBitVec> for CBitVec {
fn from(actual: SpBitVec) -> Self {
Self { actual }
impl From<BitVec<u8, Msb0>> for SPBitVec {
fn from(actual: BitVec<u8, Msb0>) -> Self {
Self(actual)
}
}
impl From<CBitVec> for SpBitVec {
fn from(value: CBitVec) -> Self {
value.actual
impl From<SPBitVec> for BitVec<u8, Msb0> {
fn from(value: SPBitVec) -> Self {
value.0
}
}
impl Clone for CBitVec {
impl Clone for SPBitVec {
fn clone(&self) -> Self {
CBitVec {
actual: self.actual.clone(),
}
SPBitVec(self.0.clone())
}
}
@ -52,10 +52,8 @@ impl Clone for CBitVec {
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_bit_vec_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_new(size: usize) -> *mut CBitVec {
Box::into_raw(Box::new(CBitVec {
actual: SpBitVec::repeat(false, size),
}))
pub unsafe extern "C" fn sp_bit_vec_new(size: usize) -> *mut SPBitVec {
Box::into_raw(Box::new(SPBitVec(BitVec::repeat(false, size))))
}
/// Interpret the data as a series of bits and load then into a new `BitVec` instance.
@ -72,11 +70,9 @@ pub unsafe extern "C" fn sp_bit_vec_new(size: usize) -> *mut CBitVec {
pub unsafe extern "C" fn sp_bit_vec_load(
data: *const u8,
data_length: usize,
) -> *mut CBitVec {
) -> *mut SPBitVec {
let data = std::slice::from_raw_parts(data, data_length);
Box::into_raw(Box::new(CBitVec {
actual: SpBitVec::from_slice(data),
}))
Box::into_raw(Box::new(SPBitVec(BitVec::from_slice(data))))
}
/// Clones a `BitVec`.
@ -91,8 +87,8 @@ pub unsafe extern "C" fn sp_bit_vec_load(
/// by explicitly calling `sp_bit_vec_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_clone(
this: *const CBitVec,
) -> *mut CBitVec {
this: *const SPBitVec,
) -> *mut SPBitVec {
Box::into_raw(Box::new((*this).clone()))
}
@ -106,7 +102,7 @@ pub unsafe extern "C" fn sp_bit_vec_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_bit_vec_dealloc(this: *mut CBitVec) {
pub unsafe extern "C" fn sp_bit_vec_dealloc(this: *mut SPBitVec) {
_ = Box::from_raw(this);
}
@ -131,10 +127,10 @@ pub unsafe extern "C" fn sp_bit_vec_dealloc(this: *mut CBitVec) {
/// - `this` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_get(
this: *const CBitVec,
this: *const SPBitVec,
index: usize,
) -> bool {
*(*this).actual.get(index).unwrap()
*(*this).0.get(index).unwrap()
}
/// Sets the value of a bit in the `BitVec`.
@ -159,11 +155,11 @@ pub unsafe extern "C" fn sp_bit_vec_get(
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_set(
this: *mut CBitVec,
this: *mut SPBitVec,
index: usize,
value: bool,
) {
(*this).actual.set(index, value)
(*this).0.set(index, value)
}
/// Sets the value of all bits in the `BitVec`.
@ -179,8 +175,8 @@ pub unsafe extern "C" fn sp_bit_vec_set(
/// - `this` points to a valid `BitVec`
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_fill(this: *mut CBitVec, value: bool) {
(*this).actual.fill(value)
pub unsafe extern "C" fn sp_bit_vec_fill(this: *mut SPBitVec, value: bool) {
(*this).0.fill(value)
}
/// Gets the length of the `BitVec` in bits.
@ -191,8 +187,8 @@ pub unsafe extern "C" fn sp_bit_vec_fill(this: *mut CBitVec, value: bool) {
///
/// - `this` points to a valid `BitVec`
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_len(this: *const CBitVec) -> usize {
(*this).actual.len()
pub unsafe extern "C" fn sp_bit_vec_len(this: *const SPBitVec) -> usize {
(*this).0.len()
}
/// Returns true if length is 0.
@ -203,8 +199,8 @@ pub unsafe extern "C" fn sp_bit_vec_len(this: *const CBitVec) -> usize {
///
/// - `this` points to a valid `BitVec`
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_is_empty(this: *const CBitVec) -> bool {
(*this).actual.is_empty()
pub unsafe extern "C" fn sp_bit_vec_is_empty(this: *const SPBitVec) -> bool {
(*this).0.is_empty()
}
/// Gets an unsafe reference to the data of the `BitVec` instance.
@ -218,10 +214,10 @@ pub unsafe extern "C" fn sp_bit_vec_is_empty(this: *const CBitVec) -> bool {
/// - the returned memory range is never accessed concurrently, either via the `BitVec` or directly
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_unsafe_data_ref(
this: *mut CBitVec,
) -> CByteSlice {
let data = (*this).actual.as_raw_mut_slice();
CByteSlice {
this: *mut SPBitVec,
) -> SPByteSlice {
let data = (*this).0.as_raw_mut_slice();
SPByteSlice {
start: data.as_mut_ptr_range().start,
length: data.len(),
}

View file

@ -2,18 +2,32 @@
//!
//! prefix `sp_brightness_grid_`
use crate::c_slice::CByteSlice;
use servicepoint::{Brightness, BrightnessGrid, DataRef, Grid, PrimitiveGrid};
use crate::c_slice::SPByteSlice;
use servicepoint::{Brightness, DataRef, Grid, PrimitiveGrid};
use std::intrinsics::transmute;
/// C-wrapper for grid containing brightness values.
pub struct CBrightnessGrid {
pub(crate) actual: BrightnessGrid,
/// A grid containing brightness values.
///
/// # Examples
/// ```C
/// SPConnection connection = sp_connection_open("127.0.0.1:2342");
/// if (connection == NULL)
/// return 1;
///
/// SPBrightnessGrid grid = sp_brightness_grid_new(2, 2);
/// sp_brightness_grid_set(grid, 0, 0, 0);
/// sp_brightness_grid_set(grid, 1, 1, 10);
///
/// SPCommand command = sp_command_char_brightness(grid);
/// sp_connection_dealloc(connection);
/// ```
pub struct SPBrightnessGrid {
pub(crate) actual: servicepoint::BrightnessGrid,
}
impl Clone for CBrightnessGrid {
impl Clone for SPBrightnessGrid {
fn clone(&self) -> Self {
CBrightnessGrid {
SPBrightnessGrid {
actual: self.actual.clone(),
}
}
@ -33,9 +47,9 @@ impl Clone for CBrightnessGrid {
pub unsafe extern "C" fn sp_brightness_grid_new(
width: usize,
height: usize,
) -> *mut CBrightnessGrid {
Box::into_raw(Box::new(CBrightnessGrid {
actual: BrightnessGrid::new(width, height),
) -> *mut SPBrightnessGrid {
Box::into_raw(Box::new(SPBrightnessGrid {
actual: servicepoint::BrightnessGrid::new(width, height),
}))
}
@ -59,12 +73,12 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
height: usize,
data: *const u8,
data_length: usize,
) -> *mut CBrightnessGrid {
) -> *mut SPBrightnessGrid {
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");
Box::into_raw(Box::new(CBrightnessGrid { actual: grid }))
let grid = servicepoint::BrightnessGrid::try_from(grid)
.expect("invalid brightness value");
Box::into_raw(Box::new(SPBrightnessGrid { actual: grid }))
}
/// Clones a `BrightnessGrid`.
@ -79,8 +93,8 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
/// by explicitly calling `sp_brightness_grid_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_clone(
this: *const CBrightnessGrid,
) -> *mut CBrightnessGrid {
this: *const SPBrightnessGrid,
) -> *mut SPBrightnessGrid {
Box::into_raw(Box::new((*this).clone()))
}
@ -95,7 +109,7 @@ pub unsafe extern "C" fn sp_brightness_grid_clone(
/// - `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,
this: *mut SPBrightnessGrid,
) {
_ = Box::from_raw(this);
}
@ -119,7 +133,7 @@ pub unsafe extern "C" fn sp_brightness_grid_dealloc(
/// - `this` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_get(
this: *const CBrightnessGrid,
this: *const SPBrightnessGrid,
x: usize,
y: usize,
) -> u8 {
@ -149,7 +163,7 @@ pub unsafe extern "C" fn sp_brightness_grid_get(
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_set(
this: *mut CBrightnessGrid,
this: *mut SPBrightnessGrid,
x: usize,
y: usize,
value: u8,
@ -178,7 +192,7 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_fill(
this: *mut CBrightnessGrid,
this: *mut SPBrightnessGrid,
value: u8,
) {
let brightness =
@ -199,7 +213,7 @@ pub unsafe extern "C" fn sp_brightness_grid_fill(
/// - `this` points to a valid `BrightnessGrid`
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_width(
this: *const CBrightnessGrid,
this: *const SPBrightnessGrid,
) -> usize {
(*this).actual.width()
}
@ -217,7 +231,7 @@ pub unsafe extern "C" fn sp_brightness_grid_width(
/// - `this` points to a valid `BrightnessGrid`
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_height(
this: *const CBrightnessGrid,
this: *const SPBrightnessGrid,
) -> usize {
(*this).actual.height()
}
@ -233,13 +247,13 @@ pub unsafe extern "C" fn sp_brightness_grid_height(
/// - the returned memory range is never accessed concurrently, either via the `BrightnessGrid` or directly
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref(
this: *mut CBrightnessGrid,
) -> CByteSlice {
this: *mut SPBrightnessGrid,
) -> SPByteSlice {
assert_eq!(std::mem::size_of::<Brightness>(), 1);
let data = (*this).actual.data_ref_mut();
let data: &mut [u8] = transmute(data);
CByteSlice {
SPByteSlice {
start: data.as_mut_ptr_range().start,
length: data.len(),
}

View file

@ -10,7 +10,7 @@
/// - accesses to the memory pointed to with `start` is never accessed outside `length`
/// - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in
/// the function returning this type.
pub struct CByteSlice {
pub struct SPByteSlice {
/// The start address of the memory
pub start: *mut u8,
/// The amount of memory in bytes

View file

@ -4,24 +4,33 @@
use std::ptr::null_mut;
use servicepoint::{
Brightness, Command, CompressionCode, Offset, Origin, Packet, PixelGrid,
};
use servicepoint::{Brightness, Origin};
use crate::bit_vec::CBitVec;
use crate::brightness_grid::CBrightnessGrid;
use crate::cp437_grid::CCp437Grid;
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;
/// A low-level display command.
///
/// This struct and associated functions implement the UDP protocol for the display.
///
/// To send a `CCommand`, use a `Connection`.
pub struct CCommand(pub(crate) Command);
/// To send a `CCommand`, use a `CConnection`.
///
/// # Examples
///
/// ```C
/// sp_connection_send(connection, sp_command_clear());
/// sp_connection_send(connection, sp_command_brightness(5));
/// ```
pub struct SPCommand(pub(crate) servicepoint::Command);
impl Clone for CCommand {
impl Clone for SPCommand {
fn clone(&self) -> Self {
CCommand(self.0.clone())
SPCommand(self.0.clone())
}
}
@ -40,12 +49,12 @@ impl Clone for CCommand {
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_try_from_packet(
packet: *mut Packet,
) -> *mut CCommand {
packet: *mut SPPacket,
) -> *mut SPCommand {
let packet = *Box::from_raw(packet);
match Command::try_from(packet) {
match servicepoint::Command::try_from(packet.0) {
Err(_) => null_mut(),
Ok(command) => Box::into_raw(Box::new(CCommand(command))),
Ok(command) => Box::into_raw(Box::new(SPCommand(command))),
}
}
@ -61,8 +70,8 @@ pub unsafe extern "C" fn sp_command_try_from_packet(
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_clone(
original: *const CCommand,
) -> *mut CCommand {
original: *const SPCommand,
) -> *mut SPCommand {
Box::into_raw(Box::new((*original).clone()))
}
@ -75,8 +84,8 @@ pub unsafe extern "C" fn sp_command_clone(
/// - 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_clear() -> *mut CCommand {
Box::into_raw(Box::new(CCommand(Command::Clear)))
pub unsafe extern "C" fn sp_command_clear() -> *mut SPCommand {
Box::into_raw(Box::new(SPCommand(servicepoint::Command::Clear)))
}
/// Allocates a new `Command::HardReset` instance.
@ -88,8 +97,8 @@ pub unsafe extern "C" fn sp_command_clear() -> *mut CCommand {
/// - 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_hard_reset() -> *mut CCommand {
Box::into_raw(Box::new(CCommand(Command::HardReset)))
pub unsafe extern "C" fn sp_command_hard_reset() -> *mut SPCommand {
Box::into_raw(Box::new(SPCommand(servicepoint::Command::HardReset)))
}
/// Allocates a new `Command::FadeOut` instance.
@ -101,8 +110,8 @@ pub unsafe extern "C" fn sp_command_hard_reset() -> *mut CCommand {
/// - 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_fade_out() -> *mut CCommand {
Box::into_raw(Box::new(CCommand(Command::FadeOut)))
pub unsafe extern "C" fn sp_command_fade_out() -> *mut SPCommand {
Box::into_raw(Box::new(SPCommand(servicepoint::Command::FadeOut)))
}
/// Allocates a new `Command::Brightness` instance for setting the brightness of all tiles to the
@ -116,36 +125,38 @@ pub unsafe extern "C" fn sp_command_fade_out() -> *mut CCommand {
///
/// The caller has to make sure that:
///
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// - the returned `SPCommand` 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_brightness(
brightness: u8,
) -> *mut CCommand {
) -> *mut SPCommand {
let brightness =
Brightness::try_from(brightness).expect("invalid brightness");
Box::into_raw(Box::new(CCommand(Command::Brightness(brightness))))
Box::into_raw(Box::new(SPCommand(servicepoint::Command::Brightness(
brightness,
))))
}
/// Allocates a new `Command::CharBrightness` instance.
/// The passed `ByteGrid` gets consumed.
/// The passed `SPBrightnessGrid` gets consumed.
///
/// # 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
/// - `grid` points to a valid instance of `SPBrightnessGrid`
/// - `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_char_brightness(
x: usize,
y: usize,
byte_grid: *mut CBrightnessGrid,
) -> *mut CCommand {
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(CCommand(Command::CharBrightness(
grid: *mut SPBrightnessGrid,
) -> *mut SPCommand {
let byte_grid = *Box::from_raw(grid);
Box::into_raw(Box::new(SPCommand(servicepoint::Command::CharBrightness(
Origin::new(x, y),
byte_grid.actual,
))))
@ -165,15 +176,15 @@ pub unsafe extern "C" fn sp_command_char_brightness(
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear(
offset: Offset,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut CCommand {
offset: SPOffset,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(CCommand(Command::BitmapLinear(
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinear(
offset,
bit_vec.into(),
compression,
compression.try_into().expect("invalid compression code"),
))))
}
@ -191,15 +202,15 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_and(
offset: Offset,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut CCommand {
offset: SPOffset,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(CCommand(Command::BitmapLinearAnd(
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearAnd(
offset,
bit_vec.into(),
compression,
compression.try_into().expect("invalid compression code"),
))))
}
@ -217,15 +228,15 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_or(
offset: Offset,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut CCommand {
offset: SPOffset,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(CCommand(Command::BitmapLinearOr(
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearOr(
offset,
bit_vec.into(),
compression,
compression.try_into().expect("invalid compression code"),
))))
}
@ -243,15 +254,15 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
offset: Offset,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut CCommand {
offset: SPOffset,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *mut SPCommand {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(CCommand(Command::BitmapLinearXor(
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearXor(
offset,
bit_vec.into(),
compression,
compression.try_into().expect("invalid compression code"),
))))
}
@ -270,10 +281,10 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
pub unsafe extern "C" fn sp_command_cp437_data(
x: usize,
y: usize,
byte_grid: *mut CCp437Grid,
) -> *mut CCommand {
byte_grid: *mut SPCp437Grid,
) -> *mut SPCommand {
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(CCommand(Command::Cp437Data(
Box::into_raw(Box::new(SPCommand(servicepoint::Command::Cp437Data(
Origin::new(x, y),
byte_grid.actual,
))))
@ -295,14 +306,16 @@ pub unsafe extern "C" fn sp_command_cp437_data(
pub unsafe extern "C" fn sp_command_bitmap_linear_win(
x: usize,
y: usize,
pixel_grid: *mut PixelGrid,
compression_code: CompressionCode,
) -> *mut CCommand {
let byte_grid = *Box::from_raw(pixel_grid);
Box::into_raw(Box::new(CCommand(Command::BitmapLinearWin(
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(
Origin::new(x, y),
byte_grid,
compression_code,
compression_code
.try_into()
.expect("invalid compression code"),
))))
}
@ -316,6 +329,6 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
/// - `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]
pub unsafe extern "C" fn sp_command_dealloc(ptr: *mut CCommand) {
pub unsafe extern "C" fn sp_command_dealloc(ptr: *mut SPCommand) {
_ = Box::from_raw(ptr);
}

View file

@ -5,7 +5,18 @@
use std::ffi::{c_char, CStr};
use std::ptr::null_mut;
use servicepoint::{Connection, Packet};
use crate::packet::SPPacket;
/// A connection to the display.
///
/// # Examples
///
/// ```C
/// CConnection connection = sp_connection_open("172.23.42.29:2342");
/// if (connection != NULL)
/// sp_connection_send(connection, sp_command_clear());
/// ```
pub struct SPConnection(pub(crate) servicepoint::Connection);
/// Creates a new instance of `Connection`.
///
@ -24,14 +35,14 @@ use servicepoint::{Connection, Packet};
#[no_mangle]
pub unsafe extern "C" fn sp_connection_open(
host: *const c_char,
) -> *mut Connection {
) -> *mut SPConnection {
let host = CStr::from_ptr(host).to_str().expect("Bad encoding");
let connection = match Connection::open(host) {
let connection = match servicepoint::Connection::open(host) {
Err(_) => return null_mut(),
Ok(value) => value,
};
Box::into_raw(Box::new(connection))
Box::into_raw(Box::new(SPConnection(connection)))
}
/// Sends a `Packet` to the display using the `Connection`.
@ -48,11 +59,11 @@ pub unsafe extern "C" fn sp_connection_open(
/// - `packet` is not used concurrently or after this call
#[no_mangle]
pub unsafe extern "C" fn sp_connection_send(
connection: *const Connection,
packet: *mut Packet,
connection: *const SPConnection,
packet: *mut SPPacket,
) -> bool {
let packet = Box::from_raw(packet);
(*connection).send(*packet).is_ok()
(*connection).0.send((*packet).0).is_ok()
}
/// Closes and deallocates a `Connection`.
@ -64,6 +75,6 @@ pub unsafe extern "C" fn sp_connection_send(
/// - `this` points to a valid `Connection`
/// - `this` is not used concurrently or after this call
#[no_mangle]
pub unsafe extern "C" fn sp_connection_dealloc(ptr: *mut Connection) {
pub unsafe extern "C" fn sp_connection_dealloc(ptr: *mut SPConnection) {
_ = Box::from_raw(ptr);
}

View file

@ -0,0 +1,48 @@
//! re-exported constants for use in C
use servicepoint::CompressionCode;
use std::time::Duration;
/// size of a single tile in one dimension
pub const SP_TILE_SIZE: usize = 8;
/// Display tile count in the x-direction
pub const SP_TILE_WIDTH: usize = 56;
/// Display tile count in the y-direction
pub const SP_TILE_HEIGHT: usize = 20;
/// Display width in pixels
pub const SP_PIXEL_WIDTH: usize = SP_TILE_WIDTH * SP_TILE_SIZE;
/// Display height in pixels
pub const SP_PIXEL_HEIGHT: usize = SP_TILE_HEIGHT * SP_TILE_SIZE;
/// pixel count on whole screen
pub const SP_PIXEL_COUNT: usize = SP_PIXEL_WIDTH * SP_PIXEL_HEIGHT;
/// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets.
pub const SP_FRAME_PACING_MS: u128 = Duration::from_millis(30).as_millis();
/// Specifies the kind of compression to use.
#[repr(u16)]
pub enum SPCompressionCode {
/// no compression
Uncompressed = 0x0,
/// compress using flate2 with zlib header
Zlib = 0x677a,
/// compress using bzip2
Bzip2 = 0x627a,
/// compress using lzma
Lzma = 0x6c7a,
/// compress using Zstandard
Zstd = 0x7a73,
}
impl TryFrom<SPCompressionCode> for CompressionCode {
type Error = ();
fn try_from(value: SPCompressionCode) -> Result<Self, Self::Error> {
CompressionCode::try_from(value as u16)
}
}

View file

@ -2,19 +2,28 @@
//!
//! prefix `sp_cp437_grid_`
use crate::c_slice::CByteSlice;
use crate::c_slice::SPByteSlice;
use servicepoint::{Cp437Grid, DataRef, Grid};
/// A C-wrapper for grid containing codepage 437 characters.
///
/// The encoding is currently not enforced.
pub struct CCp437Grid {
///
/// # Examples
///
/// ```C
/// Cp437Grid grid = sp_cp437_grid_new(4, 3);
/// sp_cp437_grid_fill(grid, '?');
/// sp_cp437_grid_set(grid, 0, 0, '!');
/// sp_cp437_grid_dealloc(grid);
/// ```
pub struct SPCp437Grid {
pub(crate) actual: Cp437Grid,
}
impl Clone for CCp437Grid {
impl Clone for SPCp437Grid {
fn clone(&self) -> Self {
CCp437Grid {
SPCp437Grid {
actual: self.actual.clone(),
}
}
@ -34,8 +43,8 @@ impl Clone for CCp437Grid {
pub unsafe extern "C" fn sp_cp437_grid_new(
width: usize,
height: usize,
) -> *mut CCp437Grid {
Box::into_raw(Box::new(CCp437Grid {
) -> *mut SPCp437Grid {
Box::into_raw(Box::new(SPCp437Grid {
actual: Cp437Grid::new(width, height),
}))
}
@ -60,9 +69,9 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
height: usize,
data: *const u8,
data_length: usize,
) -> *mut CCp437Grid {
) -> *mut SPCp437Grid {
let data = std::slice::from_raw_parts(data, data_length);
Box::into_raw(Box::new(CCp437Grid {
Box::into_raw(Box::new(SPCp437Grid {
actual: Cp437Grid::load(width, height, data),
}))
}
@ -79,8 +88,8 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
/// by explicitly calling `sp_cp437_grid_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_clone(
this: *const CCp437Grid,
) -> *mut CCp437Grid {
this: *const SPCp437Grid,
) -> *mut SPCp437Grid {
Box::into_raw(Box::new((*this).clone()))
}
@ -94,7 +103,7 @@ pub unsafe extern "C" fn sp_cp437_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_cp437_grid_dealloc(this: *mut CCp437Grid) {
pub unsafe extern "C" fn sp_cp437_grid_dealloc(this: *mut SPCp437Grid) {
_ = Box::from_raw(this);
}
@ -117,7 +126,7 @@ pub unsafe extern "C" fn sp_cp437_grid_dealloc(this: *mut CCp437Grid) {
/// - `this` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_get(
this: *const CCp437Grid,
this: *const SPCp437Grid,
x: usize,
y: usize,
) -> u8 {
@ -146,7 +155,7 @@ pub unsafe extern "C" fn sp_cp437_grid_get(
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_set(
this: *mut CCp437Grid,
this: *mut SPCp437Grid,
x: usize,
y: usize,
value: u8,
@ -168,7 +177,7 @@ pub unsafe extern "C" fn sp_cp437_grid_set(
/// - `this` points to a valid `Cp437Grid`
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_fill(this: *mut CCp437Grid, value: u8) {
pub unsafe extern "C" fn sp_cp437_grid_fill(this: *mut SPCp437Grid, value: u8) {
(*this).actual.fill(value);
}
@ -184,7 +193,9 @@ pub unsafe extern "C" fn sp_cp437_grid_fill(this: *mut CCp437Grid, value: u8) {
///
/// - `this` points to a valid `Cp437Grid`
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_width(this: *const CCp437Grid) -> usize {
pub unsafe extern "C" fn sp_cp437_grid_width(
this: *const SPCp437Grid,
) -> usize {
(*this).actual.width()
}
@ -201,7 +212,7 @@ 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,
this: *const SPCp437Grid,
) -> usize {
(*this).actual.height()
}
@ -217,10 +228,10 @@ pub unsafe extern "C" fn sp_cp437_grid_height(
/// - the returned memory range is never accessed concurrently, either via the `Cp437Grid` or directly
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref(
this: *mut CCp437Grid,
) -> CByteSlice {
this: *mut SPCp437Grid,
) -> SPByteSlice {
let data = (*this).actual.data_ref_mut();
CByteSlice {
SPByteSlice {
start: data.as_mut_ptr_range().start,
length: data.len(),
}

View file

@ -1,11 +1,6 @@
//! C API wrapper for the `servicepoint` crate.
pub use servicepoint::{
CompressionCode, PIXEL_COUNT, PIXEL_HEIGHT, PIXEL_WIDTH, TILE_HEIGHT,
TILE_SIZE, TILE_WIDTH,
};
pub use crate::c_slice::CByteSlice;
pub use crate::c_slice::SPByteSlice;
pub mod bit_vec;
@ -23,5 +18,7 @@ pub mod c_slice;
pub mod cp437_grid;
/// The minimum time needed for the display to refresh the screen in ms.
pub const FRAME_PACING_MS: u32 = servicepoint::FRAME_PACING.as_millis() as u32;
pub mod constants;
/// Type alias for documenting the meaning of the variable in enum values
pub type SPOffset = usize;

View file

@ -4,8 +4,10 @@
use std::ptr::null_mut;
use crate::command::CCommand;
use servicepoint::Packet;
use crate::command::SPCommand;
/// The raw packet
pub struct SPPacket(pub(crate) servicepoint::Packet);
/// Turns a `Command` into a `Packet`.
/// The `Command` gets consumed.
@ -20,10 +22,10 @@ use servicepoint::Packet;
/// by explicitly calling `sp_packet_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_packet_from_command(
command: *mut CCommand,
) -> *mut Packet {
command: *mut SPCommand,
) -> *mut SPPacket {
let command = *Box::from_raw(command);
let packet = command.0.into();
let packet = SPPacket(command.0.into());
Box::into_raw(Box::new(packet))
}
@ -43,11 +45,11 @@ pub unsafe extern "C" fn sp_packet_from_command(
pub unsafe extern "C" fn sp_packet_try_load(
data: *const u8,
length: usize,
) -> *mut Packet {
) -> *mut SPPacket {
let data = std::slice::from_raw_parts(data, length);
match Packet::try_from(data) {
match servicepoint::Packet::try_from(data) {
Err(_) => null_mut(),
Ok(packet) => Box::into_raw(Box::new(packet)),
Ok(packet) => Box::into_raw(Box::new(SPPacket(packet))),
}
}
@ -62,8 +64,10 @@ pub unsafe extern "C" fn sp_packet_try_load(
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_packet_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_packet_clone(this: *const Packet) -> *mut Packet {
Box::into_raw(Box::new((*this).clone()))
pub unsafe extern "C" fn sp_packet_clone(
this: *const SPPacket,
) -> *mut SPPacket {
Box::into_raw(Box::new(SPPacket((*this).0.clone())))
}
/// Deallocates a `Packet`.
@ -75,6 +79,6 @@ pub unsafe extern "C" fn sp_packet_clone(this: *const Packet) -> *mut Packet {
/// - `this` points to a valid `Packet`
/// - `this` is not used concurrently or after this call
#[no_mangle]
pub unsafe extern "C" fn sp_packet_dealloc(this: *mut Packet) {
pub unsafe extern "C" fn sp_packet_dealloc(this: *mut SPPacket) {
_ = Box::from_raw(this)
}

View file

@ -2,9 +2,21 @@
//!
//! prefix `sp_pixel_grid_`
use servicepoint::{DataRef, Grid, PixelGrid};
use servicepoint::{DataRef, Grid};
use crate::c_slice::CByteSlice;
use crate::c_slice::SPByteSlice;
/// A grid of pixels.
///
/// # Examples
///
/// ```C
/// Cp437Grid grid = sp_pixel_grid_new(8, 3);
/// sp_pixel_grid_fill(grid, true);
/// sp_pixel_grid_set(grid, 0, 0, false);
/// sp_pixel_grid_dealloc(grid);
/// ```
pub struct SPPixelGrid(pub(crate) servicepoint::PixelGrid);
/// Creates a new `PixelGrid` with the specified dimensions.
///
@ -29,8 +41,10 @@ use crate::c_slice::CByteSlice;
pub unsafe extern "C" fn sp_pixel_grid_new(
width: usize,
height: usize,
) -> *mut PixelGrid {
Box::into_raw(Box::new(PixelGrid::new(width, height)))
) -> *mut SPPixelGrid {
Box::into_raw(Box::new(SPPixelGrid(servicepoint::PixelGrid::new(
width, height,
))))
}
/// Loads a `PixelGrid` with the specified dimensions from the provided data.
@ -60,9 +74,11 @@ pub unsafe extern "C" fn sp_pixel_grid_load(
height: usize,
data: *const u8,
data_length: usize,
) -> *mut PixelGrid {
) -> *mut SPPixelGrid {
let data = std::slice::from_raw_parts(data, data_length);
Box::into_raw(Box::new(PixelGrid::load(width, height, data)))
Box::into_raw(Box::new(SPPixelGrid(servicepoint::PixelGrid::load(
width, height, data,
))))
}
/// Clones a `PixelGrid`.
@ -77,9 +93,9 @@ pub unsafe extern "C" fn sp_pixel_grid_load(
/// by explicitly calling `sp_pixel_grid_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_clone(
this: *const PixelGrid,
) -> *mut PixelGrid {
Box::into_raw(Box::new((*this).clone()))
this: *const SPPixelGrid,
) -> *mut SPPixelGrid {
Box::into_raw(Box::new(SPPixelGrid((*this).0.clone())))
}
/// Deallocates a `PixelGrid`.
@ -92,7 +108,7 @@ pub unsafe extern "C" fn sp_pixel_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_pixel_grid_dealloc(this: *mut PixelGrid) {
pub unsafe extern "C" fn sp_pixel_grid_dealloc(this: *mut SPPixelGrid) {
_ = Box::from_raw(this);
}
@ -115,11 +131,11 @@ pub unsafe extern "C" fn sp_pixel_grid_dealloc(this: *mut PixelGrid) {
/// - `this` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_get(
this: *const PixelGrid,
this: *const SPPixelGrid,
x: usize,
y: usize,
) -> bool {
(*this).get(x, y)
(*this).0.get(x, y)
}
/// Sets the value of the specified position in the `PixelGrid`.
@ -144,12 +160,12 @@ pub unsafe extern "C" fn sp_pixel_grid_get(
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_set(
this: *mut PixelGrid,
this: *mut SPPixelGrid,
x: usize,
y: usize,
value: bool,
) {
(*this).set(x, y, value);
(*this).0.set(x, y, value);
}
/// Sets the state of all pixels in the `PixelGrid`.
@ -166,8 +182,11 @@ pub unsafe extern "C" fn sp_pixel_grid_set(
/// - `this` points to a valid `PixelGrid`
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_fill(this: *mut PixelGrid, value: bool) {
(*this).fill(value);
pub unsafe extern "C" fn sp_pixel_grid_fill(
this: *mut SPPixelGrid,
value: bool,
) {
(*this).0.fill(value);
}
/// Gets the width in pixels of the `PixelGrid` instance.
@ -182,8 +201,10 @@ pub unsafe extern "C" fn sp_pixel_grid_fill(this: *mut PixelGrid, value: bool) {
///
/// - `this` points to a valid `PixelGrid`
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_width(this: *const PixelGrid) -> usize {
(*this).width()
pub unsafe extern "C" fn sp_pixel_grid_width(
this: *const SPPixelGrid,
) -> usize {
(*this).0.width()
}
/// Gets the height in pixels of the `PixelGrid` instance.
@ -198,8 +219,10 @@ pub unsafe extern "C" fn sp_pixel_grid_width(this: *const PixelGrid) -> usize {
///
/// - `this` points to a valid `PixelGrid`
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_height(this: *const PixelGrid) -> usize {
(*this).height()
pub unsafe extern "C" fn sp_pixel_grid_height(
this: *const SPPixelGrid,
) -> usize {
(*this).0.height()
}
/// Gets an unsafe reference to the data of the `PixelGrid` instance.
@ -213,10 +236,10 @@ pub unsafe extern "C" fn sp_pixel_grid_height(this: *const PixelGrid) -> usize {
/// - the returned memory range is never accessed concurrently, either via the `PixelGrid` or directly
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_unsafe_data_ref(
this: *mut PixelGrid,
) -> CByteSlice {
let data = (*this).data_ref_mut();
CByteSlice {
this: *mut SPPixelGrid,
) -> SPByteSlice {
let data = (*this).0.data_ref_mut();
SPByteSlice {
start: data.as_mut_ptr_range().start,
length: data.len(),
}