Add reading from analogue sensor and displaying it

main
trivernis 2 years ago
parent b528faf5cf
commit 44e53776c5
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -5,6 +5,8 @@ use panic_halt as _;
use fugit::RateExtU32;
use cortex_m::prelude::{_embedded_hal_watchdog_Watchdog, _embedded_hal_watchdog_WatchdogEnable};
use embedded_hal::adc::OneShot;
use hal::i2c;
pub use sparkfun_pro_micro_rp2040::hal;
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
@ -23,11 +25,14 @@ use hal::gpio::Pin;
use hal::pac::I2C1;
use hal::timer::Alarm;
use hal::timer::Alarm0;
use hal::Adc;
const XTAL_FREQ_HZ: u32 = 12_000_000u32;
#[rtic::app(device = sparkfun_pro_micro_rp2040::hal::pac, peripherals = true)]
mod app {
const CHANGE_FREQUENCY_TICKS: u32 = 1000;
const CHANGE_FREQUENCY_TICKS: u32 = 500_000;
use super::*;
type Display = Ssd1306<
@ -52,7 +57,9 @@ mod app {
#[local]
struct Local {
display: Display,
state: u32,
adc: Adc,
adc_pin: Pin<hal::gpio::bank0::Gpio29, hal::gpio::FloatingInput>,
watchdog: hal::Watchdog,
}
#[init()]
@ -60,6 +67,19 @@ mod app {
let mut resets = c.device.RESETS;
let sio = hal::Sio::new(c.device.SIO);
let mut watchdog = hal::Watchdog::new(c.device.WATCHDOG);
let _clocks = hal::clocks::init_clocks_and_plls(
XTAL_FREQ_HZ,
c.device.XOSC,
c.device.CLOCKS,
c.device.PLL_SYS,
c.device.PLL_USB,
&mut resets,
&mut watchdog,
)
.ok()
.unwrap();
let pins = rp2040_hal::gpio::Pins::new(
c.device.IO_BANK0,
c.device.PADS_BANK0,
@ -86,24 +106,43 @@ mod app {
.into_buffered_graphics_mode();
display.init().unwrap();
Text::with_alignment(
"Initialized",
display.bounding_box().center(),
MonoTextStyle::new(&FONT_10X20, BinaryColor::On),
Alignment::Center,
)
.draw(&mut display)
.unwrap();
display.flush().unwrap();
let adc = Adc::new(c.device.ADC, &mut resets);
let adc_pin = pins.gpio29.into_floating_input();
(
Shared { alarm },
Local { display, state: 0 },
Local {
display,
adc,
adc_pin,
watchdog,
},
init::Monotonics(),
)
}
#[task(binds = TIMER_IRQ_0, priority = 1, shared = [alarm], local = [display, state])]
#[task(binds = TIMER_IRQ_0, priority = 1, shared = [alarm], local = [display, adc, adc_pin, watchdog])]
fn tick(mut c: tick::Context) {
use numtoa::NumToA;
let display = c.local.display;
display.clear();
let mut buf = [0u8; 20];
let text = c.local.state.numtoa_str(10, &mut buf);
*c.local.state += 1;
let result: Result<u16, _> = c.local.adc.read(c.local.adc_pin);
let text = match result {
Ok(reading_raw) => reading_raw.numtoa_str(10, &mut buf),
Err(_) => "Error",
};
Text::with_alignment(
text,
@ -114,6 +153,7 @@ mod app {
.draw(display)
.unwrap();
display.flush().unwrap();
c.local.watchdog.feed();
c.shared.alarm.lock(|a| {
a.clear_interrupt();

Loading…
Cancel
Save