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

Post to printer when button is pressed #8

Merged
merged 4 commits into from
Dec 7, 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
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 <[email protected]>"]
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
Loading