ups-esp32c3-rust/src/main.rs

68 lines
2.1 KiB
Rust

use esp_idf_sys as _;
use log::*;
use std::{thread, time::Duration};
mod beep;
mod blink;
mod dc_out_controller;
mod screen;
fn main() {
// 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 gpio5 = peripherals.pins.gpio5;
let gpio6 = peripherals.pins.gpio6;
let ledc_timer0 = peripherals.ledc.timer0;
let ledc_channel0 = peripherals.ledc.channel0;
let dc_out_ctl_pin = peripherals.pins.gpio2;
let i2c0 = peripherals.i2c0;
let gpio9 = peripherals.pins.gpio9;
let gpio10 = peripherals.pins.gpio10;
println!("Starting screen");
let mut screen = screen::Screen::new(i2c0, gpio9, gpio10).expect("Failed to create screen");
thread::spawn(move || {
let mut blink =
blink::Blink::new(gpio5.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(
gpio6.into_output().expect("Failed to set GPIO6 as output"),
ledc_timer0,
ledc_channel0,
)
.expect("Failed to create beep");
beep.play();
});
thread::spawn(move || {
let mut dc_out_ctl = dc_out_controller::DcOutController::new(
dc_out_ctl_pin
.into_output()
.expect("Failed to set GPIO2 as output"),
);
while true {
if dc_out_ctl.state {
dc_out_ctl.off().expect("Failed to turn DC_OUT_CTL off");
} else {
dc_out_ctl.on().expect("Failed to turn DC_OUT_CTL on");
}
thread::sleep(Duration::from_millis(500));
}
});
loop {
println!("Hello, world!");
thread::sleep(Duration::from_millis(2000));
}
}