/* Generated with cbindgen:0.28.0 */

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

/**
 * pixel count on whole screen
 */
#define PIXEL_COUNT (PIXEL_WIDTH * PIXEL_HEIGHT)

/**
 * Display height in pixels
 *
 * # Examples
 *
 * ```rust
 * # use servicepoint::{PIXEL_HEIGHT, PIXEL_WIDTH, Bitmap};
 * let grid = Bitmap::new(PIXEL_WIDTH, PIXEL_HEIGHT);
 * ```
 */
#define PIXEL_HEIGHT (TILE_HEIGHT * TILE_SIZE)

/**
 * Display width in pixels
 *
 * # Examples
 *
 * ```rust
 * # use servicepoint::{PIXEL_HEIGHT, PIXEL_WIDTH, Bitmap};
 * let grid = Bitmap::new(PIXEL_WIDTH, PIXEL_HEIGHT);
 * ```
 */
#define PIXEL_WIDTH (TILE_WIDTH * TILE_SIZE)

/**
 * Display tile count in the y-direction
 *
 * # Examples
 *
 * ```rust
 * # use servicepoint::{Cp437Grid, TILE_HEIGHT, TILE_WIDTH};
 * let grid = Cp437Grid::new(TILE_WIDTH, TILE_HEIGHT);
 * ```
 */
#define TILE_HEIGHT 20

/**
 * size of a single tile in one dimension
 */
#define TILE_SIZE 8

/**
 * Display tile count in the x-direction
 *
 * # Examples
 *
 * ```rust
 * # use servicepoint::{Cp437Grid, TILE_HEIGHT, TILE_WIDTH};
 * let grid = Cp437Grid::new(TILE_WIDTH, TILE_HEIGHT);
 * ```
 */
#define TILE_WIDTH 56

/**
 * Binary operations for use with the [`BitVecCommand`] command.
 */
enum BinaryOperation
#ifdef __cplusplus
  : uint8_t
#endif // __cplusplus
 {
    /**
     * r := a
     */
    BINARY_OPERATION_OVERWRITE,
    /**
     * r := a && b
     */
    BINARY_OPERATION_AND,
    /**
     * r := a || b
     */
    BINARY_OPERATION_OR,
    /**
     * r := (a || b) && (a != b)
     */
    BINARY_OPERATION_XOR,
};
#ifndef __cplusplus
typedef uint8_t BinaryOperation;
#endif // __cplusplus

/**
 * The u16 command codes used for the [Command]s.
 */
enum CommandCode
#ifdef __cplusplus
  : uint16_t
#endif // __cplusplus
 {
    COMMAND_CODE_CLEAR = 2,
    COMMAND_CODE_CP437_DATA = 3,
    COMMAND_CODE_CHAR_BRIGHTNESS = 5,
    COMMAND_CODE_BRIGHTNESS = 7,
    COMMAND_CODE_HARD_RESET = 11,
    COMMAND_CODE_FADE_OUT = 13,
    COMMAND_CODE_BITMAP_LEGACY = 16,
    COMMAND_CODE_BITMAP_LINEAR = 18,
    COMMAND_CODE_BITMAP_LINEAR_WIN_UNCOMPRESSED = 19,
    COMMAND_CODE_BITMAP_LINEAR_AND = 20,
    COMMAND_CODE_BITMAP_LINEAR_OR = 21,
    COMMAND_CODE_BITMAP_LINEAR_XOR = 22,
    COMMAND_CODE_BITMAP_LINEAR_WIN_ZLIB = 23,
    COMMAND_CODE_BITMAP_LINEAR_WIN_BZIP2 = 24,
    COMMAND_CODE_BITMAP_LINEAR_WIN_LZMA = 25,
    COMMAND_CODE_UTF8_DATA = 32,
    COMMAND_CODE_BITMAP_LINEAR_WIN_ZSTD = 26,
};
#ifndef __cplusplus
typedef uint16_t CommandCode;
#endif // __cplusplus

/**
 * Specifies the kind of compression to use. Availability depends on features.
 *
 * # Examples
 *
 * ```rust
 * # use servicepoint::*;
 * // create command without payload compression
 * # let pixels = Bitmap::max_sized();
 * _ = BitmapCommand {
 *     origin: Origin::ZERO,
 *     bitmap: pixels,
 *     compression: CompressionCode::Uncompressed
 * };
 *
 * // create command with payload compressed with lzma and appropriate header flags
 * # let pixels = Bitmap::max_sized();
 * _ = BitmapCommand {
 *     origin: Origin::ZERO,
 *     bitmap: pixels,
 *     compression: CompressionCode::Lzma
 * };
 * ```
 */
enum CompressionCode
#ifdef __cplusplus
  : uint16_t
#endif // __cplusplus
 {
    /**
     * no compression
     */
    COMPRESSION_CODE_UNCOMPRESSED = 0,
    /**
     * compress using flate2 with zlib header
     */
    COMPRESSION_CODE_ZLIB = 26490,
    /**
     * compress using bzip2
     */
    COMPRESSION_CODE_BZIP2 = 25210,
    /**
     * compress using lzma
     */
    COMPRESSION_CODE_LZMA = 27770,
    /**
     * compress using Zstandard
     */
    COMPRESSION_CODE_ZSTD = 31347,
};
#ifndef __cplusplus
typedef uint16_t CompressionCode;
#endif // __cplusplus

/**
 * A fixed-size 2D grid of booleans.
 *
 * The values are stored in packed bytes (8 values per byte) in the same order as used by the display for storing pixels.
 * This means that no conversion is necessary for sending the data to the display.
 * The downside is that the width has to be a multiple of 8.
 *
 * # Examples
 *
 * ```rust
 * use servicepoint::Bitmap;
 * let mut bitmap = Bitmap::new(8, 2);
 *
 * ```
 */
typedef struct Bitmap Bitmap;

/**
 * The raw packet.
 *
 * Contents should probably only be used directly to use features not exposed by the library.
 *
 * You may want to use [`crate::Command`] or [`crate::TypedCommand`] instead.
 */
typedef struct Packet Packet;

/**
 * A vector of bits
 *
 * # Examples
 * ```C
 * SPBitVec vec = sp_bitvec_new(8);
 * sp_bitvec_set(vec, 5, true);
 * sp_bitvec_free(vec);
 * ```
 */
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.
 */
typedef struct UdpSocket UdpSocket;

/**
 * A 2D grid of values.
 *
 * The memory layout is the one the display expects in [`crate::Command`]s.
 *
 * This structure can be used with any type that implements the [Value] trait.
 * You can also use the concrete type aliases provided in this crate, e.g. [`crate::CharGrid`] and [`crate::ByteGrid`].
 */
typedef struct ValueGrid_Brightness ValueGrid_Brightness;

/**
 * A 2D grid of values.
 *
 * The memory layout is the one the display expects in [`crate::Command`]s.
 *
 * This structure can be used with any type that implements the [Value] trait.
 * You can also use the concrete type aliases provided in this crate, e.g. [`crate::CharGrid`] and [`crate::ByteGrid`].
 */
typedef struct ValueGrid_char ValueGrid_char;

/**
 * A 2D grid of values.
 *
 * The memory layout is the one the display expects in [`crate::Command`]s.
 *
 * This structure can be used with any type that implements the [Value] trait.
 * You can also use the concrete type aliases provided in this crate, e.g. [`crate::CharGrid`] and [`crate::ByteGrid`].
 */
typedef struct ValueGrid_u8 ValueGrid_u8;

/**
 * Represents a span of memory (`&mut [u8]` ) as a struct.
 *
 * # Safety
 *
 * The caller has to make sure that:
 *
 * - accesses to the memory pointed to with `start` is never accessed outside `length`
 * - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in
 *   the function returning this type.
 */
typedef struct {
    /**
     * The start address of the memory.
     */
    uint8_t */*notnull*/ start;
    /**
     * The amount of memory in bytes.
     */
    size_t length;
} ByteSlice;

/**
 * A grid containing brightness values.
 *
 * # Examples
 *
 * ```rust
 * # use servicepoint::*;
 * let mut grid = BrightnessGrid::new(2,2);
 * grid.set(0, 0, Brightness::MIN);
 * grid.set(1, 1, Brightness::MIN);
 *
 * # let connection = FakeConnection;
 * connection.send_command(BrightnessGridCommand {
 *     origin: Origin::new(3, 7),
 *     grid
 * }).unwrap()
 * ```
 */
typedef ValueGrid_Brightness BrightnessGrid;

/**
 * A display brightness value, checked for correct value range
 *
 * # Examples
 *
 * ```
 * # use servicepoint::*;
 * let b = Brightness::MAX;
 * let val: u8 = b.into();
 *
 * let b = Brightness::try_from(7).unwrap();
 * # let connection = FakeConnection;
 * let result = connection.send_command(GlobalBrightnessCommand::from(b));
 * ```
 */
typedef uint8_t Brightness;
/**
 * highest possible brightness value, 11
 */
#define Brightness_MAX 11
/**
 * lowest possible brightness value, 0
 */
#define Brightness_MIN 0

/**
 * A grid containing UTF-8 characters.
 *
 * To send a `CharGrid` to the display, use a [`crate::CharGridCommand`].
 *
 * Also see [`ValueGrid`] for the non-specialized operations and examples.
 *
 * # Examples
 *
 * ```rust
 * # use servicepoint::*;
 * let grid = CharGrid::from("You can\nload multiline\nstrings directly");
 * assert_eq!(grid.get_row_str(1), Some("load multiline\0\0".to_string()));
 *
 * # let connection = FakeConnection;
 * let command = CharGridCommand { origin: Origin::ZERO, grid };
 * connection.send_command(command).unwrap()
 * ```
 */
typedef ValueGrid_char CharGrid;

/**
 * A grid containing codepage 437 characters.
 *
 * The encoding is currently not enforced.
 */
typedef ValueGrid_u8 Cp437Grid;

/**
 * A raw header.
 *
 * The header specifies the kind of command, the size of the payload and where to display the
 * payload, where applicable.
 *
 * Because the meaning of most fields depend on the command, there are no speaking names for them.
 *
 * The contained values are in platform endian-ness and may need to be converted before sending.
 */
typedef struct {
    /**
     * The first two bytes specify which command this packet represents.
     */
    uint16_t command_code;
    /**
     * First command-specific value
     */
    uint16_t a;
    /**
     * Second command-specific value
     */
    uint16_t b;
    /**
     * Third command-specific value
     */
    uint16_t c;
    /**
     * Fourth command-specific value
     */
    uint16_t d;
} Header;



#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

/**
 * Clones a [Bitmap].
 */
Bitmap */*notnull*/ sp_bitmap_clone(Bitmap */*notnull*/ bitmap);

/**
 * Sets the state of all pixels in the [Bitmap].
 *
 * # Arguments
 *
 * - `bitmap`: instance to write to
 * - `value`: the value to set all pixels to
 */
void sp_bitmap_fill(Bitmap */*notnull*/ bitmap, bool value);

/**
 * Deallocates a [Bitmap].
 */
void sp_bitmap_free(Bitmap */*notnull*/ bitmap);

/**
 * Tries to convert the BitVec to a Bitmap.
 *
 * The provided BitVec gets consumed.
 *
 * Returns NULL in case of error.
 */
Bitmap *sp_bitmap_from_bitvec(size_t width, SPBitVec */*notnull*/ bitvec);

/**
 * Gets the current value at the specified position in the [Bitmap].
 *
 * # Arguments
 *
 * - `bitmap`: instance to read from
 * - `x` and `y`: position of the cell to read
 *
 * # Panics
 *
 * - when accessing `x` or `y` out of bounds
 */
bool sp_bitmap_get(Bitmap */*notnull*/ bitmap, size_t x, size_t y);

/**
 * Gets the height in pixels of the [Bitmap] instance.
 *
 * # Arguments
 *
 * - `bitmap`: instance to read from
 */
size_t sp_bitmap_height(Bitmap */*notnull*/ bitmap);

/**
 * Consumes the Bitmap and returns the contained BitVec
 */
SPBitVec */*notnull*/ sp_bitmap_into_bitvec(Bitmap */*notnull*/ bitmap);

/**
 * Creates a [BitmapCommand] and immediately turns that into a [Packet].
 *
 * The provided [Bitmap] gets consumed.
 *
 * Returns NULL in case of an error.
 */
Packet *sp_bitmap_into_packet(Bitmap */*notnull*/ bitmap,
                              size_t x,
                              size_t y,
                              CompressionCode compression);

/**
 * Loads a [Bitmap] with the specified dimensions from the provided data.
 *
 * # Arguments
 *
 * - `width`: size in pixels in x-direction
 * - `height`: size in pixels in y-direction
 *
 * returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error.
 */
Bitmap *sp_bitmap_load(size_t width,
                       size_t height,
                       ByteSlice data);

/**
 * Creates a new [Bitmap] with the specified dimensions.
 *
 * # Arguments
 *
 * - `width`: size in pixels in x-direction
 * - `height`: size in pixels in y-direction
 *
 * returns: [Bitmap] initialized to all pixels off, or NULL in case of an error.
 *
 * # Errors
 *
 * In the following cases, this function will return NULL:
 *
 * - when the width is not dividable by 8
 *
 * # Examples
 *
 * ```C
 * Cp437Grid grid = sp_bitmap_new(8, 3);
 * sp_bitmap_fill(grid, true);
 * sp_bitmap_set(grid, 0, 0, false);
 * sp_bitmap_free(grid);
 * ```
 */
Bitmap *sp_bitmap_new(size_t width, size_t height);

/**
 * Creates a new [Bitmap] with a size matching the screen.
 *
 * returns: [Bitmap] initialized to all pixels off.
 */
Bitmap */*notnull*/ sp_bitmap_new_max_sized(void);

/**
 * Sets the value of the specified position in the [Bitmap].
 *
 * # Arguments
 *
 * - `bitmap`: instance to write to
 * - `x` and `y`: position of the cell
 * - `value`: the value to write to the cell
 *
 * # Panics
 *
 * - when accessing `x` or `y` out of bounds
 */
void sp_bitmap_set(Bitmap */*notnull*/ bitmap, size_t x, size_t y, bool value);

/**
 * Gets an unsafe reference to the data of the [Bitmap] instance.
 *
 * The returned memory is valid for the lifetime of the bitmap.
 */
ByteSlice sp_bitmap_unsafe_data_ref(Bitmap */*notnull*/ bitmap);

/**
 * Gets the width in pixels of the [Bitmap] instance.
 *
 * # Arguments
 *
 * - `bitmap`: instance to read from
 *
 * # Panics
 *
 * - when `bitmap` is NULL
 *
 * # Safety
 *
 * The caller has to make sure that:
 *
 * - `bitmap` points to a valid [Bitmap]
 */
size_t sp_bitmap_width(Bitmap */*notnull*/ bitmap);

/**
 * Clones a [SPBitVec].
 */
SPBitVec */*notnull*/ sp_bitvec_clone(SPBitVec */*notnull*/ bit_vec);

/**
 * Sets the value of all bits in the [SPBitVec].
 *
 * # Arguments
 *
 * - `bit_vec`: instance to write to
 * - `value`: the value to set all bits to
 */
void sp_bitvec_fill(SPBitVec */*notnull*/ bit_vec, bool value);

/**
 * Deallocates a [SPBitVec].
 */
void sp_bitvec_free(SPBitVec */*notnull*/ bit_vec);

/**
 * Gets the value of a bit from the [SPBitVec].
 *
 * # Arguments
 *
 * - `bit_vec`: instance to read from
 * - `index`: the bit index to read
 *
 * returns: value of the bit
 *
 * # Panics
 *
 * - when accessing `index` out of bounds
 */
bool sp_bitvec_get(SPBitVec */*notnull*/ bit_vec, size_t index);

/**
 * Creates a [BitVecCommand] and immediately turns that into a [Packet].
 *
 * The provided [SPBitVec] gets consumed.
 *
 * Returns NULL in case of an error.
 */
Packet *sp_bitvec_into_packet(SPBitVec */*notnull*/ bitvec,
                              size_t offset,
                              BinaryOperation operation,
                              CompressionCode compression);

/**
 * Returns true if length is 0.
 *
 * # Arguments
 *
 * - `bit_vec`: instance to write to
 */
bool sp_bitvec_is_empty(SPBitVec */*notnull*/ bit_vec);

/**
 * Gets the length of the [SPBitVec] in bits.
 *
 * # Arguments
 *
 * - `bit_vec`: instance to write to
 */
size_t sp_bitvec_len(SPBitVec */*notnull*/ bit_vec);

/**
 * Interpret the data as a series of bits and load then into a new [SPBitVec] instance.
 *
 * returns: [SPBitVec] instance containing data.
 */
SPBitVec */*notnull*/ sp_bitvec_load(ByteSlice data);

/**
 * Creates a new [SPBitVec] instance.
 *
 * # Arguments
 *
 * - `size`: size in bits.
 *
 * returns: [SPBitVec] with all bits set to false.
 *
 * # Panics
 *
 * - when `size` is not divisible by 8.
 */
SPBitVec */*notnull*/ sp_bitvec_new(size_t size);

/**
 * Sets the value of a bit in the [SPBitVec].
 *
 * # Arguments
 *
 * - `bit_vec`: instance to write to
 * - `index`: the bit index to edit
 * - `value`: the value to set the bit to
 *
 * # Panics
 *
 * - when accessing `index` out of bounds
 */
void sp_bitvec_set(SPBitVec */*notnull*/ bit_vec, size_t index, bool value);

/**
 * Gets an unsafe reference to the data of the [SPBitVec] instance.
 *
 * The returned memory is valid for the lifetime of the bitvec.
 *
 * # Arguments
 *
 * - `bit_vec`: instance to write to
 */
ByteSlice sp_bitvec_unsafe_data_ref(SPBitVec */*notnull*/ bit_vec);

/**
 * Clones a [BrightnessGrid].
 */
BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ brightness_grid);

/**
 * Sets the value of all cells in the [BrightnessGrid].
 *
 * # Arguments
 *
 * - `brightness_grid`: instance to write to
 * - `value`: the value to set all cells to
 */
void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ brightness_grid,
                             Brightness value);

/**
 * Deallocates a [BrightnessGrid].
 */
void sp_brightness_grid_free(BrightnessGrid */*notnull*/ brightness_grid);

/**
 * Gets the current value at the specified position.
 *
 * # Arguments
 *
 * - `brightness_grid`: instance to read from
 * - `x` and `y`: position of the cell to read
 *
 * returns: value at position
 *
 * # Panics
 * - When accessing `x` or `y` out of bounds.
 */
Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ brightness_grid,
                                  size_t x,
                                  size_t y);

/**
 * Gets the height of the [BrightnessGrid] instance.
 *
 * # Arguments
 *
 * - `brightness_grid`: instance to read from
 *
 * returns: height
 */
size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ brightness_grid);

/**
 * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet].
 *
 * The provided [BrightnessGrid] gets consumed.
 *
 * Returns NULL in case of an error.
 */
Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid,
                                       size_t x,
                                       size_t y);

/**
 * Loads a [BrightnessGrid] with the specified dimensions from the provided data.
 *
 * Any out of range values will be set to [Brightness::MAX] or [Brightness::MIN].
 *
 * returns: new [BrightnessGrid] instance, or NULL in case of an error.
 */
BrightnessGrid *sp_brightness_grid_load(size_t width,
                                        size_t height,
                                        ByteSlice data);

/**
 * Creates a new [BrightnessGrid] with the specified dimensions.
 *
 * returns: [BrightnessGrid] initialized to 0.
 *
 * # Examples
 * ```C
 * UdpConnection connection = sp_udp_open("127.0.0.1:2342");
 * if (connection == NULL)
 *     return 1;
 *
 * BrightnessGrid grid = sp_brightness_grid_new(2, 2);
 * sp_brightness_grid_set(grid, 0, 0, 0);
 * sp_brightness_grid_set(grid, 1, 1, 10);
 *
 * TypedCommand command = sp_command_char_brightness(grid);
 * sp_udp_free(connection);
 * ```
 */
BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height);

/**
 * Sets the value of the specified position in the [BrightnessGrid].
 *
 * # Arguments
 *
 * - `brightness_grid`: instance to write to
 * - `x` and `y`: position of the cell
 * - `value`: the value to write to the cell
 *
 * returns: old value of the cell
 *
 * # Panics
 *
 * - When accessing `x` or `y` out of bounds.
 */
void sp_brightness_grid_set(BrightnessGrid */*notnull*/ brightness_grid,
                            size_t x,
                            size_t y,
                            Brightness value);

/**
 * Gets an unsafe reference to the data of the [BrightnessGrid] instance.
 *
 * The returned memory is valid for the lifetime of the brightness grid.
 *
 * # Arguments
 *
 * - `brightness_grid`: instance to read from
 *
 * returns: slice of bytes underlying the `brightness_grid`.
 */
ByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid */*notnull*/ brightness_grid);

/**
 * Gets the width of the [BrightnessGrid] instance.
 *
 * # Arguments
 *
 * - `brightness_grid`: instance to read from
 *
 * returns: width
 */
size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid);

/**
 * Clones a [CharGrid].
 */
CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ char_grid);

/**
 * Sets the value of all cells in the [CharGrid].
 *
 * # Arguments
 *
 * - `char_grid`: instance to write to
 * - `value`: the value to set all cells to
 */
void sp_char_grid_fill(CharGrid */*notnull*/ char_grid, uint32_t value);

/**
 * Deallocates a [CharGrid].
 */
void sp_char_grid_free(CharGrid */*notnull*/ char_grid);

/**
 * Returns the current value at the specified position.
 *
 * # Arguments
 *
 * - `char_grid`: instance to read from
 * - `x` and `y`: position of the cell to read
 *
 * # Panics
 *
 * - when accessing `x` or `y` out of bounds
 */
uint32_t sp_char_grid_get(CharGrid */*notnull*/ char_grid, size_t x, size_t y);

/**
 * Gets the height of the [CharGrid] instance.
 *
 * # Arguments
 *
 * - `char_grid`: instance to read from
 */
size_t sp_char_grid_height(CharGrid */*notnull*/ char_grid);

/**
 * Creates a [CharGridCommand] and immediately turns that into a [Packet].
 *
 * The provided [CharGrid] gets consumed.
 *
 * Returns NULL in case of an error.
 */
Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid,
                                 size_t x,
                                 size_t y);

/**
 * Loads a [CharGrid] with the specified dimensions from the provided data.
 *
 * returns: new CharGrid or NULL in case of an error
 */
CharGrid *sp_char_grid_load(size_t width, size_t height, ByteSlice data);

/**
 * Creates a new [CharGrid] with the specified dimensions.
 *
 * returns: [CharGrid] initialized to 0.
 *
 * # Examples
 *
 * ```C
 * CharGrid grid = sp_char_grid_new(4, 3);
 * sp_char_grid_fill(grid, '?');
 * sp_char_grid_set(grid, 0, 0, '!');
 * sp_char_grid_free(grid);
 * ```
 */
CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height);

/**
 * Sets the value of the specified position in the [CharGrid].
 *
 * # Arguments
 *
 * - `char_grid`: instance to write to
 * - `x` and `y`: position of the cell
 * - `value`: the value to write to the cell
 *
 * returns: old value of the cell
 *
 * # Panics
 *
 * - when accessing `x` or `y` out of bounds
 */
void sp_char_grid_set(CharGrid */*notnull*/ char_grid,
                      size_t x,
                      size_t y,
                      uint32_t value);

/**
 * Gets the width of the [CharGrid] instance.
 *
 * # Arguments
 *
 * - `char_grid`: instance to read from
 */
size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid);

/**
 * Sets a window of pixels to the specified values.
 *
 * The passed [Bitmap] gets consumed.
 *
 * Returns: a new [servicepoint::BitmapCommand] instance.
 */
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.
 *
 * 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.
 */
TypedCommand *sp_command_bitvec(size_t offset,
                                SPBitVec */*notnull*/ bit_vec,
                                CompressionCode compression,
                                BinaryOperation operation);

/**
 * 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);

/**
 * A yet-to-be-tested command.
 *
 * Returns: a new [servicepoint::Command::FadeOut] instance.
 */
TypedCommand */*notnull*/ sp_command_fade_out(void);

/**
 * Deallocates a [TypedCommand].
 *
 * # Examples
 *
 * ```C
 * TypedCommand c = sp_command_clear();
 * sp_command_free(c);
 * ```
 */
void sp_command_free(TypedCommand */*notnull*/ command);

/**
 * Set the brightness of all tiles to the same value.
 *
 * Returns: a new [servicepoint::Command::Brightness] instance.
 */
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 [servicepoint::Command::HardReset] 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);

/**
 * Clones a [Cp437Grid].
 */
Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ cp437_grid);

/**
 * Sets the value of all cells in the [Cp437Grid].
 *
 * # Arguments
 *
 * - `cp437_grid`: instance to write to
 * - `value`: the value to set all cells to
 */
void sp_cp437_grid_fill(Cp437Grid */*notnull*/ cp437_grid, uint8_t value);

/**
 * Deallocates a [Cp437Grid].
 */
void sp_cp437_grid_free(Cp437Grid */*notnull*/ cp437_grid);

/**
 * Gets the current value at the specified position.
 *
 * # Arguments
 *
 * - `cp437_grid`: instance to read from
 * - `x` and `y`: position of the cell to read
 *
 * # Panics
 *
 * - when accessing `x` or `y` out of bounds
 */
uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ cp437_grid,
                          size_t x,
                          size_t y);

/**
 * Gets the height of the [Cp437Grid] instance.
 *
 * # Arguments
 *
 * - `cp437_grid`: instance to read from
 */
size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ cp437_grid);

/**
 * Creates a [Cp437GridCommand] and immediately turns that into a [Packet].
 *
 * The provided [Cp437Grid] gets consumed.
 *
 * Returns NULL in case of an error.
 */
Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid,
                                  size_t x,
                                  size_t y);

/**
 * Loads a [Cp437Grid] with the specified dimensions from the provided data.
 */
Cp437Grid *sp_cp437_grid_load(size_t width, size_t height, ByteSlice data);

/**
 * Creates a new [Cp437Grid] with the specified dimensions.
 *
 * returns: [Cp437Grid] initialized to 0.
 */
Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height);

/**
 * Sets the value of the specified position in the [Cp437Grid].
 *
 * # Arguments
 *
 * - `cp437_grid`: instance to write to
 * - `x` and `y`: position of the cell
 * - `value`: the value to write to the cell
 *
 * returns: old value of the cell
 *
 * # Panics
 *
 * - when accessing `x` or `y` out of bounds
 */
void sp_cp437_grid_set(Cp437Grid */*notnull*/ cp437_grid,
                       size_t x,
                       size_t y,
                       uint8_t value);

/**
 * Gets an unsafe reference to the data of the [Cp437Grid] instance.
 *
 * The returned memory is valid for the lifetime of the grid.
 */
ByteSlice sp_cp437_grid_unsafe_data_ref(Cp437Grid */*notnull*/ cp437_grid);

/**
 * Gets the width of the [Cp437Grid] instance.
 *
 * # Arguments
 *
 * - `cp437_grid`: instance to read from
 */
size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ cp437_grid);

/**
 * Clones a [Packet].
 */
Packet */*notnull*/ sp_packet_clone(Packet */*notnull*/ packet);

/**
 * Deallocates a [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.
 *
 * returns: new instance. Will never return null.
 */
Packet */*notnull*/ sp_packet_from_parts(Header header,
                                         const ByteSlice *payload);

/**
 * Returns a pointer to the header field of the provided packet.
 *
 * The returned header can be changed and will be valid for the lifetime of the packet.
 */
Header */*notnull*/ sp_packet_get_header(Packet */*notnull*/ packet);

/**
 * Returns a pointer to the current payload of the provided packet.
 *
 * The returned memory can be changed and will be valid until a new payload is set.
 */
ByteSlice sp_packet_get_payload(Packet */*notnull*/ packet);

/**
 * Serialize the packet into the provided buffer.
 *
 * # Panics
 *
 * - if the buffer is not big enough to hold header+payload.
 */
void sp_packet_serialize_to(Packet */*notnull*/ packet, ByteSlice buffer);

/**
 * Sets the payload of the provided packet to the provided data.
 *
 * This makes previous payload pointers invalid.
 */
void sp_packet_set_payload(Packet */*notnull*/ packet, ByteSlice data);

/**
 * 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
 */
Packet *sp_packet_try_load(ByteSlice data);

/**
 * Converts u16 into [CommandCode].
 *
 * If the provided value is not valid, false is returned and result is not changed.
 */
bool sp_u16_to_command_code(uint16_t code,
                            CommandCode *result);

/**
 * Closes and deallocates a [UdpConnection].
 */
void sp_udp_free(UdpSocket */*notnull*/ connection);

/**
 * Creates a new instance of [UdpConnection].
 *
 * returns: NULL if connection fails, or connected instance
 *
 * # Examples
 *
 * ```C
 * UdpConnection connection = sp_udp_open("172.23.42.29:2342");
 * if (connection != NULL)
 *     sp_udp_send_command(connection, sp_command_clear());
 * ```
 */
UdpSocket *sp_udp_open(char */*notnull*/ host);

/**
 * Creates a new instance of [UdpConnection].
 *
 * returns: NULL if connection fails, or connected instance
 *
 * # Examples
 *
 * ```C
 * UdpConnection connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342);
 * if (connection != NULL)
 *     sp_udp_send_command(connection, sp_command_clear());
 * ```
 */
UdpSocket *sp_udp_open_ipv4(uint8_t ip1,
                            uint8_t ip2,
                            uint8_t ip3,
                            uint8_t ip4,
                            uint16_t port);

/**
 * Sends a [TypedCommand] to the display using the [UdpConnection].
 *
 * The passed `command` gets consumed.
 *
 * returns: true in case of success
 *
 * # Examples
 *
 * ```C
 * sp_udp_send_command(connection, sp_command_brightness(5));
 * ```
 */
bool sp_udp_send_command(UdpSocket */*notnull*/ connection,
                         TypedCommand */*notnull*/ command);

/**
 * Sends a [Header] to the display using the [UdpConnection].
 *
 * returns: true in case of success
 *
 * # Examples
 *
 * ```C
 * sp_udp_send_header(connection, sp_command_brightness(5));
 * ```
 */
bool sp_udp_send_header(UdpSocket */*notnull*/ udp_connection, Header header);

/**
 * Sends a [Packet] to the display using the [UdpConnection].
 *
 * The passed `packet` gets consumed.
 *
 * returns: true in case of success
 */
bool sp_udp_send_packet(UdpSocket */*notnull*/ connection,
                        Packet */*notnull*/ packet);

#ifdef __cplusplus
}  // extern "C"
#endif  // __cplusplus