From 441e5bec462e5dee4fd1c9eca11e2150deca6734 Mon Sep 17 00:00:00 2001 From: Brendan Ball Date: Fri, 3 Nov 2023 15:37:46 +0100 Subject: [PATCH] start using rustfmt --- display_themes/examples/simulate_theme_1.rs | 13 +- display_themes/examples/simulate_theme_2.rs | 13 +- display_themes/src/lib.rs | 20 +-- display_themes/src/theme_1.rs | 55 +++++--- display_themes/src/theme_2.rs | 139 +++++++++++--------- epd_display/src/lib.rs | 62 +++++---- esp32c3_nostd/examples/epd.rs | 51 +++---- esp32c3_nostd/examples/epd_clear.rs | 35 +++-- esp32c3_nostd/examples/epd_simple.rs | 77 ++++++----- esp32c3_nostd/examples/led_matrix.rs | 26 ++-- esp32c3_nostd/src/main.rs | 51 +++---- rustfmt.toml | 2 + sensor/src/lib.rs | 2 +- sensor/src/scd4x_sensor.rs | 39 +++--- 14 files changed, 294 insertions(+), 291 deletions(-) create mode 100644 rustfmt.toml diff --git a/display_themes/examples/simulate_theme_1.rs b/display_themes/examples/simulate_theme_1.rs index b754007..62d0461 100644 --- a/display_themes/examples/simulate_theme_1.rs +++ b/display_themes/examples/simulate_theme_1.rs @@ -1,14 +1,13 @@ +use airquamon_domain::Data; +use display_themes::{Theme, Theme1}; use embedded_graphics::prelude::*; -use embedded_graphics_simulator::{SimulatorDisplay, Window, OutputSettingsBuilder}; +use embedded_graphics_simulator::{OutputSettingsBuilder, SimulatorDisplay, Window}; use epd_waveshare::color::TriColor; -use display_themes::{Theme, Theme1}; -use airquamon_domain::Data; - fn main() -> Result<(), core::convert::Infallible> { let mut display = SimulatorDisplay::::new(Size::new(296, 128)); - let data = Data{ + let data = Data { co2: 459, temperature: 20.59, humidity: 57.42, @@ -17,9 +16,7 @@ fn main() -> Result<(), core::convert::Infallible> { let mut theme1 = Theme1::new(); theme1.draw(&data, &mut display).unwrap(); - let output_settings = OutputSettingsBuilder::new() - .scale(2) - .build(); + let output_settings = OutputSettingsBuilder::new().scale(2).build(); Window::new("Airquamon Simulator", &output_settings).show_static(&display); Ok(()) diff --git a/display_themes/examples/simulate_theme_2.rs b/display_themes/examples/simulate_theme_2.rs index 9e8f532..14f425a 100644 --- a/display_themes/examples/simulate_theme_2.rs +++ b/display_themes/examples/simulate_theme_2.rs @@ -1,14 +1,13 @@ +use airquamon_domain::Data; +use display_themes::{Theme, Theme2}; use embedded_graphics::prelude::*; -use embedded_graphics_simulator::{SimulatorDisplay, Window, OutputSettingsBuilder}; +use embedded_graphics_simulator::{OutputSettingsBuilder, SimulatorDisplay, Window}; use epd_waveshare::color::TriColor; -use display_themes::{Theme, Theme2}; -use airquamon_domain::Data; - fn main() -> Result<(), core::convert::Infallible> { let mut display = SimulatorDisplay::::new(Size::new(296, 128)); - let data = Data{ + let data = Data { co2: 900, temperature: 20.59, humidity: 57.42, @@ -17,9 +16,7 @@ fn main() -> Result<(), core::convert::Infallible> { let mut theme = Theme2::new(); theme.draw(&data, &mut display).unwrap(); - let output_settings = OutputSettingsBuilder::new() - .scale(2) - .build(); + let output_settings = OutputSettingsBuilder::new().scale(2).build(); Window::new("Airquamon Simulator", &output_settings).show_static(&display); Ok(()) diff --git a/display_themes/src/lib.rs b/display_themes/src/lib.rs index bd65b27..29f68ce 100644 --- a/display_themes/src/lib.rs +++ b/display_themes/src/lib.rs @@ -1,8 +1,8 @@ #![no_std] use airquamon_domain::Data; -use embedded_graphics::prelude::*; use core::fmt; +use embedded_graphics::prelude::*; mod theme_1; pub use theme_1::Theme1; @@ -11,11 +11,15 @@ mod theme_2; pub use theme_2::Theme2; pub trait Theme -where -COLOR: PixelColor, +where + COLOR: PixelColor, { - fn draw(&mut self, data: &Data, display: &mut DRAWTARGET) -> Result<(), DRAWTARGET::Error> - where - DRAWTARGET: DrawTarget + OriginDimensions, - DRAWTARGET::Error: fmt::Debug; -} \ No newline at end of file + fn draw( + &mut self, + data: &Data, + display: &mut DRAWTARGET, + ) -> Result<(), DRAWTARGET::Error> + where + DRAWTARGET: DrawTarget + OriginDimensions, + DRAWTARGET::Error: fmt::Debug; +} diff --git a/display_themes/src/theme_1.rs b/display_themes/src/theme_1.rs index 7f0a2fe..63666ae 100644 --- a/display_themes/src/theme_1.rs +++ b/display_themes/src/theme_1.rs @@ -1,15 +1,15 @@ use crate::Theme; use airquamon_domain::Data; +use core::fmt; +use core::fmt::Write; use embedded_graphics::{ - mono_font::{MonoTextStyleBuilder, iso_8859_1::FONT_8X13_BOLD}, + mono_font::{iso_8859_1::FONT_8X13_BOLD, MonoTextStyleBuilder}, prelude::*, primitives::{Line, PrimitiveStyle}, text::{Baseline, Text, TextStyleBuilder}, }; use epd_waveshare::color::TriColor; use heapless::String; -use core::fmt::Write; -use core::fmt; pub struct Theme1 { display_text: String<60>, @@ -17,35 +17,52 @@ pub struct Theme1 { impl Theme1 { pub fn new() -> Self { - Theme1 { + Theme1 { display_text: String::new(), } } } -impl Theme for Theme1 -{ - fn draw(&mut self, data: &Data, display: &mut DRAWTARGET) -> Result<(), DRAWTARGET::Error> - where - DRAWTARGET: DrawTarget + OriginDimensions, - DRAWTARGET::Error: fmt::Debug +impl Theme for Theme1 { + fn draw( + &mut self, + data: &Data, + display: &mut DRAWTARGET, + ) -> Result<(), DRAWTARGET::Error> + where + DRAWTARGET: DrawTarget + OriginDimensions, + DRAWTARGET::Error: fmt::Debug, { self.display_text.clear(); - write!(self.display_text, "CO2: {0} ppm | {1:#.2} °C | {2:#.2} %", data.co2, data.temperature, data.humidity).expect("Error occurred while trying to write in String"); + write!( + self.display_text, + "CO2: {0} ppm | {1:#.2} °C | {2:#.2} %", + data.co2, data.temperature, data.humidity + ) + .expect("Error occurred while trying to write in String"); let _ = Line::new( - Point::new(5, display.size().height as i32 / 2), - Point::new(display.size().width as i32 - 5, display.size().height as i32 / 2) - ) - .into_styled(PrimitiveStyle::with_stroke(TriColor::Chromatic, 4)) - .draw(display); + Point::new(5, display.size().height as i32 / 2), + Point::new( + display.size().width as i32 - 5, + display.size().height as i32 / 2, + ), + ) + .into_styled(PrimitiveStyle::with_stroke(TriColor::Chromatic, 4)) + .draw(display); draw_text(display, &self.display_text, 5, 10)?; Ok(()) } } -fn draw_text(display: &mut DRAWTARGET, text: &str, x: i32, y: i32) -> Result<(), DRAWTARGET::Error> +fn draw_text( + display: &mut DRAWTARGET, + text: &str, + x: i32, + y: i32, +) -> Result<(), DRAWTARGET::Error> where - DRAWTARGET: DrawTarget { + DRAWTARGET: DrawTarget, +{ let style = MonoTextStyleBuilder::new() .font(&FONT_8X13_BOLD) .text_color(TriColor::Black) @@ -56,4 +73,4 @@ where Text::with_text_style(text, Point::new(x, y), style, text_style).draw(display)?; Ok(()) -} \ No newline at end of file +} diff --git a/display_themes/src/theme_2.rs b/display_themes/src/theme_2.rs index 5eb3d3f..537555e 100644 --- a/display_themes/src/theme_2.rs +++ b/display_themes/src/theme_2.rs @@ -1,20 +1,23 @@ use crate::Theme; use airquamon_domain::Data; +use core::fmt; +use core::fmt::Write; use embedded_graphics::{ - mono_font::{MonoTextStyleBuilder, iso_8859_1::{FONT_6X10, FONT_10X20}}, + mono_font::{ + iso_8859_1::{FONT_10X20, FONT_6X10}, + MonoTextStyleBuilder, + }, prelude::*, primitives::{PrimitiveStyle, Rectangle}, - text::{Text, Alignment}, + text::{Alignment, Text}, }; use embedded_layout::{ layout::linear::{spacing::FixedMargin, LinearLayout}, + prelude::*, View, - prelude::* }; use epd_waveshare::color::TriColor; use heapless::String; -use core::fmt::Write; -use core::fmt; pub struct Theme2; @@ -30,7 +33,6 @@ struct Value { value: T, } - impl View for Value { #[inline] fn translate_impl(&mut self, by: Point) { @@ -75,26 +77,32 @@ impl Drawable for Value { } else { TriColor::Black }; - + let value_text_style = MonoTextStyleBuilder::new() .font(&FONT_10X20) .text_color(value_text_color) .background_color(TriColor::White) .build(); - let text = Text::with_alignment(&self.value_text, Point::zero(), value_text_style, Alignment::Center); + let text = Text::with_alignment( + &self.value_text, + Point::zero(), + value_text_style, + Alignment::Center, + ); let label_text_style = MonoTextStyleBuilder::new() - .font(&FONT_6X10) - .text_color(TriColor::Black) - .background_color(TriColor::White) - .build(); - let label_co2 = Text::with_alignment("CO2", Point::zero(), label_text_style, Alignment::Center); - let label_ppm = Text::with_alignment("ppm", Point::zero(), label_text_style, Alignment::Center); - + .font(&FONT_6X10) + .text_color(TriColor::Black) + .background_color(TriColor::White) + .build(); + let label_co2 = + Text::with_alignment("CO2", Point::zero(), label_text_style, Alignment::Center); + let label_ppm = + Text::with_alignment("ppm", Point::zero(), label_text_style, Alignment::Center); LinearLayout::horizontal( Chain::new(text) - .append(LinearLayout::vertical(Chain::new(label_co2).append(label_ppm)).arrange()) + .append(LinearLayout::vertical(Chain::new(label_co2).append(label_ppm)).arrange()), ) .with_alignment(vertical::Center) .with_spacing(FixedMargin(4)) @@ -111,7 +119,8 @@ struct Temperature(f32); impl Value { fn new(value: Temperature, position: Point, size: Size) -> Self { let mut value_text: String<4> = String::new(); - write!(value_text, "{0:#.1}", value.0).expect("Error occurred while trying to write in String"); + write!(value_text, "{0:#.1}", value.0) + .expect("Error occurred while trying to write in String"); Self { bounds: Rectangle::new(position, size), value_text, @@ -131,26 +140,30 @@ impl Drawable for Value { // Create a 1px border let border = self.bounds.into_styled(border_style); border.draw(display)?; - + let value_text_style = MonoTextStyleBuilder::new() .font(&FONT_10X20) .text_color(TriColor::Black) .background_color(TriColor::White) .build(); - let text = Text::with_alignment(&self.value_text, Point::zero(), value_text_style, Alignment::Center); + let text = Text::with_alignment( + &self.value_text, + Point::zero(), + value_text_style, + Alignment::Center, + ); let label_text_style = MonoTextStyleBuilder::new() - .font(&FONT_6X10) - .text_color(TriColor::Black) - .background_color(TriColor::White) - .build(); - let label_degrees = Text::with_alignment("°C", Point::zero(), label_text_style, Alignment::Center); - + .font(&FONT_6X10) + .text_color(TriColor::Black) + .background_color(TriColor::White) + .build(); + let label_degrees = + Text::with_alignment("°C", Point::zero(), label_text_style, Alignment::Center); LinearLayout::horizontal( - Chain::new(text) - .append(LinearLayout::vertical(Chain::new(label_degrees)).arrange()) + Chain::new(text).append(LinearLayout::vertical(Chain::new(label_degrees)).arrange()), ) .with_alignment(vertical::Center) .with_spacing(FixedMargin(4)) @@ -167,7 +180,8 @@ struct Humidity(f32); impl Value { fn new(value: Humidity, position: Point, size: Size) -> Self { let mut value_text: String<4> = String::new(); - write!(value_text, "{0:#.1}", value.0).expect("Error occurred while trying to write in String"); + write!(value_text, "{0:#.1}", value.0) + .expect("Error occurred while trying to write in String"); Self { bounds: Rectangle::new(position, size), value_text, @@ -187,28 +201,33 @@ impl Drawable for Value { // Create a 1px border let border = self.bounds.into_styled(border_style); border.draw(display)?; - + let value_text_style = MonoTextStyleBuilder::new() .font(&FONT_10X20) .text_color(TriColor::Black) .background_color(TriColor::White) .build(); - let text = Text::with_alignment(&self.value_text, Point::zero(), value_text_style, Alignment::Center); + let text = Text::with_alignment( + &self.value_text, + Point::zero(), + value_text_style, + Alignment::Center, + ); let label_text_style = MonoTextStyleBuilder::new() - .font(&FONT_6X10) - .text_color(TriColor::Black) - .background_color(TriColor::White) - .build(); - let label_rh = Text::with_alignment("RH", Point::zero(), label_text_style, Alignment::Center); - let label_percentage = Text::with_alignment("%", Point::zero(), label_text_style, Alignment::Center); - - - LinearLayout::horizontal( - Chain::new(text) - .append(LinearLayout::vertical(Chain::new(label_rh).append(label_percentage)).arrange()) - ) + .font(&FONT_6X10) + .text_color(TriColor::Black) + .background_color(TriColor::White) + .build(); + let label_rh = + Text::with_alignment("RH", Point::zero(), label_text_style, Alignment::Center); + let label_percentage = + Text::with_alignment("%", Point::zero(), label_text_style, Alignment::Center); + + LinearLayout::horizontal(Chain::new(text).append( + LinearLayout::vertical(Chain::new(label_rh).append(label_percentage)).arrange(), + )) .with_alignment(vertical::Center) .with_spacing(FixedMargin(4)) .arrange() @@ -219,33 +238,33 @@ impl Drawable for Value { } } -impl Theme for Theme2 -{ - fn draw(&mut self, data: &Data, display: &mut DRAWTARGET) -> Result<(), DRAWTARGET::Error> - where - DRAWTARGET: DrawTarget + OriginDimensions, - DRAWTARGET::Error: fmt::Debug +impl Theme for Theme2 { + fn draw( + &mut self, + data: &Data, + display: &mut DRAWTARGET, + ) -> Result<(), DRAWTARGET::Error> + where + DRAWTARGET: DrawTarget + OriginDimensions, + DRAWTARGET::Error: fmt::Debug, { display.clear(TriColor::White)?; let display_area = display.bounding_box(); - let box_size = Size::new(80, 80); + let box_size = Size::new(80, 80); let co2 = Value::::new(CO2(data.co2), Point::zero(), box_size); - let temperature = Value::::new(Temperature(data.temperature), Point::zero(), box_size); + let temperature = + Value::::new(Temperature(data.temperature), Point::zero(), box_size); let humidity = Value::::new(Humidity(data.humidity), Point::zero(), box_size); - LinearLayout::horizontal( - Chain::new(co2) - .append(temperature) - .append(humidity) - ) - // .with_spacing(FixedMargin(4)) - .arrange() - .align_to(&display_area, horizontal::Center, vertical::Center) - .draw(display)?; - + LinearLayout::horizontal(Chain::new(co2).append(temperature).append(humidity)) + // .with_spacing(FixedMargin(4)) + .arrange() + .align_to(&display_area, horizontal::Center, vertical::Center) + .draw(display)?; + Ok(()) } } diff --git a/epd_display/src/lib.rs b/epd_display/src/lib.rs index 2997ad0..0218d84 100644 --- a/epd_display/src/lib.rs +++ b/epd_display/src/lib.rs @@ -1,12 +1,12 @@ #![no_std] -use epd_waveshare::{prelude::*, graphics}; -use embedded_hal::spi::SpiDevice; -use embedded_hal::delay::DelayUs; -use embedded_graphics::prelude::*; -use core::fmt; use airquamon_domain::Data; +use core::fmt; use display_themes::Theme; +use embedded_graphics::prelude::*; +use embedded_hal::delay::DelayUs; +use embedded_hal::spi::SpiDevice; +use epd_waveshare::{graphics, prelude::*}; use log::info; pub trait ChromaticBuffer { @@ -14,13 +14,9 @@ pub trait ChromaticBuffer { fn chromatic_buffer(&self) -> &[u8]; } -impl< - const WIDTH: u32, - const HEIGHT: u32, - const BWRBIT: bool, - const BYTECOUNT: usize, - > ChromaticBuffer for graphics::Display { - +impl + ChromaticBuffer for graphics::Display +{ fn bw_buffer(&self) -> &[u8] { self.bw_buffer() } @@ -31,14 +27,13 @@ impl< } pub struct Display - where +where SPI: SpiDevice, EPD: WaveshareThreeColorDisplayV2, DRAWTARGET: DrawTarget + ChromaticBuffer, DRAWTARGET::Error: fmt::Debug, DELAY: DelayUs, - THEME: Theme - + THEME: Theme, { spi: SPI, epd: EPD, @@ -47,14 +42,15 @@ pub struct Display theme: THEME, } -impl Display -where +impl Display +where SPI: SpiDevice, EPD: WaveshareThreeColorDisplayV2, DRAWTARGET: DrawTarget + ChromaticBuffer, DRAWTARGET::Error: fmt::Debug, DELAY: DelayUs, - THEME: Theme { + THEME: Theme, +{ pub fn new(spi: SPI, epd: EPD, draw_target: DRAWTARGET, delay: DELAY, theme: THEME) -> Self { Self { spi, @@ -67,42 +63,52 @@ where } pub trait DisplayTheme { - type Error; fn draw(&mut self, data: &Data) -> Result<(), Self::Error>; } -impl DisplayTheme for Display - where +impl DisplayTheme + for Display +where SPI: SpiDevice, EPD: WaveshareThreeColorDisplayV2, SPI: SpiDevice, DRAWTARGET: DrawTarget + ChromaticBuffer + OriginDimensions, DRAWTARGET::Error: fmt::Debug, DELAY: DelayUs, - THEME: Theme + THEME: Theme, { type Error = SPI::Error; fn draw(&mut self, data: &Data) -> Result<(), Self::Error> { let _ = self.theme.draw(data, &mut self.draw_target); - draw_to_epd(&mut self.spi, &mut self.epd, &mut self.draw_target, &mut self.delay)?; + draw_to_epd( + &mut self.spi, + &mut self.epd, + &mut self.draw_target, + &mut self.delay, + )?; Ok(()) } } -fn draw_to_epd<'a, SPI, EPD, BUFFER, DELAY>(spi: &mut SPI, epd: &mut EPD, buffer: &mut BUFFER, delay: &mut DELAY) -> Result<(), SPI::Error> -where +fn draw_to_epd<'a, SPI, EPD, BUFFER, DELAY>( + spi: &mut SPI, + epd: &mut EPD, + buffer: &mut BUFFER, + delay: &mut DELAY, +) -> Result<(), SPI::Error> +where SPI: SpiDevice, EPD: WaveshareThreeColorDisplayV2, BUFFER: ChromaticBuffer, - DELAY: DelayUs { + DELAY: DelayUs, +{ info!("waking up display"); epd.wake_up(spi, delay)?; - - epd.wait_until_idle(spi, delay)?; + epd.wait_until_idle(spi, delay)?; info!("updating display frame"); epd.update_color_frame(spi, delay, buffer.bw_buffer(), buffer.chromatic_buffer())?; diff --git a/esp32c3_nostd/examples/epd.rs b/esp32c3_nostd/examples/epd.rs index 24588c8..c4df3d7 100644 --- a/esp32c3_nostd/examples/epd.rs +++ b/esp32c3_nostd/examples/epd.rs @@ -4,11 +4,11 @@ use esp_backtrace as _; use esp_println::println; use hal::{ - clock::ClockControl, - peripherals::Peripherals, - spi::{Spi, SpiMode}, + clock::ClockControl, gpio::IO, - prelude::*, + peripherals::Peripherals, + prelude::*, + spi::{Spi, SpiMode}, Delay, }; // use embedded_graphics::{ @@ -20,14 +20,14 @@ use embedded_graphics::{ primitives::{Circle, Line, PrimitiveStyle}, text::{Baseline, Text, TextStyleBuilder}, }; -use epd_waveshare::{epd2in9bc::*, prelude::*, color::*}; +use epd_waveshare::{color::*, epd2in9bc::*, prelude::*}; use log::info; #[derive(Debug)] enum Error { - EpdError -} + EpdError, +} #[entry] fn main() -> ! { @@ -65,37 +65,25 @@ fn main() -> ! { info!("Connecting to display"); // Setup EPD - let mut epd = Epd2in9bc::new( - &mut spi, - cs, - busy, - dc, - rst, - &mut delay - ).unwrap(); + let mut epd = Epd2in9bc::new(&mut spi, cs, busy, dc, rst, &mut delay).unwrap(); let mut display = EpdDisplay::new(&mut spi, &mut epd, &mut delay); - // update_display(&mut delay, "Airquamon!").unwrap(); - loop { delay.delay_ms(5000u16); info!("Waiting for data ready"); - } } -trait Display +trait Display where DELAY: DelayMs, { fn sleep(&self) -> Result<(), Error>; fn update_display(text: &str) -> Result<(), Error>; - - } struct EpdDisplay { @@ -105,7 +93,6 @@ struct EpdDisplay { } impl Display for EpdDisplay { - fn sleep(&self) -> Result<(), Error> { self.epd.sleep(&mut self.spi, &mut self.delay)?; Ok(()) @@ -115,23 +102,21 @@ impl Display for EpdDisplay { let mut mono_display = Display2in9bc::default(); mono_display.set_rotation(DisplayRotation::Rotate0); draw_text(&mut mono_display, text, 5, 10); - self.epd.update_frame(&mut spi, mono_display.buffer(), &mut delay)?; self.epd - .display_frame(&mut spi, &mut delay)?; + .update_frame(&mut spi, mono_display.buffer(), &mut delay)?; + self.epd.display_frame(&mut spi, &mut delay)?; self.epd.sleep(&mut self.spi, &mut self.delay)?; Ok(()) } - - } impl EpdDisplay { - fn new(spi: &mut SPI, epd: &mut WaveshareDisplay, delay: &mut DELAY) { - EpdDisplay{ - epd, - spi, - delay, - } + fn new( + spi: &mut SPI, + epd: &mut WaveshareDisplay, + delay: &mut DELAY, + ) { + EpdDisplay { epd, spi, delay } } } @@ -183,4 +168,4 @@ fn draw_text(display: &mut Display2in9bc, text: &str, x: i32, y: i32) { let text_style = TextStyleBuilder::new().baseline(Baseline::Top).build(); let _ = Text::with_text_style(text, Point::new(x, y), style, text_style).draw(display); -} \ No newline at end of file +} diff --git a/esp32c3_nostd/examples/epd_clear.rs b/esp32c3_nostd/examples/epd_clear.rs index 630d39c..6353f37 100644 --- a/esp32c3_nostd/examples/epd_clear.rs +++ b/esp32c3_nostd/examples/epd_clear.rs @@ -3,21 +3,20 @@ use esp_backtrace as _; +use epd_waveshare::{epd2in9b_v3::*, prelude::*}; use esp32c3_hal::{ - clock::ClockControl, - peripherals::Peripherals, + clock::ClockControl, + gpio::IO, + peripherals::Peripherals, + prelude::*, spi::{ master::{Spi, SpiBusController}, SpiMode, }, - gpio::IO, - prelude::*, Delay, }; -use epd_waveshare::{epd2in9b_v3::*, prelude::*}; use log::info; - #[entry] fn main() -> ! { let peripherals = Peripherals::take(); @@ -53,24 +52,20 @@ fn main() -> ! { info!("Connecting to display"); // Setup EPD - let mut epd = Epd2in9b::new( - &mut spi, - busy, - dc, - rst, - &mut delay, - None - ).expect("failing setting up epd"); - + let mut epd = + Epd2in9b::new(&mut spi, busy, dc, rst, &mut delay, None).expect("failing setting up epd"); - epd.wait_until_idle(&mut spi, &mut delay).expect("Failed waiting until idle"); + epd.wait_until_idle(&mut spi, &mut delay) + .expect("Failed waiting until idle"); - epd.clear_frame(&mut spi, &mut delay).expect("Failed clearing frame"); - epd - .display_frame(&mut spi, &mut delay).expect("Failed displaying frame"); + epd.clear_frame(&mut spi, &mut delay) + .expect("Failed clearing frame"); + epd.display_frame(&mut spi, &mut delay) + .expect("Failed displaying frame"); // Set the EPD to sleep - epd.sleep(&mut spi, &mut delay).expect("Failed sleeping epd"); + epd.sleep(&mut spi, &mut delay) + .expect("Failed sleeping epd"); loop { info!("Sleeping"); diff --git a/esp32c3_nostd/examples/epd_simple.rs b/esp32c3_nostd/examples/epd_simple.rs index a8985af..2939d24 100644 --- a/esp32c3_nostd/examples/epd_simple.rs +++ b/esp32c3_nostd/examples/epd_simple.rs @@ -3,27 +3,26 @@ use esp_backtrace as _; +use core::fmt::{self, Write as FmtWrite}; +use embedded_graphics::{ + mono_font::MonoTextStyleBuilder, + prelude::*, + text::{Baseline, Text, TextStyleBuilder}, +}; +use epd_waveshare::{color::Color, epd2in9b_v3::*, prelude::*}; use esp32c3_hal::{ - clock::ClockControl, - peripherals::Peripherals, + clock::ClockControl, + gpio::IO, + peripherals::Peripherals, + prelude::*, spi::{ master::{Spi, SpiBusController}, SpiMode, }, - gpio::IO, - prelude::*, Delay, }; -use embedded_graphics::{ - mono_font::MonoTextStyleBuilder, - prelude::*, - text::{Baseline, Text, TextStyleBuilder}, -}; -use epd_waveshare::{epd2in9b_v3::*, prelude::*, color::Color}; use heapless::String; -use core::fmt::{self, Write as FmtWrite}; -use log::{info, error}; - +use log::{error, info}; #[entry] fn main() -> ! { @@ -60,15 +59,8 @@ fn main() -> ! { info!("Connecting to display"); // Setup EPD - let mut epd = Epd2in9b::new( - &mut spi, - busy, - dc, - rst, - &mut delay, - None - ).expect("failing setting up epd"); - + let mut epd = + Epd2in9b::new(&mut spi, busy, dc, rst, &mut delay, None).expect("failing setting up epd"); let mut mono_display = Display2in9b::default(); @@ -77,15 +69,17 @@ fn main() -> ! { // epd.wake_up(&mut spi, &mut delay).expect("Failed waking up epd"); - epd.wait_until_idle(&mut spi, &mut delay).expect("Failed waiting until idle"); + epd.wait_until_idle(&mut spi, &mut delay) + .expect("Failed waiting until idle"); - epd.update_frame(&mut spi, mono_display.buffer(), &mut delay).expect("Failed updating frame"); - epd - .display_frame(&mut spi, &mut delay).expect("Failed displaying frame"); + epd.update_frame(&mut spi, mono_display.buffer(), &mut delay) + .expect("Failed updating frame"); + epd.display_frame(&mut spi, &mut delay) + .expect("Failed displaying frame"); // Set the EPD to sleep - epd.sleep(&mut spi, &mut delay).expect("Failed sleeping epd"); - + epd.sleep(&mut spi, &mut delay) + .expect("Failed sleeping epd"); let mut counter = 0; let mut counter_text: String<20> = String::new(); @@ -93,7 +87,8 @@ fn main() -> ! { loop { counter_text.clear(); // fmt::write(&mut counter_text, format_args!("Counter: {}", counter)).expect("Error occurred while trying to write in String"); - write!(counter_text, "Counter: {counter}").expect("Error occurred while trying to write in String"); + write!(counter_text, "Counter: {counter}") + .expect("Error occurred while trying to write in String"); info!("Sleeping"); delay.delay_ms(30000u16); @@ -101,25 +96,27 @@ fn main() -> ! { mono_display.set_rotation(DisplayRotation::Rotate270); draw_text(&mut mono_display, counter_text.as_str(), 5, 10); - + info!("waking up display"); - epd.wake_up(&mut spi, &mut delay).expect("Failed waking up epd"); - - epd.wait_until_idle(&mut spi, &mut delay).expect("Failed waiting until idle"); + epd.wake_up(&mut spi, &mut delay) + .expect("Failed waking up epd"); + epd.wait_until_idle(&mut spi, &mut delay) + .expect("Failed waiting until idle"); info!("updating display frame"); - epd.update_frame(&mut spi, mono_display.buffer(), &mut delay).expect("Failed updating frame"); - epd - .display_frame(&mut spi, &mut delay).expect("Failed displaying frame"); - + epd.update_frame(&mut spi, mono_display.buffer(), &mut delay) + .expect("Failed updating frame"); + epd.display_frame(&mut spi, &mut delay) + .expect("Failed displaying frame"); + // Set the EPD to sleep - epd.sleep(&mut spi, &mut delay).expect("Failed sleeping epd"); + epd.sleep(&mut spi, &mut delay) + .expect("Failed sleeping epd"); counter += 1; } } - fn draw_text(display: &mut Display2in9b, text: &str, x: i32, y: i32) { let style = MonoTextStyleBuilder::new() .font(&embedded_graphics::mono_font::ascii::FONT_8X13_BOLD) @@ -130,4 +127,4 @@ fn draw_text(display: &mut Display2in9b, text: &str, x: i32, y: i32) { let text_style = TextStyleBuilder::new().baseline(Baseline::Top).build(); let _ = Text::with_text_style(text, Point::new(x, y), style, text_style).draw(display); -} \ No newline at end of file +} diff --git a/esp32c3_nostd/examples/led_matrix.rs b/esp32c3_nostd/examples/led_matrix.rs index bc5f292..1b39a2b 100644 --- a/esp32c3_nostd/examples/led_matrix.rs +++ b/esp32c3_nostd/examples/led_matrix.rs @@ -1,35 +1,35 @@ #![no_std] #![no_main] +use embedded_hal::blocking::spi::Write; +use embedded_hal::digital::v2::{InputPin, OutputPin}; use esp_backtrace as _; use esp_println::println; use hal::{ - clock::ClockControl, - peripherals::Peripherals, - spi::{Spi, SpiMode, FullDuplexMode}, + clock::ClockControl, gpio::IO, - prelude::*, + peripherals::Peripherals, + prelude::*, + spi::{FullDuplexMode, Spi, SpiMode}, Delay, }; -use embedded_hal::blocking::spi::Write; -use embedded_hal::digital::v2::{OutputPin, InputPin}; // use embedded_graphics::{ // pixelcolor::BinaryColor::On as Black, prelude::*, primitives::{Line, PrimitiveStyle}, // }; use embedded_graphics::{ - mono_font::{MonoTextStyle, ascii}, + mono_font::{ascii, MonoTextStyle}, + pixelcolor::BinaryColor, prelude::*, primitives::{Circle, Line, PrimitiveStyle}, text::{Baseline, Text, TextStyleBuilder}, - pixelcolor::BinaryColor, }; -use log::{info, error}; +use log::{error, info}; use max7219_driver::MAX7219LedMat; enum Error> { - EpdError(>::Error) -} + EpdError(>::Error), +} #[entry] fn main() -> ! { @@ -62,7 +62,8 @@ fn main() -> ! { ); info!("Connecting to MAX7219 display"); - let mut display: MAX7219LedMat<_, _, 256, 4> = MAX7219LedMat::new(spi, cs).expect("failed instantiating display"); + let mut display: MAX7219LedMat<_, _, 256, 4> = + MAX7219LedMat::new(spi, cs).expect("failed instantiating display"); display.init_display(true); let txtstyle = MonoTextStyle::new(&ascii::FONT_6X10, BinaryColor::On); @@ -74,6 +75,5 @@ fn main() -> ! { delay.delay_ms(5000u16); info!("Waiting"); - } } diff --git a/esp32c3_nostd/src/main.rs b/esp32c3_nostd/src/main.rs index 991936a..6a311fe 100644 --- a/esp32c3_nostd/src/main.rs +++ b/esp32c3_nostd/src/main.rs @@ -1,24 +1,27 @@ #![no_std] #![no_main] -use esp_backtrace as _; +use display_themes::Theme2; +use epd_display::{Display, DisplayTheme}; +use epd_waveshare::{ + epd2in9b_v3::{Display2in9b, Epd2in9b}, + graphics::DisplayRotation, +}; use esp32c3_hal::{ - clock::ClockControl, - peripherals::Peripherals, + clock::ClockControl, + gpio::IO, i2c::I2C, + peripherals::Peripherals, + prelude::*, spi::{ master::{Spi, SpiBusController}, SpiMode, }, - gpio::IO, - prelude::*, Delay, }; -use epd_waveshare::{epd2in9b_v3::{Display2in9b, Epd2in9b}, graphics::DisplayRotation}; +use esp_backtrace as _; use log::info; -use display_themes::Theme2; -use epd_display::{Display, DisplayTheme}; -use sensor::{Sensor, Scd4xSensor}; +use sensor::{Scd4xSensor, Sensor}; #[entry] fn main() -> ! { @@ -38,13 +41,7 @@ fn main() -> ! { let i2c_scl = io.pins.gpio0; let i2c_sda = io.pins.gpio1; - let i2c = I2C::new( - peripherals.I2C0, - i2c_sda, - i2c_scl, - 100u32.kHz(), - &clocks, - ); + let i2c = I2C::new(peripherals.I2C0, i2c_sda, i2c_scl, 100u32.kHz(), &clocks); info!("Connecting to sensor"); let mut sensor = Scd4xSensor::new(i2c, delay); @@ -68,25 +65,13 @@ fn main() -> ! { info!("Connecting to display"); - let epd = Epd2in9b::new( - &mut spi, - busy, - dc, - rst, - &mut delay, - None - ).expect("failing setting up epd"); - + let epd = + Epd2in9b::new(&mut spi, busy, dc, rst, &mut delay, None).expect("failing setting up epd"); + let mut draw_target = Display2in9b::default(); draw_target.set_rotation(DisplayRotation::Rotate270); - - let mut display = Display::new( - spi, - epd, - draw_target, - delay, - Theme2::new() - ); + + let mut display = Display::new(spi, epd, draw_target, delay, Theme2::new()); loop { let data = sensor.measure().expect("failed reading sensor"); diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..d9267ef --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +indent_style = "Block" +reorder_imports = true \ No newline at end of file diff --git a/sensor/src/lib.rs b/sensor/src/lib.rs index 094d36f..ae7e0a2 100644 --- a/sensor/src/lib.rs +++ b/sensor/src/lib.rs @@ -9,4 +9,4 @@ pub trait Sensor { type Error; fn measure(&mut self) -> Result; -} \ No newline at end of file +} diff --git a/sensor/src/scd4x_sensor.rs b/sensor/src/scd4x_sensor.rs index e1361b0..23e3d6a 100644 --- a/sensor/src/scd4x_sensor.rs +++ b/sensor/src/scd4x_sensor.rs @@ -1,32 +1,31 @@ -use airquamon_domain::Data; -use embedded_hal::{i2c::I2c, delay::DelayUs}; -use scd4x::{Scd4x, Error}; use crate::Sensor; +use airquamon_domain::Data; +use embedded_hal::{delay::DelayUs, i2c::I2c}; +use scd4x::{Error, Scd4x}; pub struct Scd4xSensor { scd4x: Scd4x, delay: DELAY, } -impl Scd4xSensor -where -I2C: I2c, -DELAY: DelayUs + Copy, +impl Scd4xSensor +where + I2C: I2c, + DELAY: DelayUs + Copy, { pub fn new(i2c: I2C, delay: DELAY) -> Self { - Self { - scd4x: Scd4x::new(i2c, delay), + Self { + scd4x: Scd4x::new(i2c, delay), delay, } } } -impl Sensor for Scd4xSensor -where -I2C: I2c, -DELAY: DelayUs, +impl Sensor for Scd4xSensor +where + I2C: I2c, + DELAY: DelayUs, { - type Error = Error; fn measure(&mut self) -> Result { @@ -40,16 +39,16 @@ DELAY: DelayUs, Ok(false) => { self.delay.delay_ms(100); Ok(()) - }, + } Err(e) => Err(e), }?; } let data = self.scd4x.measurement()?; self.scd4x.stop_periodic_measurement()?; - Ok(Data { - co2: data.co2, - temperature: data.temperature, - humidity: data.humidity + Ok(Data { + co2: data.co2, + temperature: data.temperature, + humidity: data.humidity, }) } -} \ No newline at end of file +}