From 049de19559dc360b36a5214c5a22bfbe7ed23d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Wed, 21 Dec 2022 09:58:11 +0100 Subject: [PATCH] feat: use u8 everywhere --- src/animation.rs | 82 ++++++++++++++++++++++++------------- src/entrypoint.rs | 74 +++++++++++++++------------------ src/filled_seven_segment.rs | 16 ++------ src/led_matrix.rs | 19 ++++----- 4 files changed, 96 insertions(+), 95 deletions(-) diff --git a/src/animation.rs b/src/animation.rs index 026e743..b5ad4b7 100644 --- a/src/animation.rs +++ b/src/animation.rs @@ -3,13 +3,13 @@ use super::led_matrix; const WIN_ANIMATION_MAX_LED_OUTER_STEP: u8 = 5; // max led_step const WIN_ANIMATION_MAX_LED_STEP: u8 = 4; // add led_step -const WIN_ANIMATION_MAX_LED_INNER_STEP: u16 = 2500; // 10000; // add led_quarter +const WIN_ANIMATION_MAX_LED_INNER_STEP: u8 = 10; // multiplied by 256 internally // add led_quarter -const HELO_ANIMATION_MAX_INNER_STEP: u16 = 5000; // 20000; -const HELO_ANIMATION_MAX_OUTER_STEP: u16 = 5; +const HELO_ANIMATION_MAX_INNER_STEP: u8 = 20; // multiplied by 256 internally +const HELO_ANIMATION_MAX_OUTER_STEP: u8 = 5; -const GUESS_ANIMATION_MAX_STEP: u16 = 3000; -const DIGIT_INCREMENT_ANIMATION_MAX_STEP: u16 = 1000; +const GUESS_ANIMATION_MAX_STEP: u8 = 12; // multiplied by 256 internally +const DIGIT_INCREMENT_ANIMATION_MAX_STEP: u8 = 4; // multiplied by 256 internally #[derive(PartialEq, Eq)] pub enum AnimationState { @@ -24,32 +24,37 @@ pub trait Animation { } pub struct HelloAnimation { - pub inner_step: u16, - pub outer_step: u16, - pub hidden: bool + pub inner_step: u8, + pub outer_step: u8, + pub hidden: bool, + pub internal_step: u8, } pub struct WinAnimation { - pub number: u16, + pub number: [u8; 4], pub led_step: u8, pub led_quarter: u8, - pub led_inner: u16, - pub hidden: bool + pub led_inner: u8, + pub hidden: bool, + pub internal_step: u8, } pub struct GuessAnimation { - pub step: u16 + pub step: u8, + pub internal_step: u8, } pub struct DigitIncrementAnimation { pub digit_index: usize, - pub step: u16 + pub step: u8, + pub internal_step: u8, } impl DigitIncrementAnimation { pub fn create(digit_index: usize) -> DigitIncrementAnimation { DigitIncrementAnimation { step: 0, + internal_step: 0, digit_index } } @@ -69,7 +74,12 @@ impl Animation for DigitIncrementAnimation { if self.step == 0 { seven_segment.hide_digit(self.digit_index); } - self.step += 1; + + self.internal_step += 1; + if self.internal_step == 255 { + self.step += 1; + self.internal_step = 0; + } AnimationState::Running } @@ -86,7 +96,8 @@ impl Animation for DigitIncrementAnimation { impl GuessAnimation { pub fn create() -> GuessAnimation { GuessAnimation { - step: 0 + step: 0, + internal_step: 0 } } @@ -104,7 +115,12 @@ impl Animation for GuessAnimation { if self.step == 0 { seven_segment.hide_all_digits(); } - self.step += 1; + + self.internal_step += 1; + if self.internal_step == 255 { + self.step += 1; + self.internal_step = 0; + } AnimationState::Running } @@ -119,17 +135,18 @@ impl Animation for GuessAnimation { } impl WinAnimation { - pub fn create(number: u16) -> WinAnimation { + pub fn create(number: [u8; 4]) -> WinAnimation { WinAnimation { number, led_inner: 0, led_quarter: 0, led_step: 0, - hidden: true + hidden: true, + internal_step: 0 } } - pub fn reset(&mut self, number: u16) { + pub fn reset(&mut self, number: [u8; 4]) { self.number = number; self.led_step = 0; self.led_quarter = 0; @@ -162,17 +179,17 @@ impl Animation for WinAnimation { led_matrix.clear(); if self.led_step < 2 { - led_matrix.set(self.led_quarter, self.led_step, true); + led_matrix.set(self.led_quarter, self.led_step); } else if self.led_step == 2 { - led_matrix.set(self.led_quarter, 0, true); - led_matrix.set(self.led_quarter, 1, true); + led_matrix.set(self.led_quarter, 0); + led_matrix.set(self.led_quarter, 1); } else if self.led_step == 3 { - led_matrix.set(3 - self.led_quarter, 0, true); - led_matrix.set(3 - self.led_quarter, 1, true); + led_matrix.set(3 - self.led_quarter, 0); + led_matrix.set(3 - self.led_quarter, 1); } else { for i in 0..=self.led_quarter { - led_matrix.set(i, 0, true); - led_matrix.set(i, 1, true); + led_matrix.set(i, 0); + led_matrix.set(i, 1); } } @@ -186,7 +203,11 @@ impl Animation for WinAnimation { self.hidden = !self.hidden; } - self.led_inner += 1; + self.internal_step += 1; + if self.internal_step == 255 { + self.led_inner += 1; + self.internal_step = 0; + } AnimationState::Running } @@ -206,6 +227,7 @@ impl HelloAnimation { HelloAnimation { inner_step: 0, outer_step: 0, + internal_step: 0, hidden: false } } @@ -242,7 +264,11 @@ impl Animation for HelloAnimation { return AnimationState::End } - self.inner_step += 1; + self.internal_step += 1; + if self.internal_step == 255 { + self.inner_step += 1; + self.internal_step = 0; + } AnimationState::Running } diff --git a/src/entrypoint.rs b/src/entrypoint.rs index 967caa0..9b05f8d 100644 --- a/src/entrypoint.rs +++ b/src/entrypoint.rs @@ -25,22 +25,26 @@ static mut HELLO_ANIMATION: animation::HelloAnimation = animation::HelloAnimatio inner_step: 0, outer_step: 0, hidden: false, + internal_step: 0, }; static mut WIN_ANIMATION: animation::WinAnimation = animation::WinAnimation { - number: 0, + number: [0; 4], led_step: 0, led_quarter: 0, led_inner: 0, hidden: true, + internal_step: 0, }; static mut GUESS_ANIMATION: animation::GuessAnimation = animation::GuessAnimation { step: 0, + internal_step: 0, }; static mut DIGIT_INC_ANIMATION: animation::DigitIncrementAnimation = animation::DigitIncrementAnimation { step: 0, digit_index: 0, + internal_step: 0, }; #[atmega_hal::entry] @@ -116,7 +120,7 @@ fn main() -> ! { unsafe { HELLO_ANIMATION = animation::HelloAnimation::create(); - WIN_ANIMATION = animation::WinAnimation::create(0); + WIN_ANIMATION = animation::WinAnimation::create([0; 4]); GUESS_ANIMATION = animation::GuessAnimation::create(); DIGIT_INC_ANIMATION = animation::DigitIncrementAnimation::create(0); game.set_animation(&mut HELLO_ANIMATION); @@ -160,11 +164,11 @@ pub struct Game { seven_segment: filled_seven_segment::FilledSevenSegment, led_matrix: led_matrix::LEDMatrix, state: GameState, - guessing_number: Option, - current_number: Option, + guessing_number: Option<[u8; DIGITS]>, + current_number: Option<[u8; DIGITS]>, animation: Option<&'static mut dyn animation::Animation>, rng: rng::Rng, - buttons: [button::Button; 4], + buttons: [button::Button; DIGITS], confirm: button::Button, } @@ -224,31 +228,14 @@ impl Game { self.animation = Some(animation); } - fn get_digit(number: u16, digit_index: usize) -> u8 { - let mut digit = number; - for _ in 0..digit_index { - digit /= 10; - } - - (digit % 10).try_into().unwrap() - } - fn update_led_matrix(&mut self) { self.led_matrix.clear(); - let current_number = self.current_number.unwrap(); - let guessing_number = self.guessing_number.unwrap(); - - let mut current_digits: [u8; DIGITS] = [0, 0, 0, 0]; - let mut guessing_digits: [u8; DIGITS] = [0, 0, 0, 0]; - - for i in 0..DIGITS { - current_digits[i] = Self::get_digit(current_number, i); - guessing_digits[i] = Self::get_digit(guessing_number, i); - } + let current_digits = self.current_number.unwrap(); + let guessing_digits = self.guessing_number.unwrap(); for i in 0..DIGITS { if current_digits[i] == guessing_digits[i] { - self.led_matrix.set(i.try_into().unwrap(), LED_MATRIX_CORRECT_ROW, true); + self.led_matrix.set(i.try_into().unwrap(), LED_MATRIX_CORRECT_ROW); } for j in 0..DIGITS { @@ -258,8 +245,7 @@ impl Game { { self.led_matrix.set( i.try_into().unwrap(), - LED_MATRIX_INCORRECT_POSITION_ROW, - true + LED_MATRIX_INCORRECT_POSITION_ROW ); } } @@ -267,21 +253,18 @@ impl Game { } fn increase_digit(&mut self, digit_index: usize) { - let current_number = self.current_number.unwrap(); - let mut order = 1; - for _ in 0..digit_index { - order *= 10; - } + let mut current_number = self.current_number.unwrap(); - let current_digit = Self::get_digit(current_number, digit_index); - let new_digit: u16 = ((current_digit + 1) % 10).into(); + let mut current_digit = current_number[digit_index]; + current_digit += 1; - let trimmed_number = current_number % order; - let mut new_number = current_number - (current_number % (order * 10)); - new_number += new_digit * order + trimmed_number; + if current_digit == 10 { + current_digit = 0; + } - self.current_number = Some(new_number); - self.seven_segment.set_number(new_number); + current_number[digit_index] = current_digit; + self.current_number = Some(current_number); + self.seven_segment.set_number(current_number); } fn end_current_game(&mut self) { @@ -304,9 +287,16 @@ impl Game { self.animation = None; } - self.guessing_number = Some(self.rng.take_u16() % 10000); - self.current_number = Some(0); - self.seven_segment.set_number(self.current_number.unwrap()); + let mut guessing_number = [0; DIGITS]; + for i in 0..DIGITS { + guessing_number[i] = self.rng.take_u8() % 10; + } + + let current_number = [0; 4]; + + self.guessing_number = Some(guessing_number); + self.current_number = Some(current_number); + self.seven_segment.set_number(current_number); self.led_matrix.clear(); self.state = GameState::Play; diff --git a/src/filled_seven_segment.rs b/src/filled_seven_segment.rs index c8f0316..208e2c0 100644 --- a/src/filled_seven_segment.rs +++ b/src/filled_seven_segment.rs @@ -20,15 +20,6 @@ impl FilledSevenSegment { } } - fn get_digit(digit: u16, digit_index: usize) -> u8 { - let mut digit = digit; - for _ in 0..digit_index { - digit /= 10; - } - - return (digit % 10).try_into().unwrap(); - } - #[inline] pub fn hide_digit(&mut self, digit_index: usize) { self.hide |= 1 << digit_index; @@ -55,10 +46,9 @@ impl FilledSevenSegment { } } - pub fn set_number(&mut self, number: u16) { - for i in 0..4_usize { - let digit = Self::get_digit(number, i); - self.digits[i] = Some(digit); + pub fn set_number(&mut self, number: [u8; 4]) { + for (i, digit) in number.iter().enumerate() { + self.digits[i] = Some(*digit); } } diff --git a/src/led_matrix.rs b/src/led_matrix.rs index fca5290..6ae3f5f 100644 --- a/src/led_matrix.rs +++ b/src/led_matrix.rs @@ -27,7 +27,11 @@ impl LEDMatrix { #[inline] fn get_position(width: u8, x: u8, y: u8) -> u8 { - return width*y + x; + if y > 0 { + return width + x; + } + + return x; } #[inline] @@ -40,17 +44,8 @@ impl LEDMatrix { self.data = data; } - pub fn set(&mut self, x: u8, y: u8, value: bool) { - if x >= self.width || y >= self.height { - return - } - - let mask = 1 << Self::get_position(self.width, x, y); - if value { - self.data |= mask; - } else { - self.data &= !mask; - } + pub fn set(&mut self, x: u8, y: u8) { + self.data |= 1 << Self::get_position(self.width, x, y); } #[inline] -- 2.48.1