Further refinement

This commit is contained in:
Luke
2021-02-04 11:25:11 +13:00
parent 00bd556d7a
commit 39bbe33831
4 changed files with 69 additions and 82 deletions

View File

@@ -13,8 +13,13 @@ const APPLY: u8 = 0xc4;
// The next byte can be 0x03 for "on" and 0x00 for "off"
const ON_OFF: u8 = 0x04;
use rog_types::{anime_matrix::{ANIME_PANE1_PREFIX, ANIME_PANE2_PREFIX, AniMeDataBuffer, AniMeImageBuffer, AniMePacketType, PANE_LEN}, error::AuraError};
use log::{error, info, warn};
use rog_types::{
anime_matrix::{
AniMeDataBuffer, AniMeImageBuffer, AniMePacketType, ANIME_PANE1_PREFIX, ANIME_PANE2_PREFIX,
},
error::AuraError,
};
use rusb::{Device, DeviceHandle};
use std::convert::TryInto;
use std::error::Error;
@@ -35,30 +40,21 @@ impl GetSupported for CtrlAnimeDisplay {
}
}
#[allow(dead_code)]
#[derive(Debug)]
pub enum AnimatrixCommand {
Apply,
SetBoot(bool),
Write([u8; 640]),
WriteImage(AniMeImageBuffer),
//ReloadLast,
}
pub struct CtrlAnimeDisplay {
handle: DeviceHandle<rusb::GlobalContext>,
initialised: bool,
}
//AnimatrixWrite
pub trait Dbus {
fn write_image(&mut self, input: AniMeImageBuffer);
/// Write an image 34x56 pixels. Each pixel is 0-255 greyscale.
fn write_image(&self, input: AniMeImageBuffer);
fn write_direct(&mut self, input: AniMeDataBuffer);
/// Write a direct stream of data
fn write_direct(&self, input: AniMeDataBuffer);
fn set_on_off(&mut self, status: bool);
fn set_on_off(&self, status: bool);
fn set_boot_on_off(&mut self, status: bool);
fn set_boot_on_off(&self, status: bool);
}
impl crate::ZbusAdd for CtrlAnimeDisplay {
@@ -75,17 +71,19 @@ impl crate::ZbusAdd for CtrlAnimeDisplay {
#[dbus_interface(name = "org.asuslinux.Daemon")]
impl Dbus for CtrlAnimeDisplay {
fn write_image(&mut self, input: AniMeImageBuffer) {
/// Writes a 34x56 image
fn write_image(&self, input: AniMeImageBuffer) {
self.write_image_buffer(input)
.map_or_else(|err| warn!("{}", err), |()| info!("Writing image to Anime"));
}
fn write_direct(&mut self, input: AniMeDataBuffer) {
/// Writes a data stream of length
fn write_direct(&self, input: AniMeDataBuffer) {
self.write_data_buffer(input)
.map_or_else(|err| warn!("{}", err), |()| info!("Writing data to Anime"));
.map_or_else(|err| warn!("{}", err), |()| info!("Writing data to Anime"));
}
fn set_on_off(&mut self, status: bool) {
fn set_on_off(&self, status: bool) {
let mut buffer = [0u8; PACKET_SIZE];
buffer[0] = DEV_PAGE;
buffer[1] = WRITE;
@@ -100,21 +98,23 @@ impl Dbus for CtrlAnimeDisplay {
status_str = "off";
}
self.do_command(AnimatrixCommand::Write(buffer)).map_or_else(
self.write_bytes(&buffer).map_or_else(
|err| warn!("{}", err),
|()| info!("Turning {} the AniMe", status_str),
);
}
fn set_boot_on_off(&mut self, status: bool) {
fn set_boot_on_off(&self, status: bool) {
let status_str = if status { "on" } else { "off" };
self.do_command(AnimatrixCommand::SetBoot(status))
.and_then(|()| self.do_command(AnimatrixCommand::Apply))
.map_or_else(
|err| warn!("{}", err),
|()| info!("Turning {} the AniMe at boot/shutdown", status_str),
);
self.do_set_boot(status).map_or_else(
|err| warn!("{}", err),
|()| info!("Turning {} the AniMe at boot/shutdown", status_str),
);
self.do_apply().map_or_else(
|err| warn!("{}", err),
|()| info!("Turning {} the AniMe at boot/shutdown", status_str),
);
}
}
@@ -138,10 +138,12 @@ impl CtrlAnimeDisplay {
})?;
info!("Device has an AniMe Matrix display");
Ok(CtrlAnimeDisplay {
let ctrl = CtrlAnimeDisplay {
handle: device,
initialised: false,
})
};
ctrl.do_initialization()?;
Ok(ctrl)
}
#[inline]
@@ -155,22 +157,6 @@ impl CtrlAnimeDisplay {
Err(rusb::Error::NoDevice)
}
pub fn do_command(&mut self, command: AnimatrixCommand) -> Result<(), AuraError> {
if !self.initialised {
self.do_initialization()?
}
match command {
AnimatrixCommand::Apply => self.do_apply()?,
//AnimatrixCommand::Set => self.do_set_boot()?,
AnimatrixCommand::SetBoot(status) => self.do_set_boot(status)?,
AnimatrixCommand::Write(bytes) => self.write_bytes(&bytes)?,
AnimatrixCommand::WriteImage(effect) => self.write_image_buffer(effect)?,
//AnimatrixCommand::ReloadLast => self.reload_last_builtin(&config).await?,
}
Ok(())
}
/// Should only be used if the bytes you are writing are verified correct
#[inline]
fn write_bytes(&self, message: &[u8]) -> Result<(), AuraError> {
@@ -191,7 +177,7 @@ impl CtrlAnimeDisplay {
Ok(())
}
#[inline]
fn write_data_buffer(&mut self, buffer: AniMeDataBuffer) -> Result<(), AuraError> {
fn write_data_buffer(&self, buffer: AniMeDataBuffer) -> Result<(), AuraError> {
let mut image = AniMePacketType::from(buffer);
image[0][..7].copy_from_slice(&ANIME_PANE1_PREFIX);
image[1][..7].copy_from_slice(&ANIME_PANE2_PREFIX);
@@ -205,7 +191,7 @@ impl CtrlAnimeDisplay {
/// Write an Animatrix image
///
/// The expected input here is *two* Vectors, 640 bytes in length. The two vectors
/// The expected USB input here is *two* Vectors, 640 bytes in length. The two vectors
/// are each one half of the full image write.
///
/// After each write a flush is written, it is assumed that this tells the device to
@@ -219,7 +205,7 @@ impl CtrlAnimeDisplay {
///
/// Where led brightness is 0..255, low to high
#[inline]
fn write_image_buffer(&mut self, buffer: AniMeImageBuffer) -> Result<(), AuraError> {
fn write_image_buffer(&self, buffer: AniMeImageBuffer) -> Result<(), AuraError> {
let mut image = AniMePacketType::from(buffer);
image[0][..7].copy_from_slice(&ANIME_PANE1_PREFIX);
image[1][..7].copy_from_slice(&ANIME_PANE2_PREFIX);
@@ -232,7 +218,7 @@ impl CtrlAnimeDisplay {
}
#[inline]
fn do_initialization(&mut self) -> Result<(), AuraError> {
fn do_initialization(&self) -> Result<(), AuraError> {
let mut init = [0; PACKET_SIZE];
init[0] = DEV_PAGE; // This is the USB page we're using throughout
for (idx, byte) in INIT_STR.as_bytes().iter().enumerate() {
@@ -248,12 +234,11 @@ impl CtrlAnimeDisplay {
init[1] = INIT;
self.write_bytes(&init)?;
self.initialised = true;
Ok(())
}
#[inline]
fn do_flush(&mut self) -> Result<(), AuraError> {
fn do_flush(&self) -> Result<(), AuraError> {
let mut flush = [0; PACKET_SIZE];
flush[0] = DEV_PAGE;
flush[1] = WRITE;
@@ -264,7 +249,7 @@ impl CtrlAnimeDisplay {
}
#[inline]
fn do_set_boot(&mut self, status: bool) -> Result<(), AuraError> {
fn do_set_boot(&self, status: bool) -> Result<(), AuraError> {
let mut flush = [0; PACKET_SIZE];
flush[0] = DEV_PAGE;
flush[1] = SET;
@@ -276,7 +261,7 @@ impl CtrlAnimeDisplay {
}
#[inline]
fn do_apply(&mut self) -> Result<(), AuraError> {
fn do_apply(&self) -> Result<(), AuraError> {
let mut flush = [0; PACKET_SIZE];
flush[0] = DEV_PAGE;
flush[1] = APPLY;