expose tagged union instead of TypedCommand to C
This commit is contained in:
		
							parent
							
								
									32d39f8006
								
							
						
					
					
						commit
						85ccf4123c
					
				
					 20 changed files with 657 additions and 277 deletions
				
			
		| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
    Bitmap, BitmapCommand, CompressionCode, Origin, Packet, TypedCommand,
 | 
			
		||||
use crate::{
 | 
			
		||||
    heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
/// Sets a window of pixels to the specified values.
 | 
			
		||||
| 
						 | 
				
			
			@ -34,13 +34,6 @@ pub unsafe extern "C" fn sp_cmd_bitmap_from_bitmap(
 | 
			
		|||
    heap_move_nonnull(unsafe { heap_remove(bitmap) }.into())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_bitmap_into_typed(
 | 
			
		||||
    command: NonNull<BitmapCommand>,
 | 
			
		||||
) -> NonNull<TypedCommand> {
 | 
			
		||||
    heap_move_nonnull(unsafe { heap_remove(command) }.into())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_bitmap_into_packet(
 | 
			
		||||
    command: NonNull<BitmapCommand>,
 | 
			
		||||
| 
						 | 
				
			
			@ -52,7 +45,7 @@ pub unsafe extern "C" fn sp_cmd_bitmap_into_packet(
 | 
			
		|||
pub unsafe extern "C" fn sp_cmd_bitmap_clone(
 | 
			
		||||
    command: NonNull<BitmapCommand>,
 | 
			
		||||
) -> NonNull<BitmapCommand> {
 | 
			
		||||
    heap_move_nonnull(unsafe { command.as_ref().clone() })
 | 
			
		||||
    unsafe { heap_clone(command) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,9 @@
 | 
			
		|||
use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove};
 | 
			
		||||
use crate::{
 | 
			
		||||
    heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
    BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset,
 | 
			
		||||
    Packet, TypedCommand,
 | 
			
		||||
    Packet,
 | 
			
		||||
};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -32,13 +34,6 @@ pub unsafe extern "C" fn sp_cmd_bitvec_new(
 | 
			
		|||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_bitvec_into_typed(
 | 
			
		||||
    command: NonNull<BitVecCommand>,
 | 
			
		||||
) -> NonNull<TypedCommand> {
 | 
			
		||||
    heap_move_nonnull(unsafe { heap_remove(command) }.into())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_bitvec_into_packet(
 | 
			
		||||
    command: NonNull<BitVecCommand>,
 | 
			
		||||
| 
						 | 
				
			
			@ -50,7 +45,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_into_packet(
 | 
			
		|||
pub unsafe extern "C" fn sp_cmd_bitvec_clone(
 | 
			
		||||
    command: NonNull<BitVecCommand>,
 | 
			
		||||
) -> NonNull<BitVecCommand> {
 | 
			
		||||
    heap_move_nonnull(unsafe { command.as_ref().clone() })
 | 
			
		||||
    unsafe { heap_clone(command) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,8 @@
 | 
			
		|||
use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove};
 | 
			
		||||
use crate::{
 | 
			
		||||
    heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
    BitmapCommand, BrightnessGrid, BrightnessGridCommand, Origin, Packet,
 | 
			
		||||
    TypedCommand,
 | 
			
		||||
};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -24,13 +25,6 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_from_grid(
 | 
			
		|||
    heap_move_nonnull(unsafe { heap_remove(grid) }.into())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_brightness_grid_into_typed(
 | 
			
		||||
    command: NonNull<BrightnessGridCommand>,
 | 
			
		||||
) -> NonNull<TypedCommand> {
 | 
			
		||||
    heap_move_nonnull(unsafe { heap_remove(command) }.into())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet(
 | 
			
		||||
    command: NonNull<BrightnessGridCommand>,
 | 
			
		||||
| 
						 | 
				
			
			@ -42,7 +36,7 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet(
 | 
			
		|||
pub unsafe extern "C" fn sp_cmd_brightness_grid_clone(
 | 
			
		||||
    command: NonNull<BrightnessGridCommand>,
 | 
			
		||||
) -> NonNull<BrightnessGridCommand> {
 | 
			
		||||
    heap_move_nonnull(unsafe { command.as_ref().clone() })
 | 
			
		||||
    unsafe { heap_clone(command) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										54
									
								
								src/commands/cc_only_commands.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/commands/cc_only_commands.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,54 @@
 | 
			
		|||
use crate::{heap_drop, heap_move_nonnull};
 | 
			
		||||
use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
/// Set all pixels to the off state.
 | 
			
		||||
///
 | 
			
		||||
/// Does not affect brightness.
 | 
			
		||||
///
 | 
			
		||||
/// Returns: a new [ClearCommand] instance.
 | 
			
		||||
///
 | 
			
		||||
/// # Examples
 | 
			
		||||
///
 | 
			
		||||
/// ```C
 | 
			
		||||
/// sp_udp_send_command(connection, sp_cmd_clear());
 | 
			
		||||
/// ```
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_clear_new() -> NonNull<ClearCommand> {
 | 
			
		||||
    heap_move_nonnull(ClearCommand)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_clear_free(command: NonNull<ClearCommand>) {
 | 
			
		||||
    unsafe { heap_drop(command) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Kills the udp daemon on the display, which usually results in a restart.
 | 
			
		||||
///
 | 
			
		||||
/// Please do not send this in your normal program flow.
 | 
			
		||||
///
 | 
			
		||||
/// Returns: a new [HardResetCommand] instance.
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_hard_reset_new() -> NonNull<HardResetCommand> {
 | 
			
		||||
    heap_move_nonnull(HardResetCommand)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_hard_reset_free(
 | 
			
		||||
    command: NonNull<ClearCommand>,
 | 
			
		||||
) {
 | 
			
		||||
    unsafe { heap_drop(command) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// A yet-to-be-tested command.
 | 
			
		||||
///
 | 
			
		||||
/// Returns: a new [FadeOutCommand] instance.
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_fade_out_new() -> NonNull<FadeOutCommand> {
 | 
			
		||||
    heap_move_nonnull(FadeOutCommand)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_fade_out_free(command: NonNull<ClearCommand>) {
 | 
			
		||||
    unsafe { heap_drop(command) }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
    BitmapCommand, CharGrid, CharGridCommand, Origin, Packet, TypedCommand,
 | 
			
		||||
use crate::{
 | 
			
		||||
    heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{BitmapCommand, CharGrid, CharGridCommand, Origin, Packet};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
| 
						 | 
				
			
			@ -23,13 +23,6 @@ pub unsafe extern "C" fn sp_cmd_char_grid_from_grid(
 | 
			
		|||
    heap_move_nonnull(unsafe { heap_remove(grid) }.into())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_char_grid_into_typed(
 | 
			
		||||
    command: NonNull<CharGridCommand>,
 | 
			
		||||
) -> NonNull<TypedCommand> {
 | 
			
		||||
    heap_move_nonnull(unsafe { heap_remove(command) }.into())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_char_grid_into_packet(
 | 
			
		||||
    command: NonNull<CharGridCommand>,
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +34,7 @@ pub unsafe extern "C" fn sp_cmd_char_grid_into_packet(
 | 
			
		|||
pub unsafe extern "C" fn sp_cmd_char_grid_clone(
 | 
			
		||||
    command: NonNull<CharGridCommand>,
 | 
			
		||||
) -> NonNull<CharGridCommand> {
 | 
			
		||||
    heap_move_nonnull(unsafe { command.as_ref().clone() })
 | 
			
		||||
    unsafe { heap_clone(command) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,8 @@
 | 
			
		|||
use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove};
 | 
			
		||||
use crate::{
 | 
			
		||||
    heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
    BitmapCommand, Cp437Grid, Cp437GridCommand, Origin, Packet, TypedCommand,
 | 
			
		||||
    BitmapCommand, Cp437Grid, Cp437GridCommand, Origin, Packet,
 | 
			
		||||
};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -23,13 +25,6 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_from_grid(
 | 
			
		|||
    heap_move_nonnull(unsafe { heap_remove(grid) }.into())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_cp437_grid_into_typed(
 | 
			
		||||
    command: NonNull<Cp437GridCommand>,
 | 
			
		||||
) -> NonNull<TypedCommand> {
 | 
			
		||||
    heap_move_nonnull(unsafe { heap_remove(command) }.into())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_cp437_grid_into_packet(
 | 
			
		||||
    command: NonNull<Cp437GridCommand>,
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +36,7 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_into_packet(
 | 
			
		|||
pub unsafe extern "C" fn sp_cmd_cp437_grid_clone(
 | 
			
		||||
    command: NonNull<Cp437GridCommand>,
 | 
			
		||||
) -> NonNull<Cp437GridCommand> {
 | 
			
		||||
    heap_move_nonnull(unsafe { command.as_ref().clone() })
 | 
			
		||||
    unsafe { heap_clone(command) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										281
									
								
								src/commands/generic_command.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										281
									
								
								src/commands/generic_command.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,281 @@
 | 
			
		|||
use crate::{
 | 
			
		||||
    heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
    BitVecCommand, BitmapCommand, BitmapLegacyCommand, BrightnessGridCommand,
 | 
			
		||||
    CharGridCommand, ClearCommand, Cp437GridCommand, FadeOutCommand,
 | 
			
		||||
    GlobalBrightnessCommand, HardResetCommand, Packet, TypedCommand,
 | 
			
		||||
};
 | 
			
		||||
use std::ptr::{null_mut, NonNull};
 | 
			
		||||
 | 
			
		||||
#[repr(C)]
 | 
			
		||||
pub union CommandUnion {
 | 
			
		||||
    pub null: *mut u8,
 | 
			
		||||
    pub bitmap: NonNull<BitmapCommand>,
 | 
			
		||||
    pub bitvec: NonNull<BitVecCommand>,
 | 
			
		||||
    pub brightness_grid: NonNull<BrightnessGridCommand>,
 | 
			
		||||
    pub char_grid: NonNull<CharGridCommand>,
 | 
			
		||||
    pub cp437_grid: NonNull<Cp437GridCommand>,
 | 
			
		||||
    pub global_brightness: NonNull<GlobalBrightnessCommand>,
 | 
			
		||||
    pub clear: NonNull<ClearCommand>,
 | 
			
		||||
    #[allow(deprecated)]
 | 
			
		||||
    pub bitmap_legacy: NonNull<BitmapLegacyCommand>,
 | 
			
		||||
    pub hard_reset: NonNull<HardResetCommand>,
 | 
			
		||||
    pub fade_out: NonNull<FadeOutCommand>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[repr(u8)]
 | 
			
		||||
pub enum CommandTag {
 | 
			
		||||
    Invalid = 0,
 | 
			
		||||
    Bitmap,
 | 
			
		||||
    BitVec,
 | 
			
		||||
    BrightnessGrid,
 | 
			
		||||
    CharGrid,
 | 
			
		||||
    Cp437Grid,
 | 
			
		||||
    GlobalBrightness,
 | 
			
		||||
    Clear,
 | 
			
		||||
    HardReset,
 | 
			
		||||
    FadeOut,
 | 
			
		||||
    BitmapLegacy,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[repr(C)]
 | 
			
		||||
pub struct SPCommand {
 | 
			
		||||
    /// 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 {
 | 
			
		||||
        tag: CommandTag::Invalid,
 | 
			
		||||
        data: CommandUnion { null: null_mut() },
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Tries to turn a [Packet] into a [TypedCommand].
 | 
			
		||||
///
 | 
			
		||||
/// The packet is deallocated in the process.
 | 
			
		||||
///
 | 
			
		||||
/// Returns: pointer to new [TypedCommand] instance or NULL if parsing failed.
 | 
			
		||||
/// #[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_generic_try_from_packet(
 | 
			
		||||
    packet: NonNull<Packet>,
 | 
			
		||||
) -> *mut SPCommand {
 | 
			
		||||
    let packet = *unsafe { Box::from_raw(packet.as_ptr()) };
 | 
			
		||||
    heap_move_ok(servicepoint::TypedCommand::try_from(packet).map(|value| {
 | 
			
		||||
        match value {
 | 
			
		||||
            TypedCommand::Clear(clear) => SPCommand {
 | 
			
		||||
                tag: CommandTag::Clear,
 | 
			
		||||
                data: CommandUnion {
 | 
			
		||||
                    clear: heap_move_nonnull(clear),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
            TypedCommand::CharGrid(char_grid) => SPCommand {
 | 
			
		||||
                tag: CommandTag::CharGrid,
 | 
			
		||||
                data: CommandUnion {
 | 
			
		||||
                    char_grid: heap_move_nonnull(char_grid),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
            TypedCommand::Cp437Grid(cp437_grid) => SPCommand {
 | 
			
		||||
                tag: CommandTag::Cp437Grid,
 | 
			
		||||
                data: CommandUnion {
 | 
			
		||||
                    cp437_grid: heap_move_nonnull(cp437_grid),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
            TypedCommand::Bitmap(bitmap) => SPCommand {
 | 
			
		||||
                tag: CommandTag::Bitmap,
 | 
			
		||||
                data: CommandUnion {
 | 
			
		||||
                    bitmap: heap_move_nonnull(bitmap),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
            TypedCommand::Brightness(global_brightness) => SPCommand {
 | 
			
		||||
                tag: CommandTag::GlobalBrightness,
 | 
			
		||||
                data: CommandUnion {
 | 
			
		||||
                    global_brightness: heap_move_nonnull(global_brightness),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
            TypedCommand::BrightnessGrid(brightness_grid) => SPCommand {
 | 
			
		||||
                tag: CommandTag::BrightnessGrid,
 | 
			
		||||
                data: CommandUnion {
 | 
			
		||||
                    brightness_grid: heap_move_nonnull(brightness_grid),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
            TypedCommand::BitVec(bitvec) => SPCommand {
 | 
			
		||||
                tag: CommandTag::BitVec,
 | 
			
		||||
                data: CommandUnion {
 | 
			
		||||
                    bitvec: heap_move_nonnull(bitvec),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
            TypedCommand::HardReset(hard_reset) => SPCommand {
 | 
			
		||||
                tag: CommandTag::HardReset,
 | 
			
		||||
                data: CommandUnion {
 | 
			
		||||
                    hard_reset: heap_move_nonnull(hard_reset),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
            TypedCommand::FadeOut(fade_out) => SPCommand {
 | 
			
		||||
                tag: CommandTag::FadeOut,
 | 
			
		||||
                data: CommandUnion {
 | 
			
		||||
                    fade_out: heap_move_nonnull(fade_out),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
            #[allow(deprecated)]
 | 
			
		||||
            TypedCommand::BitmapLegacy(bitmap_legacy) => SPCommand {
 | 
			
		||||
                tag: CommandTag::BitmapLegacy,
 | 
			
		||||
                data: CommandUnion {
 | 
			
		||||
                    bitmap_legacy: heap_move_nonnull(bitmap_legacy),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
        }
 | 
			
		||||
    }))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Clones a [SPCommand] instance.
 | 
			
		||||
///
 | 
			
		||||
/// returns: new [SPCommand] instance.
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_generic_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 {
 | 
			
		||||
                    bitvec: heap_clone(command.data.bitvec),
 | 
			
		||||
                },
 | 
			
		||||
            },
 | 
			
		||||
            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 a [SPCommand].
 | 
			
		||||
///
 | 
			
		||||
/// # Examples
 | 
			
		||||
///
 | 
			
		||||
/// ```C
 | 
			
		||||
/// TypedCommand c = sp_command_clear();
 | 
			
		||||
/// sp_command_free(c);
 | 
			
		||||
/// ```
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_generic_free(command: SPCommand) {
 | 
			
		||||
    unsafe {
 | 
			
		||||
        match command.tag {
 | 
			
		||||
            CommandTag::Invalid => return,
 | 
			
		||||
            CommandTag::Bitmap => heap_drop(command.data.bitmap),
 | 
			
		||||
            CommandTag::BitVec => heap_drop(command.data.bitvec),
 | 
			
		||||
            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),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Turns a [TypedCommand] into a [Packet].
 | 
			
		||||
/// The [TypedCommand] gets consumed.
 | 
			
		||||
///
 | 
			
		||||
/// Returns NULL in case of an error.
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_generic_into_packet(
 | 
			
		||||
    command: SPCommand,
 | 
			
		||||
) -> *mut Packet {
 | 
			
		||||
    match command.tag {
 | 
			
		||||
        CommandTag::Invalid => null_mut(),
 | 
			
		||||
        CommandTag::Bitmap => {
 | 
			
		||||
            heap_move_ok(unsafe { heap_remove(command.data.bitmap).try_into() })
 | 
			
		||||
        }
 | 
			
		||||
        CommandTag::BitVec => {
 | 
			
		||||
            heap_move_ok(unsafe { heap_remove(command.data.bitvec).try_into() })
 | 
			
		||||
        }
 | 
			
		||||
        CommandTag::BrightnessGrid => heap_move_ok(unsafe {
 | 
			
		||||
            heap_remove(command.data.brightness_grid).try_into()
 | 
			
		||||
        }),
 | 
			
		||||
        CommandTag::CharGrid => heap_move_ok(unsafe {
 | 
			
		||||
            heap_remove(command.data.char_grid).try_into()
 | 
			
		||||
        }),
 | 
			
		||||
        CommandTag::Cp437Grid => heap_move_ok(unsafe {
 | 
			
		||||
            heap_remove(command.data.cp437_grid).try_into()
 | 
			
		||||
        }),
 | 
			
		||||
        CommandTag::GlobalBrightness => heap_move_ok(unsafe {
 | 
			
		||||
            heap_remove(command.data.global_brightness).try_into()
 | 
			
		||||
        }),
 | 
			
		||||
        CommandTag::Clear => {
 | 
			
		||||
            heap_move_ok(unsafe { heap_remove(command.data.clear).try_into() })
 | 
			
		||||
        }
 | 
			
		||||
        CommandTag::HardReset => heap_move_ok(unsafe {
 | 
			
		||||
            heap_remove(command.data.hard_reset).try_into()
 | 
			
		||||
        }),
 | 
			
		||||
        CommandTag::FadeOut => heap_move_ok(unsafe {
 | 
			
		||||
            heap_remove(command.data.fade_out).try_into()
 | 
			
		||||
        }),
 | 
			
		||||
        CommandTag::BitmapLegacy => heap_move_ok(unsafe {
 | 
			
		||||
            heap_remove(command.data.bitmap_legacy).try_into()
 | 
			
		||||
        }),
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										53
									
								
								src/commands/global_brightness_command.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								src/commands/global_brightness_command.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,53 @@
 | 
			
		|||
use crate::{
 | 
			
		||||
    heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
    BitmapCommand, Brightness, GlobalBrightnessCommand, Packet,
 | 
			
		||||
};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_brightness_global_new(
 | 
			
		||||
    brightness: Brightness,
 | 
			
		||||
) -> NonNull<GlobalBrightnessCommand> {
 | 
			
		||||
    heap_move_nonnull(GlobalBrightnessCommand::from(brightness))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_brightness_global_into_packet(
 | 
			
		||||
    command: NonNull<GlobalBrightnessCommand>,
 | 
			
		||||
) -> *mut Packet {
 | 
			
		||||
    heap_move_ok(unsafe { heap_remove(command) }.try_into())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_brightness_global_clone(
 | 
			
		||||
    command: NonNull<GlobalBrightnessCommand>,
 | 
			
		||||
) -> NonNull<GlobalBrightnessCommand> {
 | 
			
		||||
    unsafe { heap_clone(command) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_brightness_global_free(
 | 
			
		||||
    command: NonNull<BitmapCommand>,
 | 
			
		||||
) {
 | 
			
		||||
    unsafe { heap_drop(command) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Moves the provided bitmap to be contained in the command.
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_brightness_global_set(
 | 
			
		||||
    mut command: NonNull<GlobalBrightnessCommand>,
 | 
			
		||||
    brightness: Brightness,
 | 
			
		||||
) {
 | 
			
		||||
    unsafe {
 | 
			
		||||
        command.as_mut().brightness = brightness;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub unsafe extern "C" fn sp_cmd_brightness_global_get(
 | 
			
		||||
    mut command: NonNull<GlobalBrightnessCommand>,
 | 
			
		||||
) -> *mut Brightness {
 | 
			
		||||
    &mut unsafe { command.as_mut() }.brightness
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,11 +1,15 @@
 | 
			
		|||
mod bitmap_command;
 | 
			
		||||
mod bitvec_command;
 | 
			
		||||
mod brightness_grid_command;
 | 
			
		||||
mod cc_only_commands;
 | 
			
		||||
mod char_grid_command;
 | 
			
		||||
mod cp437_grid_command;
 | 
			
		||||
mod generic_command;
 | 
			
		||||
mod global_brightness_command;
 | 
			
		||||
 | 
			
		||||
pub use bitmap_command::*;
 | 
			
		||||
pub use bitvec_command::*;
 | 
			
		||||
pub use brightness_grid_command::*;
 | 
			
		||||
pub use char_grid_command::*;
 | 
			
		||||
pub use cp437_grid_command::*;
 | 
			
		||||
pub use generic_command::*;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue