1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::{containers::ByteSlice, macros::wrap};
use servicepoint::{
Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid,
Origin, Packet,
};
use std::{mem::transmute, ptr::NonNull};
wrap! {
BrightnessGrid {
derives: crate::containers::derive_container, crate::containers::derive_grid[Brightness];
functions:
/// 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: val usize, height: val usize) -> move NonNull<BrightnessGrid> {
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: val usize,
height: val usize,
data: slice ByteSlice,
) -> move_some *mut BrightnessGrid {
ByteGrid::load(width, height, data)
.map(move |grid| grid.map(Brightness::saturating_from))
};
methods:
/// 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(move grid, x: val usize, y: val usize) -> move_ok *mut Packet {
Packet::try_from(BrightnessGridCommand {
grid,
origin: Origin::new(x, y),
})
};
/// Gets an unsafe reference to the data of the instance.
///
/// The returned memory is valid for the lifetime of the grid.
fn data_ref_mut(mut instance) -> slice ByteSlice {
//noinspection RsAssertEqual
const _: () = assert!(size_of::<Brightness>() == 1);
let br_slice = instance.data_ref_mut();
unsafe {
transmute::<&mut [Brightness], &mut [u8]>(br_slice)
}
};
}
}