4

A common pattern in Arduino C++ sketches is the use of Serial.print() or Serial.println() to debug problems.

What is the corresponding Rust idiom when programming for the Arduino Uno?

Mutant Bob
  • 3,121
  • 2
  • 27
  • 52

1 Answers1

3

One technique involves the use of arduino_hal::default_serial! and ufmt::writeln!

For setup:

let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let mut serial = arduino_hal::default_serial!(dp, pins, 115200);

and when you need to print

ufmt::uwriteln!(&mut serial, "{} bytes available", count);

The uwriteln! macro is not as powerful as format! or println! from std. For instance, it does not support {:02x} for integers.

Mutant Bob
  • 3,121
  • 2
  • 27
  • 52
  • I think you're mistaken (or perhaps out of date?) that padding is not supported. From the [ufmt docs](https://docs.rs/ufmt/latest/ufmt/): `padding ({:02x}) [is] supported on primitive integer types.` – weberc2 Aug 15 '22 at 20:03
  • 2
    Yeah, this question was asked in January, and my pull request https://github.com/japaric/ufmt/pull/35 to add hex support has since been accepted. – Mutant Bob Sep 02 '22 at 13:38
  • Hah, that explains it. – weberc2 Sep 02 '22 at 18:00