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

@ -33,7 +33,7 @@ bool log_command(struct Command command) {
size_t x, y;
sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y);
Bitmap *bitmap = sp_cmd_bitmap_get(bitmapCommand);
Bitmap *bitmap = sp_cmd_bitmap_get_bitmap_mut(bitmapCommand);
size_t w = sp_bitmap_width(bitmap);
size_t h = sp_bitmap_height(bitmap);
@ -47,7 +47,7 @@ bool log_command(struct Command command) {
size_t x, y;
sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y);
BrightnessGrid *grid = sp_cmd_brightness_grid_get(gridCommand);
BrightnessGrid *grid = sp_cmd_brightness_grid_get_grid_mut(gridCommand);
size_t w = sp_brightness_grid_width(grid);
size_t h = sp_brightness_grid_height(grid);
@ -61,7 +61,7 @@ bool log_command(struct Command command) {
size_t x, y;
sp_cmd_char_grid_get_origin(gridCommand, &x, &y);
CharGrid *grid = sp_cmd_char_grid_get(gridCommand);
CharGrid *grid = sp_cmd_char_grid_get_grid_mut(gridCommand);
size_t w = sp_char_grid_width(grid);
size_t h = sp_char_grid_height(grid);
@ -75,7 +75,7 @@ bool log_command(struct Command command) {
size_t x, y;
sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y);
Cp437Grid *grid = sp_cmd_cp437_grid_get(gridCommand);
Cp437Grid *grid = sp_cmd_cp437_grid_get_grid_mut(gridCommand);
size_t w = sp_cp437_grid_width(grid);
size_t h = sp_cp437_grid_height(grid);
@ -84,7 +84,7 @@ bool log_command(struct Command command) {
break;
}
case COMMAND_TAG_BIT_VEC: {
BitVecCommand *bitvecCommand = command.data.bitvec;
BitVecCommand *bitvecCommand = command.data.bit_vec;
size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand);
CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand);
@ -109,7 +109,7 @@ bool log_command(struct Command command) {
break;
}
BitVec *bitvec = sp_cmd_bitvec_get(bitvecCommand);
BitVec *bitvec = sp_cmd_bitvec_get_bitvec_mut(bitvecCommand);
size_t len = sp_bitvec_len(bitvec);
printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n",

View file

@ -99,7 +99,7 @@ typedef uint8_t BinaryOperation;
#endif // __cplusplus
/**
* The u16 command codes used for the [Command]s.
* The u16 command codes used for the [`crate::Command`]s.
*/
enum CommandCode
#ifdef __cplusplus
@ -560,7 +560,7 @@ typedef struct ValueGrid_u8 Cp437Grid;
typedef union CommandUnion {
uint8_t *null;
struct BitmapCommand */*notnull*/ bitmap;
struct BitVecCommand */*notnull*/ bitvec;
struct BitVecCommand */*notnull*/ bit_vec;
struct BrightnessGridCommand */*notnull*/ brightness_grid;
struct CharGridCommand */*notnull*/ char_grid;
struct Cp437GridCommand */*notnull*/ cp437_grid;
@ -1432,20 +1432,6 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance,
*/
struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command);
/**
*Clones a [`GlobalBrightnessCommand`] instance.
*
* This function is part of the sp_cmd_brightness_global module.
*/
struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struct GlobalBrightnessCommand */*notnull*/ instance);
/**
*Deallocates a [`GlobalBrightnessCommand`] instance.
*
* This function is part of the sp_cmd_brightness_global module.
*/
void sp_cmd_brightness_global_free(struct GlobalBrightnessCommand */*notnull*/ instance);
/**
* Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`].
*
@ -1477,20 +1463,6 @@ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightn
void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance,
Brightness value);
/**
*Clones a [`BrightnessGridCommand`] instance.
*
* This function is part of the sp_cmd_brightness_grid module.
*/
struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(struct BrightnessGridCommand */*notnull*/ instance);
/**
*Deallocates a [`BrightnessGridCommand`] instance.
*
* This function is part of the sp_cmd_brightness_grid module.
*/
void sp_cmd_brightness_grid_free(struct BrightnessGridCommand */*notnull*/ instance);
/**
* Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand],
* leaving other fields as their default values.
@ -1559,18 +1531,18 @@ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/
size_t origin_y);
/**
*Clones a [`CharGridCommand`] instance.
*Clones a [`BrightnessGridCommand`] instance.
*
* This function is part of the sp_cmd_char_grid module.
* This function is part of the sp_cmd_brightnessgrid module.
*/
struct CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(struct CharGridCommand */*notnull*/ instance);
struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance);
/**
*Deallocates a [`CharGridCommand`] instance.
*Deallocates a [`BrightnessGridCommand`] instance.
*
* This function is part of the sp_cmd_char_grid module.
* This function is part of the sp_cmd_brightnessgrid module.
*/
void sp_cmd_char_grid_free(struct CharGridCommand */*notnull*/ instance);
void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance);
/**
* Moves the provided [CharGrid] into a new [CharGridCommand],
@ -1639,6 +1611,27 @@ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command,
*/
struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command);
/**
*Clones a [`CharGridCommand`] instance.
*
* This function is part of the sp_cmd_chargrid module.
*/
struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance);
/**
*Deallocates a [`CharGridCommand`] instance.
*
* This function is part of the sp_cmd_chargrid module.
*/
void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance);
/**
*Clones a [`ClearCommand`] instance.
*
* This function is part of the sp_cmd_clear module.
*/
struct ClearCommand */*notnull*/ sp_cmd_clear_clone(struct ClearCommand */*notnull*/ instance);
/**
*Deallocates a [`ClearCommand`] instance.
*
@ -1657,20 +1650,6 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance);
*/
struct ClearCommand */*notnull*/ sp_cmd_clear_new(void);
/**
*Clones a [`Cp437GridCommand`] instance.
*
* This function is part of the sp_cmd_cp437_grid module.
*/
struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(struct Cp437GridCommand */*notnull*/ instance);
/**
*Deallocates a [`Cp437GridCommand`] instance.
*
* This function is part of the sp_cmd_cp437_grid module.
*/
void sp_cmd_cp437_grid_free(struct Cp437GridCommand */*notnull*/ instance);
/**
* Moves the provided [Cp437Grid] into a new [Cp437GridCommand],
* leaving other fields as their default values.
@ -1738,21 +1717,42 @@ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command,
*/
struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command);
/**
*Clones a [`Cp437GridCommand`] instance.
*
* This function is part of the sp_cmd_cp437grid module.
*/
struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance);
/**
*Deallocates a [`Cp437GridCommand`] instance.
*
* This function is part of the sp_cmd_cp437grid module.
*/
void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance);
/**
*Clones a [`FadeOutCommand`] instance.
*
* This function is part of the sp_cmd_fadeout module.
*/
struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_clone(struct FadeOutCommand */*notnull*/ instance);
/**
*Deallocates a [`FadeOutCommand`] instance.
*
* This function is part of the sp_cmd_fade_out module.
* This function is part of the sp_cmd_fadeout module.
*/
void sp_cmd_fade_out_free(struct FadeOutCommand */*notnull*/ instance);
void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance);
/**
* A yet-to-be-tested command.
*
* Returns: a new [`FadeOutCommand`] instance.
*
* This function is part of the sp_cmd_fade_out module.
* This function is part of the sp_cmd_fadeout module.
*/
struct FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void);
struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void);
/**
* Clones an [SPCommand] instance.
@ -1800,12 +1800,33 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command);
*/
struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet);
/**
*Clones a [`GlobalBrightnessCommand`] instance.
*
* This function is part of the sp_cmd_globalbrightness module.
*/
struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_clone(struct GlobalBrightnessCommand */*notnull*/ instance);
/**
*Deallocates a [`GlobalBrightnessCommand`] instance.
*
* This function is part of the sp_cmd_globalbrightness module.
*/
void sp_cmd_globalbrightness_free(struct GlobalBrightnessCommand */*notnull*/ instance);
/**
*Clones a [`HardResetCommand`] instance.
*
* This function is part of the sp_cmd_hardreset module.
*/
struct HardResetCommand */*notnull*/ sp_cmd_hardreset_clone(struct HardResetCommand */*notnull*/ instance);
/**
*Deallocates a [`HardResetCommand`] instance.
*
* This function is part of the sp_cmd_hard_reset module.
* This function is part of the sp_cmd_hardreset module.
*/
void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance);
void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance);
/**
* Kills the udp daemon on the display, which usually results in a restart.
@ -1814,9 +1835,9 @@ void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance);
*
* Returns: a new [`HardResetCommand`] instance.
*
* This function is part of the sp_cmd_hard_reset module.
* This function is part of the sp_cmd_hardreset module.
*/
struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void);
struct HardResetCommand */*notnull*/ sp_cmd_hardreset_new(void);
/**
*Clones a [`Cp437Grid`] instance.

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)),