Skip to content

Commit

Permalink
Merge pull request #8 from purduehackers/matthew/button-pressed
Browse files Browse the repository at this point in the history
Post to printer when button is pressed
  • Loading branch information
ImTheSquid authored Dec 7, 2024
2 parents 2219d38 + b25351a commit cc8ecba
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sign-firmware"
version = "0.1.11"
version = "0.1.12"
authors = ["Jack Hogan <jackhogan11@gmail.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
43 changes: 34 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#![feature(type_alias_impl_trait)]

use build_time::build_time_utc;
use chrono::{Duration, Local};
use chrono_tz::US::Eastern;
use embassy_time::Timer;
use esp_idf_svc::{
eventloop::EspSystemEventLoop,
hal::{
gpio::PinDriver,
gpio::{Gpio15, Gpio36, Input, Level, Output, PinDriver},
ledc::{config::TimerConfig, LedcDriver, LedcTimerDriver},
peripherals::Peripherals,
task::block_on,
Expand All @@ -32,7 +33,12 @@ fn midnight(time: &LightningTime) -> bool {
time.bolts == 0 && time.zaps == 0 && time.sparks == 0 && time.charges == 0
}

async fn amain(mut leds: Leds, mut wifi: AsyncWifi<EspWifi<'static>>) {
async fn amain(
mut leds: Leds,
mut wifi: AsyncWifi<EspWifi<'static>>,
button_switch: PinDriver<'static, Gpio36, Input>,
mut button_led: PinDriver<'static, Gpio15, Output>,
) {
// Red before wifi
leds.set_all_colors(Rgb::new(255, 0, 0));

Expand All @@ -46,10 +52,11 @@ async fn amain(mut leds: Leds, mut wifi: AsyncWifi<EspWifi<'static>>) {
// Check for update
self_update(&mut leds).await.expect("Self-update to work");

let mut last_time =
LightningTime::from(chrono::offset::Local::now().with_timezone(&Eastern).time());
let mut last_led_change = Local::now();
let mut button_pressed = false;
let mut last_time = LightningTime::from(Local::now().with_timezone(&Eastern).time());
loop {
let time = LightningTime::from(chrono::offset::Local::now().with_timezone(&Eastern).time());
let time = LightningTime::from(Local::now().with_timezone(&Eastern).time());

if midnight(&time) && !midnight(&last_time) {
if let Err(e) = printer::post_event(printer::PrinterEvent::Zero).await {
Expand All @@ -65,6 +72,24 @@ async fn amain(mut leds: Leds, mut wifi: AsyncWifi<EspWifi<'static>>) {
}
}

match (button_switch.get_level(), button_pressed) {
(Level::High, false) => {
if let Err(e) = printer::post_event(printer::PrinterEvent::ButtonPressed).await {
log::error!("BUTTON PRESSED: Printer error: {e}");
}
button_pressed = true;
}
(Level::Low, true) => {
button_pressed = false;
}
_ => {}
}

if Local::now() - last_led_change > Duration::seconds(1) {
button_led.toggle().unwrap();
last_led_change = Local::now();
}

last_time = time;

let colors = time.colors();
Expand All @@ -79,7 +104,7 @@ async fn amain(mut leds: Leds, mut wifi: AsyncWifi<EspWifi<'static>>) {
leds.set_color(colors.spark, block);
}

Timer::after_millis(1000).await;
Timer::after_millis(10).await;
}
}

Expand Down Expand Up @@ -229,16 +254,16 @@ fn main() {
];

// The buttonled and button switch pins are reversed from the original board schematic since pin 36 is input only (oops)
let _button_led = PinDriver::output(peripherals.pins.gpio15);
let _button_switch = PinDriver::input(peripherals.pins.gpio36);
let button_led = PinDriver::output(peripherals.pins.gpio15).unwrap();
let button_switch = PinDriver::input(peripherals.pins.gpio36).unwrap();

let leds = Leds::create(leds);

std::thread::Builder::new()
.stack_size(60_000)
.spawn(|| {
io::vfs::initialize_eventfd(5).unwrap();
block_on(amain(leds, wifi))
block_on(amain(leds, wifi, button_switch, button_led))
})
.unwrap()
.join()
Expand Down
7 changes: 7 additions & 0 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub enum PrinterEvent {
NewBolt(u8),
NewZap(u8),
Zero,
ButtonPressed,
}

impl PrinterEvent {
Expand All @@ -62,6 +63,12 @@ impl PrinterEvent {
)),
PrinterInstruction::PrintCut,
],
PrinterEvent::ButtonPressed => vec![
PrinterInstruction::Justify(JustifyMode::Center),
PrinterInstruction::Bold(true),
PrinterInstruction::Text("SOMEONE JUST PRESSED THE SECRET BUTTON!".to_string()),
PrinterInstruction::PrintCut,
],
}
}
}
Expand Down

0 comments on commit cc8ecba

Please sign in to comment.