From c0f57afff43b4251f4eb6cb8094cf7e007c98296 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Thu, 15 Feb 2024 22:47:26 +0100 Subject: [PATCH] Calculate total energy and print it, also in JSON format --- src/main.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index d1c83fd..bb9ac11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -221,6 +221,7 @@ fn main() -> ! { // track nano_joules (== nanoVoltAmpereSeconds) generated, for LED flashing let mut nano_joules: u32 = 0; + let mut joules: u32 = 0; // total energy generated let mut loopcnt_led_y_off = 0; let mut loopcnt: u64 = 0; @@ -292,6 +293,7 @@ fn main() -> ! { pin_led_y.set_high().unwrap(); loopcnt_led_y_off = loopcnt + 10; nano_joules -= 1000000000; + joules += 1; } if loopcnt == loopcnt_led_y_off { @@ -322,8 +324,14 @@ fn main() -> ! { logger.log(b"mA - Pout: ").unwrap(); data = String::from(vout * iout / 1000); logger.log(data.as_bytes()).unwrap(); + logger.log(b"mW - E: ").unwrap(); + data = String::from(joules); + logger.log(data.as_bytes()).unwrap(); + logger.log(b"J / ").unwrap(); + data = String::from(joules/3600); + logger.log(data.as_bytes()).unwrap(); - logger.log(b"mW - raw: [").unwrap(); + logger.log(b"Wh - raw: [").unwrap(); for i in 0..adc_value.len() { data = String::from(adc_value[i]); @@ -341,6 +349,29 @@ fn main() -> ! { logger.log(b"\r\n").unwrap(); }, 2 => { + let mut data: String<16>; + + logger.log(b"{\"vin\":").unwrap(); + data = String::from(vin); + logger.log(data.as_bytes()).unwrap(); + logger.log(b",\"vout\":").unwrap(); + data = String::from(vout); + logger.log(data.as_bytes()).unwrap(); + logger.log(b",\"iout\":").unwrap(); + data = String::from(iout); + logger.log(data.as_bytes()).unwrap(); + logger.log(b",\"pout\":").unwrap(); + data = String::from(vout * iout); + logger.log(data.as_bytes()).unwrap(); + logger.log(b",\"energy\":").unwrap(); + data = String::from(joules); + logger.log(data.as_bytes()).unwrap(); + logger.log(b",\"pwm\"").unwrap(); + data = String::from(pwmval); + logger.log(data.as_bytes()).unwrap(); + logger.log(b"}\r\n").unwrap(); + }, + 3 => { switchctrl.log_status(&mut logger); }, _ => {}