Rust-for-Arduboy/Examples/Arduboy-Tutorials/eeprom/src/lib.rs

75 lines
1.6 KiB
Rust
Raw Normal View History

2023-08-06 18:14:49 +02:00
#![no_std]
#![allow(non_upper_case_globals)]
2023-08-06 18:14:49 +02:00
//Include the Arduboy Library
#[allow(unused_imports)]
use arduboy_rust::prelude::*;
// Progmem data
2023-08-06 18:14:49 +02:00
// 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",
};
2023-08-06 18:14:49 +02:00
// 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();
arduboy.set_frame_rate(1);
2023-08-06 18:14:49 +02:00
arduboy.clear();
e.init(&mut s);
2023-08-06 18:14:49 +02:00
}
2023-08-06 18:14:49 +02:00
// 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.poll_buttons();
if arduboy.just_pressed(B) {
s.player1 += 1;
e.put(&s);
2023-08-06 18:14:49 +02:00
}
if arduboy.just_pressed(DOWN) {
s.player1 -= 1;
e.put(&s);
2023-08-06 18:14:49 +02:00
}
if arduboy.just_pressed(A) {
s.player1 += 1;
e.get(&mut s);
2023-08-06 18:14:49 +02:00
}
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)
2023-08-06 18:14:49 +02:00
}
//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);
2023-08-06 18:14:49 +02:00
arduboy.display();
}