Skip to content

Commit

Permalink
Merge pull request #144 from card-io-ecg/init
Browse files Browse the repository at this point in the history
Clean up startup code
  • Loading branch information
bugadani authored Oct 26, 2023
2 parents 0358a59 + 7abf903 commit be1709c
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 285 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ macros = { path = "macros" }
embassy-alloc-taskpool = { path = "embassy-alloc-taskpool" }
ads129x = { path = "ads129x", features = ["ufmt-impl"] }
max17055 = { path = "max17055", optional = true, features = ["ufmt-impl"] }
signal-processing = { workspace = true }
signal-processing = { workspace = true, features = ["alloc"] }
replace_with = { version = "0.1", default-features = false, features = [
"nightly",
] }
Expand Down
24 changes: 23 additions & 1 deletion signal-processing/src/compressing_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! difference from the last. This is useful for storing a sequence of values that are
//! close to each other, such as a sequence of samples from a sensor.
use core::slice;
use core::{fmt::Debug, slice};

use embedded_io::blocking::{Read, ReadExactError, Write};

Expand Down Expand Up @@ -85,6 +85,28 @@ pub struct CompressingBuffer<const N: usize> {
buffer: Buffer<u8, N, true>,
}

impl<const N: usize> Debug for CompressingBuffer<N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("CompressingBuffer")
.field(&self.element_count)
.finish()
}
}

#[cfg(feature = "defmt")]
impl<const N: usize> defmt::Format for CompressingBuffer<N> {
fn format(&self, fmt: defmt::Formatter<'_>) {
defmt::write!(fmt, "CompressingBuffer({})", self.element_count)
}
}

#[cfg(all(feature = "defmt", feature = "alloc"))]
impl<const N: usize> defmt::Format for alloc::boxed::Box<CompressingBuffer<N>> {
fn format(&self, fmt: defmt::Formatter<'_>) {
self.as_ref().format(fmt)
}
}

impl<const N: usize> CompressingBuffer<N> {
pub const EMPTY: Self = Self::new();

Expand Down
66 changes: 27 additions & 39 deletions src/board/drivers/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
Self { display, reset }
}

pub async fn enable(mut self) -> Result<PoweredDisplay<RESET>, DisplayError> {
pub async fn enable(&mut self) -> Result<(), DisplayError> {
unwrap!(self
.display
.reset_async::<_, Delay>(&mut self.reset, &mut Delay)
Expand All @@ -54,37 +54,9 @@ where
self.display.clear(BinaryColor::Off)?;
self.display.flush_async().await?;

Ok(PoweredDisplay { display: self })
Ok(())
}
}

pub struct PoweredDisplay<RESET> {
display: Display<RESET>,
}

impl<RESET> Dimensions for PoweredDisplay<RESET> {
fn bounding_box(&self) -> Rectangle {
self.display.display.bounding_box()
}
}

impl<RESET> DrawTarget for PoweredDisplay<RESET> {
type Color = BinaryColor;
type Error = DisplayError;

fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>>,
{
self.display.display.draw_iter(pixels)
}

fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
self.display.display.clear(color)
}
}

impl<RESET> PoweredDisplay<RESET> {
async fn frame_impl(
&mut self,
render: impl FnOnce(&mut Self) -> Result<(), DisplayError>,
Expand All @@ -101,23 +73,39 @@ impl<RESET> PoweredDisplay<RESET> {
}

pub async fn flush(&mut self) -> Result<(), DisplayError> {
self.display.display.flush_async().await
self.display.flush_async().await
}

pub async fn update_brightness_async(
&mut self,
brightness: Brightness,
) -> Result<(), DisplayError> {
self.display.display.set_brightness_async(brightness).await
self.display.set_brightness_async(brightness).await
}

pub fn shut_down(&mut self) {
unwrap!(self.reset.set_low().ok());
}
}

impl<RESET> PoweredDisplay<RESET>
where
RESET: OutputPin,
{
pub fn shut_down(mut self) -> Display<RESET> {
unwrap!(self.display.reset.set_low().ok());
self.display
impl<RESET> Dimensions for Display<RESET> {
fn bounding_box(&self) -> Rectangle {
self.display.bounding_box()
}
}

impl<RESET> DrawTarget for Display<RESET> {
type Color = BinaryColor;
type Error = DisplayError;

fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>>,
{
self.display.draw_iter(pixels)
}

fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
self.display.clear(color)
}
}
24 changes: 10 additions & 14 deletions src/board/hardware/v1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::board::{
drivers::{
battery_monitor::battery_adc::BatteryAdc as BatteryAdcType,
display::{Display as DisplayType, PoweredDisplay as PoweredDisplayType},
battery_monitor::{battery_adc::BatteryAdc as BatteryAdcType, BatteryMonitor},
display::Display as DisplayType,
frontend::{Frontend, PoweredFrontend},
},
hal::{
Expand All @@ -20,7 +20,6 @@ use crate::board::{
startup::WIFI_DRIVER,
utils::DummyOutputPin,
wifi::WifiDriver,
*,
};
use embassy_time::Delay;
use embedded_hal_bus::spi::ExclusiveDevice;
Expand Down Expand Up @@ -66,12 +65,11 @@ pub type PoweredEcgFrontend =
PoweredFrontend<AdcSpi<'static>, AdcDrdy, AdcReset, AdcClockEnable, TouchDetect>;

pub type Display = DisplayType<DisplayReset>;
pub type PoweredDisplay = PoweredDisplayType<DisplayReset>;

pub type BatteryAdc = BatteryAdcType<BatteryAdcInput, ChargeCurrentInput, BatteryAdcEnable, ADC2>;

impl super::startup::StartupResources {
pub fn initialize() -> Self {
pub async fn initialize() -> Self {
Self::common_init();

let peripherals = Peripherals::take();
Expand Down Expand Up @@ -116,17 +114,20 @@ impl super::startup::StartupResources {

// Battery ADC
let analog = peripherals.SENS.split();

let battery_adc =
BatteryAdc::new(analog.adc2, io.pins.gpio17, io.pins.gpio14, io.pins.gpio8);
let battery_monitor = BatteryMonitor::start(
io.pins.gpio47.into(),
io.pins.gpio21.into(),
BatteryAdc::new(analog.adc2, io.pins.gpio17, io.pins.gpio14, io.pins.gpio8),
)
.await;

// Wifi
let (wifi, _) = peripherals.RADIO.split();

Self {
display,
frontend: adc,
battery_adc,
battery_monitor,
wifi: WIFI_DRIVER.init_with(|| {
WifiDriver::new(
wifi,
Expand All @@ -138,11 +139,6 @@ impl super::startup::StartupResources {
}),
clocks,
rtc: Rtc::new(peripherals.RTC_CNTL),

misc_pins: MiscPins {
vbus_detect: io.pins.gpio47.into(),
chg_status: io.pins.gpio21.into(),
},
}
}
}
Loading

0 comments on commit be1709c

Please sign in to comment.