make BitVec type alias pub, uniffi bitvec

original c# example equivalent works

add uniffi bitvec

original c# example now works with a few tweaks via uniffi
This commit is contained in:
Vinzenz Schroeter 2024-11-04 21:06:01 +01:00
parent f9c8d20654
commit 960f12ebc5
8 changed files with 482 additions and 26 deletions

View file

@ -1,5 +1,5 @@
use std::sync::{Arc, RwLock};
use servicepoint::Grid;
use std::sync::{Arc, RwLock};
#[derive(uniffi::Object)]
pub struct Bitmap {
@ -7,8 +7,10 @@ pub struct Bitmap {
}
impl Bitmap {
fn internal_new(actual: servicepoint::Bitmap)-> Arc<Self> {
Arc::new(Self { actual: RwLock::new(actual) })
fn internal_new(actual: servicepoint::Bitmap) -> Arc<Self> {
Arc::new(Self {
actual: RwLock::new(actual),
})
}
}
@ -16,7 +18,10 @@ impl Bitmap {
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))
Self::internal_new(servicepoint::Bitmap::new(
width as usize,
height as usize,
))
}
#[uniffi::constructor]
@ -24,8 +29,20 @@ impl Bitmap {
Self::internal_new(servicepoint::Bitmap::max_sized())
}
#[uniffi::constructor]
pub fn load(width: u64, height: u64, data: Vec<u8>) -> Arc<Self> {
Self::internal_new(servicepoint::Bitmap::load(
width as usize,
height as usize,
&data,
))
}
pub fn set(&self, x: u64, y: u64, value: bool) {
self.actual.write().unwrap().set(x as usize, y as usize, value)
self.actual
.write()
.unwrap()
.set(x as usize, y as usize, value)
}
pub fn get(&self, x: u64, y: u64) -> bool {
@ -38,7 +55,7 @@ impl Bitmap {
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

@ -0,0 +1,46 @@
use std::sync::{Arc, RwLock};
#[derive(uniffi::Object)]
pub struct BitVec {
pub(crate) actual: RwLock<servicepoint::BitVec>,
}
impl BitVec {
fn internal_new(actual: servicepoint::BitVec) -> Arc<Self> {
Arc::new(Self {
actual: RwLock::new(actual),
})
}
}
#[uniffi::export]
impl BitVec {
#[uniffi::constructor]
pub fn new(size: u64) -> Arc<Self> {
Self::internal_new(servicepoint::BitVec::repeat(false, size as usize))
}
#[uniffi::constructor]
pub fn load(data: Vec<u8>) -> Arc<Self> {
Self::internal_new(servicepoint::BitVec::from_slice(&data))
}
pub fn set(&self, index: u64, value: bool) {
self.actual.write().unwrap().set(index as usize, value)
}
pub fn get(&self, index: u64) -> bool {
self.actual
.read()
.unwrap()
.get(index as usize)
.is_some_and(move |bit| *bit)
}
pub fn fill(&self, value: bool) {
self.actual.write().unwrap().fill(value)
}
pub fn len(&self) -> u64 {
self.actual.read().unwrap().len() as u64
}
}

View file

@ -1,15 +1,16 @@
use std::sync::Arc;
use servicepoint::{CompressionCode, Origin};
use crate::bitmap::Bitmap;
use crate::bitvec::BitVec;
use crate::errors::ServicePointError;
use servicepoint::{CompressionCode, Origin};
use std::sync::Arc;
#[derive(uniffi::Object)]
pub struct Command {
pub(crate)actual: servicepoint::Command
pub(crate) actual: servicepoint::Command,
}
impl Command {
fn internal_new(actual: servicepoint::Command)-> Arc<Command> {
fn internal_new(actual: servicepoint::Command) -> Arc<Command> {
Arc::new(Command { actual })
}
}
@ -24,7 +25,9 @@ impl Command {
#[uniffi::constructor]
pub fn brightness(brightness: u8) -> Result<Arc<Self>, ServicePointError> {
servicepoint::Brightness::try_from(brightness)
.map_err(move |value| ServicePointError::InvalidBrightness{value})
.map_err(move |value| ServicePointError::InvalidBrightness {
value,
})
.map(servicepoint::Command::Brightness)
.map(Self::internal_new)
}
@ -38,13 +41,68 @@ impl Command {
pub fn hard_reset() -> Arc<Self> {
Self::internal_new(servicepoint::Command::HardReset)
}
#[uniffi::constructor]
pub fn bitmap_linear_win(offset_x: u64, offset_y: u64, bitmap: &Arc<Bitmap>) -> Arc<Self> {
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);
let actual = servicepoint::Command::BitmapLinearWin(
origin,
bitmap,
CompressionCode::Uncompressed,
);
Self::internal_new(actual)
}
#[uniffi::constructor]
pub fn bitmap_linear(offset: u64, bitmap: &Arc<BitVec>) -> Arc<Self> {
let bitmap = bitmap.actual.read().unwrap().clone();
// TODO: compression codes
let actual = servicepoint::Command::BitmapLinear(
offset as usize,
bitmap,
CompressionCode::Uncompressed,
);
Self::internal_new(actual)
}
#[uniffi::constructor]
pub fn bitmap_linear_and(offset: u64, bitmap: &Arc<BitVec>) -> Arc<Self> {
let bitmap = bitmap.actual.read().unwrap().clone();
// TODO: compression codes
let actual = servicepoint::Command::BitmapLinearAnd(
offset as usize,
bitmap,
CompressionCode::Uncompressed,
);
Self::internal_new(actual)
}
#[uniffi::constructor]
pub fn bitmap_linear_or(offset: u64, bitmap: &Arc<BitVec>) -> Arc<Self> {
let bitmap = bitmap.actual.read().unwrap().clone();
// TODO: compression codes
let actual = servicepoint::Command::BitmapLinearOr(
offset as usize,
bitmap,
CompressionCode::Uncompressed,
);
Self::internal_new(actual)
}
#[uniffi::constructor]
pub fn bitmap_linear_xor(offset: u64, bitmap: &Arc<BitVec>) -> Arc<Self> {
let bitmap = bitmap.actual.read().unwrap().clone();
// TODO: compression codes
let actual = servicepoint::Command::BitmapLinearXor(
offset as usize,
bitmap,
CompressionCode::Uncompressed,
);
Self::internal_new(actual)
}
}

View file

@ -14,16 +14,23 @@ 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]
pub fn new_fake() -> Arc<Self> {
Arc::new(Self { actual: servicepoint::Connection::Fake })
Arc::new(Self {
actual: servicepoint::Connection::Fake,
})
}
pub fn send(&self, command: Arc<Command>) -> Result<(), ServicePointError> {
self.actual.send(command.actual.clone())
.map_err(|err| ServicePointError::IoError { error: format!("{err:?}") })
self.actual.send(command.actual.clone()).map_err(|err| {
ServicePointError::IoError {
error: format!("{err:?}"),
}
})
}
}

View file

@ -1,8 +1,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},
}
InvalidBrightness { value: u8 },
}

View file

@ -1,6 +1,7 @@
uniffi::setup_scaffolding!();
mod bitmap;
mod bitvec;
mod command;
mod connection;
mod errors;
mod bitmap;