Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LED Matrix startup animations #83

Merged
merged 15 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions fl16-inputmodules/src/animations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
use crate::control::*;
use crate::games::game_of_life::*;
use crate::games::pong_animation::*;
use crate::games::snake_animation::*;
use crate::matrix::Grid;
use crate::matrix::*;
use crate::patterns::*;

// TODO
// - [ ] Is there a cancellable Iterator? I think Java/Kotlin has one
// - [ ] Each one has a number of frames
// - [ ] Each one might have a different frame-rate

#[allow(clippy::large_enum_variant)]
pub enum Animation {
ZigZag(ZigZagIterator),
Gof(GameOfLifeIterator),
Percentage(StartupPercentageIterator),
Breathing(BreathingIterator),
Snake(SnakeIterator),
Pong(PongIterator),
}
impl Iterator for Animation {
type Item = Grid;

fn next(&mut self) -> Option<Self::Item> {
match self {
Animation::ZigZag(x) => x.next(),
Animation::Gof(x) => x.next(),
Animation::Percentage(x) => x.next(),
Animation::Breathing(x) => x.next(),
Animation::Snake(x) => x.next(),
Animation::Pong(x) => x.next(),
}
}
}

pub struct ZigZagIterator {
frames: usize,
current_frame: usize,
}

impl ZigZagIterator {
pub fn new(frames: usize) -> Self {
Self {
frames,
current_frame: 0,
}
}
}

impl Default for ZigZagIterator {
fn default() -> Self {
Self::new(34)
}
}

impl Iterator for ZigZagIterator {
type Item = Grid;

fn next(&mut self) -> Option<Self::Item> {
if self.current_frame < self.frames {
let mut next = zigzag();
next.rotate(self.current_frame);
self.current_frame += 1;
Some(next)
} else {
None
}
}
}

pub struct StartupPercentageIterator {
frames: usize,
current_frame: usize,
}

impl Default for StartupPercentageIterator {
fn default() -> Self {
Self {
frames: 34,
current_frame: 0,
}
}
}

impl Iterator for StartupPercentageIterator {
type Item = Grid;

fn next(&mut self) -> Option<Self::Item> {
if self.current_frame < self.frames {
self.current_frame += 1;
Some(rows(self.current_frame))
} else {
None
}
}
}

pub struct GameOfLifeIterator {
state: GameOfLifeState,
frames_remaining: usize,
}

impl GameOfLifeIterator {
pub fn new(start_param: GameOfLifeStartParam, frames: usize) -> Self {
Self {
// Could start with a custom grid
state: GameOfLifeState::new(start_param, &Grid::default()),
frames_remaining: frames,
}
}
}

impl Iterator for GameOfLifeIterator {
type Item = Grid;

fn next(&mut self) -> Option<Self::Item> {
if self.frames_remaining > 0 {
self.frames_remaining -= 1;
// Only update every 8th frame, otherwise the animation is too fast
if self.frames_remaining % 8 == 0 {
self.state.tick();
}
Some(self.state.draw_matrix())
} else {
None
}
}
}

pub struct BreathingIterator {
frames_remaining: usize,
current_brightness: u8,
}

impl BreathingIterator {
pub fn new(frames: usize) -> Self {
Self {
frames_remaining: frames,
current_brightness: 0,
}
}
}
impl Default for BreathingIterator {
fn default() -> Self {
Self::new(64)
}
}

impl Iterator for BreathingIterator {
type Item = Grid;

fn next(&mut self) -> Option<Self::Item> {
if self.frames_remaining > 0 {
let mut grid = Grid::default();
let breath_step = 4;
// TODO: Make it cycle up and down
self.current_brightness = (self.current_brightness + breath_step) % 255;
for y in 0..HEIGHT {
for x in 0..WIDTH {
grid.0[x][y] = self.current_brightness;
}
}
self.frames_remaining -= 1;
Some(grid)
} else {
None
}
}
}
1 change: 1 addition & 0 deletions fl16-inputmodules/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub enum GameOfLifeStartParam {
Toad = 0x03,
Beacon = 0x04,
Glider = 0x05,
BeaconToadBlinker = 0x06,
}

#[derive(Copy, Clone, num_derive::FromPrimitive)]
Expand Down
64 changes: 41 additions & 23 deletions fl16-inputmodules/src/games/game_of_life.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::control::{GameControlArg, GameOfLifeStartParam};
use crate::matrix::{GameState, Grid, LedmatrixState, HEIGHT, WIDTH};

#[derive(Clone, Copy, num_derive::FromPrimitive)]
#[derive(Clone, Copy, num_derive::FromPrimitive, PartialEq, Eq)]
pub enum Cell {
Dead = 0,
Alive = 1,
Expand All @@ -12,6 +12,20 @@ pub struct GameOfLifeState {
cells: [[Cell; WIDTH]; HEIGHT],
}

impl GameOfLifeState {
pub fn combine(&self, other: &Self) -> Self {
let mut state = self.clone();
for x in 0..WIDTH {
for y in 0..HEIGHT {
if other.cells[y][x] == Cell::Alive {
state.cells[y][x] = Cell::Alive;
}
}
}
state
}
}

pub fn start_game(state: &mut LedmatrixState, _random: u8, param: GameOfLifeStartParam) {
let gol = GameOfLifeState::new(param, &state.grid);
state.grid = gol.draw_matrix();
Expand All @@ -34,6 +48,7 @@ pub fn game_step(state: &mut LedmatrixState, _random: u8) {
}

impl GameOfLifeState {
// TODO: Integrate Grid into GameOfLifeStartParam because it's only used in one of the enum variants
pub fn new(param: GameOfLifeStartParam, grid: &Grid) -> Self {
match param {
GameOfLifeStartParam::Beacon => Self::beacon(),
Expand All @@ -57,6 +72,9 @@ impl GameOfLifeState {
GameOfLifeStartParam::Blinker => Self::blinker(),
GameOfLifeStartParam::Toad => Self::toad(),
GameOfLifeStartParam::Glider => Self::glider(),
GameOfLifeStartParam::BeaconToadBlinker => Self::beacon()
.combine(&Self::toad())
.combine(&Self::blinker()),
}
}
fn pattern1() -> Self {
Expand All @@ -81,12 +99,12 @@ impl GameOfLifeState {
// X
// X
let mut cells = [[Cell::Dead; WIDTH]; HEIGHT];
cells[10][5] = Cell::Alive;
cells[10][6] = Cell::Alive;
cells[10][7] = Cell::Alive;
cells[14][5] = Cell::Alive;
cells[14][6] = Cell::Alive;
cells[14][7] = Cell::Alive;
cells[4][5] = Cell::Alive;
cells[4][6] = Cell::Alive;
cells[4][7] = Cell::Alive;
cells[8][5] = Cell::Alive;
cells[8][6] = Cell::Alive;
cells[8][7] = Cell::Alive;
GameOfLifeState { cells }
}
fn toad() -> Self {
Expand All @@ -99,12 +117,12 @@ impl GameOfLifeState {
// X X
// X
let mut cells = [[Cell::Dead; WIDTH]; HEIGHT];
cells[10][4] = Cell::Alive;
cells[10][5] = Cell::Alive;
cells[10][6] = Cell::Alive;
cells[11][5] = Cell::Alive;
cells[11][6] = Cell::Alive;
cells[11][7] = Cell::Alive;
cells[17][4] = Cell::Alive;
cells[17][5] = Cell::Alive;
cells[17][6] = Cell::Alive;
cells[18][5] = Cell::Alive;
cells[18][6] = Cell::Alive;
cells[18][7] = Cell::Alive;
GameOfLifeState { cells }
}
fn beacon() -> Self {
Expand All @@ -119,15 +137,15 @@ impl GameOfLifeState {
// X
// XX
let mut cells = [[Cell::Dead; WIDTH]; HEIGHT];
cells[10][4] = Cell::Alive;
cells[10][5] = Cell::Alive;
cells[11][4] = Cell::Alive;
cells[11][5] = Cell::Alive;

cells[12][6] = Cell::Alive;
cells[12][7] = Cell::Alive;
cells[13][6] = Cell::Alive;
cells[13][7] = Cell::Alive;
cells[26][4] = Cell::Alive;
cells[26][5] = Cell::Alive;
cells[27][4] = Cell::Alive;
cells[27][5] = Cell::Alive;

cells[28][6] = Cell::Alive;
cells[28][7] = Cell::Alive;
cells[29][6] = Cell::Alive;
cells[29][7] = Cell::Alive;
GameOfLifeState { cells }
}

Expand Down Expand Up @@ -197,7 +215,7 @@ impl GameOfLifeState {
self.cells = next_generation;
}

fn draw_matrix(&self) -> Grid {
pub fn draw_matrix(&self) -> Grid {
let mut grid = Grid::default();

for row in 0..HEIGHT {
Expand Down
2 changes: 2 additions & 0 deletions fl16-inputmodules/src/games/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod game_of_life;
pub mod pong;
pub mod pong_animation;
pub mod snake;
pub mod snake_animation;
Loading
Loading