From 0dcd9a055b218f86a4474592b6bb54a2c7276bef Mon Sep 17 00:00:00 2001 From: hodasemi Date: Thu, 11 Jul 2024 12:02:25 +0200 Subject: [PATCH] Create extra `DhtProgram` struct --- src/dht.rs | 8 +++----- src/lib.rs | 11 ++++++++--- src/program.rs | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 src/program.rs diff --git a/src/dht.rs b/src/dht.rs index 1c50490..884008a 100644 --- a/src/dht.rs +++ b/src/dht.rs @@ -4,9 +4,8 @@ use embassy_rp::pio::{ use embassy_time::Timer; use fixed::traits::ToFixed; use fixed_macro::types::U56F8; -use pio_proc::pio_file; -use crate::DhtError; +use crate::{DhtError, DhtProgram}; pub struct DhtPio<'d, PIO: Instance, const SM: usize> { sm: StateMachine<'d, PIO, SM>, @@ -14,16 +13,15 @@ pub struct DhtPio<'d, PIO: Instance, const SM: usize> { impl<'d, PIO: Instance, const SM: usize> DhtPio<'d, PIO, SM> { pub fn new( + dht_program: &DhtProgram<'d, PIO>, pio: &mut Common<'d, PIO>, mut sm: StateMachine<'d, PIO, SM>, dht_pin: impl PioPin, ) -> Self { - let program = pio_file!("./src/dht.pio"); - let pin = pio.make_pio_pin(dht_pin); let mut config = Config::default(); - config.use_program(&pio.load_program(&program.program), &[]); + config.use_program(dht_program.program(), &[]); config.set_out_pins(&[&pin]); config.set_set_pins(&[&pin]); config.set_in_pins(&[&pin]); diff --git a/src/lib.rs b/src/lib.rs index b82bb4f..39ca8a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,9 @@ use embassy_rp::pio::{Common, Instance, PioPin, StateMachine}; mod dht; +mod program; use dht::DhtPio; +pub use program::DhtProgram; use serde::{Deserialize, Serialize}; #[derive(Debug)] @@ -32,12 +34,13 @@ pub struct Dht22<'d, PIO: Instance, const SM: usize> { impl<'d, PIO: Instance, const SM: usize> Dht22<'d, PIO, SM> { pub fn new( + dht_program: &DhtProgram<'d, PIO>, pio: &mut Common<'d, PIO>, sm: StateMachine<'d, PIO, SM>, dht_pin: impl PioPin, ) -> Self { Self { - dht: DhtPio::new(pio, sm, dht_pin), + dht: DhtPio::new(dht_program, pio, sm, dht_pin), } } } @@ -66,12 +69,13 @@ pub struct Dht22Type2<'d, PIO: Instance, const SM: usize> { impl<'d, PIO: Instance, const SM: usize> Dht22Type2<'d, PIO, SM> { pub fn new( + dht_program: &DhtProgram<'d, PIO>, pio: &mut Common<'d, PIO>, sm: StateMachine<'d, PIO, SM>, dht_pin: impl PioPin, ) -> Self { Self { - dht: DhtPio::new(pio, sm, dht_pin), + dht: DhtPio::new(dht_program, pio, sm, dht_pin), } } } @@ -97,12 +101,13 @@ pub struct Dht11<'d, PIO: Instance, const SM: usize> { impl<'d, PIO: Instance, const SM: usize> Dht11<'d, PIO, SM> { pub fn new( + dht_program: &DhtProgram<'d, PIO>, pio: &mut Common<'d, PIO>, sm: StateMachine<'d, PIO, SM>, dht_pin: impl PioPin, ) -> Self { Self { - dht: DhtPio::new(pio, sm, dht_pin), + dht: DhtPio::new(dht_program, pio, sm, dht_pin), } } } diff --git a/src/program.rs b/src/program.rs new file mode 100644 index 0000000..6e0fd3c --- /dev/null +++ b/src/program.rs @@ -0,0 +1,19 @@ +use embassy_rp::pio::{Common, Instance, LoadedProgram}; +use pio_proc::pio_file; + +pub struct DhtProgram<'d, PIO: Instance> { + program: LoadedProgram<'d, PIO>, +} + +impl<'d, PIO: Instance> DhtProgram<'d, PIO> { + pub fn new(pio: &mut Common<'d, PIO>) -> Self { + let program = pio_file!("./src/dht.pio"); + Self { + program: pio.load_program(&program.program), + } + } + + pub(crate) fn program(&self) -> &LoadedProgram<'d, PIO> { + &self.program + } +}