From 3f9535cc1fd4c70f20790a4c92f3613c77d17b21 Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Wed, 12 Jan 2022 21:48:22 +0100 Subject: [PATCH] Cleaned up the code and made it more idiomatic. --- .../examples/pico_i2c_oled_display_ssd1306.rs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/boards/rp-pico/examples/pico_i2c_oled_display_ssd1306.rs b/boards/rp-pico/examples/pico_i2c_oled_display_ssd1306.rs index 54b361d..c3d4fef 100644 --- a/boards/rp-pico/examples/pico_i2c_oled_display_ssd1306.rs +++ b/boards/rp-pico/examples/pico_i2c_oled_display_ssd1306.rs @@ -41,6 +41,9 @@ #![no_std] #![no_main] +// For string formatting. +use core::fmt::Write; + // The macro for our start-up function use cortex_m_rt::entry; @@ -73,7 +76,7 @@ use embedded_graphics::{ }; // The display driver: -use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306}; +use ssd1306::{prelude::*, Ssd1306}; /// Entry point to our bare-metal application. /// @@ -122,7 +125,7 @@ fn main() -> ! { let sda_pin = pins.gpio16.into_mode::(); let scl_pin = pins.gpio17.into_mode::(); - // Create the I²C drive, using the two pre-configured pins. This will fail + // Create the I²C driver, using the two pre-configured pins. This will fail // at compile time if the pins are in the wrong mode, or if this I²C // peripheral isn't available on these pins! let i2c = hal::I2C::i2c0( @@ -135,7 +138,7 @@ fn main() -> ! { ); // Create the I²C display interface: - let interface = I2CDisplayInterface::new(i2c); + let interface = ssd1306::I2CDisplayInterface::new(i2c); // Create a driver instance and initialize: let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0) @@ -157,7 +160,7 @@ fn main() -> ! { loop { buf.reset(); // Format some text into a static buffer: - core::fmt::write(&mut buf, format_args!("counter: {}", count)).unwrap(); + write!(&mut buf, "counter: {}", count).unwrap(); count += 1; // Empty the display: @@ -184,7 +187,8 @@ fn main() -> ! { } } -/// This is a very simple buffer to pre format a short line of text. +/// This is a very simple buffer to pre format a short line of text +/// limited arbitrarily to 64 bytes. struct FmtBuf { buf: [u8; 64], ptr: usize, @@ -209,8 +213,13 @@ impl FmtBuf { impl core::fmt::Write for FmtBuf { fn write_str(&mut self, s: &str) -> core::fmt::Result { - let len = s.len(); - self.buf[self.ptr..(self.ptr + len)].copy_from_slice(s.as_bytes()); + let rest_len = self.buf.len() - self.ptr; + let len = if rest_len < s.len() { + rest_len + } else { + s.len() + }; + self.buf[self.ptr..(self.ptr + len)].copy_from_slice(&s.as_bytes()[0..len]); self.ptr += len; Ok(()) }