133 lines
4.4 KiB
Rust
133 lines
4.4 KiB
Rust
use embedded_svc::event_bus::EventBus;
|
|
use esp_idf_svc::eventloop::{Background, EspBackgroundEventLoop, EspEventLoop};
|
|
use esp_idf_sys::{self as _};
|
|
use log::*;
|
|
use std::{
|
|
env,
|
|
thread::{self, sleep},
|
|
time::Duration,
|
|
};
|
|
|
|
mod beep;
|
|
mod blink;
|
|
mod dc_out_controller;
|
|
mod message_queue;
|
|
mod time;
|
|
mod voltage_detection;
|
|
mod wifi;
|
|
|
|
use crate::{
|
|
beep::{ringtone, Beep},
|
|
dc_out_controller::{DcOutController, DcOutControllerState, DC_OUT_STATE_EVENT_LOOP},
|
|
voltage_detection::VoltageDetectionWorker,
|
|
};
|
|
use crate::{
|
|
message_queue::MessageQueue, time::Time, voltage_detection::VoltageDetection, wifi::Internet,
|
|
};
|
|
|
|
static mut VOLTAGE_EVENTLOOP: Option<EspEventLoop<esp_idf_svc::eventloop::User<Background>>> = None;
|
|
|
|
fn main() {
|
|
env::set_var("DEFMT_LOG", "trace");
|
|
env::set_var("RUST_BACKTRACE", "1");
|
|
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();
|
|
|
|
info!("Hello, world!");
|
|
|
|
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;
|
|
|
|
info!("About to start a background event loop");
|
|
match EspBackgroundEventLoop::new(&Default::default()) {
|
|
Ok(eventloop) => unsafe { VOLTAGE_EVENTLOOP = Some(eventloop) },
|
|
Err(err) => error!("Init Event Loop failed. {:?}", err),
|
|
};
|
|
|
|
thread::spawn(move || {
|
|
let mut blink = blink::Blink::new(
|
|
blink_pin
|
|
.into_output()
|
|
.expect("Failed to set GPIO5 as output"),
|
|
);
|
|
blink.play();
|
|
});
|
|
|
|
let mut voltage_detection = VoltageDetection::new();
|
|
|
|
voltage_detection
|
|
.watching()
|
|
.expect("Can not watch voltages.");
|
|
|
|
let _wifi = Internet::new().unwrap();
|
|
|
|
let mut mq = MessageQueue::new();
|
|
mq.init().unwrap();
|
|
|
|
let mut time = Time::new();
|
|
time.sync().unwrap();
|
|
|
|
let mut beep = Beep::new();
|
|
|
|
// let _voltage_subscription;
|
|
// if let Some(eventloop) = unsafe { VOLTAGE_EVENTLOOP.as_mut() } {
|
|
// _voltage_subscription = eventloop
|
|
// .subscribe(move |message: &VoltageDetectionWorker| {
|
|
// if let Ok(json_str) = serde_json::to_string(&message) {
|
|
// if let Err(err) = mq.publish("voltage", json_str.as_bytes()) {
|
|
// warn!("Can not publish message to MQTT. {}", err);
|
|
// }
|
|
// }
|
|
// })
|
|
// .expect(" Listening Event Loop Failed");
|
|
// }
|
|
|
|
let mut dc_out_controller =
|
|
DcOutController::get_instance().expect("Can not get DcOutController instance");
|
|
dc_out_controller
|
|
.watch()
|
|
.expect("Can not watch for dc_out_controller");
|
|
|
|
let _dc_out_state_subscription;
|
|
if let Some(eventloop) = unsafe { DC_OUT_STATE_EVENT_LOOP.as_mut() } {
|
|
_dc_out_state_subscription = eventloop
|
|
.subscribe(move |message: &DcOutControllerState| {
|
|
info!("Event Loop Value");
|
|
match message.status {
|
|
dc_out_controller::DcOutStatus::WaitingOff => {
|
|
beep.play(ringtone::ADAPTER_DOWN).expect("Can not beep.")
|
|
}
|
|
dc_out_controller::DcOutStatus::WaitingOn(_) => {
|
|
beep.play(ringtone::BATTERY_LOW).expect("Can not beep.")
|
|
}
|
|
dc_out_controller::DcOutStatus::TurningOff(_) => {
|
|
beep.play(ringtone::SHUTDOWN).expect("Can not beep.")
|
|
}
|
|
_ => {beep.play(ringtone::SILENCE).expect("Can not beep.")}
|
|
}
|
|
if let Err(err) = mq.publish("dcOut", message.to_json().as_bytes()) {
|
|
warn!("Can not publish message to MQTT. {}", err);
|
|
}
|
|
})
|
|
.expect(" Listening Event Loop Failed");
|
|
} else {
|
|
error!("DC_OUT_STATE_EVENT_LOOP is None");
|
|
}
|
|
|
|
loop {
|
|
sleep(Duration::from_millis(1000));
|
|
}
|
|
}
|