Skip to content

Commit

Permalink
Merge pull request #23 from UCI-HyperXite/feature/signal-light
Browse files Browse the repository at this point in the history
Add `SignalLight` component and demo with RPPAL
  • Loading branch information
taesungh authored Mar 11, 2024
2 parents 3533a05 + 6517870 commit bb1b221
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
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

0 comments on commit bb1b221

Please sign in to comment.