add bitmap for uniffi, BitmapLinearWin

add uniffi BitmapLinearWin command
This commit is contained in:
Vinzenz Schroeter 2024-11-04 19:20:40 +01:00
parent 6e467502f2
commit f9c8d20654
9 changed files with 387 additions and 21 deletions

View file

@ -1,23 +1,24 @@
#!/usr/bin/env bash
set -x
set -e
cargo build --release
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
TARGETPATH="$(realpath $SCRIPTPATH/../../target/release/)"
SERVICEPOINT_SO="$TARGETPATH/libservicepoint_binding_uniffi.so"
CONFIG_TOML="$(realpath $SCRIPTPATH/../uniffi.toml)"
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
TARGET_PATH="$(realpath "$SCRIPT_PATH"/../../target/release)"
SERVICEPOINT_SO="$TARGET_PATH/libservicepoint_binding_uniffi.so"
LIBRARIES_PATH="$SCRIPT_PATH/libraries"
echo "Source: $SERVICEPOINT_SO"
echo "Output: $LIBRARIES_PATH"
BINDGEN="cargo run --features=uniffi/cli --bin uniffi-bindgen -- "
BINDGEN_CS="cargo run --features=cs --bin uniffi-bindgen-cs -- "
BINDGEN_GO="cargo run --features=go --bin uniffi-bindgen-go -- "
COMMON_ARGS="--library $SERVICEPOINT_SO"
${BINDGEN} generate $COMMON_ARGS --language python --out-dir libraries/python
${BINDGEN} generate $COMMON_ARGS --language kotlin --out-dir libraries/kotlin
${BINDGEN} generate $COMMON_ARGS --language swift --out-dir libraries/swift
${BINDGEN} generate $COMMON_ARGS --language ruby --out-dir libraries/ruby
${BINDGEN_CS} $COMMON_ARGS --out-dir libraries/csharp/ServicePoint
${BINDGEN_GO} $COMMON_ARGS --out-dir libraries/go/
${BINDGEN} generate $COMMON_ARGS --language python --out-dir "$LIBRARIES_PATH/python"
${BINDGEN} generate $COMMON_ARGS --language kotlin --out-dir "$LIBRARIES_PATH/kotlin"
${BINDGEN} generate $COMMON_ARGS --language swift --out-dir "$LIBRARIES_PATH/swift"
${BINDGEN} generate $COMMON_ARGS --language ruby --out-dir "$LIBRARIES_PATH/ruby"
${BINDGEN_CS} $COMMON_ARGS --out-dir "$LIBRARIES_PATH/csharp/ServicePoint"
${BINDGEN_GO} $COMMON_ARGS --out-dir "$LIBRARIES_PATH/go/"

View file

@ -0,0 +1,16 @@
namespace ServicePoint.Tests;
public class BitmapTests
{
[Fact]
public void BasicFunctions()
{
var bitmap = new Bitmap(8, 2);
Assert.False(bitmap.Get(0, 0));
Assert.False(bitmap.Get(bitmap.Width() - 1, bitmap.Height() - 1));
bitmap.Fill(true);
Assert.True(bitmap.Get(1, 1));
bitmap.Set(1, 1, false);
Assert.False(bitmap.Get(1, 1));
}
}

View file

@ -33,4 +33,10 @@ public class CommandTests
{
_connection.Send(Command.HardReset());
}
[Fact]
public void BitmapLinearWinSendable()
{
_connection.Send(Command.BitmapLinearWin(0, 0, Bitmap.NewMaxSized()));
}
}

View file

@ -428,11 +428,48 @@ static class _UniFFILib {
}
[DllImport("servicepoint_binding_uniffi")]
public static extern void uniffi_servicepoint_binding_uniffi_fn_free_bitmap(
IntPtr ptr,ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern BitmapSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_bitmap_new(ulong @width,ulong @height,ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern BitmapSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_bitmap_new_max_sized(ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern void uniffi_servicepoint_binding_uniffi_fn_method_bitmap_fill(BitmapSafeHandle @ptr,sbyte @value,ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern sbyte uniffi_servicepoint_binding_uniffi_fn_method_bitmap_get(BitmapSafeHandle @ptr,ulong @x,ulong @y,ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ulong uniffi_servicepoint_binding_uniffi_fn_method_bitmap_height(BitmapSafeHandle @ptr,ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern void uniffi_servicepoint_binding_uniffi_fn_method_bitmap_set(BitmapSafeHandle @ptr,ulong @x,ulong @y,sbyte @value,ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ulong uniffi_servicepoint_binding_uniffi_fn_method_bitmap_width(BitmapSafeHandle @ptr,ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern void uniffi_servicepoint_binding_uniffi_fn_free_command(
IntPtr ptr,ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern CommandSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_command_bitmap_linear_win(ulong @offsetX,ulong @offsetY,BitmapSafeHandle @bitmap,ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern CommandSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_command_brightness(byte @brightness,ref RustCallStatus _uniffi_out_err
);
@ -694,10 +731,42 @@ static class _UniFFILib {
public static extern void ffi_servicepoint_binding_uniffi_rust_future_complete_void(IntPtr @handle,ref RustCallStatus _uniffi_out_err
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_fill(
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_get(
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_height(
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_set(
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_width(
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_connection_send(
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_bitmap_new(
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_bitmap_new_max_sized(
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_command_bitmap_linear_win(
);
[DllImport("servicepoint_binding_uniffi")]
public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_command_brightness(
);
@ -736,12 +805,60 @@ static class _UniFFILib {
}
static void uniffiCheckApiChecksums() {
{
var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_fill();
if (checksum != 43887) {
throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_fill` checksum `43887`, library returned `{checksum}`");
}
}
{
var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_get();
if (checksum != 61136) {
throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_get` checksum `61136`, library returned `{checksum}`");
}
}
{
var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_height();
if (checksum != 44991) {
throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_height` checksum `44991`, library returned `{checksum}`");
}
}
{
var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_set();
if (checksum != 25290) {
throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_set` checksum `25290`, library returned `{checksum}`");
}
}
{
var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_width();
if (checksum != 30837) {
throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_bitmap_width` checksum `30837`, library returned `{checksum}`");
}
}
{
var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_connection_send();
if (checksum != 23796) {
throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_connection_send` checksum `23796`, library returned `{checksum}`");
}
}
{
var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_bitmap_new();
if (checksum != 49832) {
throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_bitmap_new` checksum `49832`, library returned `{checksum}`");
}
}
{
var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_bitmap_new_max_sized();
if (checksum != 63762) {
throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_bitmap_new_max_sized` checksum `63762`, library returned `{checksum}`");
}
}
{
var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_command_bitmap_linear_win();
if (checksum != 51700) {
throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_command_bitmap_linear_win` checksum `51700`, library returned `{checksum}`");
}
}
{
var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_command_brightness();
if (checksum != 11291) {
@ -814,6 +931,58 @@ class FfiConverterUInt8: FfiConverter<byte, byte> {
class FfiConverterUInt64: FfiConverter<ulong, ulong> {
public static FfiConverterUInt64 INSTANCE = new FfiConverterUInt64();
public override ulong Lift(ulong value) {
return value;
}
public override ulong Read(BigEndianStream stream) {
return stream.ReadULong();
}
public override ulong Lower(ulong value) {
return value;
}
public override int AllocationSize(ulong value) {
return 8;
}
public override void Write(ulong value, BigEndianStream stream) {
stream.WriteULong(value);
}
}
class FfiConverterBoolean: FfiConverter<bool, sbyte> {
public static FfiConverterBoolean INSTANCE = new FfiConverterBoolean();
public override bool Lift(sbyte value) {
return value != 0;
}
public override bool Read(BigEndianStream stream) {
return Lift(stream.ReadSByte());
}
public override sbyte Lower(bool value) {
return value ? (sbyte)1 : (sbyte)0;
}
public override int AllocationSize(bool value) {
return (sbyte)1;
}
public override void Write(bool value, BigEndianStream stream) {
stream.WriteSByte(Lower(value));
}
}
class FfiConverterString: FfiConverter<string, RustBuffer> {
public static FfiConverterString INSTANCE = new FfiConverterString();
@ -946,6 +1115,114 @@ static class FFIObjectUtil {
}
}
}
public interface IBitmap {
void Fill(bool @value);
bool Get(ulong @x, ulong @y);
ulong Height();
void Set(ulong @x, ulong @y, bool @value);
ulong Width();
}
public class BitmapSafeHandle: FFISafeHandle {
public BitmapSafeHandle(): base() {
}
public BitmapSafeHandle(IntPtr pointer): base(pointer) {
}
override protected bool ReleaseHandle() {
_UniffiHelpers.RustCall((ref RustCallStatus status) => {
_UniFFILib.uniffi_servicepoint_binding_uniffi_fn_free_bitmap(this.handle, ref status);
});
return true;
}
}
public class Bitmap: FFIObject<BitmapSafeHandle>, IBitmap {
public Bitmap(BitmapSafeHandle pointer): base(pointer) {}
public Bitmap(ulong @width, ulong @height) :
this(
_UniffiHelpers.RustCall( (ref RustCallStatus _status) =>
_UniFFILib.uniffi_servicepoint_binding_uniffi_fn_constructor_bitmap_new(FfiConverterUInt64.INSTANCE.Lower(@width), FfiConverterUInt64.INSTANCE.Lower(@height), ref _status)
)) {}
public void Fill(bool @value) {
_UniffiHelpers.RustCall( (ref RustCallStatus _status) =>
_UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_bitmap_fill(this.GetHandle(), FfiConverterBoolean.INSTANCE.Lower(@value), ref _status)
);
}
public bool Get(ulong @x, ulong @y) {
return FfiConverterBoolean.INSTANCE.Lift(
_UniffiHelpers.RustCall( (ref RustCallStatus _status) =>
_UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_bitmap_get(this.GetHandle(), FfiConverterUInt64.INSTANCE.Lower(@x), FfiConverterUInt64.INSTANCE.Lower(@y), ref _status)
));
}
public ulong Height() {
return FfiConverterUInt64.INSTANCE.Lift(
_UniffiHelpers.RustCall( (ref RustCallStatus _status) =>
_UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_bitmap_height(this.GetHandle(), ref _status)
));
}
public void Set(ulong @x, ulong @y, bool @value) {
_UniffiHelpers.RustCall( (ref RustCallStatus _status) =>
_UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_bitmap_set(this.GetHandle(), FfiConverterUInt64.INSTANCE.Lower(@x), FfiConverterUInt64.INSTANCE.Lower(@y), FfiConverterBoolean.INSTANCE.Lower(@value), ref _status)
);
}
public ulong Width() {
return FfiConverterUInt64.INSTANCE.Lift(
_UniffiHelpers.RustCall( (ref RustCallStatus _status) =>
_UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_bitmap_width(this.GetHandle(), ref _status)
));
}
public static Bitmap NewMaxSized() {
return new Bitmap(
_UniffiHelpers.RustCall( (ref RustCallStatus _status) =>
_UniFFILib.uniffi_servicepoint_binding_uniffi_fn_constructor_bitmap_new_max_sized( ref _status)
));
}
}
class FfiConverterTypeBitmap: FfiConverter<Bitmap, BitmapSafeHandle> {
public static FfiConverterTypeBitmap INSTANCE = new FfiConverterTypeBitmap();
public override BitmapSafeHandle Lower(Bitmap value) {
return value.GetHandle();
}
public override Bitmap Lift(BitmapSafeHandle value) {
return new Bitmap(value);
}
public override Bitmap Read(BigEndianStream stream) {
return Lift(new BitmapSafeHandle(new IntPtr(stream.ReadLong())));
}
public override int AllocationSize(Bitmap value) {
return 8;
}
public override void Write(Bitmap value, BigEndianStream stream) {
stream.WriteLong(Lower(value).DangerousGetRawFfiValue().ToInt64());
}
}
public interface ICommand {
}
@ -968,6 +1245,13 @@ public class Command: FFIObject<CommandSafeHandle>, ICommand {
public static Command BitmapLinearWin(ulong @offsetX, ulong @offsetY, Bitmap @bitmap) {
return new Command(
_UniffiHelpers.RustCall( (ref RustCallStatus _status) =>
_UniFFILib.uniffi_servicepoint_binding_uniffi_fn_constructor_command_bitmap_linear_win(FfiConverterUInt64.INSTANCE.Lower(@offsetX), FfiConverterUInt64.INSTANCE.Lower(@offsetY), FfiConverterTypeBitmap.INSTANCE.Lower(@bitmap), ref _status)
));
}
/// <exception cref="ServicePointException"></exception>
public static Command Brightness(byte @brightness) {
return new Command(

View file

@ -0,0 +1,45 @@
use std::sync::{Arc, RwLock};
use servicepoint::Grid;
#[derive(uniffi::Object)]
pub struct Bitmap {
pub(crate) actual: RwLock<servicepoint::Bitmap>,
}
impl Bitmap {
fn internal_new(actual: servicepoint::Bitmap)-> Arc<Self> {
Arc::new(Self { actual: RwLock::new(actual) })
}
}
#[uniffi::export]
impl Bitmap {
#[uniffi::constructor]
pub fn new(width: u64, height: u64) -> Arc<Self> {
Self::internal_new(servicepoint::Bitmap::new(width as usize, height as usize))
}
#[uniffi::constructor]
pub fn new_max_sized() -> Arc<Self> {
Self::internal_new(servicepoint::Bitmap::max_sized())
}
pub fn set(&self, x: u64, y: u64, value: bool) {
self.actual.write().unwrap().set(x as usize, y as usize, value)
}
pub fn get(&self, x: u64, y: u64) -> bool {
self.actual.read().unwrap().get(x as usize, y as usize)
}
pub fn fill(&self, value: bool) {
self.actual.write().unwrap().fill(value)
}
pub fn width(&self) -> u64 {
self.actual.read().unwrap().width() as u64
}
pub fn height(&self) -> u64 {
self.actual.read().unwrap().height() as u64
}
}

View file

@ -1,4 +1,6 @@
use std::sync::Arc;
use servicepoint::{CompressionCode, Origin};
use crate::bitmap::Bitmap;
use crate::errors::ServicePointError;
#[derive(uniffi::Object)]
@ -6,15 +8,17 @@ pub struct Command {
pub(crate)actual: servicepoint::Command
}
fn actual_into_arc(actual: servicepoint::Command) -> Arc<Command> {
Arc::new(Command { actual })
impl Command {
fn internal_new(actual: servicepoint::Command)-> Arc<Command> {
Arc::new(Command { actual })
}
}
#[uniffi::export]
impl Command {
#[uniffi::constructor]
pub fn clear() -> Arc<Self> {
actual_into_arc(servicepoint::Command::Clear)
Self::internal_new(servicepoint::Command::Clear)
}
#[uniffi::constructor]
@ -22,16 +26,25 @@ impl Command {
servicepoint::Brightness::try_from(brightness)
.map_err(move |value| ServicePointError::InvalidBrightness{value})
.map(servicepoint::Command::Brightness)
.map(actual_into_arc)
.map(Self::internal_new)
}
#[uniffi::constructor]
pub fn fade_out() -> Arc<Self> {
actual_into_arc(servicepoint::Command::FadeOut)
Self::internal_new(servicepoint::Command::FadeOut)
}
#[uniffi::constructor]
pub fn hard_reset() -> Arc<Self> {
actual_into_arc(servicepoint::Command::HardReset)
Self::internal_new(servicepoint::Command::HardReset)
}
#[uniffi::constructor]
pub fn bitmap_linear_win(offset_x: u64, offset_y: u64, bitmap: &Arc<Bitmap>) -> Arc<Self> {
let origin = Origin::new(offset_x as usize, offset_y as usize);
let bitmap = bitmap.actual.read().unwrap().clone();
// TODO: compression codes
let actual = servicepoint::Command::BitmapLinearWin(origin, bitmap, CompressionCode::Uncompressed);
Self::internal_new(actual)
}
}

View file

@ -14,7 +14,7 @@ impl Connection {
pub fn new(host: String) -> Result<Arc<Self>, ServicePointError> {
servicepoint::Connection::open(host)
.map(|actual| Arc::new(Connection { actual }))
.map_err(|err| ServicePointError::IOError { error: err.to_string() })
.map_err(|err| ServicePointError::IoError { error: err.to_string() })
}
#[uniffi::constructor]
@ -24,6 +24,6 @@ impl Connection {
pub fn send(&self, command: Arc<Command>) -> Result<(), ServicePointError> {
self.actual.send(command.actual.clone())
.map_err(|err| ServicePointError::IOError { error: format!("{err:?}") })
.map_err(|err| ServicePointError::IoError { error: format!("{err:?}") })
}
}

View file

@ -2,7 +2,7 @@
#[derive(uniffi::Error, thiserror::Error, Debug)]
pub enum ServicePointError {
#[error("An IO error occurred: {error}")]
IOError {error: String},
IoError {error: String},
#[error("The specified brightness value {value} is out of range")]
InvalidBrightness {value:u8},
}

View file

@ -3,3 +3,4 @@ uniffi::setup_scaffolding!();
mod command;
mod connection;
mod errors;
mod bitmap;