Complete anime diagonal gif support for GA402

This commit is contained in:
I-Al-Istannen
2022-07-10 13:23:21 +02:00
committed by Luke D. Jones
parent f3f6fadfe2
commit a2c8a226a4
10 changed files with 69 additions and 81 deletions

View File

@@ -268,7 +268,7 @@ fn handle_anime(
std::process::exit(1); std::process::exit(1);
} }
let matrix = AnimeDiagonal::from_png(Path::new(&image.path), None, image.bright)?; let matrix = AnimeDiagonal::from_png(Path::new(&image.path), None, image.bright, anime_type)?;
dbus.proxies() dbus.proxies()
.anime() .anime()

View File

@@ -80,8 +80,7 @@ pub struct AnimeConfigCached {
} }
impl AnimeConfigCached { impl AnimeConfigCached {
pub fn init_from_config(&mut self, config: &AnimeConfig) -> Result<(), AnimeError> { pub fn init_from_config(&mut self, config: &AnimeConfig, anime_type: AnimeType) -> Result<(), AnimeError> {
let anime_type = AnimeType::GA401; // TODO: detect
let mut sys = Vec::with_capacity(config.system.len()); let mut sys = Vec::with_capacity(config.system.len());
for ani in config.system.iter() { for ani in config.system.iter() {
sys.push(ActionData::from_anime_action(anime_type, ani)?); sys.push(ActionData::from_anime_action(anime_type, ani)?);

View File

@@ -11,8 +11,7 @@ use rog_anime::{
find_node, get_anime_type, pkt_for_apply, pkt_for_flush, pkt_for_set_boot, pkt_for_set_on, find_node, get_anime_type, pkt_for_apply, pkt_for_flush, pkt_for_set_boot, pkt_for_set_on,
pkts_for_init, PROD_ID, VENDOR_ID, pkts_for_init, PROD_ID, VENDOR_ID,
}, },
ActionData, AnimeDataBuffer, AnimePacketType, AnimeType, ANIME_GA401_DATA_LEN, ActionData, AnimeDataBuffer, AnimePacketType, AnimeType,
ANIME_GA402_DATA_LEN,
}; };
use rog_supported::AnimeSupportedFunctions; use rog_supported::AnimeSupportedFunctions;
use rusb::{Device, DeviceHandle}; use rusb::{Device, DeviceHandle};
@@ -61,7 +60,7 @@ impl CtrlAnime {
info!("Device has an AniMe Matrix display"); info!("Device has an AniMe Matrix display");
let mut cache = AnimeConfigCached::default(); let mut cache = AnimeConfigCached::default();
cache.init_from_config(&config)?; cache.init_from_config(&config, anime_type)?;
let ctrl = CtrlAnime { let ctrl = CtrlAnime {
_node: node, _node: node,
@@ -202,16 +201,10 @@ impl CtrlAnime {
} }
// Clear the display on exit // Clear the display on exit
if let Ok(lock) = inner.try_lock() { if let Ok(lock) = inner.try_lock() {
let data = match anime_type { let data = AnimeDataBuffer::from_vec(
AnimeType::GA401 => AnimeDataBuffer::from_vec( anime_type,
anime_type, vec![0u8; anime_type.data_length()],
[0u8; ANIME_GA401_DATA_LEN].to_vec(), );
),
AnimeType::GA402 => AnimeDataBuffer::from_vec(
anime_type,
[0u8; ANIME_GA402_DATA_LEN].to_vec(),
),
};
lock.write_data_buffer(data); lock.write_data_buffer(data);
} }
// Loop ended, set the atmonics // Loop ended, set the atmonics

View File

@@ -16,9 +16,6 @@ const BLOCK_START: usize = 7;
const BLOCK_END: usize = 634; const BLOCK_END: usize = 634;
/// Individual usable data length of each USB packet /// Individual usable data length of each USB packet
const PANE_LEN: usize = BLOCK_END - BLOCK_START; const PANE_LEN: usize = BLOCK_END - BLOCK_START;
/// The length of usable data
pub const ANIME_GA401_DATA_LEN: usize = PANE_LEN * 2;
pub const ANIME_GA402_DATA_LEN: usize = PANE_LEN * 3;
/// First packet is for GA401 + GA402 /// First packet is for GA401 + GA402
const USB_PREFIX1: [u8; 7] = [0x5e, 0xc0, 0x02, 0x01, 0x00, 0x73, 0x02]; const USB_PREFIX1: [u8; 7] = [0x5e, 0xc0, 0x02, 0x01, 0x00, 0x73, 0x02];
@@ -42,6 +39,32 @@ pub enum AnimeType {
GA402, GA402,
} }
impl AnimeType {
/// The width of diagonal images
pub fn width(&self) -> usize {
match self {
AnimeType::GA401 => 74,
AnimeType::GA402 => 74,
}
}
/// The height of diagonal images
pub fn height(&self) -> usize {
match self {
AnimeType::GA401 => 32,
AnimeType::GA402 => 39,
}
}
/// The length of usable data for this type
pub fn data_length(&self) -> usize {
match self {
AnimeType::GA401 => PANE_LEN * 2,
AnimeType::GA402 => PANE_LEN * 3,
}
}
}
/// The minimal serializable data that can be transferred over wire types. /// The minimal serializable data that can be transferred over wire types.
/// Other data structures in `rog_anime` will convert to this. /// Other data structures in `rog_anime` will convert to this.
#[cfg_attr(feature = "dbus", derive(Type))] #[cfg_attr(feature = "dbus", derive(Type))]
@@ -54,10 +77,7 @@ pub struct AnimeDataBuffer {
impl AnimeDataBuffer { impl AnimeDataBuffer {
#[inline] #[inline]
pub fn new(anime: AnimeType) -> Self { pub fn new(anime: AnimeType) -> Self {
let len = match anime { let len = anime.data_length();
AnimeType::GA401 => ANIME_GA401_DATA_LEN,
AnimeType::GA402 => ANIME_GA402_DATA_LEN,
};
AnimeDataBuffer { AnimeDataBuffer {
data: vec![0u8; len], data: vec![0u8; len],
@@ -83,10 +103,8 @@ impl AnimeDataBuffer {
/// Will panic if the vector length is not `ANIME_DATA_LEN` /// Will panic if the vector length is not `ANIME_DATA_LEN`
#[inline] #[inline]
pub fn from_vec(anime: AnimeType, data: Vec<u8>) -> Self { pub fn from_vec(anime: AnimeType, data: Vec<u8>) -> Self {
match anime { assert_eq!(data.len(), anime.data_length());
AnimeType::GA401 => assert_eq!(data.len(), ANIME_GA401_DATA_LEN),
AnimeType::GA402 => assert_eq!(data.len(), ANIME_GA402_DATA_LEN),
}
Self { data, anime } Self { data, anime }
} }
} }
@@ -97,15 +115,11 @@ pub type AnimePacketType = Vec<[u8; 640]>;
impl From<AnimeDataBuffer> for AnimePacketType { impl From<AnimeDataBuffer> for AnimePacketType {
#[inline] #[inline]
fn from(anime: AnimeDataBuffer) -> Self { fn from(anime: AnimeDataBuffer) -> Self {
assert_eq!(anime.data.len(), anime.anime.data_length());
let mut buffers = match anime.anime { let mut buffers = match anime.anime {
AnimeType::GA401 => { AnimeType::GA401 => vec![[0; 640]; 2],
assert!(anime.data.len() == ANIME_GA401_DATA_LEN); AnimeType::GA402 => vec![[0; 640]; 3],
vec![[0; 640]; 2]
}
AnimeType::GA402 => {
assert!(anime.data.len() == ANIME_GA402_DATA_LEN);
vec![[0; 640]; 3]
}
}; };
for (idx, chunk) in anime.data.as_slice().chunks(PANE_LEN).enumerate() { for (idx, chunk) in anime.data.as_slice().chunks(PANE_LEN).enumerate() {

View File

@@ -1,42 +1,31 @@
use std::{path::Path, time::Duration}; use std::{path::Path, time::Duration};
use crate::{ use crate::{data::AnimeDataBuffer, error::AnimeError, AnimeType};
data::{AnimeDataBuffer, ANIME_GA401_DATA_LEN},
error::AnimeError,
AnimeType, ANIME_GA402_DATA_LEN,
};
const WIDTH: usize = 74;
// TODO: Change for GA402
const HEIGHT: usize = 36;
/// Mostly intended to be used with ASUS gifs, but can be used for other purposes (like images) /// Mostly intended to be used with ASUS gifs, but can be used for other purposes (like images)
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AnimeDiagonal([[u8; WIDTH]; HEIGHT], Option<Duration>); pub struct AnimeDiagonal(AnimeType, Vec<Vec<u8>>, Option<Duration>);
impl Default for AnimeDiagonal {
#[inline]
fn default() -> Self {
Self::new(None)
}
}
impl AnimeDiagonal { impl AnimeDiagonal {
#[inline] #[inline]
pub fn new(duration: Option<Duration>) -> Self { pub fn new(anime_type: AnimeType, duration: Option<Duration>) -> Self {
Self([[0u8; WIDTH]; HEIGHT], duration) Self(
anime_type,
vec![vec![0; anime_type.width()]; anime_type.height()],
duration,
)
} }
#[inline] #[inline]
pub fn get_mut(&mut self) -> &mut [[u8; WIDTH]; HEIGHT] { pub fn get_mut(&mut self) -> &mut Vec<Vec<u8>> {
&mut self.0 &mut self.1
} }
/// Get a full diagonal row where `x` `y` is the starting point and `len` is the length of data. /// Get a full diagonal row where `x` `y` is the starting point and `len` is the length of data.
fn get_row(&self, x: usize, y: usize, len: usize) -> Vec<u8> { fn get_row(&self, x: usize, y: usize, len: usize) -> Vec<u8> {
let mut buf = Vec::with_capacity(len); let mut buf = Vec::with_capacity(len);
for i in 0..len { for i in 0..len {
let val = self.0[HEIGHT - y - i - 1][x + i]; let val = self.1[self.0.height() - y - i - 1][x + i];
buf.push(val); buf.push(val);
} }
buf buf
@@ -49,13 +38,14 @@ impl AnimeDiagonal {
path: &Path, path: &Path,
duration: Option<Duration>, duration: Option<Duration>,
bright: f32, bright: f32,
anime_type: AnimeType,
) -> Result<Self, AnimeError> { ) -> Result<Self, AnimeError> {
let data = std::fs::read(path)?; let data = std::fs::read(path)?;
let data = std::io::Cursor::new(data); let data = std::io::Cursor::new(data);
let decoder = png_pong::Decoder::new(data)?.into_steps(); let decoder = png_pong::Decoder::new(data)?.into_steps();
let png_pong::Step { raster, delay: _ } = decoder.last().ok_or(AnimeError::NoFrames)??; let png_pong::Step { raster, delay: _ } = decoder.last().ok_or(AnimeError::NoFrames)??;
let mut matrix = AnimeDiagonal::new(duration); let mut matrix = AnimeDiagonal::new(anime_type, duration);
match raster { match raster {
png_pong::PngRaster::Gray8(ras) => { png_pong::PngRaster::Gray8(ras) => {
@@ -102,7 +92,7 @@ impl AnimeDiagonal {
+ (<u8>::from(px.two()) / 3) as f32 + (<u8>::from(px.two()) / 3) as f32
+ (<u8>::from(px.three()) / 3) as f32 + (<u8>::from(px.three()) / 3) as f32
}; };
matrix.0[y][x] = (v * bright) as u8; matrix.1[y][x] = (v * bright) as u8;
} }
} }
} }
@@ -125,7 +115,7 @@ impl AnimeDiagonal {
+ ((<u16>::from(px.two()) / 3) >> 8) as f32 + ((<u16>::from(px.two()) / 3) >> 8) as f32
+ ((<u16>::from(px.three()) / 3) >> 8) as f32 + ((<u16>::from(px.three()) / 3) >> 8) as f32
}; };
matrix.0[y][x] = (v * bright) as u8; matrix.1[y][x] = (v * bright) as u8;
} }
} }
} }
@@ -142,7 +132,7 @@ impl AnimeDiagonal {
/// Do conversion from the nested Vec in AnimeMatrix to the two required /// Do conversion from the nested Vec in AnimeMatrix to the two required
/// packets suitable for sending over USB /// packets suitable for sending over USB
fn into_ga401_packets(&self) -> AnimeDataBuffer { fn into_ga401_packets(&self) -> AnimeDataBuffer {
let mut buf = vec![0u8; ANIME_GA401_DATA_LEN]; let mut buf = vec![0u8; AnimeType::GA401.data_length()];
buf[1..=32].copy_from_slice(&self.get_row(0, 3, 32)); buf[1..=32].copy_from_slice(&self.get_row(0, 3, 32));
buf[34..=66].copy_from_slice(&self.get_row(0, 2, 33)); buf[34..=66].copy_from_slice(&self.get_row(0, 2, 33));
@@ -204,7 +194,7 @@ impl AnimeDiagonal {
} }
fn into_ga402_packets(&self) -> AnimeDataBuffer { fn into_ga402_packets(&self) -> AnimeDataBuffer {
let mut buf = vec![0u8; ANIME_GA402_DATA_LEN]; let mut buf = vec![0u8; AnimeType::GA402.data_length()];
let mut start_index: usize = 0; let mut start_index: usize = 0;
fn copy_slice( fn copy_slice(
@@ -291,7 +281,7 @@ impl AnimeDiagonal {
mod tests { mod tests {
use std::path::PathBuf; use std::path::PathBuf;
use crate::{AnimeDiagonal, AnimePacketType}; use crate::{AnimeDiagonal, AnimePacketType, AnimeType};
#[test] #[test]
fn ga401_diagonal_packet_check() { fn ga401_diagonal_packet_check() {
@@ -380,7 +370,7 @@ mod tests {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("test/ga401-diagonal.png"); path.push("test/ga401-diagonal.png");
let matrix = AnimeDiagonal::from_png(&path, None, 255.0).unwrap(); let matrix = AnimeDiagonal::from_png(&path, None, 255.0, AnimeType::GA401).unwrap();
let data = matrix.into_data_buffer(crate::AnimeType::GA401); let data = matrix.into_data_buffer(crate::AnimeType::GA401);
let pkt = AnimePacketType::from(data); let pkt = AnimePacketType::from(data);

View File

@@ -90,12 +90,11 @@ impl AnimeGif {
#[inline] #[inline]
pub fn from_diagonal_gif( pub fn from_diagonal_gif(
file_name: &Path, file_name: &Path,
duration: AnimTime, duration: AnimTime,
brightness: f32, brightness: f32,
anime_type: AnimeType, anime_type: AnimeType,
) -> Result<Self, AnimeError> { ) -> Result<Self, AnimeError> {
let mut matrix = AnimeDiagonal::new(None); let mut matrix = AnimeDiagonal::new(anime_type, None);
let mut decoder = gif::DecodeOptions::new(); let mut decoder = gif::DecodeOptions::new();
// Configure the decoder such that it will expand the image to RGBA. // Configure the decoder such that it will expand the image to RGBA.
@@ -138,7 +137,7 @@ impl AnimeGif {
duration: AnimTime, duration: AnimTime,
brightness: f32, brightness: f32,
) -> Result<Self, AnimeError> { ) -> Result<Self, AnimeError> {
let image = AnimeDiagonal::from_png(file_name, None, brightness)?; let image = AnimeDiagonal::from_png(file_name, None, brightness, anime_type)?;
let mut total = Duration::from_millis(1000); let mut total = Duration::from_millis(1000);
if let AnimTime::Fade(fade) = duration { if let AnimTime::Fade(fade) = duration {

View File

@@ -1,5 +1,5 @@
use crate::data::{AnimeDataBuffer, ANIME_GA401_DATA_LEN}; use crate::data::AnimeDataBuffer;
use crate::{AnimeImage, AnimeType, ANIME_GA402_DATA_LEN}; use crate::{AnimeImage, AnimeType};
// TODO: Adjust these sizes as WIDTH_GA401 WIDTH_GA402 // TODO: Adjust these sizes as WIDTH_GA401 WIDTH_GA402
const WIDTH: usize = 33; const WIDTH: usize = 33;
@@ -92,10 +92,7 @@ impl From<AnimeGrid> for AnimeDataBuffer {
/// packets suitable for sending over USB /// packets suitable for sending over USB
#[inline] #[inline]
fn from(anime: AnimeGrid) -> Self { fn from(anime: AnimeGrid) -> Self {
let mut buf = match anime.anime_type { let mut buf = vec![0u8; anime.anime_type.data_length()];
AnimeType::GA401 => vec![0u8; ANIME_GA401_DATA_LEN],
AnimeType::GA402 => vec![0u8; ANIME_GA402_DATA_LEN],
};
for (idx, pos) in AnimeImage::generate_image_positioning(anime.anime_type) for (idx, pos) in AnimeImage::generate_image_positioning(anime.anime_type)
.iter() .iter()

View File

@@ -3,11 +3,7 @@ use std::path::Path;
pub use glam::Vec2; pub use glam::Vec2;
use glam::{Mat3, Vec3}; use glam::{Mat3, Vec3};
use crate::{ use crate::{data::AnimeDataBuffer, error::AnimeError, AnimeType};
data::{AnimeDataBuffer, ANIME_GA401_DATA_LEN},
error::AnimeError,
AnimeType,
};
/// A single greyscale + alpha pixel in the image /// A single greyscale + alpha pixel in the image
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@@ -392,7 +388,7 @@ impl From<&AnimeImage> for AnimeDataBuffer {
.iter() .iter()
.map(|l| if let Some(l) = l { l.bright() } else { 0 }) .map(|l| if let Some(l) = l { l.bright() } else { 0 })
.collect(); .collect();
let mut v = Vec::with_capacity(ANIME_GA401_DATA_LEN); let mut v = Vec::with_capacity(leds.anime_type.data_length());
v.push(0); v.push(0);
v.append(&mut l); v.append(&mut l);
v.append(&mut vec![0u8; 9]); v.append(&mut vec![0u8; 9]);

View File

@@ -86,7 +86,7 @@ impl ActionData {
brightness, brightness,
} => match time { } => match time {
AnimTime::Infinite => { AnimTime::Infinite => {
let image = AnimeDiagonal::from_png(file, None, *brightness)?; let image = AnimeDiagonal::from_png(file, None, *brightness, anime_type)?;
let data = image.into_data_buffer(anime_type); let data = image.into_data_buffer(anime_type);
ActionData::Image(Box::new(data)) ActionData::Image(Box::new(data))
} }

View File

@@ -28,7 +28,7 @@ pub fn get_anime_type() -> Result<AnimeType, AnimeError> {
if board_name.contains("GA401Q") { if board_name.contains("GA401Q") {
return Ok(AnimeType::GA401); return Ok(AnimeType::GA401);
} else if board_name.contains("GA401R") { } else if board_name.contains("GA402R") {
return Ok(AnimeType::GA402); return Ok(AnimeType::GA402);
} }
Err(AnimeError::UnsupportedDevice) Err(AnimeError::UnsupportedDevice)