Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNS - Keyence Configurable Update Edge #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion boards/stm32f767zi/src/bin/can_io_recv_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use embassy_stm32::can::{
use embassy_stm32::peripherals::CAN1;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::blocking_mutex::Mutex;
use embassy_time::Timer;
use hyped_boards_stm32f767zi::io::Stm32f767ziCan;
use hyped_can::HypedCan;
use static_cell::StaticCell;
Expand Down
3 changes: 2 additions & 1 deletion boards/stm32f767zi/src/tasks/read_keyence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::io::Stm32f767ziGpioInput;
use embassy_stm32::gpio::Input;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, watch::Sender};
use embassy_time::{Duration, Timer};
use hyped_core::types::DigitalSignal;
use hyped_sensors::keyence::Keyence;

/// Test task that just continually updates the stripe count from the Keyence sensor (or other GPIO pin input)
Expand All @@ -10,7 +11,7 @@ pub async fn read_keyence(
gpio_pin: Input<'static>,
sender: Sender<'static, CriticalSectionRawMutex, u32, 1>,
) -> ! {
let mut keyence = Keyence::new(Stm32f767ziGpioInput::new(gpio_pin));
let mut keyence = Keyence::new(Stm32f767ziGpioInput::new(gpio_pin), DigitalSignal::High);

loop {
keyence.update_stripe_count();
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32l476rg/Cargo.lock

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

3 changes: 2 additions & 1 deletion boards/stm32l476rg/src/tasks/read_keyence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::io::Stm32l476rgGpioInput;
use embassy_stm32::gpio::Input;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, watch::Sender};
use embassy_time::{Duration, Timer};
use hyped_core::types::DigitalSignal;
use hyped_sensors::keyence::Keyence;

/// Test task that just continually updates the stripe count from the Keyence sensor (or other GPIO pin input)
Expand All @@ -10,7 +11,7 @@ pub async fn read_keyence(
gpio_pin: Input<'static>,
sender: Sender<'static, CriticalSectionRawMutex, u32, 1>,
) -> ! {
let mut keyence = Keyence::new(Stm32l476rgGpioInput::new(gpio_pin));
let mut keyence = Keyence::new(Stm32l476rgGpioInput::new(gpio_pin), DigitalSignal::High);

loop {
keyence.update_stripe_count();
Expand Down
25 changes: 14 additions & 11 deletions lib/sensors/src/keyence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ use hyped_core::types::DigitalSignal;
use hyped_gpio::HypedGpioInputPin;

/// Keyence represents a Keyence sensor which keeps track of the number of stripes that have passed
/// by the sensor. The Keyence sensor is connected to a GPIO pin which reads a high signal when a
/// stripe is detected and a low signal when no stripe is detected. The stripe count is updated
/// whenever the signal changes from low to high (positive edge).
/// by the sensor. The Keyence sensor is connected to a GPIO pin and the stripe count is updated based
/// on the signal read from the sensor.
pub struct Keyence<T: HypedGpioInputPin> {
/// The number of stripes that have passed by the sensor.
stripe_count: u32,
/// The last signal that was read from the sensor.
last_signal: DigitalSignal,
/// Which edge to update the stripe count on.
update_on: DigitalSignal,
gpio: T,
}

impl<T: HypedGpioInputPin> Keyence<T> {
/// Creates a new Keyence sensor with an initial stripe count of 0 and a last signal of low.
pub fn new(gpio: T) -> Keyence<T> {
/// The stripe count will be increase on the update_on signal edge.
pub fn new(gpio: T, update_on: DigitalSignal) -> Keyence<T> {
Keyence {
stripe_count: 0,
last_signal: DigitalSignal::Low,
update_on,
gpio,
}
}
Expand All @@ -28,10 +31,10 @@ impl<T: HypedGpioInputPin> Keyence<T> {
self.stripe_count
}

/// Increments the stripe count if the signal changes from low to high (positive edge).
/// Increments the stripe count if the signal changes to the update_on signal.
pub fn update_stripe_count(&mut self) {
let current_signal = DigitalSignal::from_bool(self.gpio.is_high());
if current_signal == DigitalSignal::High && self.last_signal == DigitalSignal::Low {
if current_signal == self.update_on && self.last_signal != self.update_on {
self.stripe_count += 1;
}
self.last_signal = current_signal;
Expand All @@ -47,7 +50,7 @@ mod tests {
#[test]
fn test_keyence_new() {
let gpio = MockGpioInput::new(Vec::from_slice(&[DigitalSignal::Low]).unwrap());
let keyence = Keyence::new(gpio);
let keyence = Keyence::new(gpio, DigitalSignal::High);
assert_eq!(keyence.get_stripe_count(), 0);
}

Expand All @@ -56,7 +59,7 @@ mod tests {
let gpio = MockGpioInput::new(
Vec::from_slice(&[DigitalSignal::Low, DigitalSignal::High]).unwrap(),
);
let mut keyence = Keyence::new(gpio);
let mut keyence = Keyence::new(gpio, DigitalSignal::High);

keyence.update_stripe_count();
assert_eq!(keyence.get_stripe_count(), 0);
Expand All @@ -69,7 +72,7 @@ mod tests {
let gpio = MockGpioInput::new(
Vec::from_slice(&[DigitalSignal::High, DigitalSignal::Low]).unwrap(),
);
let mut keyence = Keyence::new(gpio);
let mut keyence = Keyence::new(gpio, DigitalSignal::High);

keyence.update_stripe_count();
assert_eq!(keyence.get_stripe_count(), 1);
Expand All @@ -82,7 +85,7 @@ mod tests {
let gpio = MockGpioInput::new(
Vec::from_slice(&[DigitalSignal::High, DigitalSignal::High]).unwrap(),
);
let mut keyence = Keyence::new(gpio);
let mut keyence = Keyence::new(gpio, DigitalSignal::High);

keyence.update_stripe_count();
assert_eq!(keyence.get_stripe_count(), 1);
Expand All @@ -94,7 +97,7 @@ mod tests {
fn test_keyence_update_stripe_count_low_to_low() {
let gpio =
MockGpioInput::new(Vec::from_slice(&[DigitalSignal::Low, DigitalSignal::Low]).unwrap());
let mut keyence = Keyence::new(gpio);
let mut keyence = Keyence::new(gpio, DigitalSignal::High);

keyence.update_stripe_count();
assert_eq!(keyence.get_stripe_count(), 0);
Expand Down
Loading