From 18f1ccb2dc7dd9d7f9a3a180e5408dfdbb605475 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 4 Nov 2024 22:06:30 +0100 Subject: [PATCH] add uniffi brightness grid and related command --- .../servicepoint_binding_uniffi.cs | 232 ++++++++++++++++++ .../src/brightness_grid.rs | 65 +++++ .../src/command.rs | 14 ++ crates/servicepoint_binding_uniffi/src/lib.rs | 1 + 4 files changed, 312 insertions(+) create mode 100644 crates/servicepoint_binding_uniffi/src/brightness_grid.rs diff --git a/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint/servicepoint_binding_uniffi.cs b/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint/servicepoint_binding_uniffi.cs index 9aaf721..a35e75f 100644 --- a/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint/servicepoint_binding_uniffi.cs +++ b/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint/servicepoint_binding_uniffi.cs @@ -494,6 +494,39 @@ static class _UniFFILib { 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_brightnessgrid( + IntPtr ptr,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern BrightnessGridSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_brightnessgrid_load(ulong @width,ulong @height,RustBuffer @data,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern BrightnessGridSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_brightnessgrid_new(ulong @width,ulong @height,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern void uniffi_servicepoint_binding_uniffi_fn_method_brightnessgrid_fill(BrightnessGridSafeHandle @ptr,byte @value,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern byte uniffi_servicepoint_binding_uniffi_fn_method_brightnessgrid_get(BrightnessGridSafeHandle @ptr,ulong @x,ulong @y,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ulong uniffi_servicepoint_binding_uniffi_fn_method_brightnessgrid_height(BrightnessGridSafeHandle @ptr,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern void uniffi_servicepoint_binding_uniffi_fn_method_brightnessgrid_set(BrightnessGridSafeHandle @ptr,ulong @x,ulong @y,byte @value,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ulong uniffi_servicepoint_binding_uniffi_fn_method_brightnessgrid_width(BrightnessGridSafeHandle @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 @@ -523,6 +556,10 @@ static class _UniFFILib { public static extern CommandSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_command_brightness(byte @brightness,ref RustCallStatus _uniffi_out_err ); + [DllImport("servicepoint_binding_uniffi")] + public static extern CommandSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_command_char_brightness(ulong @offsetX,ulong @offsetY,BrightnessGridSafeHandle @grid,ref RustCallStatus _uniffi_out_err + ); + [DllImport("servicepoint_binding_uniffi")] public static extern CommandSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_command_clear(ref RustCallStatus _uniffi_out_err ); @@ -816,6 +853,26 @@ static class _UniFFILib { 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_brightnessgrid_fill( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_get( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_height( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_set( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_width( + ); + [DllImport("servicepoint_binding_uniffi")] public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_connection_send( ); @@ -840,6 +897,14 @@ static class _UniFFILib { 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_brightnessgrid_load( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_brightnessgrid_new( + ); + [DllImport("servicepoint_binding_uniffi")] public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_command_bitmap_linear( ); @@ -864,6 +929,10 @@ static class _UniFFILib { public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_command_brightness( ); + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_command_char_brightness( + ); + [DllImport("servicepoint_binding_uniffi")] public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_command_clear( ); @@ -952,6 +1021,36 @@ static class _UniFFILib { 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_brightnessgrid_fill(); + if (checksum != 63376) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_fill` checksum `63376`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_get(); + if (checksum != 28736) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_get` checksum `28736`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_height(); + if (checksum != 39528) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_height` checksum `39528`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_set(); + if (checksum != 6330) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_set` checksum `6330`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_width(); + if (checksum != 26384) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_width` checksum `26384`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_connection_send(); if (checksum != 23796) { @@ -988,6 +1087,18 @@ static class _UniFFILib { 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_brightnessgrid_load(); + if (checksum != 24788) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_brightnessgrid_load` checksum `24788`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_brightnessgrid_new(); + if (checksum != 4979) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_brightnessgrid_new` checksum `4979`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_command_bitmap_linear(); if (checksum != 14881) { @@ -1024,6 +1135,12 @@ static class _UniFFILib { throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_command_brightness` checksum `11291`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_command_char_brightness(); + if (checksum != 29467) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_command_char_brightness` checksum `29467`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_command_clear(); if (checksum != 11035) { @@ -1509,6 +1626,114 @@ class FfiConverterTypeBitmap: FfiConverter { +public interface IBrightnessGrid { + + void Fill(byte @value); + + byte Get(ulong @x, ulong @y); + + ulong Height(); + + void Set(ulong @x, ulong @y, byte @value); + + ulong Width(); + +} + +public class BrightnessGridSafeHandle: FFISafeHandle { + public BrightnessGridSafeHandle(): base() { + } + public BrightnessGridSafeHandle(IntPtr pointer): base(pointer) { + } + override protected bool ReleaseHandle() { + _UniffiHelpers.RustCall((ref RustCallStatus status) => { + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_free_brightnessgrid(this.handle, ref status); + }); + return true; + } +} +public class BrightnessGrid: FFIObject, IBrightnessGrid { + public BrightnessGrid(BrightnessGridSafeHandle pointer): base(pointer) {} + public BrightnessGrid(ulong @width, ulong @height) : + this( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_constructor_brightnessgrid_new(FfiConverterUInt64.INSTANCE.Lower(@width), FfiConverterUInt64.INSTANCE.Lower(@height), ref _status) +)) {} + + + public void Fill(byte @value) { + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_brightnessgrid_fill(this.GetHandle(), FfiConverterUInt8.INSTANCE.Lower(@value), ref _status) +); + } + + + public byte Get(ulong @x, ulong @y) { + return FfiConverterUInt8.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_brightnessgrid_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_brightnessgrid_height(this.GetHandle(), ref _status) +)); + } + + public void Set(ulong @x, ulong @y, byte @value) { + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_brightnessgrid_set(this.GetHandle(), FfiConverterUInt64.INSTANCE.Lower(@x), FfiConverterUInt64.INSTANCE.Lower(@y), FfiConverterUInt8.INSTANCE.Lower(@value), ref _status) +); + } + + + public ulong Width() { + return FfiConverterUInt64.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_brightnessgrid_width(this.GetHandle(), ref _status) +)); + } + + + + public static BrightnessGrid Load(ulong @width, ulong @height, byte[] @data) { + return new BrightnessGrid( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_constructor_brightnessgrid_load(FfiConverterUInt64.INSTANCE.Lower(@width), FfiConverterUInt64.INSTANCE.Lower(@height), FfiConverterByteArray.INSTANCE.Lower(@data), ref _status) +)); + } + + +} + +class FfiConverterTypeBrightnessGrid: FfiConverter { + public static FfiConverterTypeBrightnessGrid INSTANCE = new FfiConverterTypeBrightnessGrid(); + + public override BrightnessGridSafeHandle Lower(BrightnessGrid value) { + return value.GetHandle(); + } + + public override BrightnessGrid Lift(BrightnessGridSafeHandle value) { + return new BrightnessGrid(value); + } + + public override BrightnessGrid Read(BigEndianStream stream) { + return Lift(new BrightnessGridSafeHandle(new IntPtr(stream.ReadLong()))); + } + + public override int AllocationSize(BrightnessGrid value) { + return 8; + } + + public override void Write(BrightnessGrid value, BigEndianStream stream) { + stream.WriteLong(Lower(value).DangerousGetRawFfiValue().ToInt64()); + } +} + + + public interface ICommand { } @@ -1574,6 +1799,13 @@ public class Command: FFIObject, ICommand { )); } + public static Command CharBrightness(ulong @offsetX, ulong @offsetY, BrightnessGrid @grid) { + return new Command( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_constructor_command_char_brightness(FfiConverterUInt64.INSTANCE.Lower(@offsetX), FfiConverterUInt64.INSTANCE.Lower(@offsetY), FfiConverterTypeBrightnessGrid.INSTANCE.Lower(@grid), ref _status) +)); + } + public static Command Clear() { return new Command( _UniffiHelpers.RustCall( (ref RustCallStatus _status) => diff --git a/crates/servicepoint_binding_uniffi/src/brightness_grid.rs b/crates/servicepoint_binding_uniffi/src/brightness_grid.rs new file mode 100644 index 0000000..ee97c53 --- /dev/null +++ b/crates/servicepoint_binding_uniffi/src/brightness_grid.rs @@ -0,0 +1,65 @@ +use servicepoint::{Brightness, Grid}; +use std::sync::{Arc, RwLock}; + +#[derive(uniffi::Object)] +pub struct BrightnessGrid { + pub(crate) actual: RwLock, +} + +impl BrightnessGrid { + fn internal_new(actual: servicepoint::BrightnessGrid) -> Arc { + Arc::new(Self { + actual: RwLock::new(actual), + }) + } +} + +#[uniffi::export] +impl BrightnessGrid { + #[uniffi::constructor] + pub fn new(width: u64, height: u64) -> Arc { + Self::internal_new(servicepoint::BrightnessGrid::new( + width as usize, + height as usize, + )) + } + + #[uniffi::constructor] + pub fn load(width: u64, height: u64, data: Vec) -> Arc { + Self::internal_new(servicepoint::BrightnessGrid::saturating_load( + width as usize, + height as usize, + &data, + )) + } + + pub fn set(&self, x: u64, y: u64, value: u8) { + self.actual.write().unwrap().set( + x as usize, + y as usize, + Brightness::saturating_from(value), + ) + } + + pub fn get(&self, x: u64, y: u64) -> u8 { + self.actual + .read() + .unwrap() + .get(x as usize, y as usize) + .into() + } + + pub fn fill(&self, value: u8) { + self.actual + .write() + .unwrap() + .fill(Brightness::saturating_from(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 + } +} diff --git a/crates/servicepoint_binding_uniffi/src/command.rs b/crates/servicepoint_binding_uniffi/src/command.rs index ad4be53..1cd3b7d 100644 --- a/crates/servicepoint_binding_uniffi/src/command.rs +++ b/crates/servicepoint_binding_uniffi/src/command.rs @@ -1,5 +1,6 @@ use crate::bitmap::Bitmap; use crate::bitvec::BitVec; +use crate::brightness_grid::BrightnessGrid; use crate::errors::ServicePointError; use servicepoint::{CompressionCode, Origin}; use std::sync::Arc; @@ -59,6 +60,18 @@ impl Command { Self::internal_new(actual) } + #[uniffi::constructor] + pub fn char_brightness( + offset_x: u64, + offset_y: u64, + grid: &Arc, + ) -> Arc { + let origin = Origin::new(offset_x as usize, offset_y as usize); + let grid = grid.actual.read().unwrap().clone(); + let actual = servicepoint::Command::CharBrightness(origin, grid); + Self::internal_new(actual) + } + #[uniffi::constructor] pub fn bitmap_linear(offset: u64, bitmap: &Arc) -> Arc { let bitmap = bitmap.actual.read().unwrap().clone(); @@ -94,6 +107,7 @@ impl Command { ); Self::internal_new(actual) } + #[uniffi::constructor] pub fn bitmap_linear_xor(offset: u64, bitmap: &Arc) -> Arc { let bitmap = bitmap.actual.read().unwrap().clone(); diff --git a/crates/servicepoint_binding_uniffi/src/lib.rs b/crates/servicepoint_binding_uniffi/src/lib.rs index 865a82e..89823ad 100644 --- a/crates/servicepoint_binding_uniffi/src/lib.rs +++ b/crates/servicepoint_binding_uniffi/src/lib.rs @@ -2,6 +2,7 @@ uniffi::setup_scaffolding!(); mod bitmap; mod bitvec; +mod brightness_grid; mod command; mod connection; mod errors;