From c21aa97915f5091885262bf082fdd7aa86093c63 Mon Sep 17 00:00:00 2001 From: ZennCode Date: Mon, 14 Aug 2023 16:05:11 +0200 Subject: [PATCH] added support for structs inside of eeprom memory --- Cargo.lock | 7 + Cargo.toml | 1 + .../Arduboy-Tutorials/eeprom-byte/Cargo.toml | 12 ++ .../Arduboy-Tutorials/eeprom-byte/src/lib.rs | 55 +++++++ Examples/Arduboy-Tutorials/eeprom/src/lib.rs | 65 +++++---- Examples/snake/src/lib.rs | 63 +++++--- .../src/library/arduboy/arduboy.cpp | 131 ----------------- Wrapper-Project/src/library/arduboy/arduboy.h | 38 ----- .../src/library/arduboy/arduboy_export.h | 134 ++++++++++++++++++ .../src/library/arduboy/arduboy_tones.cpp | 54 ------- .../src/library/arduboy/arduboy_tones.h | 23 --- .../library/arduboy/arduboy_tones_export.h | 62 ++++++++ .../src/library/arduboy/sprites.cpp | 23 --- Wrapper-Project/src/library/arduboy/sprites.h | 12 -- .../src/library/arduboy/sprites_export.h | 27 ++++ .../src/library/arduino/arduino.cpp | 14 -- Wrapper-Project/src/library/arduino/arduino.h | 9 -- .../src/library/arduino/arduino_export.h | 18 +++ .../src/library/arduino/eeprom.cpp | 26 ---- Wrapper-Project/src/library/arduino/eeprom.h | 12 -- .../src/library/arduino/eeprom_export.h | 26 ++++ Wrapper-Project/src/main.h | 10 +- arduboy-rust/src/library/eeprom.rs | 118 ++++++++++++--- arduboy-rust/src/library/eepromold.rs | 71 ++++++++++ arduboy-rust/src/prelude.rs | 2 +- run | 3 + run.bat | 3 + 27 files changed, 605 insertions(+), 414 deletions(-) create mode 100644 Examples/Arduboy-Tutorials/eeprom-byte/Cargo.toml create mode 100644 Examples/Arduboy-Tutorials/eeprom-byte/src/lib.rs delete mode 100644 Wrapper-Project/src/library/arduboy/arduboy.cpp delete mode 100644 Wrapper-Project/src/library/arduboy/arduboy.h create mode 100644 Wrapper-Project/src/library/arduboy/arduboy_export.h delete mode 100644 Wrapper-Project/src/library/arduboy/arduboy_tones.cpp delete mode 100644 Wrapper-Project/src/library/arduboy/arduboy_tones.h create mode 100644 Wrapper-Project/src/library/arduboy/arduboy_tones_export.h delete mode 100644 Wrapper-Project/src/library/arduboy/sprites.cpp delete mode 100644 Wrapper-Project/src/library/arduboy/sprites.h create mode 100644 Wrapper-Project/src/library/arduboy/sprites_export.h delete mode 100644 Wrapper-Project/src/library/arduino/arduino.cpp delete mode 100644 Wrapper-Project/src/library/arduino/arduino.h create mode 100644 Wrapper-Project/src/library/arduino/arduino_export.h delete mode 100644 Wrapper-Project/src/library/arduino/eeprom.cpp delete mode 100644 Wrapper-Project/src/library/arduino/eeprom.h create mode 100644 Wrapper-Project/src/library/arduino/eeprom_export.h create mode 100644 arduboy-rust/src/library/eepromold.rs diff --git a/Cargo.lock b/Cargo.lock index 91118bc..2ff6046 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,6 +44,13 @@ dependencies = [ "arduboy-rust", ] +[[package]] +name = "eeprom-byte-demo" +version = "0.1.0" +dependencies = [ + "arduboy-rust", +] + [[package]] name = "eeprom-demo" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index c9320b5..fa13098 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "arduboy-rust", "Examples/Arduboy-Tutorials/eeprom", + "Examples/Arduboy-Tutorials/eeprom-byte", "Examples/Arduboy-Tutorials/progmem", "Examples/Arduboy-Tutorials/tone", "Examples/Arduboy-Tutorials/demo2", diff --git a/Examples/Arduboy-Tutorials/eeprom-byte/Cargo.toml b/Examples/Arduboy-Tutorials/eeprom-byte/Cargo.toml new file mode 100644 index 0000000..72b5f65 --- /dev/null +++ b/Examples/Arduboy-Tutorials/eeprom-byte/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "eeprom-byte-demo" +version = "0.1.0" +authors = ["ZennDev "] +license = "MIT OR Apache-2.0" +edition = "2021" + +[lib] +crate-type = ["staticlib"] + +[dependencies] +arduboy-rust = { path = "../../../arduboy-rust" } diff --git a/Examples/Arduboy-Tutorials/eeprom-byte/src/lib.rs b/Examples/Arduboy-Tutorials/eeprom-byte/src/lib.rs new file mode 100644 index 0000000..32119d6 --- /dev/null +++ b/Examples/Arduboy-Tutorials/eeprom-byte/src/lib.rs @@ -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(); +} diff --git a/Examples/Arduboy-Tutorials/eeprom/src/lib.rs b/Examples/Arduboy-Tutorials/eeprom/src/lib.rs index 0fa5e50..fbae7a8 100644 --- a/Examples/Arduboy-Tutorials/eeprom/src/lib.rs +++ b/Examples/Arduboy-Tutorials/eeprom/src/lib.rs @@ -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(); } diff --git a/Examples/snake/src/lib.rs b/Examples/snake/src/lib.rs index 825dd8a..b6aaad8 100644 --- a/Examples/snake/src/lib.rs +++ b/Examples/snake/src/lib.rs @@ -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; diff --git a/Wrapper-Project/src/library/arduboy/arduboy.cpp b/Wrapper-Project/src/library/arduboy/arduboy.cpp deleted file mode 100644 index f71bade..0000000 --- a/Wrapper-Project/src/library/arduboy/arduboy.cpp +++ /dev/null @@ -1,131 +0,0 @@ -#include "arduboy.h" -extern Arduboy2 arduboy; - -void arduboy_begin(void) -{ - arduboy.begin(); -} -void arduboy_clear(void) -{ - arduboy.clear(); -} -void arduboy_display(void) -{ - arduboy.display(); -} -void arduboy_display_and_clear_buffer(void) -{ - arduboy.display(CLEAR_BUFFER); -} -void arduboy_draw_fast_hline(int16_t x, int16_t y, uint8_t w, uint8_t color) -{ - arduboy.drawFastHLine(x, y, w, color); -} -void arduboy_draw_fast_vline(int16_t x, int16_t y, uint8_t h, uint8_t color) -{ - arduboy.drawFastVLine(x, y, h, color); -} -void arduboy_draw_pixel(int16_t x, int16_t y, uint8_t color) -{ - arduboy.drawPixel(x, y, color); -} -void arduboy_draw_circle(int16_t x, int16_t y, uint8_t r, uint8_t color) -{ - arduboy.drawCircle(x, y, r, color); -} -void arduboy_fill_circle(int16_t x, int16_t y, uint8_t r, uint8_t color) -{ - arduboy.fillCircle(x, y, r, color); -} -void arduboy_fill_rect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t color) -{ - arduboy.fillRect(x, y, w, h, color); -} -void arduboy_draw_rect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t color) -{ - arduboy.drawRect(x, y, w, h, color); -} -unsigned long arduboy_generate_random_seed() -{ - return arduboy.generateRandomSeed(); -} -uint8_t arduboy_get_pixel(uint8_t x, uint8_t y) -{ - return arduboy.getPixel(x, y); -} -void arduboy_init_random_seed(void) -{ - arduboy.initRandomSeed(); -} -bool arduboy_just_pressed(uint8_t button) -{ - return arduboy.justPressed(button); -} -bool arduboy_just_released(uint8_t button) -{ - return arduboy.justReleased(button); -} -bool arduboy_next_frame(void) -{ - return arduboy.nextFrame(); -} -void arduboy_poll_buttons() -{ - arduboy.pollButtons(); -} -bool arduboy_pressed(uint8_t buttons) -{ - return arduboy.pressed(buttons); -} -void arduboy_print_chars(const char *cstr) -{ - arduboy.print(cstr); -} -size_t arduboy_print_chars_progmem(const char *cstr) -{ - return arduboy.print(reinterpret_cast(cstr)); -} -size_t arduboy_print_char(char c) -{ - return arduboy.print(c); -} -size_t arduboy_print_int(int n, int base) -{ - return arduboy.print(n, base); -} -size_t arduboy_print_long(long n, int base) -{ - return arduboy.print(n, base); -} -size_t arduboy_print_unsigned_char(unsigned char n, int base) -{ - return arduboy.print(n, base); -} -size_t arduboy_print_unsigned_int(unsigned int n, int base) -{ - return arduboy.print(n, base); -} -size_t arduboy_print_unsigned_long(unsigned long n, int base) -{ - return arduboy.print(n, base); -} -void arduboy_set_cursor(int16_t x, int16_t y) -{ - arduboy.setCursor(x, y); -} -void arduboy_set_frame_rate(uint8_t rate) -{ - arduboy.setFrameRate(rate); -} -bool arduboy_not_pressed(uint8_t button) -{ - arduboy.notPressed(button); -} -void arduboy_set_text_size(uint8_t s) -{ - arduboy.setTextSize(s); -} -void arduboy_invert(bool inverse) -{ - arduboy.invert(inverse); -} \ No newline at end of file diff --git a/Wrapper-Project/src/library/arduboy/arduboy.h b/Wrapper-Project/src/library/arduboy/arduboy.h deleted file mode 100644 index 2b80fa6..0000000 --- a/Wrapper-Project/src/library/arduboy/arduboy.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include - -extern "C" -{ - void arduboy_begin(void); - void arduboy_clear(void); - void arduboy_display(void); - void arduboy_display_and_clear_buffer(void); - void arduboy_draw_fast_hline(int16_t x, int16_t y, uint8_t w, uint8_t color); - void arduboy_draw_fast_vline(int16_t x, int16_t y, uint8_t h, uint8_t color); - void arduboy_draw_pixel(int16_t x, int16_t y, uint8_t color); - void arduboy_draw_circle(int16_t x, int16_t y, uint8_t r, uint8_t color); - void arduboy_fill_circle(int16_t x, int16_t y, uint8_t r, uint8_t color); - void arduboy_draw_rect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t color); - void arduboy_fill_rect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t color); - unsigned long arduboy_generate_random_seed(); - uint8_t arduboy_get_pixel(uint8_t x, uint8_t y); - void arduboy_init_random_seed(void); - bool arduboy_just_pressed(uint8_t button); - bool arduboy_just_released(uint8_t button); - bool arduboy_next_frame(void); - void arduboy_poll_buttons(); - bool arduboy_pressed(uint8_t buttons); - void arduboy_print_chars(const char *cstr); - size_t arduboy_print_char(char c); - size_t arduboy_print_chars_progmem(const char *); - size_t arduboy_print_int(int n, int base); - size_t arduboy_print_long(long n, int base); - size_t arduboy_print_unsigned_char(unsigned char n, int base); - size_t arduboy_print_unsigned_int(unsigned int n, int base); - size_t arduboy_print_unsigned_long(unsigned long n, int base); - void arduboy_set_cursor(int16_t x, int16_t y); - void arduboy_set_frame_rate(uint8_t rate); - bool arduboy_not_pressed(uint8_t button); - void arduboy_set_text_size(uint8_t s); - void arduboy_invert(bool inverse); -} \ No newline at end of file diff --git a/Wrapper-Project/src/library/arduboy/arduboy_export.h b/Wrapper-Project/src/library/arduboy/arduboy_export.h new file mode 100644 index 0000000..b9c9ca2 --- /dev/null +++ b/Wrapper-Project/src/library/arduboy/arduboy_export.h @@ -0,0 +1,134 @@ +#pragma once +#include +extern Arduboy2 arduboy; +extern "C" +{ + void arduboy_begin(void) + { + arduboy.begin(); + } + void arduboy_clear(void) + { + arduboy.clear(); + } + void arduboy_display(void) + { + arduboy.display(); + } + void arduboy_display_and_clear_buffer(void) + { + arduboy.display(CLEAR_BUFFER); + } + void arduboy_draw_fast_hline(int16_t x, int16_t y, uint8_t w, uint8_t color) + { + arduboy.drawFastHLine(x, y, w, color); + } + void arduboy_draw_fast_vline(int16_t x, int16_t y, uint8_t h, uint8_t color) + { + arduboy.drawFastVLine(x, y, h, color); + } + void arduboy_draw_pixel(int16_t x, int16_t y, uint8_t color) + { + arduboy.drawPixel(x, y, color); + } + void arduboy_draw_circle(int16_t x, int16_t y, uint8_t r, uint8_t color) + { + arduboy.drawCircle(x, y, r, color); + } + void arduboy_fill_circle(int16_t x, int16_t y, uint8_t r, uint8_t color) + { + arduboy.fillCircle(x, y, r, color); + } + void arduboy_fill_rect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t color) + { + arduboy.fillRect(x, y, w, h, color); + } + void arduboy_draw_rect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t color) + { + arduboy.drawRect(x, y, w, h, color); + } + unsigned long arduboy_generate_random_seed() + { + return arduboy.generateRandomSeed(); + } + uint8_t arduboy_get_pixel(uint8_t x, uint8_t y) + { + return arduboy.getPixel(x, y); + } + void arduboy_init_random_seed(void) + { + arduboy.initRandomSeed(); + } + bool arduboy_just_pressed(uint8_t button) + { + return arduboy.justPressed(button); + } + bool arduboy_just_released(uint8_t button) + { + return arduboy.justReleased(button); + } + bool arduboy_next_frame(void) + { + return arduboy.nextFrame(); + } + void arduboy_poll_buttons() + { + arduboy.pollButtons(); + } + bool arduboy_pressed(uint8_t buttons) + { + return arduboy.pressed(buttons); + } + void arduboy_print_chars(const char *cstr) + { + arduboy.print(cstr); + } + size_t arduboy_print_chars_progmem(const char *cstr) + { + return arduboy.print(reinterpret_cast(cstr)); + } + size_t arduboy_print_char(char c) + { + return arduboy.print(c); + } + size_t arduboy_print_int(int n, int base) + { + return arduboy.print(n, base); + } + size_t arduboy_print_long(long n, int base) + { + return arduboy.print(n, base); + } + size_t arduboy_print_unsigned_char(unsigned char n, int base) + { + return arduboy.print(n, base); + } + size_t arduboy_print_unsigned_int(unsigned int n, int base) + { + return arduboy.print(n, base); + } + size_t arduboy_print_unsigned_long(unsigned long n, int base) + { + return arduboy.print(n, base); + } + void arduboy_set_cursor(int16_t x, int16_t y) + { + arduboy.setCursor(x, y); + } + void arduboy_set_frame_rate(uint8_t rate) + { + arduboy.setFrameRate(rate); + } + bool arduboy_not_pressed(uint8_t button) + { + arduboy.notPressed(button); + } + void arduboy_set_text_size(uint8_t s) + { + arduboy.setTextSize(s); + } + void arduboy_invert(bool inverse) + { + arduboy.invert(inverse); + } +} \ No newline at end of file diff --git a/Wrapper-Project/src/library/arduboy/arduboy_tones.cpp b/Wrapper-Project/src/library/arduboy/arduboy_tones.cpp deleted file mode 100644 index cfeb6d4..0000000 --- a/Wrapper-Project/src/library/arduboy/arduboy_tones.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "arduboy_tones.h" - -void arduboy_audio_on() -{ - arduboy.audio.on(); -} -void arduboy_audio_off() -{ - arduboy.audio.off(); -} -void arduboy_audio_toggle() -{ - arduboy.audio.toggle(); -} -void arduboy_audio_save_on_off() -{ - arduboy.audio.saveOnOff(); -} -bool arduboy_audio_enabled() -{ - return arduboy.audio.enabled(); -} -void sound_tone(unsigned int frequency, unsigned long duration) -{ - sound.tone(frequency, duration); -} -void sound_tone2(unsigned int frequency1, unsigned long duration1, unsigned int frequency2, unsigned long duration2) -{ - sound.tone(frequency1, duration1, frequency2, duration2); -} -void sound_tone3(unsigned int frequency1, unsigned long duration1, unsigned int frequency2, unsigned long duration2, unsigned int frequency3, unsigned long duration3) -{ - sound.tone(frequency1, duration1, frequency2, duration2, frequency3, duration3); -} -void sound_tones(const uint16_t *tones) -{ - sound.tones(tones); -} -void sound_no_tone() -{ - sound.noTone(); -} -bool sound_playing() -{ - sound.playing(); -} -void sound_tones_in_ram(uint16_t *tones) -{ - sound.tonesInRAM(tones); -} -void sound_volume_mode(uint8_t mode) -{ - sound.volumeMode(mode); -} diff --git a/Wrapper-Project/src/library/arduboy/arduboy_tones.h b/Wrapper-Project/src/library/arduboy/arduboy_tones.h deleted file mode 100644 index 3345b74..0000000 --- a/Wrapper-Project/src/library/arduboy/arduboy_tones.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include -#include -extern ArduboyTones sound; -extern Arduboy2 arduboy; - -extern "C" -{ - void arduboy_audio_on(); - void arduboy_audio_off(); - bool arduboy_audio_enabled(); - void arduboy_audio_toggle(); - void arduboy_audio_save_on_off(); - void sound_tone(unsigned int frequency, unsigned long duration); - void sound_tone2(unsigned int frequency1, unsigned long duration1, unsigned int frequency2, unsigned long duration2); - void sound_tone3(unsigned int frequency1, unsigned long duration1, unsigned int frequency2, unsigned long duration2, unsigned int frequency3, unsigned long duration3); - void sound_tones(const uint16_t *tones); - void sound_no_tone(); - bool sound_playing(); - void sound_tones_in_ram(uint16_t *tones); - void sound_volume_mode(uint8_t mode); -} \ No newline at end of file diff --git a/Wrapper-Project/src/library/arduboy/arduboy_tones_export.h b/Wrapper-Project/src/library/arduboy/arduboy_tones_export.h new file mode 100644 index 0000000..9d63dc6 --- /dev/null +++ b/Wrapper-Project/src/library/arduboy/arduboy_tones_export.h @@ -0,0 +1,62 @@ +#pragma once + +#include +#include +extern ArduboyTones sound; +extern Arduboy2 arduboy; + +extern "C" +{ + void arduboy_audio_on() + { + arduboy.audio.on(); + } + void arduboy_audio_off() + { + arduboy.audio.off(); + } + void arduboy_audio_toggle() + { + arduboy.audio.toggle(); + } + void arduboy_audio_save_on_off() + { + arduboy.audio.saveOnOff(); + } + bool arduboy_audio_enabled() + { + return arduboy.audio.enabled(); + } + void sound_tone(unsigned int frequency, unsigned long duration) + { + sound.tone(frequency, duration); + } + void sound_tone2(unsigned int frequency1, unsigned long duration1, unsigned int frequency2, unsigned long duration2) + { + sound.tone(frequency1, duration1, frequency2, duration2); + } + void sound_tone3(unsigned int frequency1, unsigned long duration1, unsigned int frequency2, unsigned long duration2, unsigned int frequency3, unsigned long duration3) + { + sound.tone(frequency1, duration1, frequency2, duration2, frequency3, duration3); + } + void sound_tones(const uint16_t *tones) + { + sound.tones(tones); + } + void sound_no_tone() + { + sound.noTone(); + } + bool sound_playing() + { + sound.playing(); + } + void sound_tones_in_ram(uint16_t *tones) + { + sound.tonesInRAM(tones); + } + void sound_volume_mode(uint8_t mode) + { + sound.volumeMode(mode); + } +} \ No newline at end of file diff --git a/Wrapper-Project/src/library/arduboy/sprites.cpp b/Wrapper-Project/src/library/arduboy/sprites.cpp deleted file mode 100644 index 79cf43f..0000000 --- a/Wrapper-Project/src/library/arduboy/sprites.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "sprites.h" - -void arduino_draw_override(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame) -{ - Sprites::drawOverwrite(x, y, bitmap, frame); -} -void arduino_draw_external_mask(int16_t x, int16_t y, const uint8_t *bitmap, - const uint8_t *mask, uint8_t frame, uint8_t mask_frame) -{ - Sprites::drawExternalMask(x, y, bitmap, mask, frame, mask_frame); -} -void arduino_draw_plus_mask(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame) -{ - Sprites::drawPlusMask(x, y, bitmap, frame); -} -void arduino_draw_erase(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame) -{ - Sprites::drawErase(x, y, bitmap, frame); -} -void arduino_draw_self_masked(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame) -{ - Sprites::drawSelfMasked(x, y, bitmap, frame); -} \ No newline at end of file diff --git a/Wrapper-Project/src/library/arduboy/sprites.h b/Wrapper-Project/src/library/arduboy/sprites.h deleted file mode 100644 index 07c7136..0000000 --- a/Wrapper-Project/src/library/arduboy/sprites.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include - -extern "C" -{ - void arduino_draw_override(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame); - void arduino_draw_external_mask(int16_t x, int16_t y, const uint8_t *bitmap, - const uint8_t *mask, uint8_t frame, uint8_t mask_frame); - void arduino_draw_plus_mask(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame); - void arduino_draw_erase(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame); - void arduino_draw_self_masked(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame); -} \ No newline at end of file diff --git a/Wrapper-Project/src/library/arduboy/sprites_export.h b/Wrapper-Project/src/library/arduboy/sprites_export.h new file mode 100644 index 0000000..b82db27 --- /dev/null +++ b/Wrapper-Project/src/library/arduboy/sprites_export.h @@ -0,0 +1,27 @@ +#pragma once +#include + +extern "C" +{ + void arduino_draw_override(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame) + { + Sprites::drawOverwrite(x, y, bitmap, frame); + } + void arduino_draw_external_mask(int16_t x, int16_t y, const uint8_t *bitmap, + const uint8_t *mask, uint8_t frame, uint8_t mask_frame) + { + Sprites::drawExternalMask(x, y, bitmap, mask, frame, mask_frame); + } + void arduino_draw_plus_mask(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame) + { + Sprites::drawPlusMask(x, y, bitmap, frame); + } + void arduino_draw_erase(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame) + { + Sprites::drawErase(x, y, bitmap, frame); + } + void arduino_draw_self_masked(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t frame) + { + Sprites::drawSelfMasked(x, y, bitmap, frame); + } +} \ No newline at end of file diff --git a/Wrapper-Project/src/library/arduino/arduino.cpp b/Wrapper-Project/src/library/arduino/arduino.cpp deleted file mode 100644 index 0630437..0000000 --- a/Wrapper-Project/src/library/arduino/arduino.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "arduino.h" - -long arduino_random_between(long min, long max) -{ - return random(min, max); -} -long arduino_random_less_than(long max) -{ - return random(max); -} -void arduino_delay(unsigned long ms) -{ - delay(ms); -} \ No newline at end of file diff --git a/Wrapper-Project/src/library/arduino/arduino.h b/Wrapper-Project/src/library/arduino/arduino.h deleted file mode 100644 index b3e180e..0000000 --- a/Wrapper-Project/src/library/arduino/arduino.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include - -extern "C" -{ - long arduino_random_between(long min, long max); - long arduino_random_less_than(long max); - void arduino_delay(unsigned long ms); -} diff --git a/Wrapper-Project/src/library/arduino/arduino_export.h b/Wrapper-Project/src/library/arduino/arduino_export.h new file mode 100644 index 0000000..773a0a7 --- /dev/null +++ b/Wrapper-Project/src/library/arduino/arduino_export.h @@ -0,0 +1,18 @@ +#pragma once +#include + +extern "C" +{ + long arduino_random_between(long min, long max) + { + return random(min, max); + } + long arduino_random_less_than(long max) + { + return random(max); + } + void arduino_delay(unsigned long ms) + { + delay(ms); + } +} diff --git a/Wrapper-Project/src/library/arduino/eeprom.cpp b/Wrapper-Project/src/library/arduino/eeprom.cpp deleted file mode 100644 index 11918f0..0000000 --- a/Wrapper-Project/src/library/arduino/eeprom.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "eeprom.h" - -int eeprom; - -uint8_t arduboy_eeprom_read(int idx) -{ - eeprom = EEPROM.read(idx); - return eeprom; -} -void arduboy_eeprom_update(int idx, uint8_t val) -{ - EEPROM.update(idx, val); -} -void arduboy_eeprom_write(int idx, uint8_t val) -{ - EEPROM.write(idx, val); -} -uint8_t arduboy_eeprom_get(int idx) -{ - EEPROM.get(idx, eeprom); - return eeprom; -} -void arduboy_eeprom_put(int idx, uint8_t val) -{ - EEPROM.put(idx, val); -} \ No newline at end of file diff --git a/Wrapper-Project/src/library/arduino/eeprom.h b/Wrapper-Project/src/library/arduino/eeprom.h deleted file mode 100644 index c9ce39e..0000000 --- a/Wrapper-Project/src/library/arduino/eeprom.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -extern "C" -{ - uint8_t arduboy_eeprom_read(int idx); - void arduboy_eeprom_update(int idx, uint8_t val); - void arduboy_eeprom_write(int idx, uint8_t val); - uint8_t arduboy_eeprom_get(int idx); - void arduboy_eeprom_put(int idx, uint8_t val); -} diff --git a/Wrapper-Project/src/library/arduino/eeprom_export.h b/Wrapper-Project/src/library/arduino/eeprom_export.h new file mode 100644 index 0000000..985275b --- /dev/null +++ b/Wrapper-Project/src/library/arduino/eeprom_export.h @@ -0,0 +1,26 @@ +#pragma once +#include + +extern "C" +{ + uint8_t arduboy_eeprom_read(int idx) + { + return eeprom_read_byte(reinterpret_cast(idx)); + } + void arduboy_eeprom_update(int idx, uint8_t val) + { + eeprom_update_byte(reinterpret_cast(idx), val); + } + void arduboy_eeprom_write(int idx, uint8_t val) + { + eeprom_write_byte(reinterpret_cast(idx), val); + } + void arduboy_eeprom_get(uint16_t address, uint8_t *object, size_t size) + { + eeprom_read_block(object, reinterpret_cast(address), size); + } + void arduboy_eeprom_put(uint16_t address, const uint8_t *object, size_t size) + { + eeprom_update_block(object, reinterpret_cast(address), size); + } +} diff --git a/Wrapper-Project/src/main.h b/Wrapper-Project/src/main.h index 0b015e5..fae1ad8 100644 --- a/Wrapper-Project/src/main.h +++ b/Wrapper-Project/src/main.h @@ -2,8 +2,8 @@ #include #include -#include "./library/arduboy/arduboy.h" -#include "./library/arduboy/sprites.h" -#include "./library/arduboy/arduboy_tones.h" -#include "./library/arduino/arduino.h" -#include "./library/arduino/eeprom.h" +#include "./library/arduboy/arduboy_export.h" +#include "./library/arduboy/sprites_export.h" +#include "./library/arduboy/arduboy_tones_export.h" +#include "./library/arduino/arduino_export.h" +#include "./library/arduino/eeprom_export.h" diff --git a/arduboy-rust/src/library/eeprom.rs b/arduboy-rust/src/library/eeprom.rs index 4ad7837..0937519 100644 --- a/arduboy-rust/src/library/eeprom.rs +++ b/arduboy-rust/src/library/eeprom.rs @@ -1,6 +1,6 @@ use core::ffi::{c_int, c_uchar}; -pub const EEPROM_STORAGE_SPACE_START: c_int = 16; +pub const EEPROM_STORAGE_SPACE_START: i16 = 16; extern "C" { #[link_name = "arduboy_eeprom_read"] @@ -10,15 +10,31 @@ extern "C" { #[link_name = "arduboy_eeprom_write"] fn arduboy_eeprom_write_raw(idx: c_int, val: c_uchar); #[link_name = "arduboy_eeprom_get"] - fn arduboy_eeprom_get_raw(idx: c_int) -> c_uchar; + fn arduboy_eeprom_get_raw(idx: c_int, object: *mut u8, size: usize); #[link_name = "arduboy_eeprom_put"] - fn arduboy_eeprom_put_raw(idx: c_int, val: c_uchar); + fn arduboy_eeprom_put_raw(idx: c_int, object: *const u8, size: usize); } -///This is the struct to interact in a save way with the EEPROM C++ library. +///This struct to store and read structs objects to eeprom memory. +/// ## Example +/// ``` +/// static e: EEPROM = EEPROM::new(10); +/// struct Scorebord { +/// player1: u16, +/// text: &'static str, +/// } +/// static mut s: Scorebord = Scorebord { +/// player1: 0, +/// text: "lol\0", +/// }; +/// +/// // init inside of the setup function +/// e.init(&mut s); +/// } +/// ``` pub struct EEPROM { start_c1: i16, start_c2: i16, - pub idx: i16, + idx: i16, } impl EEPROM { pub const fn new(mut idx: i16) -> EEPROM { @@ -31,24 +47,90 @@ impl EEPROM { idx: EEPROM_STORAGE_SPACE_START + idx + 2, } } - pub fn init(&self) { - let c1 = self.read_start_c1(); - let c2 = self.read_start_c2(); + pub fn init(&self, your_struct: &mut T) { + let c1 = self.read(self.start_c1); + let c2 = self.read(self.start_c2); if c1 != b'Z' || c2 != b'D' { - self.update(b'Z'); - self.update(b'D'); + self.update(self.start_c1, b'Z'); + self.update(self.start_c2, b'D'); + self.put(your_struct); + }; + self.get(your_struct) + } + fn read(&self, idx: i16) -> u8 { + unsafe { arduboy_eeprom_read_raw(idx) } + } + fn update(&self, idx: i16, val: u8) { + unsafe { arduboy_eeprom_update_raw(idx, val) } + } + pub fn get(&self, your_struct: &mut T) { + let pointer = your_struct as *mut T; + let object_pointer = pointer as *mut u8; + let object_size = core::mem::size_of::(); + + unsafe { + arduboy_eeprom_get_raw(self.idx, object_pointer, object_size); + }; + } + pub fn get_direct(&self) -> T { + let mut buffer = core::mem::MaybeUninit::::uninit(); + + let pointer = buffer.as_mut_ptr(); + let object_pointer = pointer as *mut u8; + let object_size = core::mem::size_of::(); + + unsafe { + arduboy_eeprom_get_raw(self.idx, object_pointer, object_size); + }; + + return unsafe { buffer.assume_init() }; + } + pub fn put(&self, your_struct: &T) { + let pointer = your_struct as *const T; + let object_pointer = pointer as *const u8; + let object_size = core::mem::size_of::(); + + unsafe { + arduboy_eeprom_put_raw(self.idx, object_pointer, object_size); + }; + } +} +///Use this struct to store and read single bytes to/from eeprom memory. +pub struct EEPROMBYTE { + start_c1: i16, + start_c2: i16, + idx: i16, +} +impl EEPROMBYTE { + pub const fn new(mut idx: i16) -> EEPROMBYTE { + if idx > 1010 { + idx = 0 + } + EEPROMBYTE { + start_c1: EEPROM_STORAGE_SPACE_START + idx, + start_c2: EEPROM_STORAGE_SPACE_START + idx + 1, + idx: EEPROM_STORAGE_SPACE_START + idx + 2, + } + } + pub fn init(&self) { + let c1 = self.read_intern(self.start_c1); + let c2 = self.read_intern(self.start_c2); + + if c1 != b'Z' || c2 != b'D' { + self.update_intern(self.start_c1, b'Z'); + self.update_intern(self.start_c2, b'D'); self.update(0); }; } + fn read_intern(&self, idx: i16) -> u8 { + unsafe { arduboy_eeprom_read_raw(idx) } + } pub fn read(&self) -> u8 { unsafe { arduboy_eeprom_read_raw(self.idx) } } - fn read_start_c1(&self) -> u8 { - unsafe { arduboy_eeprom_read_raw(self.start_c1) } - } - fn read_start_c2(&self) -> u8 { - unsafe { arduboy_eeprom_read_raw(self.start_c2) } + fn update_intern(&self, idx: i16, val: u8) { + unsafe { arduboy_eeprom_update_raw(idx, val) } } pub fn update(&self, val: u8) { unsafe { arduboy_eeprom_update_raw(self.idx, val) } @@ -56,10 +138,4 @@ impl EEPROM { pub fn write(&self, val: u8) { unsafe { arduboy_eeprom_write_raw(self.idx, val) } } - pub fn get(&self, buffer: &mut u8) { - *buffer = unsafe { arduboy_eeprom_get_raw(self.idx) } - } - pub fn put(&self, val: u8) { - unsafe { arduboy_eeprom_put_raw(self.idx, val) } - } } diff --git a/arduboy-rust/src/library/eepromold.rs b/arduboy-rust/src/library/eepromold.rs new file mode 100644 index 0000000..114cd19 --- /dev/null +++ b/arduboy-rust/src/library/eepromold.rs @@ -0,0 +1,71 @@ +use core::ffi::{c_int, c_uchar}; + +pub const EEPROM_STORAGE_SPACE_START: c_int = 16; + +extern "C" { + #[link_name = "arduboy_eeprom_read"] + fn arduboy_eeprom_read_raw(idx: c_int) -> c_uchar; + #[link_name = "arduboy_eeprom_update"] + fn arduboy_eeprom_update_raw(idx: c_int, val: c_uchar); + #[link_name = "arduboy_eeprom_write"] + fn arduboy_eeprom_write_raw(idx: c_int, val: c_uchar); + #[link_name = "arduboy_eeprom_get"] + fn arduboy_eeprom_get_raw(idx: c_int) -> c_uchar; + #[link_name = "arduboy_eeprom_put"] + fn arduboy_eeprom_put_raw(idx: c_int, val: c_uchar); +} +///This is the struct to interact in a save way with the EEPROM C++ library. +pub struct EEPROM { + start_c1: i16, + start_c2: i16, + pub idx: i16, +} +impl EEPROM { + pub const fn new(mut idx: i16) -> EEPROM { + if idx > 950 { + idx = 0 + } + EEPROM { + start_c1: EEPROM_STORAGE_SPACE_START + idx, + start_c2: EEPROM_STORAGE_SPACE_START + idx + 1, + idx: EEPROM_STORAGE_SPACE_START + idx + 2, + } + } + pub fn init(&self) { + let c1 = self.read_start_c1(); + let c2 = self.read_start_c2(); + + if c1 != b'Z' || c2 != b'D' { + self.update_start_c1(b'Z'); + self.update_start_c2(b'D'); + self.update(0); + }; + } + pub fn read(&self) -> u8 { + unsafe { arduboy_eeprom_read_raw(self.idx) } + } + pub fn read_start_c1(&self) -> u8 { + unsafe { arduboy_eeprom_read_raw(self.start_c1) } + } + pub fn read_start_c2(&self) -> u8 { + unsafe { arduboy_eeprom_read_raw(self.start_c2) } + } + pub fn update(&self, val: u8) { + unsafe { arduboy_eeprom_update_raw(self.idx, val) } + } + pub fn update_start_c1(&self, val: u8) { + unsafe { arduboy_eeprom_update_raw(self.start_c1, val) } + } + pub fn update_start_c2(&self, val: u8) { + unsafe { arduboy_eeprom_update_raw(self.start_c2, val) } + } + pub fn write(&self, val: u8) { + unsafe { arduboy_eeprom_write_raw(self.idx, val) } + } + pub fn get(&self, buffer: &mut u8) { + *buffer = unsafe { arduboy_eeprom_get_raw(self.idx) } + } + pub fn put(&self, val: u8) { + unsafe { arduboy_eeprom_put_raw(self.idx, val) } + } +} diff --git a/arduboy-rust/src/prelude.rs b/arduboy-rust/src/prelude.rs index 086ecb9..aa1bc49 100644 --- a/arduboy-rust/src/prelude.rs +++ b/arduboy-rust/src/prelude.rs @@ -17,7 +17,7 @@ pub use crate::library::arduboy::{Color, Point, Rect, FONT_SIZE, HEIGHT, WIDTH}; pub use crate::library::arduboy_tone::*; pub use crate::library::arduino::*; pub use crate::library::c::*; -pub use crate::library::eeprom::EEPROM; +pub use crate::library::eeprom::{EEPROM, EEPROMBYTE}; pub use crate::library::progmem::Pstring; pub use crate::library::sprites; pub use crate::print::*; diff --git a/run b/run index fa1f445..5f26d90 100755 --- a/run +++ b/run @@ -20,6 +20,9 @@ then elif [ "$option" = "eeprom" ] then cargo build -p eeprom-demo --release && cp ./target/arduboy/release/libeeprom_demo.a ./Wrapper-Project/lib/libgame.a && cd Wrapper-Project/ && pio run -v -t upload && cp ./.pio/build/arduboy/firmware.hex ./.pio/build/eeprom.hex && pio run -t clean && rm lib/libgame.a && cd .. +elif [ "$option" = "eeprom-byte" ] +then + cargo build -p eeprom-byte-demo --release && cp ./target/arduboy/release/libeeprom_byte_demo.a ./Wrapper-Project/lib/libgame.a && cd Wrapper-Project/ && pio run -v -t upload && cp ./.pio/build/arduboy/firmware.hex ./.pio/build/eeprom-byte.hex && pio run -t clean && rm lib/libgame.a && cd .. elif [ "$option" = "progmem" ] then cargo build -p progmem --release && cp ./target/arduboy/release/libprogmem.a ./Wrapper-Project/lib/libgame.a && cd Wrapper-Project/ && pio run -v -t upload && cp ./.pio/build/arduboy/firmware.hex ./.pio/build/progmem.hex && pio run -t clean && rm lib/libgame.a && cd .. diff --git a/run.bat b/run.bat index c48f793..894716e 100644 --- a/run.bat +++ b/run.bat @@ -22,6 +22,9 @@ if %option%==snake ( ) else if %option%==eeprom ( powershell -Command "cargo build -p eeprom-demo --release; cp ./target/arduboy/release/libeeprom_demo.a ./Wrapper-Project/lib/libgame.a; cd Wrapper-Project/; pio run -v -t upload; cp ./.pio/build/arduboy/firmware.hex ./.pio/build/eeprom.hex; pio run -t clean; rm lib/libgame.a; cd .." goto :eof +) else if %option%==eeprom-byte ( + powershell -Command "cargo build -p eeprom-byte-demo --release; cp ./target/arduboy/release/libeeprom_byte_demo.a ./Wrapper-Project/lib/libgame.a; cd Wrapper-Project/; pio run -v -t upload; cp ./.pio/build/arduboy/firmware.hex ./.pio/build/eeprom-byte.hex; pio run -t clean; rm lib/libgame.a; cd .." + goto :eof ) else if %option%==progmem ( powershell -Command "cargo build -p progmem --release; cp ./target/arduboy/release/libprogmem.a ./Wrapper-Project/lib/libgame.a; cd Wrapper-Project/; pio run -v -t upload; cp ./.pio/build/arduboy/firmware.hex ./.pio/build/progmem.hex; pio run -t clean; rm lib/libgame.a; cd .." goto :eof