minimal startup running
This commit is contained in:
141
rust/src/main.rs
141
rust/src/main.rs
@@ -64,7 +64,7 @@ mod webserver;
|
||||
extern crate alloc;
|
||||
//mod webserver;
|
||||
|
||||
pub static BOARD_ACCESS: OnceLock<Mutex<CriticalSectionRawMutex, HAL>> = OnceLock::new();
|
||||
pub static BOARD_ACCESS: OnceLock<Mutex<CriticalSectionRawMutex, HAL<'static>>> = OnceLock::new();
|
||||
|
||||
pub static STAY_ALIVE: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
@@ -143,7 +143,7 @@ enum NetworkMode {
|
||||
OFFLINE,
|
||||
}
|
||||
|
||||
async fn safe_main() -> anyhow::Result<()> {
|
||||
async fn safe_main(spawner: Spawner) -> anyhow::Result<()> {
|
||||
info!("Startup Rust");
|
||||
|
||||
let mut to_config = false;
|
||||
@@ -179,10 +179,8 @@ async fn safe_main() -> anyhow::Result<()> {
|
||||
//};
|
||||
//log(LogMessage::PartitionState, 0, 0, "", ota_state_string);
|
||||
let _ota_state_string = "unknown";
|
||||
println!("faul led");
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
board.board_hal.general_fault(false).await;
|
||||
println!("faul led2");
|
||||
let cur = board
|
||||
.board_hal
|
||||
.get_rtc_module()
|
||||
@@ -196,7 +194,7 @@ async fn safe_main() -> anyhow::Result<()> {
|
||||
//check if we know the time current > 2020 (plausibility checks, this code is newer than 2020)
|
||||
if cur.year() < 2020 {
|
||||
to_config = true;
|
||||
log(LogMessage::YearInplausibleForceConfig, 0, 0, "", "");
|
||||
log(LogMessage::YearInplausibleForceConfig, 0, 0, "", "").await;
|
||||
}
|
||||
|
||||
info!("cur is {}", cur);
|
||||
@@ -237,12 +235,15 @@ async fn safe_main() -> anyhow::Result<()> {
|
||||
&& board.board_hal.get_config().network.ssid.is_none()
|
||||
{
|
||||
info!("No wifi configured, starting initial config mode");
|
||||
|
||||
let stack = board.board_hal.get_esp().wifi_ap().await?;
|
||||
|
||||
let reboot_now = Arc::new(AtomicBool::new(false));
|
||||
println!("starting webserver");
|
||||
let _webserver = httpd(reboot_now.clone(), stack).await;
|
||||
wait_infinity(WaitType::MissingConfig, reboot_now.clone()).await;
|
||||
|
||||
spawner.spawn(httpd(reboot_now.clone(), stack))?;
|
||||
|
||||
wait_infinity(board, WaitType::MissingConfig, reboot_now.clone()).await;
|
||||
}
|
||||
|
||||
info!("attempting to connect wifi");
|
||||
@@ -257,7 +258,12 @@ async fn safe_main() -> anyhow::Result<()> {
|
||||
|
||||
if matches!(network_mode, NetworkMode::OFFLINE) && to_config {
|
||||
info!("Could not connect to station and config mode forced, switching to ap mode!");
|
||||
match board.board_hal.get_esp().wifi_ap().await {
|
||||
|
||||
let res = {
|
||||
let esp = board.board_hal.get_esp();
|
||||
esp.wifi_ap().await
|
||||
};
|
||||
match res {
|
||||
Ok(_) => {
|
||||
info!("Started ap, continuing")
|
||||
}
|
||||
@@ -274,8 +280,6 @@ async fn safe_main() -> anyhow::Result<()> {
|
||||
// };
|
||||
let _timezone = Tz::UTC;
|
||||
|
||||
drop(board);
|
||||
|
||||
let timezone_time = cur; //TODO.with_timezone(&timezone);
|
||||
info!(
|
||||
"Running logic at utc {} and {} {}",
|
||||
@@ -283,9 +287,9 @@ async fn safe_main() -> anyhow::Result<()> {
|
||||
);
|
||||
|
||||
if let NetworkMode::WIFI { ref ip_address, .. } = network_mode {
|
||||
publish_firmware_info(version, ip_address, &timezone_time.to_rfc3339()).await;
|
||||
publish_battery_state().await;
|
||||
let _ = publish_mppt_state().await;
|
||||
publish_firmware_info(&mut board, version, ip_address, &timezone_time.to_rfc3339()).await;
|
||||
publish_battery_state(&mut board).await;
|
||||
let _ = publish_mppt_state(&mut board).await;
|
||||
}
|
||||
|
||||
log(
|
||||
@@ -312,7 +316,8 @@ async fn safe_main() -> anyhow::Result<()> {
|
||||
let reboot_now = Arc::new(AtomicBool::new(false));
|
||||
//TODO
|
||||
//let _webserver = httpd(reboot_now.clone());
|
||||
wait_infinity(WaitType::ConfigButton, reboot_now.clone()).await;
|
||||
let board = BOARD_ACCESS.get().await.lock().await;
|
||||
wait_infinity(board, WaitType::ConfigButton, reboot_now.clone()).await;
|
||||
} else {
|
||||
log(LogMessage::NormalRun, 0, 0, "", "").await;
|
||||
}
|
||||
@@ -589,11 +594,10 @@ async fn safe_main() -> anyhow::Result<()> {
|
||||
|
||||
if stay_alive {
|
||||
info!("Go to stay alive move");
|
||||
drop(board);
|
||||
let reboot_now = Arc::new(AtomicBool::new(false));
|
||||
//TODO
|
||||
//let _webserver = httpd(reboot_now.clone());
|
||||
wait_infinity(WaitType::MqttConfig, reboot_now.clone()).await;
|
||||
wait_infinity(board, WaitType::MqttConfig, reboot_now.clone()).await;
|
||||
} else {
|
||||
board.board_hal.get_esp().set_restart_to_conf(false);
|
||||
board
|
||||
@@ -831,8 +835,12 @@ async fn update_charge_indicator(board: &mut MutexGuard<'_, CriticalSectionRawMu
|
||||
// }
|
||||
// }
|
||||
|
||||
async fn publish_firmware_info(version: VersionInfo, ip_address: &String, timezone_time: &String) {
|
||||
let board = &mut BOARD_ACCESS.get().await.lock().await;
|
||||
async fn publish_firmware_info(
|
||||
mut board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
||||
version: VersionInfo,
|
||||
ip_address: &String,
|
||||
timezone_time: &String,
|
||||
) {
|
||||
let esp = board.board_hal.get_esp();
|
||||
let _ = esp
|
||||
.mqtt_publish("/firmware/address", ip_address.as_bytes())
|
||||
@@ -952,10 +960,11 @@ async fn pump_info(
|
||||
};
|
||||
}
|
||||
|
||||
async fn publish_mppt_state() -> anyhow::Result<()> {
|
||||
let board_hal = &mut BOARD_ACCESS.get().await.lock().await.board_hal;
|
||||
let current = board_hal.get_mptt_current().await?;
|
||||
let voltage = board_hal.get_mptt_voltage().await?;
|
||||
async fn publish_mppt_state(
|
||||
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
||||
) -> anyhow::Result<()> {
|
||||
let current = board.board_hal.get_mptt_current().await?;
|
||||
let voltage = board.board_hal.get_mptt_voltage().await?;
|
||||
let solar_state = Solar {
|
||||
current_ma: current.as_milliamperes() as u32,
|
||||
voltage_ma: voltage.as_millivolts() as u32,
|
||||
@@ -963,15 +972,17 @@ async fn publish_mppt_state() -> anyhow::Result<()> {
|
||||
if let Ok(serialized_solar_state_bytes) =
|
||||
serde_json::to_string(&solar_state).map(|s| s.into_bytes())
|
||||
{
|
||||
let _ = board_hal
|
||||
let _ = board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish("/mppt", &serialized_solar_state_bytes);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn publish_battery_state() -> () {
|
||||
let board = &mut BOARD_ACCESS.get().await.lock().await;
|
||||
async fn publish_battery_state(
|
||||
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
||||
) -> () {
|
||||
let state = board
|
||||
.board_hal
|
||||
.get_battery_monitor()
|
||||
@@ -983,55 +994,65 @@ async fn publish_battery_state() -> () {
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish("/battery", &serialized_battery_state_bytes);
|
||||
.mqtt_publish("/battery", &serialized_battery_state_bytes)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_infinity(wait_type: WaitType, reboot_now: Arc<AtomicBool>) -> ! {
|
||||
async fn wait_infinity(
|
||||
board: MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
||||
wait_type: WaitType,
|
||||
reboot_now: Arc<AtomicBool>,
|
||||
) -> ! {
|
||||
//since we force to have the lock when entering, we can release it to ensure the caller does not forget to dispose of it
|
||||
drop(board);
|
||||
|
||||
let delay = wait_type.blink_pattern();
|
||||
let mut led_count = 8;
|
||||
let mut pattern_step = 0;
|
||||
loop {
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
update_charge_indicator(&mut board).await;
|
||||
{
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
update_charge_indicator(&mut board).await;
|
||||
|
||||
match wait_type {
|
||||
WaitType::MissingConfig => {
|
||||
// Keep existing behavior: circular filling pattern
|
||||
led_count %= 8;
|
||||
led_count += 1;
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, i < led_count);
|
||||
}
|
||||
}
|
||||
WaitType::ConfigButton => {
|
||||
// Alternating pattern: 1010 1010 -> 0101 0101
|
||||
pattern_step = (pattern_step + 1) % 2;
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, (i + pattern_step) % 2 == 0);
|
||||
}
|
||||
}
|
||||
WaitType::MqttConfig => {
|
||||
// Moving dot pattern
|
||||
pattern_step = (pattern_step + 1) % 8;
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, i == pattern_step);
|
||||
match wait_type {
|
||||
WaitType::MissingConfig => {
|
||||
// Keep existing behavior: circular filling pattern
|
||||
led_count %= 8;
|
||||
led_count += 1;
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, i < led_count);
|
||||
}
|
||||
}
|
||||
WaitType::ConfigButton => {
|
||||
// Alternating pattern: 1010 1010 -> 0101 0101
|
||||
pattern_step = (pattern_step + 1) % 2;
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, (i + pattern_step) % 2 == 0);
|
||||
}
|
||||
}
|
||||
WaitType::MqttConfig => {
|
||||
// Moving dot pattern
|
||||
pattern_step = (pattern_step + 1) % 8;
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, i == pattern_step);
|
||||
}
|
||||
}
|
||||
}
|
||||
board.board_hal.general_fault(true).await;
|
||||
}
|
||||
|
||||
board.board_hal.general_fault(true);
|
||||
drop(board);
|
||||
|
||||
Timer::after_millis(delay).await;
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
board.board_hal.general_fault(false);
|
||||
{
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
board.board_hal.general_fault(false).await;
|
||||
|
||||
// Clear all LEDs
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, false);
|
||||
// Clear all LEDs
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, false);
|
||||
}
|
||||
}
|
||||
drop(board);
|
||||
|
||||
Timer::after_millis(delay).await;
|
||||
|
||||
if wait_type == WaitType::MqttConfig && !STAY_ALIVE.load(Ordering::Relaxed) {
|
||||
@@ -1065,7 +1086,7 @@ async fn main(spawner: Spawner) {
|
||||
}
|
||||
println!("Hal init done, starting logic");
|
||||
|
||||
match safe_main().await {
|
||||
match safe_main(spawner).await {
|
||||
// this should not get triggered, safe_main should not return but go into deep sleep with sensible
|
||||
// timeout, this is just a fallback
|
||||
Ok(_) => {
|
||||
|
||||
Reference in New Issue
Block a user