-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
134 lines (110 loc) · 3.61 KB
/
main.rs
File metadata and controls
134 lines (110 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#![warn(clippy::unwrap_used)]
#![deny(unused_must_use)]
use crate::sysc::{logging::OsLogger, ReportableError};
use esp_idf_svc::hal::{
i2c::{config::Config, I2cDriver},
units::FromValueType,
};
use std::time::Instant;
use sysc::{
battery::Battery, ledctl::BoardLed, ota::Ota, periph::SystemPeripherals, power::mcu_sleep,
usbctl, OsError,
};
mod config;
mod firmware;
mod sysc;
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)]
fn main() {
esp_idf_svc::sys::link_patches();
// Turn off logging when USB is not connected
if !usbctl::is_connected() {
OsLogger::disable();
}
OsLogger::init();
log::info!(
"PixelWeatherOS v{}-{}{} ({})",
env!("CARGO_PKG_VERSION"),
env!("PWOS_COMMIT"),
env!("PWOS_REL_OR_DEV"),
env!("BUILD_DATE_TIME")
);
log::info!("(C) Fábián Varga 2025");
#[cfg(debug_assertions)]
{
log::debug!(
"Using ESP-IDF {}",
sysc::get_idf_version().as_deref().unwrap_or("?")
);
log::debug!("Disabling brownout detector");
sysc::brownout::disable_brownout_detector();
}
log::debug!("Initializing system peripherals");
let peripherals = SystemPeripherals::take();
log::debug!("Initializing system LED");
let led = BoardLed::new(
peripherals.onboard_led.pin.degrade_output(),
peripherals.onboard_led.invert,
)
.expect("Failed to set up onboard LED");
log::debug!("Setting panic handle");
sysc::panic::setup();
log::debug!("Initializing OTA system");
let mut ota = Ota::new().expect("Failed to initialize OTA");
ota.rollback_if_needed()
.expect("Failed to check/perform rollback");
log::debug!(
"Reported current version: {}",
ota.current_version()
.map_or_else(|_| "?".to_string(), |v| v.to_string())
);
log::debug!(
"Previous installed version: {}",
ota.previous_version()
.map_or_else(|_| "?".to_string(), |v| v.to_string())
);
log::debug!("Initializing NVS");
let nvs = sysc::nvs::NonVolatileStorage::new().expect("Failed to initialize NVS");
log::debug!("Initializing system Battery");
let battery = Battery::new(peripherals.battery.adc, peripherals.battery.pin)
.expect("Failed to initialize battery ADC");
log::debug!("Initializing I2C bus");
let i2c = I2cDriver::new(
peripherals.i2c.i2c,
peripherals.i2c.sda,
peripherals.i2c.scl,
&Config::default().baudrate(400u32.kHz().into()),
)
.expect("Failed to initialize I2C");
log::debug!("Initializing app configuration");
let mut appcfg = config::get_settings();
log::info!("Staring main");
let start = Instant::now();
let fw_exit = firmware::fw_main(
battery,
i2c,
peripherals.wifi.modem,
peripherals.wifi.sys_loop,
led,
&nvs,
&mut ota,
&mut appcfg,
);
let runtime = start.elapsed();
match fw_exit {
Ok(()) => log::info!("Tasks completed successfully"),
Err(why) => {
log::error!("OS Error: {why}");
nvs.store_last_os_error(&why)
.report("Failed to store error in NVS");
if !why.recoverable() {
log::error!("System will now halt");
mcu_sleep(None);
}
ota.inc_failiures()
.expect("Failed to increment failiure count");
}
}
log::info!("Tasks completed in {runtime:.02?}");
log::debug!("Sleeping for {:?}", appcfg.sleep_time());
mcu_sleep(Some(appcfg.sleep_time()));
}