expose tagged union instead of TypedCommand to C
Some checks failed
Rust / build-gnu-apt (pull_request) Failing after 1m18s
Rust / build-size-gnu-unstable (pull_request) Failing after 1m11s

This commit is contained in:
Vinzenz Schroeter 2025-05-06 22:54:54 +02:00
parent 32d39f8006
commit 85ccf4123c
20 changed files with 657 additions and 277 deletions

View file

@ -125,6 +125,27 @@ 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.
*
@ -239,6 +260,23 @@ typedef struct Bitmap Bitmap;
*/
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.
*/
@ -260,6 +298,19 @@ typedef struct BrightnessGridCommand BrightnessGridCommand;
*/
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.
*
@ -292,6 +343,50 @@ typedef struct Cp437GridCommand Cp437GridCommand;
*/
typedef struct DisplayBitVec DisplayBitVec;
/**
* <div class="warning">Untested</div>
*
* 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.
*
@ -313,14 +408,6 @@ 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.
*/
@ -456,6 +543,31 @@ 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.
*
@ -770,7 +882,7 @@ ByteSlice sp_bitvec_unsafe_data_ref(SPBitVec */*notnull*/ bit_vec);
/**
* Clones a [BrightnessGrid].
*/
BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ brightness_grid);
BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ grid);
/**
* Sets the value of all cells in the [BrightnessGrid].
@ -906,7 +1018,7 @@ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid);
/**
* Clones a [CharGrid].
*/
CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ char_grid);
CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ grid);
/**
* Sets the value of all cells in the [CharGrid].
@ -1039,8 +1151,6 @@ void sp_cmd_bitmap_get_origin(BitmapCommand */*notnull*/ command,
Packet *sp_cmd_bitmap_into_packet(BitmapCommand */*notnull*/ command);
TypedCommand */*notnull*/ sp_cmd_bitmap_into_typed(BitmapCommand */*notnull*/ command);
/**
* Sets a window of pixels to the specified values.
*
@ -1080,8 +1190,6 @@ BinaryOperation sp_cmd_bitvec_get_operation(BitVecCommand */*notnull*/ command);
Packet *sp_cmd_bitvec_into_packet(BitVecCommand */*notnull*/ command);
TypedCommand */*notnull*/ sp_cmd_bitvec_into_typed(BitVecCommand */*notnull*/ command);
/**
* Set pixel data starting at the pixel offset on screen.
*
@ -1116,6 +1224,22 @@ void sp_cmd_bitvec_set_offset(BitVecCommand */*notnull*/ command,
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);
/**
* Moves the provided bitmap to be contained in the command.
*/
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);
@ -1130,8 +1254,6 @@ void sp_cmd_brightness_grid_get_origin(BrightnessGridCommand */*notnull*/ comman
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);
@ -1160,8 +1282,6 @@ void sp_cmd_char_grid_get_origin(CharGridCommand */*notnull*/ command,
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);
@ -1176,6 +1296,23 @@ 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);
/**
* 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());
* ```
*/
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);
@ -1190,8 +1327,6 @@ void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command,
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);
@ -1206,70 +1341,24 @@ 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.
*
* The passed [BrightnessGrid] gets consumed.
*
* Returns: a new [servicepoint::Command::CharBrightness] instance.
*/
TypedCommand */*notnull*/ sp_command_brightness_grid(size_t x,
size_t y,
BrightnessGrid */*notnull*/ grid);
/**
* Show UTF-8 encoded text on the screen.
*
* The passed [CharGrid] gets consumed.
*
* Returns: a new [servicepoint::CharGridCommand] instance.
*/
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 [servicepoint::Command::Clear] instance.
*
* # Examples
*
* ```C
* sp_udp_send_command(connection, sp_command_clear());
* ```
*/
TypedCommand */*notnull*/ sp_command_clear(void);
/**
* Clones a [TypedCommand] instance.
*
* returns: new [TypedCommand] instance.
*/
TypedCommand */*notnull*/ sp_command_clone(TypedCommand */*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);
void sp_cmd_fade_out_free(ClearCommand */*notnull*/ command);
/**
* A yet-to-be-tested command.
*
* Returns: a new [servicepoint::Command::FadeOut] instance.
* Returns: a new [FadeOutCommand] instance.
*/
TypedCommand */*notnull*/ sp_command_fade_out(void);
FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void);
/**
* Deallocates a [TypedCommand].
* Clones a [SPCommand] instance.
*
* returns: new [SPCommand] instance.
*/
SPCommand sp_cmd_generic_clone(SPCommand command);
/**
* Deallocates a [SPCommand].
*
* # Examples
*
@ -1278,37 +1367,31 @@ TypedCommand */*notnull*/ sp_command_fade_out(void);
* sp_command_free(c);
* ```
*/
void sp_command_free(TypedCommand */*notnull*/ command);
void sp_cmd_generic_free(SPCommand command);
/**
* Set the brightness of all tiles to the same value.
* Turns a [TypedCommand] into a [Packet].
* The [TypedCommand] gets consumed.
*
* Returns: a new [servicepoint::Command::Brightness] instance.
* Returns NULL in case of an error.
*/
TypedCommand */*notnull*/ sp_command_global_brightness(Brightness brightness);
Packet *sp_cmd_generic_into_packet(SPCommand command);
void sp_cmd_hard_reset_free(ClearCommand */*notnull*/ 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 [servicepoint::Command::HardReset] instance.
* Returns: a new [HardResetCommand] instance.
*/
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);
HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void);
/**
* Clones a [Cp437Grid].
*/
Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ cp437_grid);
Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ grid);
/**
* Sets the value of all cells in the [Cp437Grid].
@ -1419,14 +1502,6 @@ 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.
*
@ -1532,8 +1607,7 @@ 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,
TypedCommand */*notnull*/ command);
bool sp_udp_send_command(UdpSocket */*notnull*/ connection, SPCommand command);
/**
* Sends a [Header] to the display using the [UdpSocket].