anime: prep rog-anime for publish, rename *all* AniMe~ to Anime

This commit is contained in:
Luke D Jones
2021-04-11 10:46:05 +12:00
parent e515741efa
commit 0657c6cc74
29 changed files with 476 additions and 383 deletions
+20 -9
View File
@@ -1,5 +1,5 @@
use serde_derive::{Deserialize, Serialize};
#[cfg(feature = "z")]
#[cfg(feature = "dbus")]
use zvariant_derive::Type;
/// The first 7 bytes of a USB packet are accounted for by `USB_PREFIX1` and `USB_PREFIX2`
@@ -16,40 +16,51 @@ const USB_PREFIX2: [u8; 7] = [0x5e, 0xc0, 0x02, 0x74, 0x02, 0x73, 0x02];
/// The minimal serializable data that can be transferred over wire types.
/// Other data structures in `rog_anime` will convert to this.
#[cfg_attr(feature = "z", derive(Type))]
#[cfg_attr(feature = "dbus", derive(Type))]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AniMeDataBuffer(Vec<u8>);
pub struct AnimeDataBuffer(Vec<u8>);
impl Default for AniMeDataBuffer {
impl Default for AnimeDataBuffer {
fn default() -> Self {
Self::new()
}
}
impl AniMeDataBuffer {
impl AnimeDataBuffer {
#[inline]
pub fn new() -> Self {
AniMeDataBuffer(vec![0u8; ANIME_DATA_LEN])
AnimeDataBuffer(vec![0u8; ANIME_DATA_LEN])
}
/// Get the inner data buffer
#[inline]
pub fn get(&self) -> &[u8] {
&self.0
}
/// Get a mutable slice of the inner buffer
#[inline]
pub fn get_mut(&mut self) -> &mut [u8] {
&mut self.0
}
/// Create from a vector of bytes
///
/// # Panics
/// Will panic if the vector length is not `ANIME_DATA_LEN`
#[inline]
pub fn from_vec(input: Vec<u8>) -> Self {
assert_eq!(input.len(), ANIME_DATA_LEN);
Self(input)
}
}
/// The two packets to be written to USB
pub type AniMePacketType = [[u8; 640]; 2];
pub type AnimePacketType = [[u8; 640]; 2];
impl From<AniMeDataBuffer> for AniMePacketType {
impl From<AnimeDataBuffer> for AnimePacketType {
#[inline]
fn from(anime: AniMeDataBuffer) -> Self {
fn from(anime: AnimeDataBuffer) -> Self {
assert!(anime.0.len() == ANIME_DATA_LEN);
let mut buffers = [[0; 640]; 2];
for (idx, chunk) in anime.0.as_slice().chunks(PANE_LEN).enumerate() {
+13 -9
View File
@@ -1,7 +1,7 @@
use std::{path::Path, time::Duration};
use crate::{
data::{AniMeDataBuffer, ANIME_DATA_LEN},
data::{AnimeDataBuffer, ANIME_DATA_LEN},
error::AnimeError,
};
@@ -10,19 +10,22 @@ const HEIGHT: usize = 36;
/// Mostly intended to be used with ASUS gifs, but can be used for other purposes (like images)
#[derive(Debug, Clone)]
pub struct AniMeDiagonal([[u8; WIDTH]; HEIGHT], Option<Duration>);
pub struct AnimeDiagonal([[u8; WIDTH]; HEIGHT], Option<Duration>);
impl Default for AniMeDiagonal {
impl Default for AnimeDiagonal {
#[inline]
fn default() -> Self {
Self::new(None)
}
}
impl AniMeDiagonal {
impl AnimeDiagonal {
#[inline]
pub fn new(duration: Option<Duration>) -> Self {
Self([[0u8; WIDTH]; HEIGHT], duration)
}
#[inline]
pub fn get_mut(&mut self) -> &mut [[u8; WIDTH]; HEIGHT] {
&mut self.0
}
@@ -39,6 +42,7 @@ impl AniMeDiagonal {
/// Generate the base image from inputs. The result can be displayed as is or
/// updated via scale, position, or angle then displayed again after `update()`.
#[inline]
pub fn from_png(
path: &Path,
duration: Option<Duration>,
@@ -50,7 +54,7 @@ impl AniMeDiagonal {
let decoder = png_pong::Decoder::new(data)?.into_steps();
let png_pong::Step { raster, delay: _ } = decoder.last().ok_or(AnimeError::NoFrames)??;
let mut matrix = AniMeDiagonal::new(duration);
let mut matrix = AnimeDiagonal::new(duration);
let width;
match raster {
@@ -70,11 +74,11 @@ impl AniMeDiagonal {
}
}
impl From<&AniMeDiagonal> for AniMeDataBuffer {
/// Do conversion from the nested Vec in AniMeMatrix to the two required
impl From<&AnimeDiagonal> for AnimeDataBuffer {
/// Do conversion from the nested Vec in AnimeMatrix to the two required
/// packets suitable for sending over USB
#[inline]
fn from(anime: &AniMeDiagonal) -> Self {
fn from(anime: &AnimeDiagonal) -> Self {
let mut buf = vec![0u8; ANIME_DATA_LEN];
buf[1..=32].copy_from_slice(&anime.get_row(0, 3, 32));
@@ -133,6 +137,6 @@ impl From<&AniMeDiagonal> for AniMeDataBuffer {
buf[1226..=1234].copy_from_slice(&anime.get_row(50, 0, 9));
buf[1236..=1244].copy_from_slice(&anime.get_row(51, 0, 9));
AniMeDataBuffer::from_vec(buf)
AnimeDataBuffer::from_vec(buf)
}
}
+9 -5
View File
@@ -3,7 +3,7 @@ use png_pong::decode::Error as PngError;
use std::error::Error;
use std::fmt;
#[cfg(feature = "z")]
#[cfg(feature = "dbus")]
use zbus::fdo;
#[derive(Debug)]
@@ -15,12 +15,12 @@ pub enum AnimeError {
Format,
/// The input was incorrect size, expected size is `IncorrectSize(width, height)`
IncorrectSize(u32, u32),
#[cfg(feature = "z")]
#[cfg(feature = "dbus")]
Zbus(fdo::Error)
}
impl fmt::Display for AnimeError {
// This trait requires `fmt` with this exact signature.
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AnimeError::NoFrames => write!(f, "No frames in PNG"),
@@ -33,7 +33,7 @@ impl fmt::Display for AnimeError {
"The input image size is incorrect, expected {}x{}",
width, height
),
#[cfg(feature = "z")]
#[cfg(feature = "dbus")]
AnimeError::Zbus(e) => write!(f, "ZBUS error: {}", e),
}
}
@@ -42,25 +42,29 @@ impl fmt::Display for AnimeError {
impl Error for AnimeError {}
impl From<std::io::Error> for AnimeError {
#[inline]
fn from(err: std::io::Error) -> Self {
AnimeError::Io(err)
}
}
impl From<PngError> for AnimeError {
#[inline]
fn from(err: PngError) -> Self {
AnimeError::Png(err)
}
}
impl From<DecodingError> for AnimeError {
#[inline]
fn from(err: DecodingError) -> Self {
AnimeError::Gif(err)
}
}
#[cfg(feature = "z")]
#[cfg(feature = "dbus")]
impl From<AnimeError> for fdo::Error {
#[inline]
fn from(err: AnimeError) -> Self {
fdo::Error::Failed(format!("{}", err))
}
+30 -16
View File
@@ -2,35 +2,43 @@ use glam::Vec2;
use serde_derive::{Deserialize, Serialize};
use std::{fs::File, path::Path, time::Duration};
use crate::{error::AnimeError, AniMeDataBuffer, AniMeDiagonal, AniMeImage, Pixel};
use crate::{error::AnimeError, AnimeDataBuffer, AnimeDiagonal, AnimeImage, Pixel};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AniMeFrame {
pub struct AnimeFrame {
/// Precomputed data for the frame. This can be transferred directly to the
/// the `asusd` daemon over dbus or converted to USB packet with `AniMePacketType::from(buffer)`
data: AniMeDataBuffer,
/// the `asusd` daemon over dbus or converted to USB packet with `AnimePacketType::from(buffer)`
data: AnimeDataBuffer,
delay: Duration,
}
impl AniMeFrame {
pub fn frame(&self) -> &AniMeDataBuffer {
impl AnimeFrame {
/// Get the inner data buffer of the gif frame
#[inline]
pub fn frame(&self) -> &AnimeDataBuffer {
&self.data
}
/// Get the `Duration` of the delay for this frame
#[inline]
pub fn delay(&self) -> Duration {
self.delay
}
}
/// Defines the time or animation cycle count to use for a gif
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
pub enum AnimTime {
/// Time in milliseconds for animation to run
Time(Duration),
/// How many full animation loops to run
Cycles(u32),
/// Run for infinite time
Infinite,
}
impl Default for AnimTime {
#[inline]
fn default() -> Self {
Self::Infinite
}
@@ -39,16 +47,17 @@ impl Default for AnimTime {
/// A gif animation. This is a collection of frames from the gif, and a duration
/// that the animation should be shown for.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AniMeGif(Vec<AniMeFrame>, AnimTime);
pub struct AnimeGif(Vec<AnimeFrame>, AnimTime);
impl AniMeGif {
impl AnimeGif {
/// Create an animation using the 74x36 ASUS gif format
#[inline]
pub fn create_diagonal_gif(
file_name: &Path,
duration: AnimTime,
brightness: f32,
) -> Result<Self, AnimeError> {
let mut matrix = AniMeDiagonal::new(None);
let mut matrix = AnimeDiagonal::new(None);
let mut decoder = gif::DecodeOptions::new();
// Configure the decoder such that it will expand the image to RGBA.
@@ -74,8 +83,8 @@ impl AniMeGif {
}
}
frames.push(AniMeFrame {
data: <AniMeDataBuffer>::from(&matrix),
frames.push(AnimeFrame {
data: <AnimeDataBuffer>::from(&matrix),
delay: Duration::from_millis(wait as u64),
});
}
@@ -84,6 +93,7 @@ impl AniMeGif {
/// Create an animation using a gif of any size. This method must precompute the
/// result.
#[inline]
pub fn create_png_gif(
file_name: &Path,
scale: f32,
@@ -105,7 +115,7 @@ impl AniMeGif {
let width = decoder.width();
let pixels: Vec<Pixel> =
vec![Pixel::default(); (decoder.width() as u32 * decoder.height() as u32) as usize];
let mut image = AniMeImage::new(
let mut image = AnimeImage::new(
Vec2::new(scale, scale),
angle,
translation,
@@ -119,7 +129,7 @@ impl AniMeGif {
if matches!(frame.dispose, gif::DisposalMethod::Background) {
let pixels: Vec<Pixel> =
vec![Pixel::default(); (width as u32 * height as u32) as usize];
image = AniMeImage::new(
image = AnimeImage::new(
Vec2::new(scale, scale),
angle,
translation,
@@ -144,18 +154,22 @@ impl AniMeGif {
}
image.update();
frames.push(AniMeFrame {
data: <AniMeDataBuffer>::from(&image),
frames.push(AnimeFrame {
data: <AnimeDataBuffer>::from(&image),
delay: Duration::from_millis(wait as u64),
});
}
Ok(Self(frames, duration))
}
pub fn frames(&self) -> &[AniMeFrame] {
/// Get a slice of the frames this gif has
#[inline]
pub fn frames(&self) -> &[AnimeFrame] {
&self.0
}
/// Get the time/count for this gif
#[inline]
pub fn duration(&self) -> AnimTime {
self.1
}
+21 -11
View File
@@ -1,6 +1,6 @@
use std::time::Duration;
use crate::data::{AniMeDataBuffer, ANIME_DATA_LEN};
use crate::data::{AnimeDataBuffer, ANIME_DATA_LEN};
use crate::image::LED_IMAGE_POSITIONS;
const WIDTH: usize = 33;
@@ -8,35 +8,45 @@ const HEIGHT: usize = 55;
/// Helper structure for writing images.
///
/// See the examples for ways to write an image to `AniMeMatrix` format.
/// See the examples for ways to write an image to `AnimeMatrix` format.
/// Width = 33
/// height = 55
///
/// **Note:** the columns in each odd row are offset by half a pixel left
#[derive(Debug, Clone)]
pub struct AniMeGrid([[u8; WIDTH]; HEIGHT], Option<Duration>);
pub struct AnimeGrid([[u8; WIDTH]; HEIGHT], Option<Duration>);
impl Default for AniMeGrid {
impl Default for AnimeGrid {
#[inline]
fn default() -> Self {
Self::new(None)
}
}
impl AniMeGrid {
impl AnimeGrid {
#[inline]
pub fn new(duration: Option<Duration>) -> Self {
AniMeGrid([[0u8; WIDTH]; HEIGHT], duration)
AnimeGrid([[0u8; WIDTH]; HEIGHT], duration)
}
/// Set a position in the grid with a brightness value
#[inline]
pub fn set(&mut self, x: usize, y: usize, b: u8) {
self.0[y][x] = b;
}
#[inline]
pub fn get(&self) -> &[[u8; WIDTH]; HEIGHT] {
&self.0
}
#[inline]
pub fn get_mut(&mut self) -> &mut [[u8; WIDTH]; HEIGHT] {
&mut self.0
}
/// Fill the grid with a value
#[inline]
pub fn fill_with(&mut self, fill: u8) {
for row in self.0.iter_mut() {
for x in row.iter_mut() {
@@ -79,11 +89,11 @@ impl AniMeGrid {
// }
}
impl From<AniMeGrid> for AniMeDataBuffer {
impl From<AnimeGrid> for AnimeDataBuffer {
/// Do conversion from the nested Vec in AniMeMatrix to the two required
/// packets suitable for sending over USB
#[inline]
fn from(anime: AniMeGrid) -> Self {
fn from(anime: AnimeGrid) -> Self {
let mut buf = vec![0u8; ANIME_DATA_LEN];
for (idx, pos) in LED_IMAGE_POSITIONS.iter().enumerate() {
@@ -93,7 +103,7 @@ impl From<AniMeGrid> for AniMeDataBuffer {
buf[idx + 1] = anime.0[y][x];
}
}
AniMeDataBuffer::from_vec(buf)
AnimeDataBuffer::from_vec(buf)
}
}
@@ -103,7 +113,7 @@ mod tests {
#[test]
fn check_data_alignment() {
let mut matrix = AniMeGrid::new(None);
let mut matrix = AnimeGrid::new(None);
{
let tmp = matrix.get_mut();
for row in tmp.iter_mut() {
@@ -112,7 +122,7 @@ mod tests {
}
}
let matrix = <AniMeDataBuffer>::from(matrix);
let matrix = <AnimeDataBuffer>::from(matrix);
let data_cmp = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+59 -48
View File
@@ -4,12 +4,13 @@ pub use glam::Vec2;
use glam::{Mat3, Vec3};
use crate::{
data::{AniMeDataBuffer, ANIME_DATA_LEN},
data::{AnimeDataBuffer, ANIME_DATA_LEN},
error::AnimeError,
};
const LED_PIXEL_LEN: usize = 1244;
/// A single greyscale + alpha pixel in the image
#[derive(Copy, Clone, Debug)]
pub(crate) struct Pixel {
pub color: u32,
@@ -17,6 +18,7 @@ pub(crate) struct Pixel {
}
impl Default for Pixel {
#[inline]
fn default() -> Self {
Pixel {
color: 0,
@@ -58,10 +60,11 @@ impl Led {
/// Container of `Led`, each of which specifies a position within the image
/// The main use of this is to position and sample colours for the final image
/// to show on AniMe
pub struct AniMeImage {
pub struct AnimeImage {
pub scale: Vec2,
/// Angle in radians
pub angle: f32,
/// Position of the image ont he display
pub translation: Vec2,
/// Brightness of final image, `0.0` = off, `1.0` = full
pub bright: f32,
@@ -69,10 +72,11 @@ pub struct AniMeImage {
led_pos: [Option<Led>; LED_PIXEL_LEN],
/// THe image data for sampling
img_pixels: Vec<Pixel>,
/// width of the image
width: u32,
}
impl AniMeImage {
impl AnimeImage {
pub(crate) const fn new(
scale: Vec2,
angle: f32,
@@ -119,6 +123,7 @@ impl AniMeImage {
36 - (y + 1) / 2
}
/// Physical display width
fn phys_width() -> f32 {
(32.0 - -0.5 + 1.0) * Self::scale_x()
}
@@ -128,10 +133,12 @@ impl AniMeImage {
55
}
/// Physical display height
fn phys_height() -> f32 {
(54.0 + 1.0) * Self::scale_y()
}
/// Find the actual width of the data including the dead pixels
const fn pitch(y: u32) -> u32 {
match y {
0 | 2 | 4 => 33,
@@ -145,12 +152,13 @@ impl AniMeImage {
}
/// Really only used to generate the output for including as a full const in `LED_IMAGE_POSITIONS`
#[inline]
pub fn generate() -> Vec<Option<Led>> {
(0..AniMeImage::height())
(0..AnimeImage::height())
.flat_map(|y| {
(0..AniMeImage::pitch(y)).map(move |l| {
if l < AniMeImage::width(y) {
let x = AniMeImage::first_x(y) + l;
(0..AnimeImage::pitch(y)).map(move |l| {
if l < AnimeImage::width(y) {
let x = AnimeImage::first_x(y) + l;
Some(Led::new(x as f32 - 0.5 * (y % 2) as f32, y as f32))
} else {
None
@@ -163,6 +171,7 @@ impl AniMeImage {
/// Called after setting new angle, position, or scale to refresh the image
/// samples, the result can then been transformed to the appropriate data
/// for displaying
#[inline]
pub fn update(&mut self) {
let width = self.width as i32;
let height = self.img_pixels.len() as i32 / width;
@@ -204,12 +213,13 @@ impl AniMeImage {
}
}
/// Put the render window in place on the image
fn put(&self, bmp_w: f32, bmp_h: f32) -> Mat3 {
// Center of image
let center = Mat3::from_translation(Vec2::new(-0.5 * bmp_w, -0.5 * bmp_h));
// Find the scale required for cleanly showing the image
let h = AniMeImage::phys_height() / bmp_h;
let mut base_scale = AniMeImage::phys_width() / bmp_w;
let h = AnimeImage::phys_height() / bmp_h;
let mut base_scale = AnimeImage::phys_width() / bmp_w;
if base_scale > h {
base_scale = h;
}
@@ -217,8 +227,8 @@ impl AniMeImage {
let cm_from_px = Mat3::from_scale(Vec2::new(base_scale, base_scale));
let led_from_cm = Mat3::from_scale(Vec2::new(
1.0 / AniMeImage::scale_x(),
1.0 / AniMeImage::scale_y(),
1.0 / AnimeImage::scale_x(),
1.0 / AnimeImage::scale_y(),
));
let transform =
@@ -233,6 +243,7 @@ impl AniMeImage {
/// Generate the base image from inputs. The result can be displayed as is or
/// updated via scale, position, or angle then displayed again after `update()`.
#[inline]
pub fn from_png(
path: &Path,
scale: f32,
@@ -261,7 +272,7 @@ impl AniMeImage {
_ => return Err(AnimeError::Format),
};
let mut matrix = AniMeImage::new(
let mut matrix = AnimeImage::new(
Vec2::new(scale, scale),
angle,
translation,
@@ -275,11 +286,11 @@ impl AniMeImage {
}
}
impl From<&AniMeImage> for AniMeDataBuffer {
impl From<&AnimeImage> for AnimeDataBuffer {
/// Do conversion from the nested Vec in AniMeMatrix to the two required
/// packets suitable for sending over USB
#[inline]
fn from(leds: &AniMeImage) -> Self {
fn from(leds: &AnimeImage) -> Self {
let mut l: Vec<u8> = leds
.led_pos
.iter()
@@ -289,7 +300,7 @@ impl From<&AniMeImage> for AniMeDataBuffer {
v.push(0);
v.append(&mut l);
v.append(&mut vec![0u8; 9]);
AniMeDataBuffer::from_vec(v)
AnimeDataBuffer::from_vec(v)
}
}
@@ -1550,7 +1561,7 @@ mod tests {
#[test]
fn led_positions() {
let leds = AniMeImage::generate();
let leds = AnimeImage::generate();
assert_eq!(leds[0], Some(Led(0.0, 0.0, 0)));
assert_eq!(leds[1], Some(Led(1.0, 0.0, 0)));
assert_eq!(leds[2], Some(Led(2.0, 0.0, 0)));
@@ -1579,7 +1590,7 @@ mod tests {
#[test]
fn led_positions_const() {
let leds = AniMeImage::generate();
let leds = AnimeImage::generate();
assert_eq!(leds[1], LED_IMAGE_POSITIONS[1]);
assert_eq!(leds[34], LED_IMAGE_POSITIONS[34]);
assert_eq!(leds[69], LED_IMAGE_POSITIONS[69]);
@@ -1593,44 +1604,44 @@ mod tests {
#[test]
fn row_starts() {
assert_eq!(AniMeImage::first_x(5), 0);
assert_eq!(AniMeImage::first_x(6), 0);
assert_eq!(AniMeImage::first_x(7), 1);
assert_eq!(AniMeImage::first_x(8), 1);
assert_eq!(AniMeImage::first_x(9), 2);
assert_eq!(AniMeImage::first_x(10), 2);
assert_eq!(AniMeImage::first_x(11), 3);
assert_eq!(AnimeImage::first_x(5), 0);
assert_eq!(AnimeImage::first_x(6), 0);
assert_eq!(AnimeImage::first_x(7), 1);
assert_eq!(AnimeImage::first_x(8), 1);
assert_eq!(AnimeImage::first_x(9), 2);
assert_eq!(AnimeImage::first_x(10), 2);
assert_eq!(AnimeImage::first_x(11), 3);
}
#[test]
fn row_widths() {
assert_eq!(AniMeImage::width(5), 33);
assert_eq!(AniMeImage::width(6), 33);
assert_eq!(AniMeImage::width(7), 32);
assert_eq!(AniMeImage::width(8), 32);
assert_eq!(AniMeImage::width(9), 31);
assert_eq!(AniMeImage::width(10), 31);
assert_eq!(AniMeImage::width(11), 30);
assert_eq!(AniMeImage::width(12), 30);
assert_eq!(AniMeImage::width(13), 29);
assert_eq!(AniMeImage::width(14), 29);
assert_eq!(AniMeImage::width(15), 28);
assert_eq!(AniMeImage::width(16), 28);
assert_eq!(AniMeImage::width(17), 27);
assert_eq!(AniMeImage::width(18), 27);
assert_eq!(AnimeImage::width(5), 33);
assert_eq!(AnimeImage::width(6), 33);
assert_eq!(AnimeImage::width(7), 32);
assert_eq!(AnimeImage::width(8), 32);
assert_eq!(AnimeImage::width(9), 31);
assert_eq!(AnimeImage::width(10), 31);
assert_eq!(AnimeImage::width(11), 30);
assert_eq!(AnimeImage::width(12), 30);
assert_eq!(AnimeImage::width(13), 29);
assert_eq!(AnimeImage::width(14), 29);
assert_eq!(AnimeImage::width(15), 28);
assert_eq!(AnimeImage::width(16), 28);
assert_eq!(AnimeImage::width(17), 27);
assert_eq!(AnimeImage::width(18), 27);
}
#[test]
fn row_pitch() {
assert_eq!(AniMeImage::pitch(5), 34);
assert_eq!(AniMeImage::pitch(6), 33);
assert_eq!(AniMeImage::pitch(7), 33);
assert_eq!(AniMeImage::pitch(8), 32);
assert_eq!(AniMeImage::pitch(9), 32);
assert_eq!(AniMeImage::pitch(10), 31);
assert_eq!(AniMeImage::pitch(11), 31);
assert_eq!(AniMeImage::pitch(12), 30);
assert_eq!(AniMeImage::pitch(13), 30);
assert_eq!(AniMeImage::pitch(14), 29);
assert_eq!(AnimeImage::pitch(5), 34);
assert_eq!(AnimeImage::pitch(6), 33);
assert_eq!(AnimeImage::pitch(7), 33);
assert_eq!(AnimeImage::pitch(8), 32);
assert_eq!(AnimeImage::pitch(9), 32);
assert_eq!(AnimeImage::pitch(10), 31);
assert_eq!(AnimeImage::pitch(11), 31);
assert_eq!(AnimeImage::pitch(12), 30);
assert_eq!(AnimeImage::pitch(13), 30);
assert_eq!(AnimeImage::pitch(14), 29);
}
}
+10 -2
View File
@@ -1,10 +1,9 @@
/// The main data conversion for transfering in shortform over dbus or other,
/// or writing directly to the USB device
mod data;
pub use data::*;
/// Useful for specialised effects that required a grid of data
/// Useful for specialised effects that require a grid of data
mod grid;
pub use grid::*;
@@ -12,14 +11,23 @@ pub use grid::*;
mod image;
pub use image::*;
/// A grid of data that is intended to be read out and displayed on the ANiMe as
/// a diagonal
mod diagonal;
pub use diagonal::*;
/// A gif. Can be created from the ASUS gifs which are diagonal layout, or from
/// any standard gif
mod gif;
pub use crate::gif::*;
/// A container of images/grids/gifs/pauses which can be iterated over to generate
/// cool effects
mod sequencer;
pub use sequencer::*;
/// Base errors that are possible
pub mod error;
/// Provides const methods to create the USB HID control packets
pub mod usb;
+22 -9
View File
@@ -6,8 +6,10 @@ use std::{
use glam::Vec2;
use serde_derive::{Deserialize, Serialize};
use crate::{error::AnimeError, AniMeDataBuffer, AniMeGif, AniMeImage, AnimTime};
use crate::{error::AnimeError, AnimeDataBuffer, AnimeGif, AnimeImage, AnimTime};
/// All the possible AniMe actions that can be used. This enum is intended to be
/// a helper for loading up `ActionData`.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum AnimeAction {
/// Full gif sequence. Immutable.
@@ -36,13 +38,14 @@ pub enum AnimeAction {
Pause(Duration),
}
///
/// All the possible AniMe actions that can be used. The enum is intended to be
/// used in a array allowing the user to cycle through a series of actions.
#[derive(Debug, Deserialize, Serialize)]
pub enum ActionData {
/// Full gif sequence. Immutable.
Animation(AniMeGif),
Animation(AnimeGif),
/// Basic image, can have properties changed and image updated via those properties
Image(Box<AniMeDataBuffer>),
Image(Box<AnimeDataBuffer>),
/// A pause to be used between sequences
Pause(Duration),
/// Placeholder
@@ -55,15 +58,19 @@ pub enum ActionData {
Matrix,
}
/// An optimised precomputed set of actions
/// An optimised precomputed set of actions that the user can cycle through
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct Sequences(Vec<ActionData>);
impl Sequences {
#[inline]
pub fn new() -> Self {
Self(Vec::new())
}
/// Use a base `AnimeAction` to generate the precomputed data and insert in to
/// the run buffer
#[inline]
pub fn insert(&mut self, index: usize, action: &AnimeAction) -> Result<(), AnimeError> {
match action {
AnimeAction::AsusAnimation {
@@ -99,6 +106,10 @@ impl Sequences {
Ok(())
}
/// Remove an item at this position from the run buffer. If the `index` supplied
/// is not in range then `None` is returned, otherwise the `ActionData` at that location
/// is yeeted and returned.
#[inline]
pub fn remove_item(&mut self, index: usize) -> Option<ActionData> {
if index < self.0.len() {
return Some(self.0.remove(index));
@@ -116,7 +127,7 @@ impl Sequences {
if index > self.0.len() {
index = self.0.len() - 1;
}
let frames = AniMeGif::create_diagonal_gif(file, duration, brightness)?;
let frames = AnimeGif::create_diagonal_gif(file, duration, brightness)?;
self.0.insert(index, ActionData::Animation(frames));
Ok(())
}
@@ -133,8 +144,8 @@ impl Sequences {
if index > self.0.len() {
index = self.0.len() - 1;
}
let image = AniMeImage::from_png(file, scale, angle, translation, brightness)?;
let data = <AniMeDataBuffer>::from(&image);
let image = AnimeImage::from_png(file, scale, angle, translation, brightness)?;
let data = <AnimeDataBuffer>::from(&image);
self.0.insert(index, ActionData::Image(Box::new(data)));
Ok(())
}
@@ -154,7 +165,7 @@ impl Sequences {
index = self.0.len() - 1;
}
let frames =
AniMeGif::create_png_gif(file, scale, angle, translation, duration, brightness)?;
AnimeGif::create_png_gif(file, scale, angle, translation, duration, brightness)?;
self.0.insert(index, ActionData::Animation(frames));
Ok(())
}
@@ -174,6 +185,7 @@ impl Sequences {
}
}
/// Iteractor helper for iterating over all the actions in `Sequences`
pub struct ActionIterator<'a> {
actions: &'a Sequences,
next_idx: usize,
@@ -182,6 +194,7 @@ pub struct ActionIterator<'a> {
impl<'a> Iterator for ActionIterator<'a> {
type Item = &'a ActionData;
#[inline]
fn next(&mut self) -> Option<&'a ActionData> {
if self.next_idx == self.actions.0.len() {
self.next_idx = 0;
+79
View File
@@ -0,0 +1,79 @@
//! Utils for writing to the AniMe USB device
//!
//! Use of the device requires a few steps:
//! 1. Initialise the device by writing the two packets from `get_init_packets()`
//! 2. Write data from `AnimePacketType`
//! 3. Write the packet from `get_flush_packet()`, which tells the device to display the data from step 2
//!
//! Step 1 need to applied only on fresh system boot.
const INIT_STR: [u8; 15] = [
0x5e, b'A', b'S', b'U', b'S', b' ', b'T', b'E', b'C', b'H', b'.', b'I', b'N', b'C', b'.',
];
const PACKET_SIZE: usize = 640;
const DEV_PAGE: u8 = 0x5e;
pub const VENDOR_ID: u16 = 0x0b05;
pub const PROD_ID: u16 = 0x193b;
/// Get the two device initialization packets. These are required for device start
/// after the laptop boots.
#[inline]
pub const fn pkts_for_init() -> [[u8; PACKET_SIZE]; 2] {
let mut packets = [[0; PACKET_SIZE]; 2];
packets[0][0] = DEV_PAGE; // This is the USB page we're using throughout
let mut count = 0;
while count < INIT_STR.len() {
packets[0][count] = INIT_STR[count];
count +=1;
}
//
packets[1][0] = DEV_PAGE; // write it to be sure?
packets[1][1] = 0xc2;
packets
}
/// Should be written to the device after writing the two main data packets that
/// make up the display data packet
#[inline]
pub const fn pkt_for_flush() -> [u8; PACKET_SIZE] {
let mut pkt = [0; PACKET_SIZE];
pkt[0] = DEV_PAGE;
pkt[1] = 0xc0;
pkt[2] = 0x03;
pkt
}
/// Get the packet required for setting the device to on, on boot. Requires
/// pkt_for_apply()` to be written after.
#[inline]
pub const fn pkt_for_set_boot(status: bool) -> [u8; PACKET_SIZE] {
let mut pkt = [0; PACKET_SIZE];
pkt[0] = DEV_PAGE;
pkt[1] = 0xc3;
pkt[2] = 0x01;
pkt[3] = if status { 0x00 } else { 0x80 };
pkt
}
/// Get the packet required for setting the device to on. Requires `pkt_for_apply()`
/// to be written after.
#[inline]
pub const fn pkt_for_set_on(on: bool) -> [u8; PACKET_SIZE] {
let mut pkt = [0; PACKET_SIZE];
pkt[0] = DEV_PAGE;
pkt[1] = 0xc0;
pkt[2] = 0x04;
pkt[3] = if on { 0x03 } else { 0x00 };
pkt
}
/// Packet required to apply a device setting
#[inline]
pub const fn pkt_for_apply() -> [u8; PACKET_SIZE] {
let mut pkt = [0; PACKET_SIZE];
pkt[0] = DEV_PAGE;
pkt[1] = 0xc4;
pkt[2] = 0x01;
pkt[3] = 0x80;
pkt
}