added support for structs inside of eeprom memory
This commit is contained in:
parent
d382da0300
commit
c21aa97915
27 changed files with 605 additions and 414 deletions
12
Examples/Arduboy-Tutorials/eeprom-byte/Cargo.toml
Normal file
12
Examples/Arduboy-Tutorials/eeprom-byte/Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "eeprom-byte-demo"
|
||||
version = "0.1.0"
|
||||
authors = ["ZennDev <zenndev@protonmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["staticlib"]
|
||||
|
||||
[dependencies]
|
||||
arduboy-rust = { path = "../../../arduboy-rust" }
|
55
Examples/Arduboy-Tutorials/eeprom-byte/src/lib.rs
Normal file
55
Examples/Arduboy-Tutorials/eeprom-byte/src/lib.rs
Normal file
|
@ -0,0 +1,55 @@
|
|||
#![no_std]
|
||||
#![allow(non_upper_case_globals)]
|
||||
//Include the Arduboy Library
|
||||
//Initialize the arduboy object
|
||||
#[allow(unused_imports)]
|
||||
use arduboy_rust::prelude::*;
|
||||
|
||||
// #[link_section = ".progmem.data"]
|
||||
|
||||
// Setup eeprom memory
|
||||
static mut eeprom: EEPROMBYTE = EEPROMBYTE::new(10);
|
||||
|
||||
static mut count: u8 = 0;
|
||||
|
||||
// The setup() function runs once when you turn your Arduboy on
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn setup() {
|
||||
// put your setup code here, to run once:
|
||||
arduboy.begin();
|
||||
eeprom.init();
|
||||
arduboy.clear();
|
||||
arduboy.set_frame_rate(30);
|
||||
}
|
||||
// The loop() function repeats forever after setup() is done
|
||||
#[no_mangle]
|
||||
#[export_name = "loop"]
|
||||
pub unsafe extern "C" fn loop_() {
|
||||
// put your main code here, to run repeatedly:
|
||||
if !arduboy.next_frame() {
|
||||
return;
|
||||
}
|
||||
arduboy.clear();
|
||||
arduboy.poll_buttons();
|
||||
if arduboy.just_pressed(UP) {
|
||||
count += 1;
|
||||
}
|
||||
if arduboy.just_pressed(DOWN) {
|
||||
count -= 1;
|
||||
}
|
||||
if arduboy.just_pressed(A) {
|
||||
eeprom.update(count)
|
||||
}
|
||||
arduboy.set_cursor(0, 0);
|
||||
arduboy.print(count as u16);
|
||||
|
||||
arduboy.set_cursor(0, 30);
|
||||
arduboy.print(f!(b"Counter:\0"));
|
||||
arduboy.print(count as u16);
|
||||
arduboy.set_cursor(0, 40);
|
||||
arduboy.print(f!(b"eeprom:\0"));
|
||||
|
||||
arduboy.print(eeprom.read() as u16);
|
||||
|
||||
arduboy.display();
|
||||
}
|
|
@ -1,27 +1,33 @@
|
|||
#![no_std]
|
||||
#![allow(non_upper_case_globals)]
|
||||
|
||||
//Include the Arduboy Library
|
||||
//Initialize the arduboy object
|
||||
#[allow(unused_imports)]
|
||||
use arduboy_rust::prelude::*;
|
||||
|
||||
// #[link_section = ".progmem.data"]
|
||||
// Progmem data
|
||||
|
||||
// Setup eeprom memory
|
||||
static mut eeprom: EEPROM = EEPROM::new(10);
|
||||
|
||||
static mut count: u8 = 0;
|
||||
static mut MEM: u8 = 0;
|
||||
// dynamic ram variables
|
||||
static e: EEPROM = EEPROM::new(10);
|
||||
struct Scorebord {
|
||||
player1: u16,
|
||||
text: &'static str,
|
||||
}
|
||||
static mut s: Scorebord = Scorebord {
|
||||
player1: 0,
|
||||
text: "lol\0",
|
||||
};
|
||||
|
||||
// The setup() function runs once when you turn your Arduboy on
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn setup() {
|
||||
// put your setup code here, to run once:
|
||||
arduboy.begin();
|
||||
eeprom.init();
|
||||
arduboy.set_frame_rate(1);
|
||||
arduboy.clear();
|
||||
arduboy.set_frame_rate(30);
|
||||
e.init(&mut s);
|
||||
}
|
||||
|
||||
// The loop() function repeats forever after setup() is done
|
||||
#[no_mangle]
|
||||
#[export_name = "loop"]
|
||||
|
@ -30,30 +36,39 @@ pub unsafe extern "C" fn loop_() {
|
|||
if !arduboy.next_frame() {
|
||||
return;
|
||||
}
|
||||
arduboy.clear();
|
||||
arduboy.poll_buttons();
|
||||
if arduboy.just_pressed(UP) {
|
||||
count += 1;
|
||||
if arduboy.just_pressed(B) {
|
||||
s.player1 += 1;
|
||||
e.put(&s);
|
||||
}
|
||||
if arduboy.just_pressed(DOWN) {
|
||||
count -= 1;
|
||||
s.player1 -= 1;
|
||||
e.put(&s);
|
||||
}
|
||||
if arduboy.just_pressed(A) {
|
||||
unsafe { eeprom.put(count) }
|
||||
s.player1 += 1;
|
||||
e.get(&mut s);
|
||||
}
|
||||
if arduboy.just_pressed(B) {
|
||||
eeprom.get(&mut MEM)
|
||||
arduboy.clear();
|
||||
if s.player1 == 5 {
|
||||
arduboy.print(f!(b"lolxd\0"));
|
||||
s.text = "it works!!!\0";
|
||||
e.put(&s)
|
||||
} else {
|
||||
arduboy.print(f!(b"nope\0"));
|
||||
s.text = "lol\0";
|
||||
e.put(&s)
|
||||
}
|
||||
arduboy.set_cursor(0, 0);
|
||||
arduboy.print(count as u16);
|
||||
|
||||
arduboy.set_cursor(0, 30);
|
||||
arduboy.print(f!(b"Memory:\0"));
|
||||
arduboy.print(MEM as u16);
|
||||
arduboy.set_cursor(0, 40);
|
||||
arduboy.print(f!(b"eeprom:\0"));
|
||||
|
||||
arduboy.print(eeprom.read() as u16);
|
||||
//e.get(&mut s);
|
||||
arduboy.print("\n\0");
|
||||
arduboy.print("eeprom save: \0");
|
||||
let ss: Scorebord = e.get_direct();
|
||||
arduboy.print(ss.player1);
|
||||
arduboy.print("\nscore save: \0");
|
||||
arduboy.print(s.player1);
|
||||
arduboy.print("\n \0");
|
||||
arduboy.print(s.text);
|
||||
|
||||
arduboy.display();
|
||||
}
|
||||
|
|
|
@ -1,10 +1,21 @@
|
|||
#![no_std]
|
||||
#![allow(non_upper_case_globals)]
|
||||
use arduboy_rust::prelude::*;
|
||||
#[allow(dead_code)]
|
||||
#[repr(C)]
|
||||
struct Scorebord {
|
||||
places: [u16; 3],
|
||||
}
|
||||
impl Scorebord {
|
||||
fn check_score(&mut self, mut score: u16) -> bool {
|
||||
let res = self.places[2] < score;
|
||||
//todo
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
static mut MEM: u8 = 0;
|
||||
static SCORE: EEPROM = EEPROM::new(100);
|
||||
|
||||
static mut scoreboard: Scorebord = Scorebord { places: [0, 0, 0] };
|
||||
static eeprom: EEPROM = EEPROM::new(100);
|
||||
const WORLD_WIDTH: u8 = 32;
|
||||
const WORLD_HEIGHT: u8 = 16;
|
||||
const SCALE_FACTOR: u8 = 4;
|
||||
|
@ -13,6 +24,7 @@ enum State {
|
|||
Title,
|
||||
Game,
|
||||
Win,
|
||||
Scorebord,
|
||||
GameOver,
|
||||
Reset,
|
||||
Pause,
|
||||
|
@ -26,7 +38,7 @@ enum Direction {
|
|||
}
|
||||
struct Snake {
|
||||
game_state: State,
|
||||
points: u8,
|
||||
points: u16,
|
||||
len: u8,
|
||||
pos: [(u8, u8); 255 as usize],
|
||||
next_food: (u8, u8),
|
||||
|
@ -179,7 +191,7 @@ static mut snake: Snake = Snake {
|
|||
#[no_mangle]
|
||||
pub unsafe extern "C" fn setup() {
|
||||
arduboy.begin();
|
||||
SCORE.init();
|
||||
eeprom.init(&mut scoreboard);
|
||||
arduboy.init_random_seed();
|
||||
arduboy.set_frame_rate(60);
|
||||
arduboy.clear();
|
||||
|
@ -204,7 +216,8 @@ 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\n\0"));
|
||||
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"\nZennDev 2023\n\0"));
|
||||
if A.just_pressed() {
|
||||
snake.game_state = State::Game;
|
||||
|
@ -240,28 +253,38 @@ pub unsafe extern "C" fn loop_() {
|
|||
}
|
||||
}
|
||||
State::GameOver => {
|
||||
SCORE.get(&mut MEM);
|
||||
if MEM == 255 {
|
||||
MEM = 0;
|
||||
}
|
||||
if snake.points > MEM {
|
||||
SCORE.put(snake.points);
|
||||
}
|
||||
|
||||
arduboy.set_cursor(0, 0);
|
||||
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);
|
||||
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"));
|
||||
} 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(MEM as u16);
|
||||
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() {
|
||||
snake.game_state = State::Reset;
|
||||
}
|
||||
}
|
||||
State::Scorebord => {
|
||||
arduboy.set_cursor(0, 0);
|
||||
arduboy.print(f!(b"1 place: \0"));
|
||||
arduboy.print(scoreboard.places[0]);
|
||||
arduboy.print(f!(b"\n2 place: \0"));
|
||||
arduboy.print(scoreboard.places[1]);
|
||||
arduboy.print(f!(b"\n3 place: \0"));
|
||||
arduboy.print(scoreboard.places[2]);
|
||||
}
|
||||
}
|
||||
if (A | B).pressed() {
|
||||
snake.game_state = State::Reset;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue