Skip to content

Commit

Permalink
try to use uart
Browse files Browse the repository at this point in the history
  • Loading branch information
gauteh committed Apr 5, 2024
1 parent c50a809 commit a29ffdc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
2 changes: 2 additions & 0 deletions sfy-buoy/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 23 additions & 14 deletions sfy-buoy/src/temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use core::marker::PhantomData;
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
use embedded_hal::digital::v2::{InputPin, OutputPin};

use embedded_hal::serial::{Read, Write};
use heapless::Vec;
use one_wire_bus::{Address, OneWire, OneWireError};
use one_wire_bus::{Address, BaudRate, OneWire, OneWireError};

use crate::waves::wire::ScaledF32;

Expand All @@ -18,11 +19,9 @@ enum TempsState {
Failed(i64), // failure, with timestamp (millis) since failure
}

type E = Infallible;

pub struct Temps {
wire: OneWire,
probes: Vec<Probe, MAX_PROBES>,
pub struct Temps<U> {
wire: OneWire<U>,
pub probes: Vec<Probe, MAX_PROBES>,
resolution: ds18b20::Resolution,
state: TempsState,
}
Expand All @@ -32,13 +31,20 @@ pub struct Probe {
pub sensor: ds18b20::Ds18b20,
}

impl Temps {
impl<U, E> Temps<U>
where
U: Read<u8, Error = E> + Write<u8, Error = E>,
{
/// Scan for devices, init and set up logging.
///
/// Can be re-run to reset.
pub fn new(w: one_wire_bus::TI, delay: &mut impl DelayUs<u16>) -> Result<Temps, OneWireError<E>> {
pub fn new(
w: U,
set_baudrate: fn(&mut U, BaudRate) -> (),
delay: &mut impl DelayUs<u16>,
) -> Result<Temps<U>, OneWireError<E>> {
defmt::info!("setting up temperature sensors..");
let mut wire = OneWire::new(w)?;
let mut wire = OneWire::new(w, set_baudrate)?;
let resolution = ds18b20::Resolution::Bits12;
let mut addresses = heapless::Vec::<_, MAX_PROBES>::new();

Expand Down Expand Up @@ -77,8 +83,8 @@ impl Temps {
for addr in addresses {
probes
.push(Probe::new(addr, &mut wire, resolution, delay)?)
.map_err(|_| ()) // ds18b20 doesn't impl Debug
.unwrap(); // already checked size
.ok();
// .unwrap(); // already checked size
}

Ok(Temps {
Expand Down Expand Up @@ -155,12 +161,15 @@ impl Temps {
}

impl Probe {
pub fn new(
pub fn new<U, E>(
address: Address,
wire: &mut OneWire,
wire: &mut OneWire<U>,
resolution: ds18b20::Resolution,
delay: &mut impl DelayUs<u16>,
) -> Result<Probe, OneWireError<E>> {
) -> Result<Probe, OneWireError<E>>
where
U: Read<u8, Error = E> + Write<u8, Error = E>,
{
let sensor = ds18b20::Ds18b20::new(address)?;

// configure
Expand Down
1 change: 1 addition & 0 deletions sfy-buoy/target-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ embedded-hal = "0.2.6"
cmsis_dsp = { version = "0.1.0", features = [ "micromath" ] }
embedded-sdmmc = { version = "0.6.0", default-features = false, features = ["defmt-log"] }
micromath = "2.1.0"
one-wire-bus = "*"

[dependencies.ambiq-hal]
version = "0.3"
Expand Down
24 changes: 21 additions & 3 deletions sfy-buoy/target-test/tests/temp_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ use panic_probe as _; // memory layout + panic handler
use embedded_hal::{
blocking::delay::DelayUs,
blocking::spi::{write::Default as DefaultWrite, Transfer},
digital::v2::{OutputPin, InputPin},
digital::v2::{InputPin, OutputPin},
spi::FullDuplex,
};
use embedded_sdmmc::{
Error as GenericSdMmcError, Mode, SdCard, SdCardError, VolumeIdx, VolumeManager,
};
use one_wire_bus::BaudRate;
use sfy::storage::{self, Storage};

pub static COUNT: AtomicI32 = AtomicI32::new(0);
Expand Down Expand Up @@ -123,6 +124,11 @@ mod tests {

let mut led = pins.d19.into_push_pull_output();

let tx = pins.a16;
let rx = pins.a0;

let uart = hal::uart::new_12_13(dp.UART1, tx, rx, 115200);

// pin D8 (on artemis nano)
let mut tp = pins.d8.into_input();
// let mut tp = pins.d10.into_input_output();
Expand Down Expand Up @@ -158,7 +164,20 @@ mod tests {
defmt::info!("setting up dsb driver");
defmt::flush();
delay.delay_ms(1000_u32);
let temp = sfy::temp::Temps::new(tp, &mut delay).unwrap();
let temp = sfy::temp::Temps::new(
uart,
|uart, br| match br {
BaudRate::B9600 => {
uart.set_baudrate(9600);
}
BaudRate::B115200 => {
uart.set_baudrate(115200);
}
},
&mut delay,
).unwrap();

defmt::info!("devices: {}", temp.probes.len());

for _ in 0..1000 {
defmt::info!("loop");
Expand All @@ -170,4 +189,3 @@ mod tests {
}
}
}

0 comments on commit a29ffdc

Please sign in to comment.