Create extra DhtProgram struct

This commit is contained in:
hodasemi 2024-07-11 12:02:25 +02:00
parent a289b2f4c9
commit 0dcd9a055b
3 changed files with 30 additions and 8 deletions

View file

@ -4,9 +4,8 @@ use embassy_rp::pio::{
use embassy_time::Timer; use embassy_time::Timer;
use fixed::traits::ToFixed; use fixed::traits::ToFixed;
use fixed_macro::types::U56F8; 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> { pub struct DhtPio<'d, PIO: Instance, const SM: usize> {
sm: StateMachine<'d, PIO, SM>, 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> { impl<'d, PIO: Instance, const SM: usize> DhtPio<'d, PIO, SM> {
pub fn new( pub fn new(
dht_program: &DhtProgram<'d, PIO>,
pio: &mut Common<'d, PIO>, pio: &mut Common<'d, PIO>,
mut sm: StateMachine<'d, PIO, SM>, mut sm: StateMachine<'d, PIO, SM>,
dht_pin: impl PioPin, dht_pin: impl PioPin,
) -> Self { ) -> Self {
let program = pio_file!("./src/dht.pio");
let pin = pio.make_pio_pin(dht_pin); let pin = pio.make_pio_pin(dht_pin);
let mut config = Config::default(); 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_out_pins(&[&pin]);
config.set_set_pins(&[&pin]); config.set_set_pins(&[&pin]);
config.set_in_pins(&[&pin]); config.set_in_pins(&[&pin]);

View file

@ -3,7 +3,9 @@
use embassy_rp::pio::{Common, Instance, PioPin, StateMachine}; use embassy_rp::pio::{Common, Instance, PioPin, StateMachine};
mod dht; mod dht;
mod program;
use dht::DhtPio; use dht::DhtPio;
pub use program::DhtProgram;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug)] #[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> { impl<'d, PIO: Instance, const SM: usize> Dht22<'d, PIO, SM> {
pub fn new( pub fn new(
dht_program: &DhtProgram<'d, PIO>,
pio: &mut Common<'d, PIO>, pio: &mut Common<'d, PIO>,
sm: StateMachine<'d, PIO, SM>, sm: StateMachine<'d, PIO, SM>,
dht_pin: impl PioPin, dht_pin: impl PioPin,
) -> Self { ) -> Self {
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> { impl<'d, PIO: Instance, const SM: usize> Dht22Type2<'d, PIO, SM> {
pub fn new( pub fn new(
dht_program: &DhtProgram<'d, PIO>,
pio: &mut Common<'d, PIO>, pio: &mut Common<'d, PIO>,
sm: StateMachine<'d, PIO, SM>, sm: StateMachine<'d, PIO, SM>,
dht_pin: impl PioPin, dht_pin: impl PioPin,
) -> Self { ) -> Self {
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> { impl<'d, PIO: Instance, const SM: usize> Dht11<'d, PIO, SM> {
pub fn new( pub fn new(
dht_program: &DhtProgram<'d, PIO>,
pio: &mut Common<'d, PIO>, pio: &mut Common<'d, PIO>,
sm: StateMachine<'d, PIO, SM>, sm: StateMachine<'d, PIO, SM>,
dht_pin: impl PioPin, dht_pin: impl PioPin,
) -> Self { ) -> Self {
Self { Self {
dht: DhtPio::new(pio, sm, dht_pin), dht: DhtPio::new(dht_program, pio, sm, dht_pin),
} }
} }
} }

19
src/program.rs Normal file
View file

@ -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
}
}