diff --git a/README.md b/README.md index fa0a49b..4788c75 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,9 @@ # servicepoint_binding_c -[![Release](https://git.berlin.ccc.de/servicepoint/servicepoint_binding_c/badges/release.svg)](https://git.berlin.ccc.de/servicepoint/servicepoint_binding_c/releases) [![crates.io](https://img.shields.io/crates/v/servicepoint_binding_c.svg)](https://crates.io/crates/servicepoint) [![Crates.io Total Downloads](https://img.shields.io/crates/d/servicepoint_binding_c)](https://crates.io/crates/servicepoint) [![docs.rs](https://img.shields.io/docsrs/servicepoint_binding_c)](https://docs.rs/servicepoint/latest/servicepoint/) [![GPLv3 licensed](https://img.shields.io/crates/l/servicepoint_binding_c)](./LICENSE) -[![CI](https://git.berlin.ccc.de/servicepoint/servicepoint_binding_c/badges/workflows/rust.yml/badge.svg)](https://git.berlin.ccc.de/servicepoint/servicepoint_binding_c) In [CCCB](https://berlin.ccc.de/), there is a big pixel matrix hanging on the wall. It is called "Service Point Display" or "Airport Display". @@ -14,7 +12,7 @@ This crate contains C bindings for the [servicepoint](https://git.berlin.ccc.de/ ## Examples -```c +```c++ #include #include "servicepoint.h" diff --git a/cbindgen.toml b/cbindgen.toml index eca3164..221e915 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -32,12 +32,11 @@ features = ["full"] [export] include = [] -exclude = ["BitVec"] +exclude = [] [export.rename] "SpBitVec" = "BitVec" "SpByteSlice" = "ByteSlice" -"SpCommand" = "Command" [enum] rename_variants = "QualifiedScreamingSnakeCase" diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 9476422..c3e42ba 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -1,44 +1,30 @@ #include "servicepoint.h" -static UdpSocket *connection = NULL; +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; -void enable_all_pixels(void) { Bitmap *all_on = sp_bitmap_new_max_sized(); sp_bitmap_fill(all_on, true); - 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); -} + 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); -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)); } -} - -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; + packet = sp_brightness_grid_into_packet(grid, 0, 0); sp_udp_send_packet(connection, packet); + + sp_udp_free(connection); return 0; } diff --git a/include/servicepoint.h b/include/servicepoint.h index 404e291..5863ef5 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -125,27 +125,6 @@ enum CommandCode typedef uint16_t CommandCode; #endif // __cplusplus -enum CommandTag -#ifdef __cplusplus - : uint8_t -#endif // __cplusplus - { - COMMAND_TAG_INVALID = 0, - COMMAND_TAG_BITMAP, - COMMAND_TAG_BIT_VEC, - COMMAND_TAG_BRIGHTNESS_GRID, - COMMAND_TAG_CHAR_GRID, - COMMAND_TAG_CP437_GRID, - COMMAND_TAG_GLOBAL_BRIGHTNESS, - COMMAND_TAG_CLEAR, - COMMAND_TAG_HARD_RESET, - COMMAND_TAG_FADE_OUT, - COMMAND_TAG_BITMAP_LEGACY, -}; -#ifndef __cplusplus -typedef uint8_t CommandTag; -#endif // __cplusplus - /** * Specifies the kind of compression to use. Availability depends on features. * @@ -200,22 +179,6 @@ enum CompressionCode typedef uint16_t CompressionCode; #endif // __cplusplus -/** - * Set pixel data starting at the pixel offset on screen. - * - * The screen will continuously overwrite more pixel data without regarding the offset, meaning - * once the starting row is full, overwriting will continue on column 0. - * - * The [`BinaryOperation`] will be applied on the display comparing old and sent bit. - * - * `new_bit = old_bit op sent_bit` - * - * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. - * - * The contained [`DisplayBitVec`] is always uncompressed. - */ -typedef struct BitVecCommand BitVecCommand; - /** * A fixed-size 2D grid of booleans. * @@ -233,160 +196,6 @@ typedef struct BitVecCommand BitVecCommand; */ typedef struct Bitmap Bitmap; -/** - * Overwrites a rectangular region of pixels. - * - * Origin coordinates must be divisible by 8. - * - * # Examples - * - * ```rust - * # use servicepoint::*; - * # let connection = FakeConnection; - * # - * let mut bitmap = Bitmap::max_sized(); - * // draw something to the pixels here - * # bitmap.set(2, 5, true); - * - * // create command to send pixels - * let command = BitmapCommand { - * bitmap, - * origin: Origin::ZERO, - * compression: CompressionCode::Uncompressed - * }; - * - * connection.send_command(command).expect("send failed"); - * ``` - */ -typedef struct BitmapCommand BitmapCommand; - -/** - * Legacy command code, gets ignored by the real display. - * - * Might be useful as a noop package. - * - * # Examples - * - * ```rust - * # use servicepoint::*; - * # let connection = FakeConnection; - * // this sends a packet that does nothing - * # #[allow(deprecated)] - * connection.send_command(BitmapLegacyCommand).unwrap(); - * ``` - */ -typedef struct BitmapLegacyCommand BitmapLegacyCommand; - -/** - * 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; - -/** - * Set all pixels to the off state. Does not affect brightness. - * - * # Examples - * - * ```rust - * # use servicepoint::*; - * # let connection = FakeConnection; - * connection.send_command(ClearCommand).unwrap(); - * ``` - */ -typedef struct ClearCommand ClearCommand; - -/** - * Show text on the screen. - * - * The text is sent in the form of a 2D grid of [CP-437] encoded characters. - * - *
You probably want to use [Command::Utf8Data] instead
- * - * # 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. - */ -typedef struct DisplayBitVec DisplayBitVec; - -/** - *
Untested
- * - * Slowly decrease brightness until off or something like that? - * - * # Examples - * - * ```rust - * # use servicepoint::*; - * # let connection = FakeConnection; - * connection.send_command(FadeOutCommand).unwrap(); - * ``` - */ -typedef struct FadeOutCommand FadeOutCommand; - -/** - * Set the brightness of all tiles to the same value. - * - * # Examples - * - * ```rust - * # use servicepoint::*; - * # let connection = FakeConnection; - * let command = GlobalBrightnessCommand { brightness: Brightness::MAX }; - * connection.send_command(command).unwrap(); - * ``` - */ -typedef struct GlobalBrightnessCommand GlobalBrightnessCommand; - -/** - * Kills the udp daemon on the display, which usually results in a restart. - * - * Please do not send this in your normal program flow. - * - * # Examples - * - * ```rust - * # use servicepoint::*; - * # let connection = FakeConnection; - * connection.send_command(HardResetCommand).unwrap(); - * ``` - */ -typedef struct HardResetCommand HardResetCommand; - /** * The raw packet. * @@ -408,6 +217,14 @@ typedef struct Packet Packet; */ typedef struct SPBitVec SPBitVec; +/** + * This enum contains all commands provided by the library. + * This is useful in case you want one data type for all kinds of commands without using `dyn`. + * + * Please look at the contained structs for documentation per command. + */ +typedef struct TypedCommand TypedCommand; + /** * This is a type only used by cbindgen to have a type for pointers. */ @@ -531,11 +348,6 @@ typedef uint8_t Brightness; */ typedef ValueGrid_char CharGrid; -/** - * Type alias for documenting the meaning of the u16 in enum values - */ -typedef size_t Offset; - /** * A grid containing codepage 437 characters. * @@ -543,31 +355,6 @@ typedef size_t Offset; */ typedef ValueGrid_u8 Cp437Grid; -typedef union { - uint8_t *null; - BitmapCommand */*notnull*/ bitmap; - BitVecCommand */*notnull*/ bitvec; - BrightnessGridCommand */*notnull*/ brightness_grid; - CharGridCommand */*notnull*/ char_grid; - Cp437GridCommand */*notnull*/ cp437_grid; - GlobalBrightnessCommand */*notnull*/ global_brightness; - ClearCommand */*notnull*/ clear; - BitmapLegacyCommand */*notnull*/ bitmap_legacy; - HardResetCommand */*notnull*/ hard_reset; - FadeOutCommand */*notnull*/ fade_out; -} CommandUnion; - -typedef struct { - /** - * Specifies which kind of command struct is contained in `data` - */ - CommandTag tag; - /** - * The pointer to the command struct - */ - CommandUnion data; -} SPCommand; - /** * A raw header. * @@ -882,7 +669,7 @@ ByteSlice sp_bitvec_unsafe_data_ref(SPBitVec */*notnull*/ bit_vec); /** * Clones a [BrightnessGrid]. */ -BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ grid); +BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ brightness_grid); /** * Sets the value of all cells in the [BrightnessGrid]. @@ -957,15 +744,15 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, * * # Examples * ```C - * UdpSocket *connection = sp_udp_open("127.0.0.1:2342"); + * UdpConnection connection = sp_udp_open("127.0.0.1:2342"); * if (connection == NULL) * return 1; * - * BrightnessGrid *grid = sp_brightness_grid_new(2, 2); + * 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); + * TypedCommand command = sp_command_char_brightness(grid); * sp_udp_free(connection); * ``` */ @@ -1018,7 +805,7 @@ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid); /** * Clones a [CharGrid]. */ -CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ grid); +CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ char_grid); /** * Sets the value of all cells in the [CharGrid]. @@ -1121,74 +908,17 @@ 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); - -/** - * Move the provided [Bitmap] into a new [BitmapCommand], - * leaving other fields as their default values. - * - * Rust equivalent: [`>::from`] - */ -BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap); - -/** - * Returns a pointer to the provided `BitmapCommand`. - * - * # Safety - * - * - The returned bitmap inherits the lifetime of the command in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - */ -Bitmap */*notnull*/ sp_cmd_bitmap_get(BitmapCommand */*notnull*/ command); - -CompressionCode sp_cmd_bitmap_get_compression(BitmapCommand */*notnull*/ command); - -void sp_cmd_bitmap_get_origin(BitmapCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - -Packet *sp_cmd_bitmap_into_packet(BitmapCommand */*notnull*/ command); - /** * Sets a window of pixels to the specified values. * * The passed [Bitmap] gets consumed. * - * Returns: a new [BitmapCommand] instance. + * Returns: a new [servicepoint::BitmapCommand] instance. */ -BitmapCommand */*notnull*/ sp_cmd_bitmap_new(Bitmap */*notnull*/ bitmap, - size_t origin_x, - size_t origin_y, - CompressionCode compression); - -/** - * Moves the provided bitmap to be contained in the command. - */ -void sp_cmd_bitmap_set(BitmapCommand */*notnull*/ command, - Bitmap */*notnull*/ bitmap); - -void sp_cmd_bitmap_set_compression(BitmapCommand */*notnull*/ command, - CompressionCode compression); - -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); - -Offset sp_cmd_bitvec_get_offset(BitVecCommand */*notnull*/ command); - -BinaryOperation sp_cmd_bitvec_get_operation(BitVecCommand */*notnull*/ command); - -Packet *sp_cmd_bitvec_into_packet(BitVecCommand */*notnull*/ command); +TypedCommand *sp_command_bitmap(size_t x, + size_t y, + Bitmap */*notnull*/ bitmap, + CompressionCode compression); /** * Set pixel data starting at the pixel offset on screen. @@ -1202,163 +932,77 @@ Packet *sp_cmd_bitvec_into_packet(BitVecCommand */*notnull*/ command); * * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. * - * The contained [`DisplayBitVec`] is always uncompressed. + * The contained [`BitVecU8Msb0`] is always uncompressed. */ -BitVecCommand */*notnull*/ sp_cmd_bitvec_new(DisplayBitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); +TypedCommand *sp_command_bitvec(size_t offset, + SPBitVec */*notnull*/ bit_vec, + CompressionCode compression, + BinaryOperation operation); /** - * Moves the provided bitmap to be contained in the command. + * 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. */ -void sp_cmd_bitvec_set(BitVecCommand */*notnull*/ command, - DisplayBitVec */*notnull*/ bitvec); - -void sp_cmd_bitvec_set_compression(BitVecCommand */*notnull*/ command, - CompressionCode compression); - -void sp_cmd_bitvec_set_offset(BitVecCommand */*notnull*/ command, - Offset offset); - -void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command, - BinaryOperation operation); - -GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(GlobalBrightnessCommand */*notnull*/ command); - -void sp_cmd_brightness_global_free(BitmapCommand */*notnull*/ command); - -Brightness *sp_cmd_brightness_global_get(GlobalBrightnessCommand */*notnull*/ command); - -Packet *sp_cmd_brightness_global_into_packet(GlobalBrightnessCommand */*notnull*/ command); - -GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); +TypedCommand */*notnull*/ sp_command_brightness_grid(size_t x, + size_t y, + BrightnessGrid */*notnull*/ grid); /** - * Moves the provided bitmap to be contained in the command. + * Show UTF-8 encoded text on the screen. + * + * The passed [CharGrid] gets consumed. + * + * Returns: a new [servicepoint::CharGridCommand] instance. */ -void sp_cmd_brightness_global_set(GlobalBrightnessCommand */*notnull*/ command, - Brightness brightness); - -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); - -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); - -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); - -void sp_cmd_clear_free(ClearCommand */*notnull*/ command); +TypedCommand */*notnull*/ sp_command_char_grid(size_t x, + size_t y, + CharGrid */*notnull*/ grid); /** * Set all pixels to the off state. * * Does not affect brightness. * - * Returns: a new [ClearCommand] instance. + * Returns: a new [servicepoint::Command::Clear] instance. * * # Examples * * ```C - * sp_udp_send_command(connection, sp_cmd_clear()); + * sp_udp_send_command(connection, sp_command_clear()); * ``` */ -ClearCommand */*notnull*/ sp_cmd_clear_new(void); - -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); - -Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +TypedCommand */*notnull*/ sp_command_clear(void); /** - * Moves the provided bitmap to be contained in the command. + * Clones a [TypedCommand] instance. + * + * returns: new [TypedCommand] instance. */ -void sp_cmd_cp437_grid_set(Cp437GridCommand */*notnull*/ command, - Cp437Grid */*notnull*/ grid); +TypedCommand */*notnull*/ sp_command_clone(TypedCommand */*notnull*/ command); -void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - -void sp_cmd_fade_out_free(ClearCommand */*notnull*/ command); +/** + * Show codepage 437 encoded text on the screen. + * + * The passed [Cp437Grid] gets consumed. + * + * Returns: a new [servicepoint::Cp437GridCommand] instance. + */ +TypedCommand */*notnull*/ sp_command_cp437_grid(size_t x, + size_t y, + Cp437Grid */*notnull*/ grid); /** * A yet-to-be-tested command. * - * Returns: a new [FadeOutCommand] instance. + * Returns: a new [servicepoint::Command::FadeOut] instance. */ -FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); +TypedCommand */*notnull*/ sp_command_fade_out(void); /** - * Clones a [SPCommand] instance. - * - * returns: new [SPCommand] instance. - */ -SPCommand sp_cmd_generic_clone(SPCommand command); - -/** - * Deallocates a [SPCommand]. + * Deallocates a [TypedCommand]. * * # Examples * @@ -1367,31 +1011,37 @@ SPCommand sp_cmd_generic_clone(SPCommand command); * sp_command_free(c); * ``` */ -void sp_cmd_generic_free(SPCommand command); +void sp_command_free(TypedCommand */*notnull*/ command); /** - * Turns a [TypedCommand] into a [Packet]. - * The [TypedCommand] gets consumed. + * Set the brightness of all tiles to the same value. * - * Returns NULL in case of an error. + * Returns: a new [servicepoint::Command::Brightness] instance. */ -Packet *sp_cmd_generic_into_packet(SPCommand command); - -void sp_cmd_hard_reset_free(ClearCommand */*notnull*/ command); +TypedCommand */*notnull*/ sp_command_global_brightness(Brightness brightness); /** * 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. + * Returns: a new [servicepoint::Command::HardReset] instance. */ -HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); +TypedCommand */*notnull*/ sp_command_hard_reset(void); + +/** + * 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. + */ +TypedCommand *sp_command_try_from_packet(Packet */*notnull*/ packet); /** * Clones a [Cp437Grid]. */ -Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ grid); +Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ cp437_grid); /** * Sets the value of all cells in the [Cp437Grid]. @@ -1502,6 +1152,14 @@ Packet */*notnull*/ sp_packet_clone(Packet */*notnull*/ packet); */ void sp_packet_free(Packet */*notnull*/ packet); +/** + * Turns a [TypedCommand] into a [Packet]. + * The [TypedCommand] gets consumed. + * + * Returns NULL in case of an error. + */ +Packet *sp_packet_from_command(TypedCommand */*notnull*/ command); + /** * Creates a raw [Packet] from parts. * @@ -1556,19 +1214,19 @@ bool sp_u16_to_command_code(uint16_t code, CommandCode *result); /** - * Closes and deallocates a [UdpSocket]. + * Closes and deallocates a [UdpConnection]. */ void sp_udp_free(UdpSocket */*notnull*/ connection); /** - * Creates a new instance of [UdpSocket]. + * Creates a new instance of [UdpConnection]. * * returns: NULL if connection fails, or connected instance * * # Examples * * ```C - * UdpSocket connection = sp_udp_open("172.23.42.29:2342"); + * UdpConnection connection = sp_udp_open("172.23.42.29:2342"); * if (connection != NULL) * sp_udp_send_command(connection, sp_command_clear()); * ``` @@ -1576,14 +1234,14 @@ void sp_udp_free(UdpSocket */*notnull*/ connection); UdpSocket *sp_udp_open(char */*notnull*/ host); /** - * Creates a new instance of [UdpSocket]. + * Creates a new instance of [UdpConnection]. * * returns: NULL if connection fails, or connected instance * * # Examples * * ```C - * UdpSocket connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + * UdpConnection connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); * if (connection != NULL) * sp_udp_send_command(connection, sp_command_clear()); * ``` @@ -1595,7 +1253,7 @@ UdpSocket *sp_udp_open_ipv4(uint8_t ip1, uint16_t port); /** - * Sends a [TypedCommand] to the display using the [UdpSocket]. + * Sends a [TypedCommand] to the display using the [UdpConnection]. * * The passed `command` gets consumed. * @@ -1607,10 +1265,11 @@ UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` */ -bool sp_udp_send_command(UdpSocket */*notnull*/ connection, SPCommand command); +bool sp_udp_send_command(UdpSocket */*notnull*/ connection, + TypedCommand */*notnull*/ command); /** - * Sends a [Header] to the display using the [UdpSocket]. + * Sends a [Header] to the display using the [UdpConnection]. * * returns: true in case of success * @@ -1623,7 +1282,7 @@ bool sp_udp_send_command(UdpSocket */*notnull*/ connection, SPCommand command); bool sp_udp_send_header(UdpSocket */*notnull*/ udp_connection, Header header); /** - * Sends a [Packet] to the display using the [UdpSocket]. + * Sends a [Packet] to the display using the [UdpConnection]. * * The passed `packet` gets consumed. * diff --git a/src/bitmap.rs b/src/bitmap.rs index 8341c59..5db4af0 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -1,7 +1,5 @@ -use crate::{ - bitvec::SPBitVec, byte_slice::ByteSlice, heap_clone, heap_drop, - heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, -}; +use crate::byte_slice::ByteSlice; +use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, SPBitVec}; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, Grid, Origin, Packet, }; @@ -35,7 +33,11 @@ pub unsafe extern "C" fn sp_bitmap_new( width: usize, height: usize, ) -> *mut Bitmap { - heap_move_some(Bitmap::new(width, height)) + if let Some(bitmap) = Bitmap::new(width, height) { + heap_move(bitmap) + } else { + std::ptr::null_mut() + } } /// Creates a new [Bitmap] with a size matching the screen. @@ -61,7 +63,11 @@ pub unsafe extern "C" fn sp_bitmap_load( data: ByteSlice, ) -> *mut Bitmap { let data = unsafe { data.as_slice() }; - heap_move_ok(Bitmap::load(width, height, data)) + if let Ok(bitmap) = Bitmap::load(width, height, data) { + heap_move(bitmap) + } else { + std::ptr::null_mut() + } } /// Tries to convert the BitVec to a Bitmap. @@ -75,7 +81,11 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec( bitvec: NonNull, ) -> *mut Bitmap { let bitvec = unsafe { heap_remove(bitvec) }; - heap_move_ok(Bitmap::from_bitvec(width, bitvec.0)) + if let Ok(bitmap) = Bitmap::from_bitvec(width, bitvec.0) { + heap_move(bitmap) + } else { + std::ptr::null_mut() + } } /// Clones a [Bitmap]. @@ -83,7 +93,7 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec( pub unsafe extern "C" fn sp_bitmap_clone( bitmap: NonNull, ) -> NonNull { - unsafe { heap_clone(bitmap) } + heap_move_nonnull(unsafe { bitmap.as_ref().clone() }) } /// Deallocates a [Bitmap]. @@ -205,9 +215,12 @@ pub unsafe extern "C" fn sp_bitmap_into_packet( compression: CompressionCode, ) -> *mut Packet { let bitmap = unsafe { heap_remove(bitmap) }; - heap_move_ok(Packet::try_from(BitmapCommand { + match Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), compression, - })) + }) { + Ok(packet) => heap_move(packet), + Err(_) => std::ptr::null_mut(), + } } diff --git a/src/bitvec.rs b/src/bitvec.rs index 3fa7412..0e7ee5e 100644 --- a/src/bitvec.rs +++ b/src/bitvec.rs @@ -1,7 +1,4 @@ -use crate::{ - byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, heap_remove, -}; +use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, }; @@ -53,7 +50,7 @@ pub unsafe extern "C" fn sp_bitvec_load(data: ByteSlice) -> NonNull { pub unsafe extern "C" fn sp_bitvec_clone( bit_vec: NonNull, ) -> NonNull { - unsafe { heap_clone(bit_vec) } + heap_move_nonnull(unsafe { bit_vec.as_ref().clone() }) } /// Deallocates a [SPBitVec]. @@ -165,10 +162,13 @@ pub unsafe extern "C" fn sp_bitvec_into_packet( compression: CompressionCode, ) -> *mut Packet { let bitvec = unsafe { heap_remove(bitvec) }.0; - heap_move_ok(Packet::try_from(BitVecCommand { + match Packet::try_from(BitVecCommand { bitvec, offset, operation, compression, - })) + }) { + Ok(packet) => heap_move(packet), + Err(_) => std::ptr::null_mut(), + } } diff --git a/src/brightness_grid.rs b/src/brightness_grid.rs index dc3b2f7..1099be0 100644 --- a/src/brightness_grid.rs +++ b/src/brightness_grid.rs @@ -1,7 +1,4 @@ -use crate::{ - byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, heap_move_some, heap_remove, -}; +use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, Origin, Packet, @@ -15,15 +12,15 @@ use std::ptr::NonNull; /// /// # Examples /// ```C -/// UdpSocket *connection = sp_udp_open("127.0.0.1:2342"); +/// UdpConnection connection = sp_udp_open("127.0.0.1:2342"); /// if (connection == NULL) /// return 1; /// -/// BrightnessGrid *grid = sp_brightness_grid_new(2, 2); +/// 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); +/// TypedCommand command = sp_command_char_brightness(grid); /// sp_udp_free(connection); /// ``` #[no_mangle] @@ -46,18 +43,21 @@ pub unsafe extern "C" fn sp_brightness_grid_load( 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)), - ) + + match ByteGrid::load(width, height, data) + .map(move |grid| grid.map(Brightness::saturating_from)) + { + None => std::ptr::null_mut(), + Some(grid) => heap_move(grid), + } } /// Clones a [BrightnessGrid]. #[no_mangle] pub unsafe extern "C" fn sp_brightness_grid_clone( - grid: NonNull, + brightness_grid: NonNull, ) -> NonNull { - unsafe { heap_clone(grid) } + heap_move_nonnull(unsafe { brightness_grid.as_ref().clone() }) } /// Deallocates a [BrightnessGrid]. @@ -187,8 +187,11 @@ pub unsafe extern "C" fn sp_brightness_grid_into_packet( y: usize, ) -> *mut Packet { let grid = unsafe { heap_remove(grid) }; - heap_move_ok(Packet::try_from(BrightnessGridCommand { + match Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), - })) + }) { + Ok(packet) => heap_move(packet), + Err(_) => std::ptr::null_mut(), + } } diff --git a/src/char_grid.rs b/src/char_grid.rs index 62aba87..bfb2585 100644 --- a/src/char_grid.rs +++ b/src/char_grid.rs @@ -1,7 +1,4 @@ -use crate::{ - byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, heap_remove, -}; +use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; @@ -35,15 +32,19 @@ pub unsafe extern "C" fn sp_char_grid_load( data: ByteSlice, ) -> *mut CharGrid { let data = unsafe { data.as_slice() }; - heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) + if let Ok(grid) = CharGrid::load_utf8(width, height, data.to_vec()) { + heap_move(grid) + } else { + std::ptr::null_mut() + } } /// Clones a [CharGrid]. #[no_mangle] pub unsafe extern "C" fn sp_char_grid_clone( - grid: NonNull, + char_grid: NonNull, ) -> NonNull { - unsafe { heap_clone(grid) } + heap_move_nonnull(unsafe { char_grid.as_ref().clone() }) } /// Deallocates a [CharGrid]. @@ -144,8 +145,11 @@ pub unsafe extern "C" fn sp_char_grid_into_packet( y: usize, ) -> *mut Packet { let grid = unsafe { heap_remove(grid) }; - heap_move_ok(Packet::try_from(CharGridCommand { + match Packet::try_from(CharGridCommand { grid, origin: Origin::new(x, y), - })) + }) { + Ok(packet) => heap_move(packet), + Err(_) => std::ptr::null_mut(), + } } diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs deleted file mode 100644 index b2ff3d8..0000000 --- a/src/commands/bitmap_command.rs +++ /dev/null @@ -1,119 +0,0 @@ -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. -/// -/// The passed [Bitmap] gets consumed. -/// -/// Returns: a new [BitmapCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_new( - bitmap: NonNull, - origin_x: usize, - origin_y: usize, - compression: CompressionCode, -) -> NonNull { - heap_move_nonnull(BitmapCommand { - bitmap: unsafe { heap_remove(bitmap) }, - origin: Origin::new(origin_x, origin_y), - compression, - }) -} - -/// Move the provided [Bitmap] into a new [BitmapCommand], -/// leaving other fields as their default values. -/// -/// Rust equivalent: [`>::from`] -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_from_bitmap( - bitmap: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_into_packet( - command: NonNull, -) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_clone( - command: NonNull, -) -> NonNull { - unsafe { heap_clone(command) } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_free(command: NonNull) { - unsafe { heap_drop(command) } -} - -/// Returns a pointer to the provided `BitmapCommand`. -/// -/// # Safety -/// -/// - The returned bitmap inherits the lifetime of the command in which it is contained. -/// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_get( - mut command: NonNull, -) -> NonNull { - unsafe { NonNull::from(&mut (command.as_mut().bitmap)) } -} - -/// Moves the provided bitmap to be contained in the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_set( - mut command: NonNull, - bitmap: NonNull, -) { - unsafe { - command.as_mut().bitmap = heap_remove(bitmap); - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_get_origin( - command: NonNull, - origin_x: NonNull, - origin_y: NonNull, -) { - 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_bitmap_set_origin( - mut command: NonNull, - origin_x: usize, - origin_y: usize, -) { - unsafe { - command.as_mut().origin = Origin::new(origin_x, origin_y); - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_set_compression( - mut command: NonNull, - compression: CompressionCode, -) { - unsafe { - command.as_mut().compression = compression; - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_get_compression( - command: NonNull, -) -> CompressionCode { - unsafe { command.as_ref().compression } -} diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs deleted file mode 100644 index bfe2d33..0000000 --- a/src/commands/bitvec_command.rs +++ /dev/null @@ -1,123 +0,0 @@ -use crate::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, -}; -use servicepoint::{ - BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, - Packet, -}; -use std::ptr::NonNull; - -/// Set pixel data starting at the pixel offset on screen. -/// -/// The screen will continuously overwrite more pixel data without regarding the offset, meaning -/// once the starting row is full, overwriting will continue on column 0. -/// -/// The [`BinaryOperation`] will be applied on the display comparing old and sent bit. -/// -/// `new_bit = old_bit op sent_bit` -/// -/// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. -/// -/// The contained [`DisplayBitVec`] is always uncompressed. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_new( - bitvec: NonNull, - offset: usize, - operation: BinaryOperation, - compression: CompressionCode, -) -> NonNull { - heap_move_nonnull(BitVecCommand { - bitvec: unsafe { heap_remove(bitvec) }, - offset, - operation, - compression, - }) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_into_packet( - command: NonNull, -) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_clone( - command: NonNull, -) -> NonNull { - unsafe { heap_clone(command) } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull) { - unsafe { heap_drop(command) } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get( - mut command: NonNull, -) -> *mut DisplayBitVec { - &mut unsafe { command.as_mut() }.bitvec -} - -/// Moves the provided bitmap to be contained in the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set( - mut command: NonNull, - bitvec: NonNull, -) { - unsafe { - command.as_mut().bitvec = heap_remove(bitvec); - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get_offset( - command: NonNull, -) -> Offset { - unsafe { command.as_ref().offset } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set_offset( - mut command: NonNull, - offset: Offset, -) { - unsafe { - command.as_mut().offset = offset; - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get_operation( - command: NonNull, -) -> BinaryOperation { - unsafe { command.as_ref().operation.clone() } // TODO remove clone -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set_operation( - mut command: NonNull, - operation: BinaryOperation, -) { - unsafe { - command.as_mut().operation = operation; - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set_compression( - mut command: NonNull, - compression: CompressionCode, -) { - unsafe { - command.as_mut().compression = compression; - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get_compression( - command: NonNull, -) -> CompressionCode { - unsafe { command.as_ref().compression } -} diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs deleted file mode 100644 index bd975d6..0000000 --- a/src/commands/brightness_grid_command.rs +++ /dev/null @@ -1,89 +0,0 @@ -use crate::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, -}; -use servicepoint::{ - BitmapCommand, BrightnessGrid, BrightnessGridCommand, Origin, Packet, -}; -use std::ptr::NonNull; - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_new( - grid: NonNull, - origin_x: usize, - origin_y: usize, -) -> NonNull { - 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, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet( - command: NonNull, -) -> *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, -) -> NonNull { - unsafe { heap_clone(command) } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_free( - command: NonNull, -) { - 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, - grid: NonNull, -) { - unsafe { - command.as_mut().grid = heap_remove(grid); - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_get( - mut command: NonNull, -) -> *mut BrightnessGrid { - &mut unsafe { command.as_mut() }.grid -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_get_origin( - command: NonNull, - origin_x: NonNull, - origin_y: NonNull, -) { - 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, - origin_x: usize, - origin_y: usize, -) { - unsafe { - command.as_mut().origin = Origin::new(origin_x, origin_y); - } -} diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs deleted file mode 100644 index 751a6ab..0000000 --- a/src/commands/cc_only_commands.rs +++ /dev/null @@ -1,54 +0,0 @@ -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 { - heap_move_nonnull(ClearCommand) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_clear_free(command: NonNull) { - 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 { - heap_move_nonnull(HardResetCommand) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_hard_reset_free( - command: NonNull, -) { - 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 { - heap_move_nonnull(FadeOutCommand) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_fade_out_free(command: NonNull) { - unsafe { heap_drop(command) } -} diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs deleted file mode 100644 index bc078cd..0000000 --- a/src/commands/char_grid_command.rs +++ /dev/null @@ -1,87 +0,0 @@ -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] -pub unsafe extern "C" fn sp_cmd_char_grid_new( - grid: NonNull, - origin_x: usize, - origin_y: usize, -) -> NonNull { - 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, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_into_packet( - command: NonNull, -) -> *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, -) -> NonNull { - unsafe { heap_clone(command) } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_free( - command: NonNull, -) { - 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, - grid: NonNull, -) { - unsafe { - command.as_mut().grid = heap_remove(grid); - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_get( - mut command: NonNull, -) -> *mut CharGrid { - &mut unsafe { command.as_mut() }.grid -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_get_origin( - command: NonNull, - origin_x: NonNull, - origin_y: NonNull, -) { - 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, - origin_x: usize, - origin_y: usize, -) { - unsafe { - command.as_mut().origin = Origin::new(origin_x, origin_y); - } -} diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs deleted file mode 100644 index 1e1598a..0000000 --- a/src/commands/cp437_grid_command.rs +++ /dev/null @@ -1,89 +0,0 @@ -use crate::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, -}; -use servicepoint::{ - BitmapCommand, Cp437Grid, Cp437GridCommand, Origin, Packet, -}; -use std::ptr::NonNull; - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_new( - grid: NonNull, - origin_x: usize, - origin_y: usize, -) -> NonNull { - 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, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_into_packet( - command: NonNull, -) -> *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, -) -> NonNull { - unsafe { heap_clone(command) } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_free( - command: NonNull, -) { - 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, - grid: NonNull, -) { - unsafe { - command.as_mut().grid = heap_remove(grid); - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_get( - mut command: NonNull, -) -> *mut Cp437Grid { - &mut unsafe { command.as_mut() }.grid -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_get_origin( - command: NonNull, - origin_x: NonNull, - origin_y: NonNull, -) { - 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, - origin_x: usize, - origin_y: usize, -) { - unsafe { - command.as_mut().origin = Origin::new(origin_x, origin_y); - } -} diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs deleted file mode 100644 index 5066022..0000000 --- a/src/commands/generic_command.rs +++ /dev/null @@ -1,281 +0,0 @@ -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, - pub bitvec: NonNull, - pub brightness_grid: NonNull, - pub char_grid: NonNull, - pub cp437_grid: NonNull, - pub global_brightness: NonNull, - pub clear: NonNull, - #[allow(deprecated)] - pub bitmap_legacy: NonNull, - pub hard_reset: NonNull, - pub fade_out: NonNull, -} - -#[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, -) -> *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() - }), - } -} diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs deleted file mode 100644 index 29e7c19..0000000 --- a/src/commands/global_brightness_command.rs +++ /dev/null @@ -1,53 +0,0 @@ -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 { - heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_into_packet( - command: NonNull, -) -> *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, -) -> NonNull { - unsafe { heap_clone(command) } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_free( - command: NonNull, -) { - 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, - brightness: Brightness, -) { - unsafe { - command.as_mut().brightness = brightness; - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_get( - mut command: NonNull, -) -> *mut Brightness { - &mut unsafe { command.as_mut() }.brightness -} diff --git a/src/commands/mod.rs b/src/commands/mod.rs deleted file mode 100644 index 3cbb99a..0000000 --- a/src/commands/mod.rs +++ /dev/null @@ -1,15 +0,0 @@ -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::*; diff --git a/src/cp437_grid.rs b/src/cp437_grid.rs index b4d449e..ff70d2b 100644 --- a/src/cp437_grid.rs +++ b/src/cp437_grid.rs @@ -1,7 +1,4 @@ -use crate::{ - byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, heap_move_some, heap_remove, -}; +use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, }; @@ -26,15 +23,20 @@ pub unsafe extern "C" fn sp_cp437_grid_load( data: ByteSlice, ) -> *mut Cp437Grid { let data = unsafe { data.as_slice() }; - heap_move_some(Cp437Grid::load(width, height, data)) + let grid = Cp437Grid::load(width, height, data); + if let Some(grid) = grid { + heap_move(grid) + } else { + std::ptr::null_mut() + } } /// Clones a [Cp437Grid]. #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_clone( - grid: NonNull, + cp437_grid: NonNull, ) -> NonNull { - unsafe { heap_clone(grid) } + heap_move_nonnull(unsafe { cp437_grid.as_ref().clone() }) } /// Deallocates a [Cp437Grid]. @@ -145,8 +147,11 @@ pub unsafe extern "C" fn sp_cp437_grid_into_packet( y: usize, ) -> *mut Packet { let grid = unsafe { heap_remove(grid) }; - heap_move_ok(Packet::try_from(Cp437GridCommand { + match Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), - })) + }) { + Ok(packet) => heap_move(packet), + Err(_) => std::ptr::null_mut(), + } } diff --git a/src/lib.rs b/src/lib.rs index de22866..60c42ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ //! #include "servicepoint.h" //! //! int main(void) { -//! UdpSocket *connection = sp_udp_open("172.23.42.29:2342"); +//! UdpConnection *connection = sp_udp_open("172.23.42.29:2342"); //! if (connection == NULL) //! return 1; //! @@ -25,17 +25,27 @@ //! } //! ``` +pub use crate::bitmap::*; +pub use crate::bitvec::*; +pub use crate::brightness_grid::*; +pub use crate::byte_slice::*; +pub use crate::char_grid::*; +pub use crate::cp437_grid::*; +pub use crate::packet::*; +pub use crate::typed_command::*; +pub use crate::udp::*; +pub use servicepoint::CommandCode; use std::ptr::NonNull; -pub mod bitmap; -pub mod bitvec; -pub mod brightness_grid; -pub mod byte_slice; -pub mod char_grid; -pub mod commands; -pub mod cp437_grid; -pub mod packet; -pub mod udp; +mod bitmap; +mod bitvec; +mod brightness_grid; +mod byte_slice; +mod char_grid; +mod cp437_grid; +mod packet; +mod typed_command; +mod udp; use std::time::Duration; @@ -50,14 +60,6 @@ pub(crate) fn heap_move_nonnull(x: T) -> NonNull { NonNull::from(Box::leak(Box::new(x))) } -pub(crate) fn heap_move_ok(x: Result) -> *mut T { - x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) -} - -pub(crate) fn heap_move_some(x: Option) -> *mut T { - x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) -} - pub(crate) unsafe fn heap_drop(x: NonNull) { drop(unsafe { heap_remove(x) }); } @@ -66,12 +68,5 @@ pub(crate) unsafe fn heap_remove(x: NonNull) -> T { unsafe { *Box::from_raw(x.as_ptr()) } } -unsafe fn heap_clone(source: NonNull) -> NonNull { - heap_move_nonnull(unsafe { source.as_ref().clone() }) -} - /// This is a type only used by cbindgen to have a type for pointers. pub struct UdpSocket; - -/// This is a type only used by cbindgen to have a type for pointers. -pub struct DisplayBitVec; diff --git a/src/packet.rs b/src/packet.rs index 16509be..d8b1579 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,17 +1,33 @@ -use crate::{ - byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, -}; -use servicepoint::{CommandCode, Header, Packet}; +use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; +use servicepoint::{CommandCode, Header, Packet, TypedCommand}; use std::ptr::NonNull; +/// 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_packet_from_command( + command: NonNull, +) -> *mut Packet { + let command = unsafe { heap_remove(command) }; + if let Ok(packet) = command.try_into() { + heap_move(packet) + } else { + std::ptr::null_mut() + } +} + /// Tries to load a [Packet] from the passed array with the specified length. /// /// returns: NULL in case of an error, pointer to the allocated packet otherwise #[no_mangle] pub unsafe extern "C" fn sp_packet_try_load(data: ByteSlice) -> *mut Packet { let data = unsafe { data.as_slice() }; - heap_move_ok(servicepoint::Packet::try_from(data)) + match servicepoint::Packet::try_from(data) { + Err(_) => std::ptr::null_mut(), + Ok(packet) => heap_move(packet), + } } /// Creates a raw [Packet] from parts. @@ -83,7 +99,7 @@ pub unsafe extern "C" fn sp_packet_serialize_to( pub unsafe extern "C" fn sp_packet_clone( packet: NonNull, ) -> NonNull { - unsafe { heap_clone(packet) } + heap_move_nonnull(unsafe { packet.as_ref().clone() }) } /// Deallocates a [Packet]. diff --git a/src/typed_command.rs b/src/typed_command.rs new file mode 100644 index 0000000..9e27540 --- /dev/null +++ b/src/typed_command.rs @@ -0,0 +1,201 @@ +use crate::{heap_drop, heap_move, heap_move_nonnull, SPBitVec}; +use servicepoint::{ + BinaryOperation, Bitmap, Brightness, BrightnessGrid, CharGrid, + CompressionCode, Cp437Grid, GlobalBrightnessCommand, Packet, TypedCommand, +}; +use std::ptr::NonNull; + +/// 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_command_try_from_packet( + packet: NonNull, +) -> *mut TypedCommand { + let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; + match servicepoint::TypedCommand::try_from(packet) { + Err(_) => std::ptr::null_mut(), + Ok(command) => heap_move(command), + } +} + +/// Clones a [TypedCommand] instance. +/// +/// returns: new [TypedCommand] instance. +#[no_mangle] +pub unsafe extern "C" fn sp_command_clone( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { command.as_ref().clone() }) +} + +/// Set all pixels to the off state. +/// +/// Does not affect brightness. +/// +/// Returns: a new [servicepoint::Command::Clear] instance. +/// +/// # Examples +/// +/// ```C +/// sp_udp_send_command(connection, sp_command_clear()); +/// ``` +#[no_mangle] +pub unsafe extern "C" fn sp_command_clear() -> NonNull { + heap_move_nonnull(servicepoint::ClearCommand.into()) +} + +/// 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 [servicepoint::Command::HardReset] instance. +#[no_mangle] +pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull { + heap_move_nonnull(servicepoint::HardResetCommand.into()) +} + +/// A yet-to-be-tested command. +/// +/// Returns: a new [servicepoint::Command::FadeOut] instance. +#[no_mangle] +pub unsafe extern "C" fn sp_command_fade_out() -> NonNull { + heap_move_nonnull(servicepoint::FadeOutCommand.into()) +} + +/// Set the brightness of all tiles to the same value. +/// +/// Returns: a new [servicepoint::Command::Brightness] instance. +#[no_mangle] +pub unsafe extern "C" fn sp_command_global_brightness( + brightness: Brightness, +) -> NonNull { + 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, +) -> NonNull { + 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) +} + +/// Set pixel data starting at the pixel offset on screen. +/// +/// The screen will continuously overwrite more pixel data without regarding the offset, meaning +/// once the starting row is full, overwriting will continue on column 0. +/// +/// The [`BinaryOperation`] will be applied on the display comparing old and sent bit. +/// +/// `new_bit = old_bit op sent_bit` +/// +/// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. +/// +/// The contained [`BitVecU8Msb0`] is always uncompressed. +#[no_mangle] +pub unsafe extern "C" fn sp_command_bitvec( + offset: usize, + bit_vec: NonNull, + compression: CompressionCode, + operation: BinaryOperation, +) -> *mut TypedCommand { + let bit_vec = unsafe { *Box::from_raw(bit_vec.as_ptr()) }; + let command = servicepoint::BitVecCommand { + offset, + operation, + bitvec: bit_vec.0, + compression, + } + .into(); + heap_move(command) +} + +/// 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, +) -> NonNull { + 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, +) -> NonNull { + 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) +} + +/// Sets a window of pixels to the specified values. +/// +/// The passed [Bitmap] gets consumed. +/// +/// Returns: a new [servicepoint::BitmapCommand] instance. +#[no_mangle] +pub unsafe extern "C" fn sp_command_bitmap( + x: usize, + y: usize, + bitmap: NonNull, + compression: CompressionCode, +) -> *mut TypedCommand { + let bitmap = unsafe { *Box::from_raw(bitmap.as_ptr()) }; + let command = servicepoint::BitmapCommand { + origin: servicepoint::Origin::new(x, y), + bitmap, + compression, + } + .into(); + heap_move(command) +} + +/// Deallocates a [TypedCommand]. +/// +/// # Examples +/// +/// ```C +/// TypedCommand c = sp_command_clear(); +/// sp_command_free(c); +/// ``` +#[no_mangle] +pub unsafe extern "C" fn sp_command_free(command: NonNull) { + unsafe { heap_drop(command) } +} diff --git a/src/udp.rs b/src/udp.rs index 7b659cf..f68aa16 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,18 +1,17 @@ -use crate::commands::{CommandTag, SPCommand}; -use crate::{heap_drop, heap_move_ok, heap_remove}; -use servicepoint::{Header, Packet, UdpSocketExt}; +use crate::{heap_drop, heap_move, heap_remove}; +use servicepoint::{Header, Packet, TypedCommand, UdpSocketExt}; use std::ffi::{c_char, CStr}; use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket}; use std::ptr::NonNull; -/// Creates a new instance of [UdpSocket]. +/// Creates a new instance of [UdpConnection]. /// /// returns: NULL if connection fails, or connected instance /// /// # Examples /// /// ```C -/// UdpSocket connection = sp_udp_open("172.23.42.29:2342"); +/// UdpConnection connection = sp_udp_open("172.23.42.29:2342"); /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` @@ -21,18 +20,22 @@ pub unsafe extern "C" fn sp_udp_open(host: NonNull) -> *mut UdpSocket { let host = unsafe { CStr::from_ptr(host.as_ptr()) } .to_str() .expect("Bad encoding"); + let connection = match UdpSocket::bind_connect(host) { + Err(_) => return std::ptr::null_mut(), + Ok(value) => value, + }; - heap_move_ok(UdpSocket::bind_connect(host)) + heap_move(connection) } -/// Creates a new instance of [UdpSocket]. +/// Creates a new instance of [UdpConnection]. /// /// returns: NULL if connection fails, or connected instance /// /// # Examples /// /// ```C -/// UdpSocket connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); +/// UdpConnection connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` @@ -45,10 +48,14 @@ pub unsafe extern "C" fn sp_udp_open_ipv4( port: u16, ) -> *mut UdpSocket { let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); - heap_move_ok(UdpSocket::bind_connect(addr)) + let connection = match UdpSocket::bind_connect(addr) { + Err(_) => return std::ptr::null_mut(), + Ok(value) => value, + }; + heap_move(connection) } -/// Sends a [Packet] to the display using the [UdpSocket]. +/// Sends a [Packet] to the display using the [UdpConnection]. /// /// The passed `packet` gets consumed. /// @@ -62,7 +69,7 @@ pub unsafe extern "C" fn sp_udp_send_packet( unsafe { connection.as_ref().send(&Vec::from(packet)) }.is_ok() } -/// Sends a [TypedCommand] to the display using the [UdpSocket]. +/// Sends a [TypedCommand] to the display using the [UdpConnection]. /// /// The passed `command` gets consumed. /// @@ -76,47 +83,13 @@ pub unsafe extern "C" fn sp_udp_send_packet( #[no_mangle] pub unsafe extern "C" fn sp_udp_send_command( connection: NonNull, - command: SPCommand, + command: NonNull, ) -> bool { - unsafe { - match command.tag { - CommandTag::Invalid => return false, - CommandTag::Bitmap => connection - .as_ref() - .send_command(heap_remove(command.data.bitmap)), - CommandTag::BitVec => connection - .as_ref() - .send_command(heap_remove(command.data.bitvec)), - CommandTag::BrightnessGrid => connection - .as_ref() - .send_command(heap_remove(command.data.brightness_grid)), - CommandTag::CharGrid => connection - .as_ref() - .send_command(heap_remove(command.data.char_grid)), - CommandTag::Cp437Grid => connection - .as_ref() - .send_command(heap_remove(command.data.cp437_grid)), - CommandTag::GlobalBrightness => connection - .as_ref() - .send_command(heap_remove(command.data.global_brightness)), - CommandTag::Clear => connection - .as_ref() - .send_command(heap_remove(command.data.clear)), - CommandTag::HardReset => connection - .as_ref() - .send_command(heap_remove(command.data.hard_reset)), - CommandTag::FadeOut => connection - .as_ref() - .send_command(heap_remove(command.data.fade_out)), - CommandTag::BitmapLegacy => connection - .as_ref() - .send_command(heap_remove(command.data.bitmap_legacy)), - } - } - .is_some() + let command = unsafe { heap_remove(command) }; + unsafe { connection.as_ref().send_command(command) }.is_some() } -/// Sends a [Header] to the display using the [UdpSocket]. +/// Sends a [Header] to the display using the [UdpConnection]. /// /// returns: true in case of success /// @@ -139,7 +112,7 @@ pub unsafe extern "C" fn sp_udp_send_header( .is_ok() } -/// Closes and deallocates a [UdpSocket]. +/// Closes and deallocates a [UdpConnection]. #[no_mangle] pub unsafe extern "C" fn sp_udp_free(connection: NonNull) { unsafe { heap_drop(connection) }