Decouple analog read from display refresh and add moving average

main
trivernis 1 year ago
parent 44e53776c5
commit fded66b443
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: DFFFCC2C7A02DB45

@ -5,7 +5,7 @@ use panic_halt as _;
use fugit::RateExtU32; use fugit::RateExtU32;
use cortex_m::prelude::{_embedded_hal_watchdog_Watchdog, _embedded_hal_watchdog_WatchdogEnable}; use cortex_m::prelude::_embedded_hal_watchdog_Watchdog;
use embedded_hal::adc::OneShot; use embedded_hal::adc::OneShot;
use hal::i2c; use hal::i2c;
pub use sparkfun_pro_micro_rp2040::hal; pub use sparkfun_pro_micro_rp2040::hal;
@ -23,16 +23,17 @@ use fugit::MicrosDurationU32;
use hal::gpio::bank0::{Gpio14, Gpio15}; use hal::gpio::bank0::{Gpio14, Gpio15};
use hal::gpio::Pin; use hal::gpio::Pin;
use hal::pac::I2C1; use hal::pac::I2C1;
use hal::timer::Alarm; use hal::timer::{Alarm, Alarm0, Alarm1};
use hal::timer::Alarm0;
use hal::Adc; use hal::Adc;
const XTAL_FREQ_HZ: u32 = 12_000_000u32; const XTAL_FREQ_HZ: u32 = 12_000_000u32;
const MVG_AVG_COUNT: u16 = 20;
#[rtic::app(device = sparkfun_pro_micro_rp2040::hal::pac, peripherals = true)] #[rtic::app(device = sparkfun_pro_micro_rp2040::hal::pac, peripherals = true)]
mod app { mod app {
const CHANGE_FREQUENCY_TICKS: u32 = 500_000; const DISPLAY_UPDATE_INT_TICKS: u32 = 500_000;
const SENSOR_READ_INT_TICKS: u32 = 50_000;
use super::*; use super::*;
type Display = Ssd1306< type Display = Ssd1306<
@ -51,7 +52,9 @@ mod app {
#[shared] #[shared]
struct Shared { struct Shared {
alarm: Alarm0, alarm0: Alarm0,
alarm1: Alarm1,
sensor_value: u16,
} }
#[local] #[local]
@ -87,9 +90,13 @@ mod app {
&mut resets, &mut resets,
); );
let mut timer = hal::Timer::new(c.device.TIMER, &mut resets); let mut timer = hal::Timer::new(c.device.TIMER, &mut resets);
let mut alarm = timer.alarm_0().unwrap(); let mut alarm0 = timer.alarm_0().unwrap();
let _ = alarm.schedule(MicrosDurationU32::from_ticks(CHANGE_FREQUENCY_TICKS)); let _ = alarm0.schedule(MicrosDurationU32::from_ticks(DISPLAY_UPDATE_INT_TICKS));
alarm.enable_interrupt(); alarm0.enable_interrupt();
let mut alarm1 = timer.alarm_1().unwrap();
let _ = alarm1.schedule(MicrosDurationU32::from_ticks(SENSOR_READ_INT_TICKS));
alarm1.enable_interrupt();
let scl = pins.gpio15.into_mode(); let scl = pins.gpio15.into_mode();
let sda = pins.gpio14.into_mode(); let sda = pins.gpio14.into_mode();
@ -120,7 +127,11 @@ mod app {
let adc_pin = pins.gpio29.into_floating_input(); let adc_pin = pins.gpio29.into_floating_input();
( (
Shared { alarm }, Shared {
alarm0,
alarm1,
sensor_value: 0,
},
Local { Local {
display, display,
adc, adc,
@ -131,19 +142,17 @@ mod app {
) )
} }
#[task(binds = TIMER_IRQ_0, priority = 1, shared = [alarm], local = [display, adc, adc_pin, watchdog])] #[task(binds = TIMER_IRQ_0, priority = 1, shared = [alarm0, sensor_value], local = [display])]
fn tick(mut c: tick::Context) { fn update_display(mut c: update_display::Context) {
use numtoa::NumToA;
let display = c.local.display;
display.clear();
let mut buf = [0u8; 20]; let mut buf = [0u8; 20];
let result: Result<u16, _> = c.local.adc.read(c.local.adc_pin);
let text = match result { let text = c.shared.sensor_value.lock(|val| {
Ok(reading_raw) => reading_raw.numtoa_str(10, &mut buf), use numtoa::NumToA;
Err(_) => "Error", val.numtoa_str(10, &mut buf)
}; });
let display = c.local.display;
display.clear();
Text::with_alignment( Text::with_alignment(
text, text,
display.bounding_box().center(), display.bounding_box().center(),
@ -153,11 +162,24 @@ mod app {
.draw(display) .draw(display)
.unwrap(); .unwrap();
display.flush().unwrap(); display.flush().unwrap();
c.local.watchdog.feed();
c.shared.alarm.lock(|a| { c.shared.alarm0.lock(|a| {
a.clear_interrupt();
let _ = a.schedule(MicrosDurationU32::from_ticks(DISPLAY_UPDATE_INT_TICKS));
});
}
#[task(binds = TIMER_IRQ_1, priority = 2, shared = [alarm1, sensor_value], local = [adc, adc_pin, watchdog])]
fn read_sensor(mut c: read_sensor::Context) {
let sensor_value: u16 = c.local.adc.read(c.local.adc_pin).unwrap();
c.shared
.sensor_value
.lock(|v| *v = (*v * (MVG_AVG_COUNT - 1) + sensor_value) / MVG_AVG_COUNT);
c.local.watchdog.feed();
c.shared.alarm1.lock(|a| {
a.clear_interrupt(); a.clear_interrupt();
let _ = a.schedule(MicrosDurationU32::from_ticks(CHANGE_FREQUENCY_TICKS)); let _ = a.schedule(MicrosDurationU32::from_ticks(DISPLAY_UPDATE_INT_TICKS));
}); });
} }

Loading…
Cancel
Save