|
| 1 | +// |
| 2 | +// This file provides a example on how to use strings on an Arduino Uno. |
| 3 | +// |
| 4 | + |
| 5 | + |
| 6 | +// Define no_std only for AVR |
| 7 | +#![cfg_attr(target_arch = "avr", no_std)] |
| 8 | +#![no_main] |
| 9 | +// |
| 10 | +// To unwrap the Option in const context |
| 11 | +#![feature(const_option)] |
| 12 | + |
| 13 | + |
| 14 | +use avr_progmem::progmem; // The macro |
| 15 | +use avr_progmem::string::ByteString; // Helper for storing strings |
| 16 | +#[cfg(target_arch = "avr")] |
| 17 | +use panic_halt as _; // halting panic implementation for AVR |
| 18 | + |
| 19 | + |
| 20 | +progmem! { |
| 21 | + /// The static data to be stored in program code section |
| 22 | + /// Notice the usage of `ByteString`, to store a string as `[u8;N]`, |
| 23 | + /// because you can't store a `str` and storing a `&str` wouldn't have |
| 24 | + /// much of an effect. |
| 25 | + static progmem SOME_TEXT: ByteString<189> = ByteString::new(" |
| 26 | +A long test string literal, that is stored in progmem instead of DRAM. |
| 27 | +However, to use it, it needs to be temporarily load into DRAM, |
| 28 | +so an individual `ByteString` shouldn't be too long. |
| 29 | + ").unwrap(); |
| 30 | + |
| 31 | + /// More data to be stored in program code section |
| 32 | + static progmem MORE_TEXT: ByteString<102> = ByteString::new(" |
| 33 | +However, you easily store your strings individual, limiting the amount of |
| 34 | +temporary DRAM necessary. |
| 35 | + ").unwrap(); |
| 36 | + |
| 37 | + /// Unicode works of course as expected |
| 38 | + /// |
| 39 | + static progmem UNICODE_TEXT: ByteString<137> = ByteString::new( |
| 40 | + "dai 大賢者 kenja, Völlerei lässt grüßen, le garçon de théâtre, Ελληνική Δημοκρατία, Слава Україні" |
| 41 | + ).unwrap(); |
| 42 | +} |
| 43 | + |
| 44 | +// Include a fancy printer supporting Arduino Uno's USB-Serial output as well |
| 45 | +// as stdout on non-AVR targets. |
| 46 | +mod printer; |
| 47 | +use printer::Printer; |
| 48 | + |
| 49 | +#[no_mangle] |
| 50 | +fn main() -> ! { |
| 51 | + let mut printer = { |
| 52 | + #[cfg(target_arch = "avr")] |
| 53 | + { |
| 54 | + // Initialize the USB-Serial output on the Arduino Uno |
| 55 | + |
| 56 | + let dp = arduino_uno::Peripherals::take().unwrap(); |
| 57 | + |
| 58 | + let mut pins = arduino_uno::Pins::new(dp.PORTB, dp.PORTC, dp.PORTD); |
| 59 | + |
| 60 | + let serial = arduino_uno::Serial::new( |
| 61 | + dp.USART0, |
| 62 | + pins.d0, |
| 63 | + pins.d1.into_output(&mut pins.ddr), |
| 64 | + 9600, |
| 65 | + ); |
| 66 | + Printer(serial) |
| 67 | + } |
| 68 | + #[cfg(not(target_arch = "avr"))] |
| 69 | + { |
| 70 | + // Just use stdout for non-AVR targets |
| 71 | + Printer |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + // Print some introduction text |
| 76 | + printer.println("Hello from Arduino!"); |
| 77 | + printer.println(""); |
| 78 | + printer.println("--------------------------"); |
| 79 | + printer.println(""); |
| 80 | + |
| 81 | + // Scope to limit the lifetime of `text_buffer` |
| 82 | + { |
| 83 | + // The temporary DRAM buffer for the string |
| 84 | + let text_buffer = SOME_TEXT.load(); |
| 85 | + let text: &str = &text_buffer; // Just derefs to `str` |
| 86 | + printer.println(text); |
| 87 | + } |
| 88 | + |
| 89 | + // Or just using temporaries |
| 90 | + printer.println(&MORE_TEXT.load()); |
| 91 | + printer.println(&UNICODE_TEXT.load()); |
| 92 | + |
| 93 | + // Even more convenient: use a one-off in-place progmem static via `progmem_str` |
| 94 | + printer.println(avr_progmem::progmem_str!("Just a lone literal progmem str")); |
| 95 | + printer.println(avr_progmem::progmem_str!("And another one")); |
| 96 | + |
| 97 | + // Print some final lines |
| 98 | + printer.println(""); |
| 99 | + printer.println("--------------------------"); |
| 100 | + printer.println(""); |
| 101 | + printer.println("DONE"); |
| 102 | + |
| 103 | + // It is very convenient to just exit on non-AVR platforms, otherwise users |
| 104 | + // might get the impression that the program hangs, whereas it already |
| 105 | + // succeeded. |
| 106 | + #[cfg(not(target_arch = "avr"))] |
| 107 | + std::process::exit(0); |
| 108 | + |
| 109 | + // Otherwise, that is on AVR, just go into an infinite loop, because on AVR |
| 110 | + // we just can't exit! |
| 111 | + loop { |
| 112 | + // Done, just do nothing |
| 113 | + } |
| 114 | +} |
0 commit comments