use crate::{ containers::ByteSlice, macros::{wrap_clone, wrap_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_functions!(sp_brightness_grid; /// 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 { 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 into_packet( grid: NonNull, 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_clone!(sp_brightness_grid::BrightnessGrid); wrap_free!(sp_brightness_grid::BrightnessGrid); wrap_methods!( sp_brightness_grid::BrightnessGrid; /// Gets the current value at the specified position. /// /// # Arguments /// /// - `x` and `y`: position of the cell to read /// /// returns: value at position /// /// # Panics /// - When accessing `x` or `y` out of bounds. ref fn get(x: usize, y: usize) -> Brightness; /// Sets the value of the specified position. /// /// # Arguments /// /// - `x` and `y`: position of the cell /// - `value`: the value to write to the cell /// /// returns: old value of the cell /// /// # Panics /// /// - When accessing `x` or `y` out of bounds. mut fn set(x: usize, y: usize, value: Brightness); /// Sets the value of all cells. /// /// # Arguments /// /// - `value`: the value to set all cells to mut fn fill(value: Brightness); /// Gets the width of the grid. ref fn width() -> usize; /// Gets the height of the grid. ref fn height() -> usize; /// 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::() == 1); unsafe { ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) } }; }; );