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

@ -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;