Skip to content

Commit

Permalink
fix get length echo time function
Browse files Browse the repository at this point in the history
  • Loading branch information
yogiastawan committed Oct 29, 2023
1 parent 8a76610 commit 8eefa0e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
12 changes: 6 additions & 6 deletions examples/use_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ impl<TIM: Instance> TickerUs for MyCounter<TIM> {
}
}

// unsafe impl<TIM> Send for MyCounter<TIM> where TIM: Send {}

#[rtic::app(device=stm32f1xx_hal::pac)]
mod app {

Expand Down Expand Up @@ -86,12 +84,14 @@ mod app {
#[idle(shared=[hcsr04])]
fn idle(ctx: idle::Context) -> ! {
let mut sensor = ctx.shared.hcsr04;
sensor.lock(
|x| match x.get_distance::<f32>(gihex_hc_sr04::DistanceUnit::MilliMeter) {
sensor.lock(|x| {
match x.get_distance::<f32>(gihex_hc_sr04::DistanceUnit::MilliMeter) {
Ok(value) => hprint!("Distance: {}", value),
Err(_x) => hprint!("still waiting for sound return"),
},
);
};
let time = x.get_last_length_echo_time::<u32>(gihex_hc_sr04::TimeUnit::MicroSecond);
hprint!("last echo duration: {}", time);
});

loop {
wfi();
Expand Down
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#![no_std]

pub mod us_timer;
pub mod us_traits;

use core::marker::PhantomData;

use embedded_hal::{blocking, digital::v2};
use num_traits::{float::FloatCore, NumCast};

const ULTRASONIC_SPEED_HALF: f32 = 171_605.0; //mm/s
const ULTRASONIC_SPEED_HALF: f32 = 171_605.0; //mm per second

const TRIGGER_TIME_IN_US: u8 = 10;
// const TRIGGER_WAIT_IN_US: u8 = 10;

pub enum HsError {
OnWaitingEcho,
Expand Down Expand Up @@ -112,14 +110,17 @@ where
}
}

pub fn get_last_length_echo_time<T>(&mut self, unit: TimeUnit) -> u32 {
pub fn get_last_length_echo_time<T>(&mut self, unit: TimeUnit) -> T
where
T: NumCast,
{
let divider: u32 = match unit {
TimeUnit::MicroSecond => return self.last_length_time,
TimeUnit::MicroSecond => return NumCast::from(self.last_length_time).unwrap(),
TimeUnit::MilliSecond => 1000,
TimeUnit::Second => 1_000_000,
};

self.last_length_time / divider
NumCast::from(self.last_length_time / divider).unwrap()
}

pub fn send_ping_force(&mut self) {
Expand Down
11 changes: 0 additions & 11 deletions src/us_traits.rs

This file was deleted.

0 comments on commit 8eefa0e

Please sign in to comment.