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

Add SignalLight component and demo with RPPAL #23

Merged
merged 2 commits into from
Mar 11, 2024
Merged
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
10 changes: 10 additions & 0 deletions pod-operation/Cargo.lock

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

1 change: 1 addition & 0 deletions pod-operation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
rppal = "0.17.1"
1 change: 1 addition & 0 deletions pod-operation/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod signal_light;
31 changes: 31 additions & 0 deletions pod-operation/src/components/signal_light.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use tracing::debug;

use rppal::gpio::{Gpio, OutputPin};

pub struct SignalLight {
pin: OutputPin,
}

const PIN_SIGNAL_LIGHT: u8 = 21;

impl SignalLight {
pub fn new() -> Self {
SignalLight {
pin: Gpio::new()
.unwrap()
.get(PIN_SIGNAL_LIGHT)
.unwrap()
.into_output(),
}
}

pub fn disable(&mut self) {
debug!("Disabling signal light.");
self.pin.set_low();
}

pub fn enable(&mut self) {
debug!("Enabling signal light.");
self.pin.set_high();
}
}
19 changes: 19 additions & 0 deletions pod-operation/src/demo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use tracing::info;

use crate::components::signal_light::SignalLight;

pub async fn blink(mut signal_light: SignalLight) {
let mut i = 0;

info!("Starting blink demo.");
loop {
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
if i % 4 == 0 {
signal_light.enable();
} else if i % 4 == 1 {
signal_light.disable();
}

i += 1;
}
}
7 changes: 7 additions & 0 deletions pod-operation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ use socketioxide::{extract::SocketRef, SocketIo};
use tracing::{error, info};
use tracing_subscriber::FmtSubscriber;

mod components;
mod demo;
mod handlers;

use crate::components::signal_light::SignalLight;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing::subscriber::set_global_default(FmtSubscriber::default())?;
Expand All @@ -16,6 +20,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
socket.on("ping", handlers::handle_ping);
});

let signal_light = SignalLight::new();
tokio::spawn(demo::blink(signal_light));

let app = axum::Router::new().layer(layer);

info!("Starting server on port 5000");
Expand Down