wrap_command macro

This commit is contained in:
Vinzenz Schroeter 2025-06-21 23:25:52 +02:00
parent 18f0be072a
commit 1eb59d986a
13 changed files with 162 additions and 107 deletions

View file

@ -1,13 +1,12 @@
use crate::{
commands::wrap_origin_accessors,
macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions},
commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet};
use std::ptr::NonNull;
wrap_clone!(sp_cmd_bitmap::BitmapCommand);
wrap_free!(sp_cmd_bitmap::BitmapCommand);
wrap_command!(Bitmap);
wrap_fields!(sp_cmd_bitmap::BitmapCommand;
prop bitmap: Bitmap { mut get(); move set(value); };

View file

@ -1,5 +1,6 @@
use crate::{
macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions},
commands::wrap_command,
macros::{wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{
@ -8,8 +9,7 @@ use servicepoint::{
};
use std::ptr::NonNull;
wrap_clone!(sp_cmd_bitvec::BitVecCommand);
wrap_free!(sp_cmd_bitvec::BitVecCommand);
wrap_command!(BitVec);
wrap_fields!(sp_cmd_bitvec::BitVecCommand;
prop bitvec: DisplayBitVec { mut get(); move set(value); };

View file

@ -1,13 +1,12 @@
use crate::{
commands::wrap_origin_accessors,
macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions},
commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin, Packet};
use std::ptr::NonNull;
wrap_clone!(sp_cmd_brightness_grid::BrightnessGridCommand);
wrap_free!(sp_cmd_brightness_grid::BrightnessGridCommand);
wrap_command!(BrightnessGrid);
wrap_fields!(sp_cmd_brightness_grid::BrightnessGridCommand;
prop grid: BrightnessGrid { mut get(); move set(grid); };

View file

@ -1,37 +1,45 @@
use crate::{
macros::{wrap_free, wrap_functions},
mem::heap_move_nonnull,
commands::wrap_command, macros::wrap_functions, mem::heap_move_nonnull,
};
use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand};
use std::ptr::NonNull;
macro_rules! wrap_cc_only {
($prefix:ident :: $typ:ident ; $(#[$meta:meta])*) => {
($(#[$meta:meta])*; $command:ident, $prefix:ident, $object_type:ident) => {
wrap_command!($command);
wrap_functions!($prefix;
$(#[$meta])*
///
#[doc = concat!(" Returns: a new [`",stringify!($typ),"`] instance.")]
fn new() -> NonNull<$typ> {
heap_move_nonnull($typ)
#[doc = concat!(" Returns: a new [`",stringify!($object_type),"`] instance.")]
fn new() -> NonNull<$object_type> {
heap_move_nonnull($object_type)
}
);
};
wrap_free!($prefix :: $typ);
($(#[$meta:meta])* $command:ident) => {
::paste::paste!{
wrap_cc_only!($(#[$meta])*; $command, [< sp_cmd_ $command:lower >], [< $command Command >]);
}
};
}
wrap_cc_only!(sp_cmd_clear::ClearCommand;
wrap_cc_only!(
/// Set all pixels to the off state.
///
/// Does not affect brightness.
Clear
);
wrap_cc_only!(sp_cmd_hard_reset::HardResetCommand;
wrap_cc_only!(
/// Kills the udp daemon on the display, which usually results in a restart.
///
/// Please do not send this in your normal program flow.
HardReset
);
wrap_cc_only!(sp_cmd_fade_out::FadeOutCommand;
wrap_cc_only!(
/// A yet-to-be-tested command.
FadeOut
);

View file

@ -1,13 +1,12 @@
use crate::{
commands::wrap_origin_accessors,
macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions},
commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{CharGrid, CharGridCommand, Origin, Packet};
use std::ptr::NonNull;
wrap_clone!(sp_cmd_char_grid::CharGridCommand);
wrap_free!(sp_cmd_char_grid::CharGridCommand);
wrap_command!(CharGrid);
wrap_fields!(sp_cmd_char_grid::CharGridCommand;
prop grid: CharGrid { mut get(); move set(grid); };

View file

@ -1,13 +1,12 @@
use crate::{
commands::wrap_origin_accessors,
macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions},
commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet};
use std::ptr::NonNull;
wrap_clone!(sp_cmd_cp437_grid::Cp437GridCommand);
wrap_free!(sp_cmd_cp437_grid::Cp437GridCommand);
wrap_command!(Cp437Grid);
wrap_fields!(sp_cmd_cp437_grid::Cp437GridCommand;
prop grid: Cp437Grid { mut get(); move set(grid); };

View file

@ -18,7 +18,7 @@ use std::ptr::{null_mut, NonNull};
pub union CommandUnion {
pub null: *mut u8,
pub bitmap: NonNull<BitmapCommand>,
pub bitvec: NonNull<BitVecCommand>,
pub bit_vec: NonNull<BitVecCommand>,
pub brightness_grid: NonNull<BrightnessGridCommand>,
pub char_grid: NonNull<CharGridCommand>,
pub cp437_grid: NonNull<Cp437GridCommand>,
@ -121,7 +121,7 @@ wrap_functions!(sp_cmd_generic;
TypedCommand::BitVec(bitvec) => SPCommand {
tag: CommandTag::BitVec,
data: CommandUnion {
bitvec: heap_move_nonnull(bitvec),
bit_vec: heap_move_nonnull(bitvec),
},
},
TypedCommand::HardReset(hard_reset) => SPCommand {
@ -197,7 +197,7 @@ wrap_functions!(sp_cmd_generic;
CommandTag::BitVec => SPCommand {
tag: CommandTag::BitVec,
data: CommandUnion {
bitvec: heap_clone(command.data.bitvec),
bit_vec: heap_clone(command.data.bit_vec),
},
},
CommandTag::HardReset => SPCommand {
@ -239,7 +239,7 @@ wrap_functions!(sp_cmd_generic;
match command.tag {
CommandTag::Invalid => (),
CommandTag::Bitmap => heap_drop(command.data.bitmap),
CommandTag::BitVec => heap_drop(command.data.bitvec),
CommandTag::BitVec => heap_drop(command.data.bit_vec),
CommandTag::BrightnessGrid => {
heap_drop(command.data.brightness_grid)
}
@ -269,7 +269,7 @@ wrap_functions!(sp_cmd_generic;
heap_move_ok(unsafe { heap_remove(command.data.bitmap).try_into() })
}
CommandTag::BitVec => {
heap_move_ok(unsafe { heap_remove(command.data.bitvec).try_into() })
heap_move_ok(unsafe { heap_remove(command.data.bit_vec).try_into() })
}
CommandTag::BrightnessGrid => heap_move_ok(unsafe {
heap_remove(command.data.brightness_grid).try_into()

View file

@ -1,5 +1,6 @@
use crate::{
macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions},
commands::wrap_command,
macros::{wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_remove},
};
use servicepoint::{Brightness, GlobalBrightnessCommand, Packet};
@ -21,8 +22,7 @@ wrap_functions!(sp_cmd_brightness_global;
);
wrap_clone!(sp_cmd_brightness_global::GlobalBrightnessCommand);
wrap_free!(sp_cmd_brightness_global::GlobalBrightnessCommand);
wrap_command!(GlobalBrightness);
wrap_fields!(
sp_cmd_brightness_global::GlobalBrightnessCommand;

View file

@ -44,4 +44,35 @@ macro_rules! wrap_origin_accessors {
};
}
pub(crate) use wrap_origin_accessors;
macro_rules! derive_command_from {
($command:ident) => {
::paste::paste! {
impl From<::servicepoint::[< $command Command >]> for $crate::commands::SPCommand {
fn from(command: ::servicepoint::[< $command Command >]) -> Self {
Self {
tag: $crate::commands::CommandTag::$command,
data: $crate::commands::CommandUnion {
[< $command:snake >]: $crate::mem::heap_move_nonnull(command)
},
}
}
}
}
};
}
macro_rules! wrap_command {
($command:ident, $prefix:ident, $object_type:ident) => {
$crate::macros::wrap_clone!($prefix::$object_type);
$crate::macros::wrap_free!($prefix::$object_type);
$crate::commands::derive_command_from!($command);
};
($command:ident) => {
::paste::paste!{
wrap_command!($command, [< sp_cmd_ $command:lower >], [< $command Command >]);
}
};
}
pub(crate) use {derive_command_from, wrap_command, wrap_origin_accessors};

View file

@ -209,7 +209,7 @@ macro_rules! wrap_fields {
macro_rules! wrap_functions {
(
$prefix:ident;
$module:ident;
$(
$(#[$meta:meta])+
fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?)
@ -221,13 +221,12 @@ macro_rules! wrap_functions {
$(
$(#[$meta])*
#[doc = ""]
#[doc = concat!(" This function is part of the ", stringify!($prefix), " module.")]
#[doc = concat!(" This function is part of the ", stringify!($module), " module.")]
#[no_mangle]
pub unsafe extern "C" fn [<$prefix _ $function>](
pub unsafe extern "C" fn [< $module _ $function >](
$($param_name: $param_type),*
) $(-> $return_type)?
$block
)+
}
};

View file

@ -79,7 +79,7 @@ wrap_functions!(sp_udp;
.send_command(heap_remove(command.data.bitmap)),
CommandTag::BitVec => connection
.as_ref()
.send_command(heap_remove(command.data.bitvec)),
.send_command(heap_remove(command.data.bit_vec)),
CommandTag::BrightnessGrid => connection
.as_ref()
.send_command(heap_remove(command.data.brightness_grid)),