add move fn to dsl, rename SPCommand to GenericCommand, remove DisplayBitVec command
This commit is contained in:
parent
e434130784
commit
323ba6128e
13 changed files with 335 additions and 356 deletions
|
@ -1,5 +1,5 @@
|
|||
use crate::{
|
||||
macros::wrap_functions,
|
||||
macros::{derive_clone, derive_free, wrap_functions},
|
||||
mem::{
|
||||
heap_clone, heap_drop, heap_move, heap_move_nonnull, heap_move_ok,
|
||||
heap_remove,
|
||||
|
@ -11,6 +11,7 @@ use servicepoint::{
|
|||
HardResetCommand, Packet, TypedCommand,
|
||||
};
|
||||
use std::ptr::{null_mut, NonNull};
|
||||
use crate::macros::wrap_methods;
|
||||
|
||||
/// Pointer to one of the available command structs.
|
||||
#[repr(C)]
|
||||
|
@ -55,214 +56,211 @@ pub enum CommandTag {
|
|||
///
|
||||
/// Rust equivalent: [TypedCommand].
|
||||
#[repr(C)]
|
||||
pub struct SPCommand {
|
||||
pub struct GenericCommand {
|
||||
/// Specifies which kind of command struct is contained in `data`
|
||||
pub tag: CommandTag,
|
||||
/// The pointer to the command struct
|
||||
pub data: CommandUnion,
|
||||
}
|
||||
|
||||
impl SPCommand {
|
||||
const INVALID: SPCommand = SPCommand {
|
||||
impl GenericCommand {
|
||||
pub(crate) const INVALID: GenericCommand = GenericCommand {
|
||||
tag: CommandTag::Invalid,
|
||||
data: CommandUnion { null: null_mut() },
|
||||
};
|
||||
}
|
||||
|
||||
wrap_functions!(cmd_generic;
|
||||
derive_clone!(GenericCommand);
|
||||
|
||||
/// Tries to turn a [Packet] into a [SPCommand].
|
||||
impl Clone for GenericCommand {
|
||||
fn clone(&self) -> Self {
|
||||
unsafe {
|
||||
match self.tag {
|
||||
CommandTag::Clear => GenericCommand {
|
||||
tag: CommandTag::Clear,
|
||||
data: CommandUnion {
|
||||
clear: heap_clone(self.data.clear),
|
||||
},
|
||||
},
|
||||
CommandTag::CharGrid => GenericCommand {
|
||||
tag: CommandTag::CharGrid,
|
||||
data: CommandUnion {
|
||||
char_grid: heap_clone(self.data.char_grid),
|
||||
},
|
||||
},
|
||||
CommandTag::Cp437Grid => GenericCommand {
|
||||
tag: CommandTag::Cp437Grid,
|
||||
data: CommandUnion {
|
||||
cp437_grid: heap_clone(self.data.cp437_grid),
|
||||
},
|
||||
},
|
||||
CommandTag::Bitmap => GenericCommand {
|
||||
tag: CommandTag::Bitmap,
|
||||
data: CommandUnion {
|
||||
bitmap: heap_clone(self.data.bitmap),
|
||||
},
|
||||
},
|
||||
CommandTag::GlobalBrightness => GenericCommand {
|
||||
tag: CommandTag::GlobalBrightness,
|
||||
data: CommandUnion {
|
||||
global_brightness: heap_clone(
|
||||
self.data.global_brightness,
|
||||
),
|
||||
},
|
||||
},
|
||||
CommandTag::BrightnessGrid => GenericCommand {
|
||||
tag: CommandTag::BrightnessGrid,
|
||||
data: CommandUnion {
|
||||
brightness_grid: heap_clone(self.data.brightness_grid),
|
||||
},
|
||||
},
|
||||
CommandTag::BitVec => GenericCommand {
|
||||
tag: CommandTag::BitVec,
|
||||
data: CommandUnion {
|
||||
bit_vec: heap_clone(self.data.bit_vec),
|
||||
},
|
||||
},
|
||||
CommandTag::HardReset => GenericCommand {
|
||||
tag: CommandTag::HardReset,
|
||||
data: CommandUnion {
|
||||
hard_reset: heap_clone(self.data.hard_reset),
|
||||
},
|
||||
},
|
||||
CommandTag::FadeOut => GenericCommand {
|
||||
tag: CommandTag::FadeOut,
|
||||
data: CommandUnion {
|
||||
fade_out: heap_clone(self.data.fade_out),
|
||||
},
|
||||
},
|
||||
#[allow(deprecated)]
|
||||
CommandTag::BitmapLegacy => GenericCommand {
|
||||
tag: CommandTag::BitmapLegacy,
|
||||
data: CommandUnion {
|
||||
bitmap_legacy: heap_clone(self.data.bitmap_legacy),
|
||||
},
|
||||
},
|
||||
CommandTag::Invalid => GenericCommand::INVALID,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
derive_free!(GenericCommand);
|
||||
|
||||
impl Drop for GenericCommand {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
match self.tag {
|
||||
CommandTag::Invalid => (),
|
||||
CommandTag::Bitmap => heap_drop(self.data.bitmap),
|
||||
CommandTag::BitVec => heap_drop(self.data.bit_vec),
|
||||
CommandTag::BrightnessGrid => {
|
||||
heap_drop(self.data.brightness_grid)
|
||||
}
|
||||
CommandTag::CharGrid => heap_drop(self.data.char_grid),
|
||||
CommandTag::Cp437Grid => heap_drop(self.data.cp437_grid),
|
||||
CommandTag::GlobalBrightness => {
|
||||
heap_drop(self.data.global_brightness)
|
||||
}
|
||||
CommandTag::Clear => heap_drop(self.data.clear),
|
||||
CommandTag::HardReset => heap_drop(self.data.hard_reset),
|
||||
CommandTag::FadeOut => heap_drop(self.data.fade_out),
|
||||
CommandTag::BitmapLegacy => heap_drop(self.data.bitmap_legacy),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wrap_functions!(associate GenericCommand;
|
||||
|
||||
/// Tries to turn a [Packet] into a [GenericCommand].
|
||||
///
|
||||
/// The packet is dropped in the process.
|
||||
///
|
||||
/// Returns: pointer to new [SPCommand] instance or NULL if parsing failed.
|
||||
/// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed.
|
||||
fn try_from_packet(
|
||||
packet: NonNull<Packet>,
|
||||
) -> SPCommand {
|
||||
) -> NonNull<GenericCommand> {
|
||||
let packet = *unsafe { Box::from_raw(packet.as_ptr()) };
|
||||
servicepoint::TypedCommand::try_from(packet)
|
||||
let result = servicepoint::TypedCommand::try_from(packet)
|
||||
.map(|value| match value {
|
||||
TypedCommand::Clear(clear) => SPCommand {
|
||||
TypedCommand::Clear(clear) => GenericCommand {
|
||||
tag: CommandTag::Clear,
|
||||
data: CommandUnion {
|
||||
clear: heap_move_nonnull(clear),
|
||||
},
|
||||
},
|
||||
TypedCommand::CharGrid(char_grid) => SPCommand {
|
||||
TypedCommand::CharGrid(char_grid) => GenericCommand {
|
||||
tag: CommandTag::CharGrid,
|
||||
data: CommandUnion {
|
||||
char_grid: heap_move_nonnull(char_grid),
|
||||
},
|
||||
},
|
||||
TypedCommand::Cp437Grid(cp437_grid) => SPCommand {
|
||||
TypedCommand::Cp437Grid(cp437_grid) => GenericCommand {
|
||||
tag: CommandTag::Cp437Grid,
|
||||
data: CommandUnion {
|
||||
cp437_grid: heap_move_nonnull(cp437_grid),
|
||||
},
|
||||
},
|
||||
TypedCommand::Bitmap(bitmap) => SPCommand {
|
||||
TypedCommand::Bitmap(bitmap) => GenericCommand {
|
||||
tag: CommandTag::Bitmap,
|
||||
data: CommandUnion {
|
||||
bitmap: heap_move_nonnull(bitmap),
|
||||
},
|
||||
},
|
||||
TypedCommand::Brightness(global_brightness) => SPCommand {
|
||||
TypedCommand::Brightness(global_brightness) => GenericCommand {
|
||||
tag: CommandTag::GlobalBrightness,
|
||||
data: CommandUnion {
|
||||
global_brightness: heap_move_nonnull(global_brightness),
|
||||
},
|
||||
},
|
||||
TypedCommand::BrightnessGrid(brightness_grid) => SPCommand {
|
||||
TypedCommand::BrightnessGrid(brightness_grid) => GenericCommand {
|
||||
tag: CommandTag::BrightnessGrid,
|
||||
data: CommandUnion {
|
||||
brightness_grid: heap_move_nonnull(brightness_grid),
|
||||
},
|
||||
},
|
||||
TypedCommand::BitVec(bitvec) => SPCommand {
|
||||
TypedCommand::BitVec(bitvec) => GenericCommand {
|
||||
tag: CommandTag::BitVec,
|
||||
data: CommandUnion {
|
||||
bit_vec: heap_move_nonnull(bitvec),
|
||||
},
|
||||
},
|
||||
TypedCommand::HardReset(hard_reset) => SPCommand {
|
||||
TypedCommand::HardReset(hard_reset) => GenericCommand {
|
||||
tag: CommandTag::HardReset,
|
||||
data: CommandUnion {
|
||||
hard_reset: heap_move_nonnull(hard_reset),
|
||||
},
|
||||
},
|
||||
TypedCommand::FadeOut(fade_out) => SPCommand {
|
||||
TypedCommand::FadeOut(fade_out) => GenericCommand {
|
||||
tag: CommandTag::FadeOut,
|
||||
data: CommandUnion {
|
||||
fade_out: heap_move_nonnull(fade_out),
|
||||
},
|
||||
},
|
||||
#[allow(deprecated)]
|
||||
TypedCommand::BitmapLegacy(bitmap_legacy) => SPCommand {
|
||||
TypedCommand::BitmapLegacy(bitmap_legacy) => GenericCommand {
|
||||
tag: CommandTag::BitmapLegacy,
|
||||
data: CommandUnion {
|
||||
bitmap_legacy: heap_move_nonnull(bitmap_legacy),
|
||||
},
|
||||
},
|
||||
})
|
||||
.unwrap_or_else(move |_| SPCommand {
|
||||
.unwrap_or_else(move |_| GenericCommand {
|
||||
tag: CommandTag::Invalid,
|
||||
data: CommandUnion { null: null_mut() },
|
||||
})
|
||||
});
|
||||
heap_move_nonnull(result)
|
||||
}
|
||||
|
||||
/// Clones an [SPCommand] instance.
|
||||
///
|
||||
/// returns: a new [SPCommand] instance.
|
||||
fn clone(command: SPCommand) -> SPCommand {
|
||||
unsafe {
|
||||
match command.tag {
|
||||
CommandTag::Clear => SPCommand {
|
||||
tag: CommandTag::Clear,
|
||||
data: CommandUnion {
|
||||
clear: heap_clone(command.data.clear),
|
||||
},
|
||||
},
|
||||
CommandTag::CharGrid => SPCommand {
|
||||
tag: CommandTag::CharGrid,
|
||||
data: CommandUnion {
|
||||
char_grid: heap_clone(command.data.char_grid),
|
||||
},
|
||||
},
|
||||
CommandTag::Cp437Grid => SPCommand {
|
||||
tag: CommandTag::Cp437Grid,
|
||||
data: CommandUnion {
|
||||
cp437_grid: heap_clone(command.data.cp437_grid),
|
||||
},
|
||||
},
|
||||
CommandTag::Bitmap => SPCommand {
|
||||
tag: CommandTag::Bitmap,
|
||||
data: CommandUnion {
|
||||
bitmap: heap_clone(command.data.bitmap),
|
||||
},
|
||||
},
|
||||
CommandTag::GlobalBrightness => SPCommand {
|
||||
tag: CommandTag::GlobalBrightness,
|
||||
data: CommandUnion {
|
||||
global_brightness: heap_clone(
|
||||
command.data.global_brightness,
|
||||
),
|
||||
},
|
||||
},
|
||||
CommandTag::BrightnessGrid => SPCommand {
|
||||
tag: CommandTag::BrightnessGrid,
|
||||
data: CommandUnion {
|
||||
brightness_grid: heap_clone(command.data.brightness_grid),
|
||||
},
|
||||
},
|
||||
CommandTag::BitVec => SPCommand {
|
||||
tag: CommandTag::BitVec,
|
||||
data: CommandUnion {
|
||||
bit_vec: heap_clone(command.data.bit_vec),
|
||||
},
|
||||
},
|
||||
CommandTag::HardReset => SPCommand {
|
||||
tag: CommandTag::HardReset,
|
||||
data: CommandUnion {
|
||||
hard_reset: heap_clone(command.data.hard_reset),
|
||||
},
|
||||
},
|
||||
CommandTag::FadeOut => SPCommand {
|
||||
tag: CommandTag::FadeOut,
|
||||
data: CommandUnion {
|
||||
fade_out: heap_clone(command.data.fade_out),
|
||||
},
|
||||
},
|
||||
#[allow(deprecated)]
|
||||
CommandTag::BitmapLegacy => SPCommand {
|
||||
tag: CommandTag::BitmapLegacy,
|
||||
data: CommandUnion {
|
||||
bitmap_legacy: heap_clone(command.data.bitmap_legacy),
|
||||
},
|
||||
},
|
||||
CommandTag::Invalid => SPCommand::INVALID,
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/// Deallocates an [SPCommand].
|
||||
///
|
||||
/// Commands with an invalid `tag` do not have to be freed as the `data` pointer should be null.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```C
|
||||
/// SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new());
|
||||
/// sp_command_free(c);
|
||||
/// ```
|
||||
fn free(command: SPCommand) {
|
||||
unsafe {
|
||||
match command.tag {
|
||||
CommandTag::Invalid => (),
|
||||
CommandTag::Bitmap => heap_drop(command.data.bitmap),
|
||||
CommandTag::BitVec => heap_drop(command.data.bit_vec),
|
||||
CommandTag::BrightnessGrid => {
|
||||
heap_drop(command.data.brightness_grid)
|
||||
}
|
||||
CommandTag::CharGrid => heap_drop(command.data.char_grid),
|
||||
CommandTag::Cp437Grid => heap_drop(command.data.cp437_grid),
|
||||
CommandTag::GlobalBrightness => {
|
||||
heap_drop(command.data.global_brightness)
|
||||
}
|
||||
CommandTag::Clear => heap_drop(command.data.clear),
|
||||
CommandTag::HardReset => heap_drop(command.data.hard_reset),
|
||||
CommandTag::FadeOut => heap_drop(command.data.fade_out),
|
||||
CommandTag::BitmapLegacy => heap_drop(command.data.bitmap_legacy),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to turn a [SPCommand] into a [Packet].
|
||||
/// The [SPCommand] gets consumed.
|
||||
wrap_methods!{GenericCommand;
|
||||
/// Tries to turn a [GenericCommand] into a [Packet].
|
||||
/// The [GenericCommand] gets consumed.
|
||||
///
|
||||
/// Returns tag [CommandTag::Invalid] in case of an error.
|
||||
fn into_packet(
|
||||
command: SPCommand,
|
||||
) -> *mut Packet {
|
||||
move fn try_into_packet(command) -> *mut Packet {
|
||||
match command.tag {
|
||||
CommandTag::Invalid => null_mut(),
|
||||
CommandTag::Bitmap => {
|
||||
|
@ -296,6 +294,5 @@ wrap_functions!(cmd_generic;
|
|||
heap_move(unsafe { heap_remove(command.data.bitmap_legacy).into() })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ macro_rules! wrap_origin_accessors {
|
|||
macro_rules! derive_command_from {
|
||||
($command:ident) => {
|
||||
::paste::paste! {
|
||||
impl From<::servicepoint::[< $command Command >]> for $crate::commands::SPCommand {
|
||||
impl From<::servicepoint::[< $command Command >]> for $crate::commands::GenericCommand {
|
||||
fn from(command: ::servicepoint::[< $command Command >]) -> Self {
|
||||
Self {
|
||||
tag: $crate::commands::CommandTag::$command,
|
||||
|
@ -67,14 +67,12 @@ macro_rules! derive_command_from {
|
|||
macro_rules! derive_command_into_packet {
|
||||
($command_type:ident) => {
|
||||
::paste::paste! {
|
||||
$crate::macros::wrap_functions!(associate $command_type;
|
||||
$crate::macros::wrap_method!($command_type;
|
||||
#[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."]
|
||||
///
|
||||
/// Returns: NULL or a [Packet] containing the command.
|
||||
fn try_into_packet(
|
||||
command: ::core::ptr::NonNull<$command_type>,
|
||||
) -> *mut ::servicepoint::Packet {
|
||||
$crate::mem::heap_move_ok(unsafe { $crate::mem::heap_remove(command) }.try_into())
|
||||
move fn try_into_packet(instance) -> *mut ::servicepoint::Packet {
|
||||
$crate::mem::heap_move_ok(instance.try_into())
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ use std::ptr::NonNull;
|
|||
wrap_grid!(Bitmap, bool);
|
||||
|
||||
wrap_functions!(bitmap;
|
||||
|
||||
/// Creates a new [Bitmap] with the specified dimensions.
|
||||
///
|
||||
/// # Arguments
|
||||
|
@ -76,35 +75,26 @@ wrap_functions!(bitmap;
|
|||
let bitvec = unsafe { heap_remove(bitvec) };
|
||||
heap_move_ok(Bitmap::from_bitvec(width, bitvec))
|
||||
}
|
||||
);
|
||||
|
||||
wrap_methods!(Bitmap;
|
||||
/// Consumes the Bitmap and returns the contained BitVec.
|
||||
fn into_bitvec(
|
||||
bitmap: NonNull<Bitmap>
|
||||
) -> NonNull<DisplayBitVec> {
|
||||
let bitmap = unsafe { heap_remove(bitmap) };
|
||||
move fn into_bitvec(bitmap) -> NonNull<DisplayBitVec> {
|
||||
heap_move_nonnull(bitmap.into())
|
||||
}
|
||||
};
|
||||
|
||||
/// Creates a [BitmapCommand] and immediately turns that into a [Packet].
|
||||
///
|
||||
/// The provided [Bitmap] gets consumed.
|
||||
///
|
||||
/// Returns NULL in case of an error.
|
||||
fn try_into_packet(
|
||||
bitmap: NonNull<Bitmap>,
|
||||
x: usize,
|
||||
y: usize,
|
||||
compression: CompressionCode,
|
||||
) -> *mut Packet {
|
||||
let bitmap = unsafe { heap_remove(bitmap) };
|
||||
move fn try_into_packet(bitmap, x: usize, y: usize, compression: CompressionCode) -> *mut Packet {
|
||||
heap_move_ok(Packet::try_from(BitmapCommand {
|
||||
bitmap,
|
||||
origin: Origin::new(x, y),
|
||||
compression,
|
||||
}))
|
||||
}
|
||||
);
|
||||
|
||||
wrap_methods!(Bitmap;
|
||||
};
|
||||
|
||||
/// Gets an unsafe reference to the data of the [Bitmap] instance.
|
||||
///
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{
|
||||
containers::{wrap_container, ByteSlice},
|
||||
macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
|
||||
mem::{heap_move_nonnull, heap_move_ok},
|
||||
};
|
||||
use servicepoint::{
|
||||
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet,
|
||||
|
@ -35,29 +35,27 @@ wrap_functions!(associate DisplayBitVec;
|
|||
heap_move_nonnull(DisplayBitVec::from_slice(data))
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
wrap_methods!(DisplayBitVec;
|
||||
/// Creates a [BitVecCommand] and immediately turns that into a [Packet].
|
||||
///
|
||||
/// The provided [DisplayBitVec] gets consumed.
|
||||
///
|
||||
/// Returns NULL in case of an error.
|
||||
fn try_into_packet(
|
||||
bitvec: NonNull<DisplayBitVec>,
|
||||
move fn try_into_packet(
|
||||
bitvec,
|
||||
offset: usize,
|
||||
operation: BinaryOperation,
|
||||
compression: CompressionCode,
|
||||
compression: CompressionCode
|
||||
) -> *mut Packet {
|
||||
let bitvec = unsafe { heap_remove(bitvec) };
|
||||
heap_move_ok(Packet::try_from(BitVecCommand {
|
||||
bitvec,
|
||||
offset,
|
||||
operation,
|
||||
compression,
|
||||
}))
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
wrap_methods!(DisplayBitVec;
|
||||
};
|
||||
|
||||
/// Gets the value of a bit.
|
||||
///
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{
|
||||
containers::{wrap_grid, ByteSlice},
|
||||
macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_move_some},
|
||||
};
|
||||
use servicepoint::{
|
||||
Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid,
|
||||
|
@ -54,26 +54,21 @@ wrap_functions!(associate BrightnessGrid;
|
|||
)
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
wrap_methods!(BrightnessGrid;
|
||||
/// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet].
|
||||
///
|
||||
/// The provided [BrightnessGrid] gets consumed.
|
||||
///
|
||||
/// Returns NULL in case of an error.
|
||||
fn try_into_packet(
|
||||
grid: NonNull<BrightnessGrid>,
|
||||
x: usize,
|
||||
y: usize,
|
||||
) -> *mut Packet {
|
||||
let grid = unsafe { heap_remove(grid) };
|
||||
move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet {
|
||||
heap_move_ok(Packet::try_from(BrightnessGridCommand {
|
||||
grid,
|
||||
origin: Origin::new(x, y),
|
||||
}))
|
||||
}
|
||||
};
|
||||
|
||||
);
|
||||
|
||||
wrap_methods!(BrightnessGrid;
|
||||
/// Gets an unsafe reference to the data of the instance.
|
||||
///
|
||||
/// The returned memory is valid for the lifetime of the grid.
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{
|
||||
containers::{derive_get_width_height, wrap_container, ByteSlice},
|
||||
macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
|
||||
mem::{heap_move_nonnull, heap_move_ok},
|
||||
};
|
||||
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
|
||||
use std::ptr::NonNull;
|
||||
|
@ -23,41 +23,18 @@ wrap_functions!(associate CharGrid;
|
|||
/// sp_char_grid_set(grid, 0, 0, '!');
|
||||
/// sp_char_grid_free(grid);
|
||||
/// ```
|
||||
fn new(
|
||||
width: usize,
|
||||
height: usize,
|
||||
) -> NonNull<CharGrid> {
|
||||
fn new(width: usize, height: usize) -> NonNull<CharGrid> {
|
||||
heap_move_nonnull(CharGrid::new(width, height))
|
||||
}
|
||||
|
||||
/// Loads a [CharGrid] with the specified dimensions from the provided data.
|
||||
///
|
||||
/// returns: new CharGrid or NULL in case of an error
|
||||
fn load(
|
||||
width: usize,
|
||||
height: usize,
|
||||
data: ByteSlice,
|
||||
) -> *mut CharGrid {
|
||||
fn load(width: usize, height: usize, data: ByteSlice) -> *mut CharGrid {
|
||||
let data = unsafe { data.as_slice() };
|
||||
heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec()))
|
||||
}
|
||||
|
||||
/// Creates a [CharGridCommand] and immediately turns that into a [Packet].
|
||||
///
|
||||
/// The provided [CharGrid] gets consumed.
|
||||
///
|
||||
/// Returns NULL in case of an error.
|
||||
fn try_into_packet(
|
||||
grid: NonNull<CharGrid>,
|
||||
x: usize,
|
||||
y: usize,
|
||||
) -> *mut Packet {
|
||||
let grid = unsafe { heap_remove(grid) };
|
||||
heap_move_ok(Packet::try_from(CharGridCommand {
|
||||
grid,
|
||||
origin: Origin::new(x, y),
|
||||
}))
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
|
@ -102,4 +79,16 @@ wrap_methods!(CharGrid;
|
|||
mut fn fill(instance, value: u32) {
|
||||
instance.fill(char::from_u32(value).unwrap())
|
||||
};
|
||||
|
||||
/// Creates a [CharGridCommand] and immediately turns that into a [Packet].
|
||||
///
|
||||
/// The provided [CharGrid] gets consumed.
|
||||
///
|
||||
/// Returns NULL in case of an error.
|
||||
move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet {
|
||||
heap_move_ok(Packet::try_from(CharGridCommand {
|
||||
grid,
|
||||
origin: Origin::new(x, y),
|
||||
}))
|
||||
};
|
||||
);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{
|
||||
containers::{wrap_grid, ByteSlice},
|
||||
macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_move_some},
|
||||
};
|
||||
use servicepoint::{
|
||||
Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet,
|
||||
|
@ -32,26 +32,21 @@ wrap_functions!(cp437grid;
|
|||
heap_move_some(Cp437Grid::load(width, height, data))
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
wrap_methods!(Cp437Grid;
|
||||
/// Creates a [Cp437GridCommand] and immediately turns that into a [Packet].
|
||||
///
|
||||
/// The provided [Cp437Grid] gets consumed.
|
||||
///
|
||||
/// Returns NULL in case of an error.
|
||||
fn try_into_packet(
|
||||
grid: NonNull<Cp437Grid>,
|
||||
x: usize,
|
||||
y: usize,
|
||||
) -> *mut Packet {
|
||||
let grid = unsafe { heap_remove(grid) };
|
||||
move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet {
|
||||
heap_move_ok(Packet::try_from(Cp437GridCommand {
|
||||
grid,
|
||||
origin: Origin::new(x, y),
|
||||
}))
|
||||
}
|
||||
};
|
||||
|
||||
);
|
||||
|
||||
wrap_methods!(Cp437Grid;
|
||||
/// Gets an unsafe reference to the data of the grid.
|
||||
///
|
||||
/// The returned memory is valid for the lifetime of the instance.
|
||||
|
|
|
@ -36,6 +36,12 @@ macro_rules! nonnull_as_mut {
|
|||
};
|
||||
}
|
||||
|
||||
macro_rules! nonnull_as_move {
|
||||
($ident:ident) => {
|
||||
$crate::mem::heap_remove($ident)
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! wrap_method {
|
||||
(
|
||||
$object_type:ident;
|
||||
|
@ -229,6 +235,7 @@ macro_rules! wrap_functions {
|
|||
}
|
||||
|
||||
pub(crate) use {
|
||||
derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields,
|
||||
wrap_functions, wrap_methods, wrap_fields_accessor, wrap_method
|
||||
derive_clone, derive_free, nonnull_as_move, nonnull_as_mut, nonnull_as_ref,
|
||||
wrap_fields, wrap_fields_accessor, wrap_functions, wrap_method,
|
||||
wrap_methods,
|
||||
};
|
||||
|
|
78
src/udp.rs
78
src/udp.rs
|
@ -1,6 +1,6 @@
|
|||
use crate::{
|
||||
commands::{CommandTag, SPCommand},
|
||||
macros::{derive_free, wrap_functions},
|
||||
commands::{CommandTag, GenericCommand},
|
||||
macros::{derive_free, wrap_functions, wrap_methods},
|
||||
mem::{heap_move_ok, heap_remove},
|
||||
};
|
||||
use servicepoint::{Header, Packet, UdpSocketExt};
|
||||
|
@ -49,17 +49,20 @@ wrap_functions!(associate UdpSocket;
|
|||
heap_move_ok(UdpSocket::bind_connect(addr))
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
wrap_methods! {UdpSocket;
|
||||
/// Sends a [Packet] to the display using the [UdpSocket].
|
||||
///
|
||||
/// The passed `packet` gets consumed.
|
||||
///
|
||||
/// returns: true in case of success
|
||||
fn send_packet(connection: NonNull<UdpSocket>, packet: NonNull<Packet>) -> bool {
|
||||
ref fn send_packet(connection, packet: NonNull<Packet>) -> bool {
|
||||
let packet = unsafe { heap_remove(packet) };
|
||||
unsafe { connection.as_ref().send(&Vec::from(packet)) }.is_ok()
|
||||
}
|
||||
connection.send(&Vec::from(packet)).is_ok()
|
||||
};
|
||||
|
||||
/// Sends a [SPCommand] to the display using the [UdpSocket].
|
||||
/// Sends a [GenericCommand] to the display using the [UdpSocket].
|
||||
///
|
||||
/// The passed `command` gets consumed.
|
||||
///
|
||||
|
@ -70,44 +73,26 @@ wrap_functions!(associate UdpSocket;
|
|||
/// ```C
|
||||
/// sp_udp_send_command(connection, sp_command_brightness(5));
|
||||
/// ```
|
||||
fn send_command(connection: NonNull<UdpSocket>, command: SPCommand) -> bool {
|
||||
ref fn send_command(connection, command: NonNull<GenericCommand>) -> bool {
|
||||
unsafe {
|
||||
match command.tag {
|
||||
let command = crate::macros::nonnull_as_mut!(command);
|
||||
let result = match command.tag {
|
||||
CommandTag::Invalid => return false,
|
||||
CommandTag::Bitmap => connection
|
||||
.as_ref()
|
||||
.send_command(heap_remove(command.data.bitmap)),
|
||||
CommandTag::BitVec => connection
|
||||
.as_ref()
|
||||
.send_command(heap_remove(command.data.bit_vec)),
|
||||
CommandTag::BrightnessGrid => connection
|
||||
.as_ref()
|
||||
.send_command(heap_remove(command.data.brightness_grid)),
|
||||
CommandTag::CharGrid => connection
|
||||
.as_ref()
|
||||
.send_command(heap_remove(command.data.char_grid)),
|
||||
CommandTag::Cp437Grid => connection
|
||||
.as_ref()
|
||||
.send_command(heap_remove(command.data.cp437_grid)),
|
||||
CommandTag::GlobalBrightness => connection
|
||||
.as_ref()
|
||||
.send_command(heap_remove(command.data.global_brightness)),
|
||||
CommandTag::Clear => connection
|
||||
.as_ref()
|
||||
.send_command(heap_remove(command.data.clear)),
|
||||
CommandTag::HardReset => connection
|
||||
.as_ref()
|
||||
.send_command(heap_remove(command.data.hard_reset)),
|
||||
CommandTag::FadeOut => connection
|
||||
.as_ref()
|
||||
.send_command(heap_remove(command.data.fade_out)),
|
||||
CommandTag::BitmapLegacy => connection
|
||||
.as_ref()
|
||||
.send_command(heap_remove(command.data.bitmap_legacy)),
|
||||
}
|
||||
CommandTag::Bitmap => connection.send_command(heap_remove(command.data.bitmap)),
|
||||
CommandTag::BitVec => connection.send_command(heap_remove(command.data.bit_vec)),
|
||||
CommandTag::BrightnessGrid => connection.send_command(heap_remove(command.data.brightness_grid)),
|
||||
CommandTag::CharGrid => connection.send_command(heap_remove(command.data.char_grid)),
|
||||
CommandTag::Cp437Grid => connection.send_command(heap_remove(command.data.cp437_grid)),
|
||||
CommandTag::GlobalBrightness => connection.send_command(heap_remove(command.data.global_brightness)),
|
||||
CommandTag::Clear => connection.send_command(heap_remove(command.data.clear)),
|
||||
CommandTag::HardReset => connection.send_command(heap_remove(command.data.hard_reset)),
|
||||
CommandTag::FadeOut => connection.send_command(heap_remove(command.data.fade_out)),
|
||||
CommandTag::BitmapLegacy => connection.send_command(heap_remove(command.data.bitmap_legacy)),
|
||||
}.is_some();
|
||||
*command = GenericCommand::INVALID;
|
||||
result
|
||||
}
|
||||
.is_some()
|
||||
}
|
||||
};
|
||||
|
||||
/// Sends a [Header] to the display using the [UdpSocket].
|
||||
///
|
||||
|
@ -118,17 +103,14 @@ wrap_functions!(associate UdpSocket;
|
|||
/// ```C
|
||||
/// sp_udp_send_header(connection, sp_command_brightness(5));
|
||||
/// ```
|
||||
fn send_header(udp_connection: NonNull<UdpSocket>, header: Header) -> bool {
|
||||
ref fn send_header(udp_connection, header: Header) -> bool {
|
||||
let packet = Packet {
|
||||
header,
|
||||
payload: None,
|
||||
};
|
||||
unsafe { udp_connection.as_ref() }
|
||||
.send(&Vec::from(packet))
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
);
|
||||
udp_connection.send(&Vec::from(packet)).is_ok()
|
||||
};
|
||||
}
|
||||
|
||||
mod _hidden {
|
||||
/// This is a type only used by cbindgen to have a type for pointers.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue