diff --git a/Examples/Arduboy-Tutorials/demo2/src/lib.rs b/Examples/Arduboy-Tutorials/demo2/src/lib.rs index 23f8f6b..fe8f0a3 100644 --- a/Examples/Arduboy-Tutorials/demo2/src/lib.rs +++ b/Examples/Arduboy-Tutorials/demo2/src/lib.rs @@ -1,4 +1,5 @@ #![no_std] +#![allow(non_upper_case_globals)] //Include the Arduboy Library //Initialize the arduboy object use arduboy_rust::prelude::*; @@ -8,7 +9,7 @@ pub unsafe extern "C" fn setup() { // put your setup code here, to run once: arduboy.begin(); arduboy.clear(); - arduboy.print("Holmes is cool!\0"); + arduboy.print(f!(b"Holmes is cool!\0")); arduboy.display(); } #[no_mangle] diff --git a/Examples/Arduboy-Tutorials/demo4/src/lib.rs b/Examples/Arduboy-Tutorials/demo4/src/lib.rs index 6ed0ec8..1b57d25 100644 --- a/Examples/Arduboy-Tutorials/demo4/src/lib.rs +++ b/Examples/Arduboy-Tutorials/demo4/src/lib.rs @@ -43,7 +43,7 @@ pub unsafe extern "C" fn loop_() { //Move the cursor to the position 30, 30 of the screen arduboy.set_cursor(30, 30); //Printing the yay (important always put the \0 at the end for &str) - arduboy.print("Yay!\0"); + arduboy.print(f!(b"Yay!\0")); } //Move the cursor back to the top-left of the screen arduboy.set_cursor(0, 0); diff --git a/Examples/Arduboy-Tutorials/demo5/src/lib.rs b/Examples/Arduboy-Tutorials/demo5/src/lib.rs index fd6be95..7a9f60d 100644 --- a/Examples/Arduboy-Tutorials/demo5/src/lib.rs +++ b/Examples/Arduboy-Tutorials/demo5/src/lib.rs @@ -33,9 +33,9 @@ pub unsafe extern "C" fn loop_() { if attempts == 7 { //Game Over screen arduboy.set_cursor(0, 0); - arduboy.print("You lost!\0"); - arduboy.print("\n\0"); - arduboy.print("Correct Number: \0"); + arduboy.print(f!(b"You lost!\0")); + arduboy.print(f!(b"\n\0")); + arduboy.print(f!(b"Correct Number: \0")); arduboy.print(randomnumber); if A.just_pressed() { randomnumber = random_between(1, 101) as i16; @@ -59,30 +59,30 @@ pub unsafe extern "C" fn loop_() { } } arduboy.set_cursor(0, 0); - arduboy.print("Attempt: \0"); + arduboy.print(f!(b"Attempt: \0")); arduboy.print(attempts); - arduboy.print("\n\0"); - arduboy.print("Number to guess: \0"); + arduboy.print(f!(b"\n\0")); + arduboy.print(f!(b"Number to guess: \0")); arduboy.print(guessednumber); - arduboy.print("\n\0"); + arduboy.print(f!(b"\n\0")); if attempts == 0 { - arduboy.print("Good luck!\0"); + arduboy.print(f!(b"Good luck!\0")); } else { arduboy.print(lastguess); if lastguess > randomnumber { - arduboy.print(" is too high!\0"); + arduboy.print(f!(b" is too high!\0")); } if lastguess < randomnumber { - arduboy.print(" is too low!\0"); + arduboy.print(f!(b" is too low!\0")); } } } } else { //Tell the player that they won! arduboy.set_cursor(0, 0); - arduboy.print("You won!\0"); - arduboy.print("\n\0"); - arduboy.print("Correct Number: \0"); + arduboy.print(f!(b"You won!\0")); + arduboy.print(f!(b"\n\0")); + arduboy.print(f!(b"Correct Number: \0")); arduboy.print(randomnumber); if A.just_pressed() { diff --git a/Examples/Arduboy-Tutorials/eeprom/src/lib.rs b/Examples/Arduboy-Tutorials/eeprom/src/lib.rs index 650463e..0fa5e50 100644 --- a/Examples/Arduboy-Tutorials/eeprom/src/lib.rs +++ b/Examples/Arduboy-Tutorials/eeprom/src/lib.rs @@ -48,10 +48,10 @@ pub unsafe extern "C" fn loop_() { arduboy.print(count as u16); arduboy.set_cursor(0, 30); - arduboy.print("Memory:\0"); + arduboy.print(f!(b"Memory:\0")); arduboy.print(MEM as u16); arduboy.set_cursor(0, 40); - arduboy.print("eeprom:\0"); + arduboy.print(f!(b"eeprom:\0")); arduboy.print(eeprom.read() as u16); diff --git a/Examples/Arduboy-Tutorials/progmem/src/lib.rs b/Examples/Arduboy-Tutorials/progmem/src/lib.rs index e79dc37..2346171 100644 --- a/Examples/Arduboy-Tutorials/progmem/src/lib.rs +++ b/Examples/Arduboy-Tutorials/progmem/src/lib.rs @@ -62,58 +62,6 @@ progmem!( TONES_REPEAT, ]; ); -//#[link_section = ".progmem.data"] -//static player_sprite1: [u8; 34] = [ -// 16, 16, 0xfe, 0x01, 0x3d, 0x25, 0x25, 0x3d, 0x01, 0x01, 0xc1, 0x01, 0x3d, 0x25, 0x25, 0x3d, -// 0x01, 0xfe, 0x7f, 0x80, 0x9c, 0xbc, 0xb0, 0xb0, 0xb2, 0xb2, 0xb3, 0xb0, 0xb0, 0xb0, 0xbc, 0x9c, -// 0x80, 0x7f, -//]; -// #[link_section = ".progmem.data"] -// static tones: [u16; 43] = [ -// NOTE_E4, -// 400, -// NOTE_D4, -// 200, -// NOTE_C4, -// 400, -// NOTE_REST, -// 200, -// NOTE_D4, -// 200, -// NOTE_C4, -// 300, -// NOTE_REST, -// 100, -// NOTE_C4, -// 300, -// NOTE_REST, -// 100, -// NOTE_E4, -// 300, -// NOTE_REST, -// 100, -// NOTE_G4, -// 300, -// NOTE_REST, -// 100, -// NOTE_F4, -// 300, -// NOTE_REST, -// 100, -// NOTE_A4, -// 300, -// NOTE_REST, -// 100, -// NOTE_D5H, -// 200, -// NOTE_REST, -// 200, -// NOTE_D5H, -// 200, -// NOTE_REST, -// 1500, -// TONES_REPEAT, -// ]; // dynamic ram variables static mut playerx: c_int = 5; diff --git a/Examples/Arduboy-Tutorials/tone/src/lib.rs b/Examples/Arduboy-Tutorials/tone/src/lib.rs index c220b9b..fb9453a 100644 --- a/Examples/Arduboy-Tutorials/tone/src/lib.rs +++ b/Examples/Arduboy-Tutorials/tone/src/lib.rs @@ -341,7 +341,7 @@ pub unsafe extern "C" fn loop_() { sound.tone(1000, 0); arduboy.clear(); - arduboy.print("tone(1000)\n\nB: no_tone()\n delay(1000)\n break\0"); + arduboy.print(f!(b"tone(1000)\n\nB: no_tone()\n delay(1000)\n break\0")); while sound.playing() { move_circle(); if arduboy.pressed(B_BUTTON) { @@ -353,7 +353,7 @@ pub unsafe extern "C" fn loop_() { sound.tone(500, 4000); arduboy.clear(); - arduboy.print("tone(500, 4000)\n\nB: break\0"); + arduboy.print(f!(b"tone(500, 4000)\n\nB: break\0")); while sound.playing() { move_circle(); if arduboy.pressed(B_BUTTON) { @@ -364,7 +364,7 @@ pub unsafe extern "C" fn loop_() { sound.tone2(NOTE_C4, 500, NOTE_C5H, 5000); arduboy.clear(); - arduboy.print("tone(C4,500,C5H,5000)\n\nB: no_tone(), break\0"); + arduboy.print(f!(b"tone(C4,500,C5H,5000)\n\nB: no_tone(), break\0")); while sound.playing() { move_circle(); if arduboy.pressed(B_BUTTON) { @@ -376,7 +376,7 @@ pub unsafe extern "C" fn loop_() { sound.tone3(NOTE_C7H, 500, NOTE_REST, 1000, NOTE_C6, 5000); arduboy.clear(); - arduboy.print("tone(C7H,500,\n REST,1000,\n C6,6000)\n\nB: no_tone(), break\0"); + arduboy.print(f!(b"tone(C7H,500,\n REST,1000,\n C6,6000)\n\nB: no_tone(), break\0")); while sound.playing() { move_circle(); if arduboy.pressed(B_BUTTON) { @@ -388,7 +388,7 @@ pub unsafe extern "C" fn loop_() { sound.tones(get_tones_addr!(allNotes)); arduboy.clear(); - arduboy.print("tones(allNotes)\n\nA: no_tone(), again\nUP: again\nB: break\0"); + arduboy.print(f!(b"tones(allNotes)\n\nA: no_tone(), again\nUP: again\nB: break\0")); while sound.playing() { move_circle(); if arduboy.pressed(A_BUTTON) { @@ -407,7 +407,7 @@ pub unsafe extern "C" fn loop_() { new_notes = false; sound.tones_in_ram(get_tones_addr!(in_ram) as *mut u32); arduboy.clear(); - arduboy.print("tonesInRAM(inRAM)\n\nA: change notes\nB: break\0"); + arduboy.print(f!(b"tonesInRAM(inRAM)\n\nA: change notes\nB: break\0")); while sound.playing() { move_circle(); if arduboy.pressed(A_BUTTON) { @@ -429,7 +429,7 @@ pub unsafe extern "C" fn loop_() { sound.tones(get_tones_addr!(sound1)); arduboy.clear(); - arduboy.print("volume_mode(IN_TONES)\ntones(sound1)\n\nB: break\0"); + arduboy.print(f!(b"volume_mode(IN_TONES)\ntones(sound1)\n\nB: break\0")); while sound.playing() { move_circle(); if arduboy.pressed(B) { @@ -441,7 +441,7 @@ pub unsafe extern "C" fn loop_() { sound.volume_mode(VOLUME_ALWAYS_NORMAL); sound.tones(get_tones_addr!(sound1)); arduboy.clear(); - arduboy.print("volume_mode(NORMAL)\ntones(sound1)\n\nB: no_tone(), break\0"); + arduboy.print(f!(b"volume_mode(NORMAL)\ntones(sound1)\n\nB: no_tone(), break\0")); while sound.playing() { move_circle(); if arduboy.pressed(B) { @@ -454,7 +454,7 @@ pub unsafe extern "C" fn loop_() { sound.volume_mode(VOLUME_ALWAYS_HIGH); sound.tones(get_tones_addr!(sound1)); arduboy.clear(); - arduboy.print("volume_mode(HIGH)\ntones(sound1)\n\nB: break\0"); + arduboy.print(f!(b"volume_mode(HIGH)\ntones(sound1)\n\nB: break\0")); while sound.playing() { move_circle(); if arduboy.pressed(B) { @@ -480,13 +480,13 @@ fn move_circle() { fn display_audio() { arduboy.clear(); - arduboy.print("Audio enabled: \0"); + arduboy.print(f!(b"Audio enabled: \0")); if arduboy.audio_enabled() { - arduboy.print("YES\0"); + arduboy.print(f!(b"YES\0")); } else { - arduboy.print("NO\0") + arduboy.print(f!(b"NO\0")) } - arduboy.print("\n\nUP: enable\nDOWN: disable\nB: break\0"); + arduboy.print(f!(b"\n\nUP: enable\nDOWN: disable\nB: break\0")); arduboy.invert(!arduboy.audio_enabled()); } diff --git a/Examples/pong/src/lib.rs b/Examples/pong/src/lib.rs index 109ec2b..87496ab 100644 --- a/Examples/pong/src/lib.rs +++ b/Examples/pong/src/lib.rs @@ -1,4 +1,5 @@ #![no_std] +#![allow(non_upper_case_globals)] use arduboy_rust::prelude::*; @@ -91,9 +92,9 @@ pub unsafe extern "C" fn loop_() { match G.game_state { GameState::Title => { arduboy.set_cursor(52, 10); - arduboy.print(&b"PONG\0"[..]); + arduboy.print(f!(b"PONG\0")); arduboy.set_cursor(16, 22); - arduboy.print(&b"Press A to start\0"[..]); + arduboy.print(f!(b"Press A to start\0")); if A.just_pressed() { G.game_state = GameState::Gameplay; } @@ -106,14 +107,14 @@ pub unsafe extern "C" fn loop_() { } GameState::Win => { arduboy.set_cursor(40, 10); - arduboy.print(&b"You Win!\0"[..]); + arduboy.print(f!(b"You Win!\0")); if A.just_pressed() { reset_game(); } } GameState::Lose => { arduboy.set_cursor(37, 10); - arduboy.print(&b"Game Over\0"[..]); + arduboy.print(f!(b"Game Over\0")); if A.just_pressed() { reset_game(); } diff --git a/Examples/snake/src/lib.rs b/Examples/snake/src/lib.rs index 4465ad4..825dd8a 100644 --- a/Examples/snake/src/lib.rs +++ b/Examples/snake/src/lib.rs @@ -1,4 +1,5 @@ #![no_std] +#![allow(non_upper_case_globals)] use arduboy_rust::prelude::*; static mut MEM: u8 = 0; @@ -201,10 +202,10 @@ pub unsafe extern "C" fn loop_() { State::Title => { arduboy.set_cursor(0, 0); arduboy.set_text_size(2); - arduboy.print(&b"RustySnake\n\0"[..]); + arduboy.print(f!(b"RustySnake\n\0")); arduboy.set_text_size(1); - arduboy.print(&b"\nControls: \nB for Pause\nA&B for reset\n\0"[..]); - arduboy.print(&b"\nZennDev 2023\n\0"[..]); + arduboy.print(f!(b"\nControls: \nB for Pause\nA&B for reset\n\0")); + arduboy.print(f!(b"\nZennDev 2023\n\0")); if A.just_pressed() { snake.game_state = State::Game; } @@ -224,7 +225,7 @@ pub unsafe extern "C" fn loop_() { } State::Win => (), State::Pause => { - let msg = &b"[ Break ]\0"[..]; + let msg = "[ Break ]\0"; let l = msg.len() as u8 * FONT_SIZE / 2; arduboy.set_cursor( ((WIDTH / 2) as u16 - l as u16).try_into().unwrap(), @@ -248,14 +249,14 @@ pub unsafe extern "C" fn loop_() { } arduboy.set_cursor(0, 0); - arduboy.print(&b"Game Over!\0"[..]); - arduboy.print(&b"\n\n\0"[..]); - arduboy.print(&b"Score: \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); - arduboy.print(&b"\nHigh Score: \0"[..]); + arduboy.print(f!(b"\nHigh Score: \0")); arduboy.print(MEM as u16); - arduboy.print(&b"\n\n\0"[..]); - arduboy.print(&b"Press A to Play Again\0"[..]); + 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; diff --git a/Project/game/src/lib.rs b/Project/game/src/lib.rs index 531da5c..67223b4 100644 --- a/Project/game/src/lib.rs +++ b/Project/game/src/lib.rs @@ -14,6 +14,10 @@ use arduboy_rust::prelude::*; #[no_mangle] pub unsafe extern "C" fn setup() { // put your setup code here, to run once: + arduboy.begin(); + arduboy.clear(); + arduboy.print(f!(b"hello boys\0")); + arduboy.display(); } // The loop() function repeats forever after setup() is done diff --git a/arduboy-rust/src/library/progmem.rs b/arduboy-rust/src/library/progmem.rs index 148fa33..fffa54b 100644 --- a/arduboy-rust/src/library/progmem.rs +++ b/arduboy-rust/src/library/progmem.rs @@ -63,3 +63,22 @@ macro_rules! get_string_addr { }; } pub(super) use get_string_addr; +///This is the way to go if you want print some random text +/// +/// This doesn't waste the 2kb ram it saves to progmem (28kb) +/// This automatically saves the given text to the Progmem. +/// ## Example +/// ``` +/// arduboy.print(f!(b"Random text to print")) +/// ``` +#[macro_export] +macro_rules! f { + ($string_literal:literal) => {{ + progmem!( + static local: [u8; _] = *$string_literal; + ); + + get_string_addr!(local) + }}; +} +pub(super) use f; diff --git a/arduboy-rust/src/prelude.rs b/arduboy-rust/src/prelude.rs index a225165..a39b12a 100644 --- a/arduboy-rust/src/prelude.rs +++ b/arduboy-rust/src/prelude.rs @@ -19,7 +19,7 @@ pub use crate::library::c::*; pub use crate::library::eeprom::EEPROM; pub use crate::library::sprites; pub use crate::print::*; -pub use crate::{get_sprite_addr, get_string_addr, get_tones_addr, progmem}; +pub use crate::{f, get_sprite_addr, get_string_addr, get_tones_addr, progmem}; use core::cmp; pub fn constrain(x: T, a: T, b: T) -> T {