adust pcb to new 3.3 software improvements

This commit is contained in:
2024-12-30 20:19:37 +01:00
parent 58b63fc8ee
commit 1927449c1d
22 changed files with 16833 additions and 13984 deletions

View File

@@ -10,11 +10,7 @@ use chrono_tz::{Europe::Berlin, Tz};
use config::Mode;
use esp_idf_hal::delay::Delay;
use esp_idf_sys::{
esp_deep_sleep, esp_ota_get_app_partition_count, esp_ota_get_running_partition,
esp_ota_get_state_partition, esp_ota_img_states_t, esp_ota_img_states_t_ESP_OTA_IMG_ABORTED,
esp_ota_img_states_t_ESP_OTA_IMG_INVALID, esp_ota_img_states_t_ESP_OTA_IMG_NEW,
esp_ota_img_states_t_ESP_OTA_IMG_PENDING_VERIFY, esp_ota_img_states_t_ESP_OTA_IMG_UNDEFINED,
esp_ota_img_states_t_ESP_OTA_IMG_VALID, esp_restart, vTaskDelay, CONFIG_FREERTOS_HZ,
esp_deep_sleep, esp_ota_get_app_partition_count, esp_ota_get_running_partition, esp_ota_get_state_partition, esp_ota_img_states_t, esp_ota_img_states_t_ESP_OTA_IMG_ABORTED, esp_ota_img_states_t_ESP_OTA_IMG_INVALID, esp_ota_img_states_t_ESP_OTA_IMG_NEW, esp_ota_img_states_t_ESP_OTA_IMG_PENDING_VERIFY, esp_ota_img_states_t_ESP_OTA_IMG_UNDEFINED, esp_ota_img_states_t_ESP_OTA_IMG_VALID, esp_restart, esp_sleep_enable_ext1_wakeup, esp_sleep_ext1_wakeup_mode_t_ESP_EXT1_WAKEUP_ANY_LOW, vTaskDelay, CONFIG_FREERTOS_HZ
};
use log::error;
use once_cell::sync::Lazy;
@@ -126,16 +122,6 @@ struct PlantStateMQTT<'a> {
last_pump: &'a str,
next_pump: &'a str,
}
#[derive(Serialize)]
struct BatteryState<'a> {
voltage_milli_volt: &'a str,
current_milli_ampere: &'a str,
cycle_count: &'a str,
design_milli_ampere: &'a str,
remaining_milli_ampere: &'a str,
state_of_charge: &'a str,
state_of_health: &'a str,
}
fn safe_main() -> anyhow::Result<()> {
// It is necessary to call this function once. Otherwise some patches to the runtime
@@ -155,15 +141,16 @@ fn safe_main() -> anyhow::Result<()> {
log::info!("Startup Rust");
let git_hash = env!("VERGEN_GIT_DESCRIBE");
let build_timestamp = env!("VERGEN_BUILD_TIMESTAMP");
let mut to_config = false;
let version = get_version();
println!(
"Version useing git has {} build on {}",
git_hash, build_timestamp
version.git_hash, version.build_time
);
let count = unsafe { esp_ota_get_app_partition_count() };
println!("Partit ion count is {}", count);
println!("Partition count is {}", count);
let mut ota_state: esp_ota_img_states_t = 0;
let running_partition = unsafe { esp_ota_get_running_partition() };
let address = unsafe { (*running_partition).address };
@@ -192,6 +179,7 @@ fn safe_main() -> anyhow::Result<()> {
println!("Board hal init");
let mut board: std::sync::MutexGuard<'_, PlantCtrlBoard<'_>> = BOARD_ACCESS.lock().unwrap();
board.general_fault(false);
println!("Mounting filesystem");
board.mount_file_system()?;
let free_space = board.file_system_size()?;
@@ -232,8 +220,18 @@ fn safe_main() -> anyhow::Result<()> {
println!("cur is {}", cur);
let mut to_config = false;
if board.is_mode_override() {
if board.get_restart_to_conf() {
println!("config mode software override");
for _i in 0..2 {
board.general_fault(true);
Delay::new_default().delay_ms(100);
board.general_fault(false);
Delay::new_default().delay_ms(100);
}
to_config = true;
board.general_fault(true);
board.set_restart_to_conf(false);
} else if board.is_mode_override() {
board.general_fault(true);
println!("config mode override is pressed, waiting 5s");
for _i in 0..5 {
@@ -244,11 +242,15 @@ fn safe_main() -> anyhow::Result<()> {
}
if board.is_mode_override() {
board.general_fault(true);
to_config = true;
} else {
board.general_fault(false);
}
} else {
println!("No config override start detected");
}
let config: PlantControllerConfig;
match board.get_config() {
@@ -293,7 +295,7 @@ fn safe_main() -> anyhow::Result<()> {
board.general_fault(true);
}
}
if (config.network.mqtt_url.is_some()) {
if config.network.mqtt_url.is_some() {
match board.mqtt(&config) {
Ok(_) => {
println!("Mqtt connection ready");
@@ -338,8 +340,12 @@ fn safe_main() -> anyhow::Result<()> {
if mqtt {
let ip_string = ip_address.unwrap_or("N/A".to_owned());
let _ = board.mqtt_publish(&config, "/firmware/address", ip_string.as_bytes());
let _ = board.mqtt_publish(&config, "/firmware/githash", git_hash.as_bytes());
let _ = board.mqtt_publish(&config, "/firmware/buildtime", build_timestamp.as_bytes());
let _ = board.mqtt_publish(&config, "/firmware/githash", version.git_hash.as_bytes());
let _ = board.mqtt_publish(
&config,
"/firmware/buildtime",
version.build_time.as_bytes(),
);
let _ = board.mqtt_publish(
&config,
"/firmware/last_online",
@@ -425,6 +431,7 @@ fn safe_main() -> anyhow::Result<()> {
);
let stay_alive_mqtt = STAY_ALIVE.load(std::sync::atomic::Ordering::Relaxed);
let stay_alive = stay_alive_mqtt;
println!("Check stay alive, current state is {}", stay_alive);
@@ -550,23 +557,17 @@ fn safe_main() -> anyhow::Result<()> {
let _webserver = httpd(reboot_now.clone());
wait_infinity(WaitType::MqttConfig, reboot_now.clone());
}
unsafe { esp_deep_sleep(1000 * 1000 * 60 * deep_sleep_duration_minutes as u64) };
unsafe {
esp_sleep_enable_ext1_wakeup(0b10u64, esp_sleep_ext1_wakeup_mode_t_ESP_EXT1_WAKEUP_ANY_LOW);
esp_deep_sleep(1000 * 1000 * 60 * deep_sleep_duration_minutes as u64)
};
}
fn publish_battery_state(
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
config: &PlantControllerConfig,
) {
let bat = BatteryState {
voltage_milli_volt: &to_string(&board.voltage_milli_volt()),
current_milli_ampere: &to_string(&board.average_current_milli_ampere()),
cycle_count: &to_string(&board.cycle_count()),
design_milli_ampere: &to_string(&board.design_milli_ampere_hour()),
remaining_milli_ampere: &to_string(&board.remaining_milli_ampere_hour()),
state_of_charge: &to_string(&board.state_charge_percent()),
state_of_health: &to_string(&board.state_health_percent()),
};
let bat = board.get_battery_state();
match serde_json::to_string(&bat) {
Ok(state) => {
let _ = board.mqtt_publish(&config, "/battery", state.as_bytes());
@@ -1005,7 +1006,7 @@ fn sensor_to_string(value: &Option<u8>, error: &Option<SensorError>, enabled: bo
};
}
fn to_string<T: Display>(value: &Result<T>) -> String {
fn to_string<T: Display>(value: Result<T>) -> String {
return match value {
Ok(v) => v.to_string(),
Err(err) => {
@@ -1054,3 +1055,19 @@ fn in_time_range(cur: &DateTime<Tz>, start: u8, end: u8) -> bool {
return curhour > start || curhour < end;
}
}
fn get_version() -> VersionInfo {
let branch = env!("VERGEN_GIT_BRANCH").to_owned();
let hash = &env!("VERGEN_GIT_SHA")[0..8];
return VersionInfo {
git_hash: (branch + "@" + hash),
build_time: env!("VERGEN_BUILD_TIMESTAMP").to_owned(),
};
}
#[derive(Serialize, Debug)]
struct VersionInfo {
git_hash: String,
build_time: String,
}