84 lines
2.5 KiB
Rust
84 lines
2.5 KiB
Rust
#![feature(is_some_with)]
|
|
use esp_idf_sys as _;
|
|
use log::{error, info};
|
|
use std::{thread, time::Duration, sync::mpsc, env};
|
|
|
|
mod beep;
|
|
mod blink;
|
|
mod dc_out_controller;
|
|
mod manager;
|
|
mod screen;
|
|
fn main() {
|
|
env::set_var("DEFMT_LOG", "trace");
|
|
env::set_var("RUST_LOG", "trace");
|
|
env_logger::init();
|
|
// Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once,
|
|
// or else some patches to the runtime implemented by esp-idf-sys might not link properly.
|
|
esp_idf_sys::link_patches();
|
|
|
|
|
|
let peripherals = esp_idf_hal::peripherals::Peripherals::take().unwrap();
|
|
|
|
let blink_pin = peripherals.pins.gpio5;
|
|
let beep_pin = peripherals.pins.gpio6;
|
|
let ledc_timer0 = peripherals.ledc.timer0;
|
|
let ledc_channel0 = peripherals.ledc.channel0;
|
|
let dc_out_ctl_pin = peripherals.pins.gpio3;
|
|
let i2c0 = peripherals.i2c0;
|
|
let sda_pin = peripherals.pins.gpio4;
|
|
let scl_pin = peripherals.pins.gpio10;
|
|
|
|
let adc1 = peripherals.adc1;
|
|
let adapter_pin = peripherals.pins.gpio1;
|
|
let battery_pin = peripherals.pins.gpio2;
|
|
|
|
let (tx, mut rx) = mpsc::channel();
|
|
|
|
info!("Starting");
|
|
|
|
thread::spawn(move || {
|
|
let mut blink =
|
|
blink::Blink::new(blink_pin.into_output().expect("Failed to set GPIO5 as output"));
|
|
blink.play();
|
|
});
|
|
|
|
thread::spawn(move || {
|
|
thread::sleep(Duration::from_millis(5000));
|
|
});
|
|
|
|
thread::spawn(move || {
|
|
let mut beep = beep::Beep::new(
|
|
beep_pin.into_output().expect("Failed to set GPIO6 as output"),
|
|
ledc_timer0,
|
|
ledc_channel0,
|
|
)
|
|
.expect("Failed to create beep");
|
|
beep.play(&mut rx);
|
|
});
|
|
|
|
let display = screen::Screen::new(i2c0, sda_pin, scl_pin).expect("Failed to create screen");
|
|
|
|
let dc_out_ctl = dc_out_controller::DcOutController::new(
|
|
dc_out_ctl_pin
|
|
.into_output()
|
|
.expect("Failed to set GPIO3 as output"),
|
|
);
|
|
let mut manager = manager::Manager::new(
|
|
dc_out_ctl,
|
|
display,
|
|
adc1,
|
|
adapter_pin.into_analog_atten_11db().expect("Failed to set GPIO1 as analog input"),
|
|
battery_pin.into_analog_atten_11db().expect("Failed to set GPIO2 as analog input"),
|
|
tx,
|
|
).expect("Failed to create manager");
|
|
loop {
|
|
match manager.handling_once() {
|
|
Ok(_) => {}
|
|
Err(err) => {
|
|
error!("Exec manager tick task failed: {}", err);
|
|
}
|
|
}
|
|
thread::sleep(Duration::from_millis(100));
|
|
}
|
|
}
|