mirror of
https://github.com/italicsjenga/rp-hal-boards.git
synced 2024-12-24 05:01:31 +11:00
Cleaned up the code and made it more idiomatic.
This commit is contained in:
parent
e22540be20
commit
3f9535cc1f
|
@ -41,6 +41,9 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
|
// For string formatting.
|
||||||
|
use core::fmt::Write;
|
||||||
|
|
||||||
// The macro for our start-up function
|
// The macro for our start-up function
|
||||||
use cortex_m_rt::entry;
|
use cortex_m_rt::entry;
|
||||||
|
|
||||||
|
@ -73,7 +76,7 @@ use embedded_graphics::{
|
||||||
};
|
};
|
||||||
|
|
||||||
// The display driver:
|
// The display driver:
|
||||||
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
|
use ssd1306::{prelude::*, Ssd1306};
|
||||||
|
|
||||||
/// Entry point to our bare-metal application.
|
/// Entry point to our bare-metal application.
|
||||||
///
|
///
|
||||||
|
@ -122,7 +125,7 @@ fn main() -> ! {
|
||||||
let sda_pin = pins.gpio16.into_mode::<hal::gpio::FunctionI2C>();
|
let sda_pin = pins.gpio16.into_mode::<hal::gpio::FunctionI2C>();
|
||||||
let scl_pin = pins.gpio17.into_mode::<hal::gpio::FunctionI2C>();
|
let scl_pin = pins.gpio17.into_mode::<hal::gpio::FunctionI2C>();
|
||||||
|
|
||||||
// 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
|
// at compile time if the pins are in the wrong mode, or if this I²C
|
||||||
// peripheral isn't available on these pins!
|
// peripheral isn't available on these pins!
|
||||||
let i2c = hal::I2C::i2c0(
|
let i2c = hal::I2C::i2c0(
|
||||||
|
@ -135,7 +138,7 @@ fn main() -> ! {
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create the I²C display interface:
|
// Create the I²C display interface:
|
||||||
let interface = I2CDisplayInterface::new(i2c);
|
let interface = ssd1306::I2CDisplayInterface::new(i2c);
|
||||||
|
|
||||||
// Create a driver instance and initialize:
|
// Create a driver instance and initialize:
|
||||||
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
|
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
|
||||||
|
@ -157,7 +160,7 @@ fn main() -> ! {
|
||||||
loop {
|
loop {
|
||||||
buf.reset();
|
buf.reset();
|
||||||
// Format some text into a static buffer:
|
// 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;
|
count += 1;
|
||||||
|
|
||||||
// Empty the display:
|
// 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 {
|
struct FmtBuf {
|
||||||
buf: [u8; 64],
|
buf: [u8; 64],
|
||||||
ptr: usize,
|
ptr: usize,
|
||||||
|
@ -209,8 +213,13 @@ impl FmtBuf {
|
||||||
|
|
||||||
impl core::fmt::Write for FmtBuf {
|
impl core::fmt::Write for FmtBuf {
|
||||||
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||||
let len = s.len();
|
let rest_len = self.buf.len() - self.ptr;
|
||||||
self.buf[self.ptr..(self.ptr + len)].copy_from_slice(s.as_bytes());
|
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;
|
self.ptr += len;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue