WIP: next #4

Draft
vinzenz wants to merge 12 commits from next into main
9 changed files with 489 additions and 78 deletions
Showing only changes of commit 4f0eca3ea0 - Show all commits

View file

@ -1,30 +1,44 @@
#include "servicepoint.h"
int main(void) {
UdpSocket *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342);
//UdpSocket *connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342);
if (connection == NULL)
return -1;
static UdpSocket *connection = NULL;
void enable_all_pixels(void) {
Bitmap *all_on = sp_bitmap_new_max_sized();
sp_bitmap_fill(all_on, true);
Packet *packet = sp_bitmap_into_packet(all_on, 0, 0, COMPRESSION_CODE_UNCOMPRESSED);
if (packet == NULL)
return -1;
sp_udp_send_packet(connection, packet);
BrightnessGrid *grid = sp_brightness_grid_new(TILE_WIDTH, TILE_HEIGHT);
BitmapCommand *bitmapCommand = sp_cmd_bitmap_from_bitmap(all_on);
Packet *packet = sp_cmd_bitmap_into_packet(bitmapCommand);
if (packet != NULL)
sp_udp_send_packet(connection, packet);
}
void make_brightness_pattern(BrightnessGrid *grid) {
ByteSlice slice = sp_brightness_grid_unsafe_data_ref(grid);
for (size_t index = 0; index < slice.length; index++) {
slice.start[index] = (uint8_t) (index % ((size_t) Brightness_MAX));
}
}
packet = sp_brightness_grid_into_packet(grid, 0, 0);
sp_udp_send_packet(connection, packet);
void run_at_exit() {
sp_udp_free(connection);
}
int main(void) {
//UdpSocket *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342);
connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342);
if (connection == NULL)
return -1;
atexit(run_at_exit);
enable_all_pixels();
BrightnessGrid *grid = sp_brightness_grid_new(TILE_WIDTH, TILE_HEIGHT);
make_brightness_pattern(grid);
Packet *packet = sp_cmd_brightness_grid_into_packet(sp_cmd_brightness_grid_from_grid(grid));
if (packet == NULL)
return -2;
sp_udp_send_packet(connection, packet);
return 0;
}

View file

@ -239,6 +239,54 @@ typedef struct Bitmap Bitmap;
*/
typedef struct BitmapCommand BitmapCommand;
/**
* Set the brightness of individual tiles in a rectangular area of the display.
*/
typedef struct BrightnessGridCommand BrightnessGridCommand;
/**
* Show text on the screen.
*
* The text is sent in the form of a 2D grid of UTF-8 encoded characters (the default encoding in rust).
*
* # Examples
*
* ```rust
* # use servicepoint::*;
* # let connection = FakeConnection;
* let grid = CharGrid::from("Hello,\nWorld!");
* connection.send_command(CharGridCommand { origin: Origin::ZERO, grid }).expect("send failed");
* ```
*/
typedef struct CharGridCommand CharGridCommand;
/**
* Show text on the screen.
*
* The text is sent in the form of a 2D grid of [CP-437] encoded characters.
*
* <div class="warning">You probably want to use [Command::Utf8Data] instead</div>
*
* # Examples
*
* ```rust
* # use servicepoint::*;
* # let connection = FakeConnection;
* let grid = CharGrid::from("Hello,\nWorld!");
* let grid = Cp437Grid::from(&grid);
* connection.send_command(Cp437GridCommand{ origin: Origin::ZERO, grid }).expect("send failed");
* ```
*
* ```rust
* # use servicepoint::*;
* # let connection = FakeConnection;
* let grid = Cp437Grid::load_ascii("Hello\nWorld", 5, false).unwrap();
* connection.send_command(Cp437GridCommand{ origin: Origin::new(2, 2), grid }).unwrap();
* ```
* [CP-437]: https://en.wikipedia.org/wiki/Code_page_437
*/
typedef struct Cp437GridCommand Cp437GridCommand;
/**
* This is a type only used by cbindgen to have a type for pointers.
*/
@ -961,6 +1009,10 @@ void sp_char_grid_set(CharGrid */*notnull*/ char_grid,
*/
size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid);
BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(BitmapCommand */*notnull*/ command);
void sp_cmd_bitmap_free(BitmapCommand */*notnull*/ command);
BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap);
/**
@ -1008,6 +1060,10 @@ void sp_cmd_bitmap_set_origin(BitmapCommand */*notnull*/ command,
size_t origin_x,
size_t origin_y);
BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(BitVecCommand */*notnull*/ command);
void sp_cmd_bitvec_free(BitVecCommand */*notnull*/ command);
DisplayBitVec *sp_cmd_bitvec_get(BitVecCommand */*notnull*/ command);
CompressionCode sp_cmd_bitvec_get_compression(BitVecCommand */*notnull*/ command);
@ -1054,6 +1110,96 @@ void sp_cmd_bitvec_set_offset(BitVecCommand */*notnull*/ command,
void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command,
BinaryOperation operation);
BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(BrightnessGridCommand */*notnull*/ command);
void sp_cmd_brightness_grid_free(BitmapCommand */*notnull*/ command);
BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid);
BrightnessGrid *sp_cmd_brightness_grid_get(BrightnessGridCommand */*notnull*/ command);
void sp_cmd_brightness_grid_get_origin(BrightnessGridCommand */*notnull*/ command,
size_t */*notnull*/ origin_x,
size_t */*notnull*/ origin_y);
Packet *sp_cmd_brightness_grid_into_packet(BrightnessGridCommand */*notnull*/ command);
TypedCommand */*notnull*/ sp_cmd_brightness_grid_into_typed(BrightnessGridCommand */*notnull*/ command);
BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid,
size_t origin_x,
size_t origin_y);
/**
* Moves the provided bitmap to be contained in the command.
*/
void sp_cmd_brightness_grid_set(BrightnessGridCommand */*notnull*/ command,
BrightnessGrid */*notnull*/ grid);
void sp_cmd_brightness_grid_set_origin(BrightnessGridCommand */*notnull*/ command,
size_t origin_x,
size_t origin_y);
CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(CharGridCommand */*notnull*/ command);
void sp_cmd_char_grid_free(BitmapCommand */*notnull*/ command);
CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid);
CharGrid *sp_cmd_char_grid_get(CharGridCommand */*notnull*/ command);
void sp_cmd_char_grid_get_origin(CharGridCommand */*notnull*/ command,
size_t */*notnull*/ origin_x,
size_t */*notnull*/ origin_y);
Packet *sp_cmd_char_grid_into_packet(CharGridCommand */*notnull*/ command);
TypedCommand */*notnull*/ sp_cmd_char_grid_into_typed(CharGridCommand */*notnull*/ command);
CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid,
size_t origin_x,
size_t origin_y);
/**
* Moves the provided bitmap to be contained in the command.
*/
void sp_cmd_char_grid_set(CharGridCommand */*notnull*/ command,
CharGrid */*notnull*/ grid);
void sp_cmd_char_grid_set_origin(CharGridCommand */*notnull*/ command,
size_t origin_x,
size_t origin_y);
Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(Cp437GridCommand */*notnull*/ command);
void sp_cmd_cp437_grid_free(BitmapCommand */*notnull*/ command);
Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid);
Cp437Grid *sp_cmd_cp437_grid_get(Cp437GridCommand */*notnull*/ command);
void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command,
size_t */*notnull*/ origin_x,
size_t */*notnull*/ origin_y);
Packet *sp_cmd_cp437_grid_into_packet(Cp437GridCommand */*notnull*/ command);
TypedCommand */*notnull*/ sp_cmd_cp437_grid_into_typed(Cp437GridCommand */*notnull*/ command);
Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid,
size_t origin_x,
size_t origin_y);
/**
* Moves the provided bitmap to be contained in the command.
*/
void sp_cmd_cp437_grid_set(Cp437GridCommand */*notnull*/ command,
Cp437Grid */*notnull*/ grid);
void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command,
size_t origin_x,
size_t origin_y);
/**
* Set the brightness of individual tiles in a rectangular area of the display.
*

View file

@ -1,4 +1,4 @@
use crate::{heap_move_nonnull, heap_move_ok, heap_remove};
use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove};
use servicepoint::{
Bitmap, BitmapCommand, CompressionCode, Origin, Packet, TypedCommand,
};
@ -44,6 +44,18 @@ pub unsafe extern "C" fn sp_cmd_bitmap_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitmap_clone(
command: NonNull<BitmapCommand>,
) -> NonNull<BitmapCommand> {
heap_move_nonnull(unsafe { command.as_ref().clone() })
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitmap_free(command: NonNull<BitmapCommand>) {
unsafe { heap_drop(command) }
}
/// Returns a pointer to the provided `BitmapCommand`.
///
/// # Safety

View file

@ -1,4 +1,4 @@
use crate::{heap_move_nonnull, heap_move_ok, heap_remove};
use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove};
use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset,
Packet, TypedCommand,
@ -46,6 +46,18 @@ pub unsafe extern "C" fn sp_cmd_bitvec_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitvec_clone(
command: NonNull<BitVecCommand>,
) -> NonNull<BitVecCommand> {
heap_move_nonnull(unsafe { command.as_ref().clone() })
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull<BitVecCommand>) {
unsafe { heap_drop(command) }
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitvec_get(
mut command: NonNull<BitVecCommand>,
@ -63,6 +75,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_set(
command.as_mut().bitvec = heap_remove(bitvec);
}
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitvec_get_offset(
command: NonNull<BitVecCommand>,

View file

@ -0,0 +1,95 @@
use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove};
use servicepoint::{
BitmapCommand, BrightnessGrid, BrightnessGridCommand, Origin, Packet,
TypedCommand,
};
use std::ptr::NonNull;
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_new(
grid: NonNull<BrightnessGrid>,
origin_x: usize,
origin_y: usize,
) -> NonNull<BrightnessGridCommand> {
heap_move_nonnull(BrightnessGridCommand {
grid: unsafe { heap_remove(grid) },
origin: Origin::new(origin_x, origin_y),
})
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_from_grid(
grid: NonNull<BrightnessGrid>,
) -> NonNull<BrightnessGridCommand> {
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>,
) -> *mut Packet {
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_clone(
command: NonNull<BrightnessGridCommand>,
) -> NonNull<BrightnessGridCommand> {
heap_move_nonnull(unsafe { command.as_ref().clone() })
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_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_grid_set(
mut command: NonNull<BrightnessGridCommand>,
grid: NonNull<BrightnessGrid>,
) {
unsafe {
command.as_mut().grid = heap_remove(grid);
}
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_get(
mut command: NonNull<BrightnessGridCommand>,
) -> *mut BrightnessGrid {
&mut unsafe { command.as_mut() }.grid
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_get_origin(
command: NonNull<BrightnessGridCommand>,
origin_x: NonNull<usize>,
origin_y: NonNull<usize>,
) {
unsafe {
let origin = &command.as_ref().origin;
*origin_x.as_ptr() = origin.x;
*origin_y.as_ptr() = origin.y;
}
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_set_origin(
mut command: NonNull<BrightnessGridCommand>,
origin_x: usize,
origin_y: usize,
) {
unsafe {
command.as_mut().origin = Origin::new(origin_x, origin_y);
}
}

View file

@ -0,0 +1,94 @@
use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove};
use servicepoint::{
BitmapCommand, CharGrid, CharGridCommand, Origin, Packet, TypedCommand,
};
use std::ptr::NonNull;
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_new(
grid: NonNull<CharGrid>,
origin_x: usize,
origin_y: usize,
) -> NonNull<CharGridCommand> {
heap_move_nonnull(CharGridCommand {
grid: unsafe { heap_remove(grid) },
origin: Origin::new(origin_x, origin_y),
})
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_from_grid(
grid: NonNull<CharGrid>,
) -> NonNull<CharGridCommand> {
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>,
) -> *mut Packet {
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_clone(
command: NonNull<CharGridCommand>,
) -> NonNull<CharGridCommand> {
heap_move_nonnull(unsafe { command.as_ref().clone() })
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_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_char_grid_set(
mut command: NonNull<CharGridCommand>,
grid: NonNull<CharGrid>,
) {
unsafe {
command.as_mut().grid = heap_remove(grid);
}
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_get(
mut command: NonNull<CharGridCommand>,
) -> *mut CharGrid {
&mut unsafe { command.as_mut() }.grid
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_get_origin(
command: NonNull<CharGridCommand>,
origin_x: NonNull<usize>,
origin_y: NonNull<usize>,
) {
unsafe {
let origin = &command.as_ref().origin;
*origin_x.as_ptr() = origin.x;
*origin_y.as_ptr() = origin.y;
}
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_set_origin(
mut command: NonNull<CharGridCommand>,
origin_x: usize,
origin_y: usize,
) {
unsafe {
command.as_mut().origin = Origin::new(origin_x, origin_y);
}
}

View file

@ -0,0 +1,94 @@
use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove};
use servicepoint::{
BitmapCommand, Cp437Grid, Cp437GridCommand, Origin, Packet, TypedCommand,
};
use std::ptr::NonNull;
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_new(
grid: NonNull<Cp437Grid>,
origin_x: usize,
origin_y: usize,
) -> NonNull<Cp437GridCommand> {
heap_move_nonnull(Cp437GridCommand {
grid: unsafe { heap_remove(grid) },
origin: Origin::new(origin_x, origin_y),
})
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_from_grid(
grid: NonNull<Cp437Grid>,
) -> NonNull<Cp437GridCommand> {
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>,
) -> *mut Packet {
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_clone(
command: NonNull<Cp437GridCommand>,
) -> NonNull<Cp437GridCommand> {
heap_move_nonnull(unsafe { command.as_ref().clone() })
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_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_cp437_grid_set(
mut command: NonNull<Cp437GridCommand>,
grid: NonNull<Cp437Grid>,
) {
unsafe {
command.as_mut().grid = heap_remove(grid);
}
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_get(
mut command: NonNull<Cp437GridCommand>,
) -> *mut Cp437Grid {
&mut unsafe { command.as_mut() }.grid
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_get_origin(
command: NonNull<Cp437GridCommand>,
origin_x: NonNull<usize>,
origin_y: NonNull<usize>,
) {
unsafe {
let origin = &command.as_ref().origin;
*origin_x.as_ptr() = origin.x;
*origin_y.as_ptr() = origin.y;
}
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_set_origin(
mut command: NonNull<Cp437GridCommand>,
origin_x: usize,
origin_y: usize,
) {
unsafe {
command.as_mut().origin = Origin::new(origin_x, origin_y);
}
}

View file

@ -1,2 +1,5 @@
mod bitmap_command;
mod bitvec_command;
mod brightness_grid_command;
mod char_grid_command;
mod cp437_grid_command;

View file

@ -1,6 +1,6 @@
use crate::{heap_drop, heap_move_nonnull, heap_move_ok};
use servicepoint::{
Brightness, BrightnessGrid, CharGrid, Cp437Grid, GlobalBrightnessCommand,
Brightness, GlobalBrightnessCommand,
Packet, TypedCommand,
};
use std::ptr::NonNull;
@ -72,66 +72,6 @@ pub unsafe extern "C" fn sp_command_global_brightness(
heap_move_nonnull(GlobalBrightnessCommand::from(brightness).into())
}
/// Set the brightness of individual tiles in a rectangular area of the display.
///
/// The passed [BrightnessGrid] gets consumed.
///
/// Returns: a new [servicepoint::Command::CharBrightness] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_command_brightness_grid(
x: usize,
y: usize,
grid: NonNull<BrightnessGrid>,
) -> NonNull<TypedCommand> {
let grid = unsafe { *Box::from_raw(grid.as_ptr()) };
let result = servicepoint::BrightnessGridCommand {
origin: servicepoint::Origin::new(x, y),
grid,
}
.into();
heap_move_nonnull(result)
}
/// Show codepage 437 encoded text on the screen.
///
/// The passed [Cp437Grid] gets consumed.
///
/// Returns: a new [servicepoint::Cp437GridCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_command_cp437_grid(
x: usize,
y: usize,
grid: NonNull<Cp437Grid>,
) -> NonNull<TypedCommand> {
let grid = *unsafe { Box::from_raw(grid.as_ptr()) };
let result = servicepoint::Cp437GridCommand {
origin: servicepoint::Origin::new(x, y),
grid,
}
.into();
heap_move_nonnull(result)
}
/// Show UTF-8 encoded text on the screen.
///
/// The passed [CharGrid] gets consumed.
///
/// Returns: a new [servicepoint::CharGridCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_command_char_grid(
x: usize,
y: usize,
grid: NonNull<CharGrid>,
) -> NonNull<TypedCommand> {
let grid = unsafe { *Box::from_raw(grid.as_ptr()) };
let result = servicepoint::CharGridCommand {
origin: servicepoint::Origin::new(x, y),
grid,
}
.into();
heap_move_nonnull(result)
}
/// Deallocates a [TypedCommand].
///
/// # Examples