From 0721c6cc45d2812763f34cd9965d0df3550b6c0c Mon Sep 17 00:00:00 2001 From: Zenn Date: Mon, 14 Aug 2023 19:32:34 +0200 Subject: [PATCH] update of the docs and implementing a proper scorebord in snake demo --- Examples/Arduboy-Tutorials/tone/src/lib.rs | 2 + Examples/snake/src/lib.rs | 54 ++++++++++++++-------- Wrapper-Project/src/main.cpp | 2 +- arduboy-rust/src/lib.rs | 2 +- arduboy-rust/src/library/arduboy.rs | 8 +++- 5 files changed, 46 insertions(+), 22 deletions(-) diff --git a/Examples/Arduboy-Tutorials/tone/src/lib.rs b/Examples/Arduboy-Tutorials/tone/src/lib.rs index fb9453a..702904c 100644 --- a/Examples/Arduboy-Tutorials/tone/src/lib.rs +++ b/Examples/Arduboy-Tutorials/tone/src/lib.rs @@ -270,6 +270,8 @@ const BUTTON_DELAY: u32 = 200; pub unsafe extern "C" fn setup() { // put your setup code here, to run once: arduboy.begin(); + arduboy.audio_on(); + arduboy.audio_save_on_off() } // The loop() function repeats forever after setup() is done #[no_mangle] diff --git a/Examples/snake/src/lib.rs b/Examples/snake/src/lib.rs index b6aaad8..9edf085 100644 --- a/Examples/snake/src/lib.rs +++ b/Examples/snake/src/lib.rs @@ -7,15 +7,28 @@ struct Scorebord { places: [u16; 3], } impl Scorebord { - fn check_score(&mut self, mut score: u16) -> bool { - let res = self.places[2] < score; - //todo - res + fn check_score(&self, score: u16) -> bool { + self.places[2] < score + } + fn update_score(&mut self, score: u16) { + match score { + s if self.places[0] < s => { + self.places[2] = self.places[1]; + self.places[1] = self.places[0]; + self.places[0] = s + } + s if self.places[1] < s => { + self.places[2] = self.places[1]; + self.places[1] = s + } + s if self.places[2] < s => self.places[2] = s, + _ => (), + } } } static mut scoreboard: Scorebord = Scorebord { places: [0, 0, 0] }; -static eeprom: EEPROM = EEPROM::new(100); +static eeprom: EEPROM = EEPROM::new(101); const WORLD_WIDTH: u8 = 32; const WORLD_HEIGHT: u8 = 16; const SCALE_FACTOR: u8 = 4; @@ -216,12 +229,15 @@ pub unsafe extern "C" fn loop_() { arduboy.set_text_size(2); arduboy.print(f!(b"RustySnake\n\0")); arduboy.set_text_size(1); - arduboy.print(f!(b"\nControls: \nB for Pause\nA&B for reset\nScore: \0")); - arduboy.print(f!(b"Press A for Scorebord\0")); + arduboy.print(f!(b"\nControls: \nB Pause / A&B reset\n\0")); + arduboy.print(f!(b"Press B for Scorebord\0")); arduboy.print(f!(b"\nZennDev 2023\n\0")); if A.just_pressed() { snake.game_state = State::Game; } + if B.just_pressed() { + snake.game_state = State::Scorebord; + } } State::Game => { snake.get_new_dir(); @@ -257,33 +273,33 @@ pub unsafe extern "C" fn loop_() { if scoreboard.check_score(snake.points) { eeprom.put(&scoreboard); arduboy.print(f!(b"New Highscore!\0")); - arduboy.print(f!(b"\n\n\0")); - arduboy.print(f!(b"\nHigh Score: \0")); - arduboy.print(scoreboard.places[0] as u16); - arduboy.print(f!(b"\n\n\0")); - arduboy.print(f!(b"Press A to Play Again\0")); + arduboy.print(f!(b"\nYou are under the\ntop three player\0")); + arduboy.print(f!(b"\n\nYour Score: \0")); + arduboy.print(snake.points as u16); + arduboy.print(f!(b"\n\0")); + arduboy.print(f!(b"\nPress A to save the \nscore and play again\0")); } else { arduboy.print(f!(b"Game Over!\0")); arduboy.print(f!(b"\n\n\0")); arduboy.print(f!(b"Score: \0")); arduboy.print(snake.points as u16); } - arduboy.print(f!(b"\nHigh Score: \0")); - arduboy.print(scoreboard.places[0] as u16); - arduboy.print(f!(b"\n\n\0")); - arduboy.print(f!(b"Press A to Play Again\0")); if A.just_pressed() { + scoreboard.update_score(snake.points); snake.game_state = State::Reset; } } State::Scorebord => { - arduboy.set_cursor(0, 0); + arduboy.set_cursor(0, 10); arduboy.print(f!(b"1 place: \0")); arduboy.print(scoreboard.places[0]); - arduboy.print(f!(b"\n2 place: \0")); + arduboy.print(f!(b"\n\n2 place: \0")); arduboy.print(scoreboard.places[1]); - arduboy.print(f!(b"\n3 place: \0")); + arduboy.print(f!(b"\n\n3 place: \0")); arduboy.print(scoreboard.places[2]); + if A.just_pressed() || B.just_pressed() { + snake.game_state = State::Title; + } } } if (A | B).pressed() { diff --git a/Wrapper-Project/src/main.cpp b/Wrapper-Project/src/main.cpp index f29d497..60404c0 100644 --- a/Wrapper-Project/src/main.cpp +++ b/Wrapper-Project/src/main.cpp @@ -1,4 +1,4 @@ #include "main.h" Arduboy2 arduboy; -ArduboyTones sound(arduboy.audio.enabled); +ArduboyTones sound(arduboy.audio.enabled); \ No newline at end of file diff --git a/arduboy-rust/src/lib.rs b/arduboy-rust/src/lib.rs index acc73c5..f6ca4cc 100644 --- a/arduboy-rust/src/lib.rs +++ b/arduboy-rust/src/lib.rs @@ -8,7 +8,7 @@ pub mod prelude; mod print; pub use crate::library::arduboy::{Arduboy, Color, FONT_SIZE, HEIGHT, WIDTH}; pub use crate::library::arduboy_tone::Sound; -pub use crate::library::eeprom::EEPROM; +pub use crate::library::eeprom::{EEPROM, EEPROMBYTE}; pub use crate::library::{arduboy_tone_pitch, c, sprites}; pub use hardware::buttons; diff --git a/arduboy-rust/src/library/arduboy.rs b/arduboy-rust/src/library/arduboy.rs index 5120f84..e0a4ca9 100644 --- a/arduboy-rust/src/library/arduboy.rs +++ b/arduboy-rust/src/library/arduboy.rs @@ -328,9 +328,15 @@ impl Arduboy { pub fn audio_save_on_off(&self) { unsafe { arduboy_audio_save_on_off() } } - pub fn audio_save_toggle(&self) { + pub fn audio_toggle(&self) { unsafe { arduboy_audio_toggle() } } + pub fn audio_on_and_save(&self) { + unsafe { + arduboy_audio_on(); + arduboy_audio_save_on_off() + } + } ///Get the current sound state. /// ///### Returns