Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
577ba02d22 | ||
|
|
0f17f86120 | ||
|
|
3a966ad1a1 |
14 changed files with 140 additions and 87 deletions
14
.github/workflows/rust.yml
vendored
14
.github/workflows/rust.yml
vendored
|
|
@ -40,5 +40,15 @@ jobs:
|
|||
- name: build
|
||||
run: cargo build
|
||||
|
||||
- name: build example
|
||||
run: cd example && make
|
||||
- name: build example -- glibc release
|
||||
run: cd example && make clean && make PROFILE=release
|
||||
- name: build example -- glibc debug
|
||||
run: cd example && make clean && make PROFILE=debug
|
||||
- name: build example -- glibc size-optimized
|
||||
run: cd example && make clean && make PROFILE=size-optimized
|
||||
- name: build example -- musl release
|
||||
run: cd example && make clean && make PROFILE=release MUSL=1
|
||||
- name: build example -- musl debug
|
||||
run: cd example && make clean && make PROFILE=debug MUSL=1
|
||||
- name: build example -- musl size-optimized
|
||||
run: cd example && make clean && make PROFILE=size-optimized MUSL=1
|
||||
|
|
|
|||
|
|
@ -26,8 +26,9 @@ git = "https://git.berlin.ccc.de/servicepoint/servicepoint/"
|
|||
branch = "next"
|
||||
|
||||
[features]
|
||||
full = ["servicepoint/all_compressions", "servicepoint/default"]
|
||||
default = ["full"]
|
||||
protocol_udp = ["servicepoint/protocol_udp"]
|
||||
all_compressions = ["servicepoint/all_compressions"]
|
||||
default = ["all_compressions", "servicepoint/default", "protocol_udp"]
|
||||
|
||||
[lints.rust]
|
||||
missing-docs = "warn"
|
||||
|
|
|
|||
144
example/Makefile
144
example/Makefile
|
|
@ -1,83 +1,113 @@
|
|||
CC := gcc
|
||||
CARGO := rustup run nightly cargo
|
||||
CARGO ?= rustup run nightly cargo
|
||||
STRIP ?= strip
|
||||
|
||||
TARGET := x86_64-unknown-linux-musl
|
||||
PROFILE := size-optimized
|
||||
FEATURES := protocol_udp
|
||||
|
||||
THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
|
||||
REPO_ROOT := $(THIS_DIR)/..
|
||||
RUST_TARGET_DIR := $(REPO_ROOT)/target/$(TARGET)/$(PROFILE)
|
||||
REPO_ROOT := $(realpath $(THIS_DIR)/..)
|
||||
export SERVICEPOINT_HEADER_OUT := $(REPO_ROOT)/include
|
||||
|
||||
RUSTFLAGS := -Zlocation-detail=none \
|
||||
-Zfmt-debug=none \
|
||||
-C linker=musl-gcc \
|
||||
-C link-arg=-s \
|
||||
-C link-arg=--gc-sections \
|
||||
-C link-arg=-z,norelro \
|
||||
-C link-arg=--hash-style=gnu \
|
||||
--crate-type=staticlib \
|
||||
-C panic=abort
|
||||
override CFG_MUSL := $(if $(CFG_MUSL),$(CFG_MUSL),$(if $(MUSL),$(MUSL),0))
|
||||
override CFG_PROFILE := $(if $(CFG_PROFILE),$(CFG_PROFILE),$(if $(PROFILE),$(PROFILE),release))
|
||||
|
||||
CARGOFLAGS := --manifest-path=$(REPO_ROOT)/Cargo.toml \
|
||||
--profile=$(PROFILE) \
|
||||
CCFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wall -fwhole-program
|
||||
|
||||
RUSTFLAGS += -C link-arg=-s \
|
||||
-C link-arg=-Wl,--gc-sections \
|
||||
--crate-type=staticlib
|
||||
|
||||
STRIPFLAGS := -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag
|
||||
|
||||
ifeq ($(CFG_MUSL), 1)
|
||||
TARGET ?= x86_64-unknown-linux-musl
|
||||
CC ?= musl-gcc
|
||||
CCFLAGS += -static
|
||||
else
|
||||
TARGET ?= x86_64-unknown-linux-gnu
|
||||
CC ?= gcc
|
||||
CCFLAGS += -shared
|
||||
endif
|
||||
|
||||
RUSTFLAGS += -C linker=$(CC)
|
||||
|
||||
#ifeq ($(CFG_PROFILE), size-optimized)
|
||||
# CCFLAGS += -nodefaultlibs -lc
|
||||
#endif
|
||||
|
||||
CCFLAGS += -lservicepoint_binding_c
|
||||
RUST_TARGET_DIR := $(REPO_ROOT)/target/$(TARGET)/$(CFG_PROFILE)
|
||||
OUT_BASE_DIR := $(realpath $(THIS_DIR)/out/)
|
||||
OUT_DIR := $(realpath $(OUT_BASE_DIR)/$(CFG_PROFILE)/)
|
||||
|
||||
CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \
|
||||
--profile=$(CFG_PROFILE) \
|
||||
--no-default-features \
|
||||
--target=$(TARGET) \
|
||||
-Zbuild-std="core,std,alloc,proc_macro,panic_abort" \
|
||||
-Zbuild-std-features="panic_immediate_abort" \
|
||||
--features=$(FEATURES) \
|
||||
--target=$(TARGET)
|
||||
|
||||
CCFLAGS := -static -Os \
|
||||
-ffunction-sections -fdata-sections \
|
||||
-fwrapv -fomit-frame-pointer -fno-stack-protector\
|
||||
-fwhole-program \
|
||||
-nodefaultlibs -lservicepoint_binding_c -lc \
|
||||
-Wl,--gc-sections \
|
||||
-fno-unroll-loops \
|
||||
-fno-unwind-tables -fno-asynchronous-unwind-tables \
|
||||
-fmerge-all-constants \
|
||||
-Wl,-z,norelro \
|
||||
-Wl,--hash-style=gnu \
|
||||
-fvisibility=hidden \
|
||||
-Bsymbolic \
|
||||
-Wl,--exclude-libs,ALL \
|
||||
-fno-ident \
|
||||
-Wall
|
||||
#-fuse-ld=gold \
|
||||
-fno-exceptions
|
||||
#-Wl,--icf=all \
|
||||
ifeq ($(CFG_PROFILE), size-optimized)
|
||||
CCFLAGS += -Oz \
|
||||
-fwrapv -fomit-frame-pointer -fno-stack-protector\
|
||||
-fno-unroll-loops \
|
||||
-fno-unwind-tables -fno-asynchronous-unwind-tables \
|
||||
-fmerge-all-constants \
|
||||
-Wl,-z,norelro \
|
||||
-Wl,--hash-style=gnu \
|
||||
-fvisibility=hidden \
|
||||
-Bsymbolic \
|
||||
-Wl,--exclude-libs,ALL \
|
||||
-fno-ident \
|
||||
-fno-exceptions
|
||||
CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \
|
||||
-Zbuild-std-features="panic_immediate_abort"
|
||||
RUSTFLAGS += -Zlocation-detail=none \
|
||||
-Zfmt-debug=none \
|
||||
-C link-arg=-z,norelro \
|
||||
-C link-arg=--hash-style=gnu \
|
||||
-C panic=abort
|
||||
else ifeq ($(CFG_PROFILE), release)
|
||||
CCFLAGS += -O2
|
||||
else ifeq ($(CFG_PROFILE), debug)
|
||||
CCFLAGS += -Og
|
||||
else
|
||||
CFG_PROFILE := $(error "PROFILE has to be set to one of: debug, release, size-optimized")
|
||||
endif
|
||||
|
||||
STRIPFLAGS := -s --strip-unneeded -R .comment -R .gnu.version -R .comment -R .note -R .note.gnu.build-id -R .note.ABI-tag
|
||||
ifeq ($(LTO), 1)
|
||||
CCFLAGS += -flto
|
||||
endif
|
||||
|
||||
_c_src := $(wildcard *.c)
|
||||
_programs := $(basename $(_c_src))
|
||||
_c_src := $(wildcard ./src/*.c)
|
||||
_programs := $(basename $(notdir $(_c_src)))
|
||||
_bins := $(addprefix out/, $(_programs))
|
||||
_unstripped_bins := $(addsuffix _unstripped, $(_bins))
|
||||
_run_programs := $(addprefix run_, $(_programs))
|
||||
_rs_src := $(wildcard ../src/**.rs) ../Cargo.lock
|
||||
_rs_src := $(wildcard ../src/**.rs) ../Cargo.lock ../Cargo.toml ../cbindgen.toml
|
||||
_sp_artifacts := $(SERVICEPOINT_HEADER_OUT)/servicepoint.h $(RUST_TARGET_DIR)/libservicepoint_binding_c.a $(RUST_TARGET_DIR)/libservicepoint_binding_c.so
|
||||
|
||||
all: $(_bins)
|
||||
|
||||
clean:
|
||||
rm -r out || true
|
||||
rm include/servicepoint.h || true
|
||||
echo rm -r $(OUT_DIR) || true
|
||||
rm $(SERVICEPOINT_HEADER_OUT)/servicepoint.h || true
|
||||
cargo clean
|
||||
|
||||
PHONY: all clean sizes $(_run_programs)
|
||||
.PHONY: all clean sizes $(_run_programs)
|
||||
|
||||
$(_unstripped_bins): out/%_unstripped: %.c $(SERVICEPOINT_HEADER_OUT)/servicepoint.h $(RUST_TARGET_DIR)/libservicepoint_binding_c.a
|
||||
$(_unstripped_bins): out/%_unstripped: src/%.c $(SERVICEPOINT_HEADER_OUT)/servicepoint.h $(_sp_artifacts)
|
||||
mkdir -p out || true
|
||||
${CC} $^ \
|
||||
${CC} $< \
|
||||
-I $(SERVICEPOINT_HEADER_OUT) \
|
||||
-L $(RUST_TARGET_DIR)\
|
||||
-L $(RUST_TARGET_DIR) \
|
||||
$(CCFLAGS) \
|
||||
-o $@
|
||||
|
||||
$(_bins): out/%: out/%_unstripped
|
||||
strip $(STRIPFLAGS) $^ -o $@
|
||||
$(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
|
||||
$(_sp_artifacts): $(_rs_src)
|
||||
mkdir -p $(SERVICEPOINT_HEADER_OUT) || true
|
||||
# generate servicepoint header and library to link against
|
||||
${CARGO} rustc $(CARGOFLAGS) -- $(RUSTFLAGS)
|
||||
|
||||
$(_run_programs): run_%: out/% FORCE
|
||||
|
|
@ -86,9 +116,9 @@ $(_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
|
||||
analyze-size: $(OUT_DIR)/$(BIN)_unstripped
|
||||
nm --print-size --size-sort --reverse-sort --radix=d --demangle $(OUT_DIR)/$(BIN)_unstripped \
|
||||
| awk '{size=$$2+0; print size "\t" $$4}' \
|
||||
| less
|
||||
|
||||
FORCE: ;
|
||||
|
|
|
|||
33
example/src/announce.c
Normal file
33
example/src/announce.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#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;
|
||||
|
||||
sp_udp_send_header(connection, (Header) {.command_code = COMMAND_CODE_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');
|
||||
|
||||
Packet *packet = sp_char_grid_into_packet(grid, 0, 0);
|
||||
if (packet == NULL)
|
||||
return 1;
|
||||
sp_udp_send_packet(connection, packet);
|
||||
|
||||
sp_udp_free(connection);
|
||||
return 0;
|
||||
}
|
||||
30
example/src/brightness_tester.c
Normal file
30
example/src/brightness_tester.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#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);
|
||||
|
||||
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);
|
||||
|
||||
ByteSlice slice = sp_brightness_grid_unsafe_data_ref(grid);
|
||||
for (size_t index = 0; index < slice.length; index++) {
|
||||
slice.start[index] = (uint8_t) (index % ((size_t) Brightness_MAX));
|
||||
}
|
||||
|
||||
packet = sp_brightness_grid_into_packet(grid, 0, 0);
|
||||
sp_udp_send_packet(connection, packet);
|
||||
|
||||
sp_udp_free(connection);
|
||||
return 0;
|
||||
}
|
||||
27
example/src/random_stuff.c
Normal file
27
example/src/random_stuff.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
//#include <stdarg.h>
|
||||
#include "servicepoint.h"
|
||||
//#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
UdpConnection *connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342);
|
||||
if (connection == NULL)
|
||||
return 1;
|
||||
|
||||
Bitmap *pixels = sp_bitmap_new(PIXEL_WIDTH, PIXEL_HEIGHT);
|
||||
if (pixels == NULL)
|
||||
return 1;
|
||||
|
||||
sp_bitmap_fill(pixels, true);
|
||||
|
||||
Packet *packet = sp_bitmap_into_packet(pixels, 0, 0, COMPRESSION_CODE_UNCOMPRESSED);
|
||||
if (packet == NULL)
|
||||
return 1;
|
||||
|
||||
Header *header = sp_packet_get_header(packet);
|
||||
// printf("[%d, %d, %d, %d, %d]\n", header->command_code, header->a, header->b, header->c, header->d);
|
||||
|
||||
sp_udp_send_packet(connection, packet);
|
||||
|
||||
sp_udp_free(connection);
|
||||
return 0;
|
||||
}
|
||||
20
flake.nix
20
flake.nix
|
|
@ -33,16 +33,17 @@
|
|||
{ pkgs, system }:
|
||||
{
|
||||
default = pkgs.mkShell rec {
|
||||
buildInputs = with pkgs;[
|
||||
buildInputs = with pkgs; [
|
||||
xe
|
||||
xz
|
||||
libgcc
|
||||
#glibc.static
|
||||
musl
|
||||
libunwind
|
||||
];
|
||||
#libgcc
|
||||
#glibc
|
||||
pkgsStatic.musl
|
||||
# libunwind
|
||||
|
||||
nativeBuildInputs = with pkgs;[
|
||||
];
|
||||
|
||||
nativeBuildInputs = with pkgs; [
|
||||
(pkgs.symlinkJoin {
|
||||
name = "rust-toolchain";
|
||||
paths = with pkgs; [
|
||||
|
|
@ -57,11 +58,14 @@
|
|||
];
|
||||
})
|
||||
gcc
|
||||
gdb
|
||||
pkgsStatic.gcc
|
||||
gnumake
|
||||
pkg-config
|
||||
];
|
||||
|
||||
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
|
||||
#RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
|
||||
CARGO = "rustup run nightly cargo";
|
||||
};
|
||||
}
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
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};
|
||||
use servicepoint::{
|
||||
Bitmap, BitmapCommand, CompressionCode, DataRef, Grid, Origin, Packet,
|
||||
};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
/// Creates a new [Bitmap] with the specified dimensions.
|
||||
|
|
@ -210,7 +212,7 @@ pub unsafe extern "C" fn sp_bitmap_into_packet(
|
|||
bitmap: NonNull<Bitmap>,
|
||||
x: usize,
|
||||
y: usize,
|
||||
compression: CompressionCode
|
||||
compression: CompressionCode,
|
||||
) -> *mut Packet {
|
||||
let bitmap = unsafe { heap_remove(bitmap) };
|
||||
match Packet::try_from(BitmapCommand {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice};
|
||||
use servicepoint::{BinaryOperation, BitVecCommand, BitVecU8Msb0, CompressionCode, Packet};
|
||||
use servicepoint::{
|
||||
BinaryOperation, BitVecCommand, BitVecU8Msb0, CompressionCode, Packet,
|
||||
};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
/// A vector of bits
|
||||
|
|
@ -157,7 +159,7 @@ pub unsafe extern "C" fn sp_bitvec_into_packet(
|
|||
bitvec: NonNull<SPBitVec>,
|
||||
offset: usize,
|
||||
operation: BinaryOperation,
|
||||
compression: CompressionCode
|
||||
compression: CompressionCode,
|
||||
) -> *mut Packet {
|
||||
let bitvec = unsafe { heap_remove(bitvec) }.0;
|
||||
match Packet::try_from(BitVecCommand {
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
mod typed;
|
||||
|
||||
pub use typed::*;
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice};
|
||||
use servicepoint::{Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet};
|
||||
use servicepoint::{
|
||||
Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet,
|
||||
};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
/// Creates a new [Cp437Grid] with the specified dimensions.
|
||||
|
|
|
|||
12
src/lib.rs
12
src/lib.rs
|
|
@ -30,22 +30,26 @@ pub use crate::bitvec::*;
|
|||
pub use crate::brightness_grid::*;
|
||||
pub use crate::byte_slice::*;
|
||||
pub use crate::char_grid::*;
|
||||
pub use crate::commands::*;
|
||||
pub use crate::connection::*;
|
||||
pub use crate::cp437_grid::*;
|
||||
pub use crate::packet::*;
|
||||
pub use servicepoint::CommandCode;
|
||||
use std::ptr::NonNull;
|
||||
pub use typed_command::*;
|
||||
|
||||
mod bitmap;
|
||||
mod bitvec;
|
||||
mod brightness_grid;
|
||||
mod byte_slice;
|
||||
mod char_grid;
|
||||
mod commands;
|
||||
mod connection;
|
||||
mod cp437_grid;
|
||||
mod packet;
|
||||
mod typed_command;
|
||||
|
||||
#[cfg(feature = "protocol_udp")]
|
||||
mod udp;
|
||||
|
||||
#[cfg(feature = "protocol_udp")]
|
||||
pub use udp::*;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
|
|
|
|||
201
src/typed_command.rs
Normal file
201
src/typed_command.rs
Normal file
|
|
@ -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<Packet>,
|
||||
) -> *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<TypedCommand>,
|
||||
) -> NonNull<TypedCommand> {
|
||||
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<TypedCommand> {
|
||||
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<TypedCommand> {
|
||||
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<TypedCommand> {
|
||||
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<TypedCommand> {
|
||||
heap_move_nonnull(GlobalBrightnessCommand::from(brightness).into())
|
||||
}
|
||||
|
||||
/// Set the brightness of individual tiles in a rectangular area of the display.
|
||||
///
|
||||
/// The passed [BrightnessGrid] gets consumed.
|
||||
///
|
||||
/// Returns: a new [servicepoint::Command::CharBrightness] instance.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_command_brightness_grid(
|
||||
x: usize,
|
||||
y: usize,
|
||||
grid: NonNull<BrightnessGrid>,
|
||||
) -> NonNull<TypedCommand> {
|
||||
let grid = unsafe { *Box::from_raw(grid.as_ptr()) };
|
||||
let result = servicepoint::BrightnessGridCommand {
|
||||
origin: servicepoint::Origin::new(x, y),
|
||||
grid,
|
||||
}
|
||||
.into();
|
||||
heap_move_nonnull(result)
|
||||
}
|
||||
|
||||
/// 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<SPBitVec>,
|
||||
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<Cp437Grid>,
|
||||
) -> NonNull<TypedCommand> {
|
||||
let grid = *unsafe { Box::from_raw(grid.as_ptr()) };
|
||||
let result = servicepoint::Cp437GridCommand {
|
||||
origin: servicepoint::Origin::new(x, y),
|
||||
grid,
|
||||
}
|
||||
.into();
|
||||
heap_move_nonnull(result)
|
||||
}
|
||||
|
||||
/// Show UTF-8 encoded text on the screen.
|
||||
///
|
||||
/// The passed [CharGrid] gets consumed.
|
||||
///
|
||||
/// Returns: a new [servicepoint::CharGridCommand] instance.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_command_char_grid(
|
||||
x: usize,
|
||||
y: usize,
|
||||
grid: NonNull<CharGrid>,
|
||||
) -> NonNull<TypedCommand> {
|
||||
let grid = unsafe { *Box::from_raw(grid.as_ptr()) };
|
||||
let result = servicepoint::CharGridCommand {
|
||||
origin: servicepoint::Origin::new(x, y),
|
||||
grid,
|
||||
}
|
||||
.into();
|
||||
heap_move_nonnull(result)
|
||||
}
|
||||
|
||||
/// 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<Bitmap>,
|
||||
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<TypedCommand>) {
|
||||
unsafe { heap_drop(command) }
|
||||
}
|
||||
119
src/udp.rs
Normal file
119
src/udp.rs
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
use crate::{heap_drop, heap_move, heap_remove};
|
||||
use servicepoint::{Connection, Header, Packet, TypedCommand, UdpConnection};
|
||||
use std::ffi::{c_char, CStr};
|
||||
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
/// 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());
|
||||
/// ```
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_udp_open(
|
||||
host: NonNull<c_char>,
|
||||
) -> *mut UdpConnection {
|
||||
let host = unsafe { CStr::from_ptr(host.as_ptr()) }
|
||||
.to_str()
|
||||
.expect("Bad encoding");
|
||||
let connection = match UdpConnection::open(host) {
|
||||
Err(_) => return std::ptr::null_mut(),
|
||||
Ok(value) => value,
|
||||
};
|
||||
|
||||
heap_move(connection)
|
||||
}
|
||||
|
||||
/// 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());
|
||||
/// ```
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_udp_open_ipv4(
|
||||
ip1: u8,
|
||||
ip2: u8,
|
||||
ip3: u8,
|
||||
ip4: u8,
|
||||
port: u16,
|
||||
) -> *mut UdpConnection {
|
||||
let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port);
|
||||
let connection = match UdpConnection::open(addr) {
|
||||
Err(_) => return std::ptr::null_mut(),
|
||||
Ok(value) => value,
|
||||
};
|
||||
heap_move(connection)
|
||||
}
|
||||
|
||||
/// Sends a [Packet] to the display using the [UdpConnection].
|
||||
///
|
||||
/// The passed `packet` gets consumed.
|
||||
///
|
||||
/// returns: true in case of success
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_udp_send_packet(
|
||||
connection: NonNull<UdpConnection>,
|
||||
packet: NonNull<Packet>,
|
||||
) -> bool {
|
||||
let packet = unsafe { heap_remove(packet) };
|
||||
unsafe { connection.as_ref().send(packet) }.is_ok()
|
||||
}
|
||||
|
||||
/// 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));
|
||||
/// ```
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_udp_send_command(
|
||||
connection: NonNull<UdpConnection>,
|
||||
command: NonNull<TypedCommand>,
|
||||
) -> bool {
|
||||
let command = unsafe { heap_remove(command) };
|
||||
unsafe { connection.as_ref().send(command) }.is_ok()
|
||||
}
|
||||
|
||||
/// 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));
|
||||
/// ```
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_udp_send_header(
|
||||
udp_connection: NonNull<UdpConnection>,
|
||||
header: Header,
|
||||
) -> bool {
|
||||
let packet = Packet {
|
||||
header,
|
||||
payload: vec![],
|
||||
};
|
||||
unsafe { udp_connection.as_ref() }.send(packet).is_ok()
|
||||
}
|
||||
|
||||
/// Closes and deallocates a [UdpConnection].
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_udp_free(connection: NonNull<UdpConnection>) {
|
||||
unsafe { heap_drop(connection) }
|
||||
}
|
||||
Loading…
Reference in a new issue