91 lines
2.7 KiB
Rust
91 lines
2.7 KiB
Rust
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},
|
|
};
|
|
use servicepoint::{
|
|
Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid,
|
|
Origin, Packet,
|
|
};
|
|
use std::{mem::transmute, ptr::NonNull};
|
|
|
|
wrap_grid!(BrightnessGrid, Brightness);
|
|
|
|
wrap_functions!(associate BrightnessGrid;
|
|
|
|
/// Creates a new [BrightnessGrid] with the specified dimensions.
|
|
///
|
|
/// returns: [BrightnessGrid] initialized to 0.
|
|
///
|
|
/// # Examples
|
|
/// ```C
|
|
/// UdpSocket *connection = sp_udp_open("127.0.0.1:2342");
|
|
/// if (connection == NULL)
|
|
/// return 1;
|
|
///
|
|
/// BrightnessGrid *grid = sp_brightness_grid_new(2, 2);
|
|
/// sp_brightness_grid_set(grid, 0, 0, 0);
|
|
/// sp_brightness_grid_set(grid, 1, 1, 10);
|
|
///
|
|
/// TypedCommand *command = sp_command_char_brightness(grid);
|
|
/// sp_udp_free(connection);
|
|
/// ```
|
|
fn new(
|
|
width: usize,
|
|
height: usize,
|
|
) -> NonNull<BrightnessGrid> {
|
|
heap_move_nonnull(BrightnessGrid::new(width, height))
|
|
}
|
|
|
|
/// Loads a [BrightnessGrid] with the specified dimensions from the provided data.
|
|
///
|
|
/// Any out of range values will be set to [Brightness::MAX] or [Brightness::MIN].
|
|
///
|
|
/// returns: new [BrightnessGrid] instance, or NULL in case of an error.
|
|
fn load(
|
|
width: usize,
|
|
height: usize,
|
|
data: ByteSlice,
|
|
) -> *mut BrightnessGrid {
|
|
let data = unsafe { data.as_slice() };
|
|
heap_move_some(
|
|
ByteGrid::load(width, height, data)
|
|
.map(move |grid| grid.map(Brightness::saturating_from)),
|
|
)
|
|
}
|
|
|
|
/// 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) };
|
|
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.
|
|
mut fn data_ref_mut() -> ByteSlice {
|
|
return(br_slice) {
|
|
//noinspection RsAssertEqual
|
|
const _: () = assert!(size_of::<Brightness>() == 1);
|
|
|
|
unsafe {
|
|
ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice))
|
|
}
|
|
};
|
|
};
|
|
);
|