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 - High Power Relay #55

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions lib/control/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "hyped_control"
version = "0.1.0"
edition = "2021"

[dependencies]
hyped_io = { path = "../io" }
22 changes: 22 additions & 0 deletions lib/control/src/hp_relay.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// need to take in a GPIO pin like Keyence does
// functions: switch_on, switch_off

Comment on lines +1 to +3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

use hyped_io::gpio::HypedGpioPin;

pub struct HighPowerRelay<T: HypedGpioPin> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a Rust doc comment here explaining what this does

gpio: T,
}

impl<T: HypedGpioPin> HighPowerRelay<T> {
pub fn new(gpio: T) -> HighPowerRelay<T> {
HighPowerRelay { gpio }
}

pub fn switch_on(&mut self) {
self.gpio.switch_on();
}

pub fn switch_off(&mut self) {
self.gpio.switch_off();
}
}
3 changes: 3 additions & 0 deletions lib/control/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![no_std]

pub mod hp_relay;
80 changes: 47 additions & 33 deletions lib/io/src/gpio.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
/// Abstraction for a GPIO pin so that sensors can be tested with a mock GPIO pin
pub trait HypedGpioPin {
fn is_high(&mut self) -> bool;
}

pub mod mock_gpio {
use heapless::Vec;

/// A mock GPIO pin that can be used for testing
pub struct MockGpio {
current_value: bool,
next_values: Vec<bool, 10>,
}

impl crate::gpio::HypedGpioPin for MockGpio {
fn is_high(&mut self) -> bool {
let next_value = self.next_values.pop().unwrap_or(self.current_value);
self.current_value = next_value;
self.current_value
}
}

impl MockGpio {
pub fn new(values: Vec<bool, 10>) -> MockGpio {
let mut next_values = values.clone();
next_values.reverse();
MockGpio {
current_value: false,
next_values,
}
}
}
}
/// Abstraction for a GPIO pin so that sensors can be tested with a mock GPIO pin
pub trait HypedGpioPin {
fn is_high(&mut self) -> bool;
fn switch_on(&mut self) -> bool;
fn switch_off(&mut self) -> bool;
}

Comment on lines +1 to +6
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually need to separate the input and output GPIO pins, like in PR #40 lib/io/src/gpio.rs.

pub mod mock_gpio {
use heapless::Vec;

/// A mock GPIO pin that can be used for testing
pub struct MockGpio {
current_value: bool,
next_values: Vec<bool, 10>,
}

impl crate::gpio::HypedGpioPin for MockGpio {
fn is_high(&mut self) -> bool {
let next_value = self.next_values.pop().unwrap_or(self.current_value);
self.current_value = next_value;
self.current_value
}

fn switch_on(&mut self) -> bool {
let next_value = self.next_values.pop().unwrap_or(self.current_value);
self.current_value = next_value;
self.current_value
}

fn switch_off(&mut self) -> bool {
let next_value = self.next_values.pop().unwrap_or(self.current_value);
self.current_value = next_value;
self.current_value
}
}

impl MockGpio {
pub fn new(values: Vec<bool, 10>) -> MockGpio {
let mut next_values = values.clone();
next_values.reverse();
MockGpio {
current_value: false,
next_values,
}
}
}
}
Loading