diff --git a/example/Makefile b/example/Makefile index d3f346f..bb53fe1 100644 --- a/example/Makefile +++ b/example/Makefile @@ -4,6 +4,7 @@ CARGO := rustup run nightly cargo THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(THIS_DIR)/.. RUST_TARGET_DIR := $(REPO_ROOT)/target/x86_64-unknown-linux-musl/size-optimized +export SERVICEPOINT_HEADER_OUT := $(REPO_ROOT)/include RUSTFLAGS := -Zlocation-detail=none \ -Zfmt-debug=none \ @@ -36,46 +37,57 @@ CCFLAGS := -static -Os \ -fvisibility=hidden \ -Bsymbolic \ -Wl,--exclude-libs,ALL \ - -fno-ident + -fno-ident \ + -Wall #-fuse-ld=gold \ -fno-exceptions #-Wl,--icf=all \ -export SERVICEPOINT_HEADER_OUT := $(REPO_ROOT)/include +STRIPFLAGS := -s --strip-unneeded -R .comment -R .gnu.version -R .comment -R .note -R .note.gnu.build-id -R .note.ABI-tag -build: out/example +c_src := $(wildcard *.c) +programs := $(basename $(c_src)) +bins := $(addprefix out/, $(programs)) +unstripped_bins := $(addsuffix _unstripped, $(bins)) +run_programs := $(addprefix run_, $(programs)) +rs_src := $(wildcard ../src/*.rs) ../Cargo.lock + +all: $(bins) clean: rm -r out || true rm include/servicepoint.h || true cargo clean -run: out/example - out/example +PHONY: all clean sizes $(run_programs) -PHONY: build clean dependencies run - -out/example_unstripped: dependencies main.c +$(unstripped_bins): out/%_unstripped: %.c $(SERVICEPOINT_HEADER_OUT)/servicepoint.h $(RUST_TARGET_DIR)/libservicepoint_binding_c.a mkdir -p out || true - ${CC} main.c \ + ${CC} $^ \ -I $(SERVICEPOINT_HEADER_OUT) \ -L $(RUST_TARGET_DIR)\ $(CCFLAGS) \ - -o out/example_unstripped -out/example: out/example_unstripped - strip -s --strip-unneeded \ - -R .comment -R .gnu.version -R .comment -R .note -R .note.gnu.build-id -R .note.ABI-tag \ - out/example_unstripped \ - -o out/example + -o $@ -dependencies: FORCE +$(bins): out/%: out/%_unstripped + strip $(STRIPFLAGS) $^ -o $@ + +$(SERVICEPOINT_HEADER_OUT)/servicepoint.h $(RUST_TARGET_DIR)/libservicepoint_binding_c.a: $(rs_src) mkdir -p include || true # generate servicepoint header and binary to link against ${CARGO} rustc $(CARGOFLAGS) -- $(RUSTFLAGS) -analyze-size: out/example_unstripped - nm --print-size --size-sort --reverse-sort --radix=d --demangle out/example_unstripped \ - | awk '{size=$$2+0; print size "\t" $$4}' \ - | less +$(run_programs): run_%: out/% FORCE + ./$< + +sizes: $(bins) + ls -lB out + +#analyze-size: out/example_unstripped +# nm --print-size --size-sort --reverse-sort --radix=d --demangle out/example_unstripped \ +# | awk '{size=$$2+0; print size "\t" $$4}' \ +# | less + + FORCE: ; diff --git a/example/announce.c b/example/announce.c new file mode 100644 index 0000000..d7a55fb --- /dev/null +++ b/example/announce.c @@ -0,0 +1,31 @@ +#include "servicepoint.h" + +int main(void) { + UdpConnection *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + if (connection == NULL) + return 1; + + sp_udp_send_command(connection, sp_command_clear()); + + CharGrid *grid = sp_char_grid_new(5, 2); + if (grid == NULL) + return 1; + + sp_char_grid_set(grid, 0, 0, 'H'); + sp_char_grid_set(grid, 1, 0, 'e'); + sp_char_grid_set(grid, 2, 0, 'l'); + sp_char_grid_set(grid, 3, 0, 'l'); + sp_char_grid_set(grid, 4, 0, 'o'); + sp_char_grid_set(grid, 0, 1, 'W'); + sp_char_grid_set(grid, 1, 1, 'o'); + sp_char_grid_set(grid, 2, 1, 'r'); + sp_char_grid_set(grid, 3, 1, 'l'); + sp_char_grid_set(grid, 4, 1, 'd'); + + + TypedCommand *command = sp_command_char_grid(0, 0, grid); + sp_udp_send_command(connection, command); + + sp_udp_free(connection); + return 0; +} diff --git a/example/brightness_tester.c b/example/brightness_tester.c new file mode 100644 index 0000000..93e64ce --- /dev/null +++ b/example/brightness_tester.c @@ -0,0 +1,29 @@ +#include "servicepoint.h" + +int main(void) { + // UdpConnection *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + UdpConnection *connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342); + if (connection == NULL) + return -1; + + Bitmap *all_on = sp_bitmap_new_max_sized(); + sp_bitmap_fill(all_on, true); + TypedCommand *cmd = sp_command_bitmap(0, 0, all_on, COMPRESSION_CODE_UNCOMPRESSED); + if (cmd == NULL) + return -1; + + sp_udp_send_command(connection, cmd); + + BrightnessGrid *grid = sp_brightness_grid_new(TILE_WIDTH, TILE_HEIGHT); + + 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)); + } + + TypedCommand *command = sp_command_brightness_grid(0, 0, grid); + sp_udp_send_command(connection, command); + + sp_udp_free(connection); + return 0; +} diff --git a/example/main.c b/example/random_stuff.c similarity index 93% rename from example/main.c rename to example/random_stuff.c index 9c0ec9e..80c3847 100644 --- a/example/main.c +++ b/example/random_stuff.c @@ -1,4 +1,3 @@ -#include #include "servicepoint.h" int main(void) { @@ -8,13 +7,13 @@ int main(void) { Bitmap *pixels = sp_bitmap_new(PIXEL_WIDTH, PIXEL_HEIGHT); if (pixels == NULL) - return 1; + return 1; sp_bitmap_fill(pixels, true); TypedCommand *command = sp_command_bitmap(0, 0, pixels, COMPRESSION_CODE_UNCOMPRESSED); if (command == NULL) - return 1; + return 1; Packet *packet = sp_packet_from_command(command); diff --git a/flake.nix b/flake.nix index e1b21cd..79e0d6c 100644 --- a/flake.nix +++ b/flake.nix @@ -58,7 +58,6 @@ gcc gnumake pkg-config - ]; RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; diff --git a/include/servicepoint.h b/include/servicepoint.h index 51b00ca..aa7893f 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -668,6 +668,8 @@ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ brightness_grid); /** * 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,