diff --git a/src/main.rs b/src/main.rs index bb3e4ca..33d986a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -219,6 +219,10 @@ fn main() -> ! { let mut switchctrl = switch_control::SwitchControl::<1>::new(8000, 100, SWITCH_PWM_LIMIT); + // track nano_joules (== nanoVoltAmpereSeconds) generated, for LED flashing + let mut nano_joules: u32 = 0; + let mut loopcnt_led_y_off = 0; + let mut loopcnt: u64 = 0; pin_led_r.set_high().unwrap(); @@ -233,8 +237,6 @@ fn main() -> ! { }); if systick_received { - pin_led_g.set_high().unwrap(); - // re-shedule the systick alarm critical_section::with(|_cs| unsafe { core::ptr::write_volatile(&mut SYSTICK_FLAG, false); @@ -277,6 +279,31 @@ fn main() -> ! { pwr_switch_ch.set_duty(pwmval as u16); + // Yellow LED flashes for 10 ms whenever 1 Joule has been generated. + // This means that it flashes once per second when output power is 1 Watt, and 3 times + // per second if output power is 3 Watt. + let nano_joules_generated = vout * iout; // * 1 millisecond, implicitely + if nano_joules_generated > 0 { + nano_joules += nano_joules_generated as u32; + } + + // 1000 mV * 1000 mA * 1000 ms = 1 Joule + if nano_joules >= 1000000000 { + pin_led_y.set_high().unwrap(); + loopcnt_led_y_off = loopcnt + 10; + nano_joules -= 1000000000; + } + + if loopcnt == loopcnt_led_y_off { + pin_led_y.set_low().unwrap(); + } + + // Green LED is on in constant-voltage mode and off in MPPT mode. + match switchctrl.get_control_mode() { + switch_control::ControlMode::ConstantVoltage => {pin_led_g.set_high().unwrap()}, + switch_control::ControlMode::MPPT => {pin_led_g.set_low().unwrap()}, + } + // do not output status data every loop match loopcnt % 500 { 1 => { @@ -323,8 +350,6 @@ fn main() -> ! { logger.process().unwrap(); - pin_led_y.set_high().unwrap(); - // save some power as long as no interrupt occurs // FIXME: not working yet for some reason //cortex_m::asm::wfi(); diff --git a/src/switch_control.rs b/src/switch_control.rs index da3825a..2964758 100644 --- a/src/switch_control.rs +++ b/src/switch_control.rs @@ -1,7 +1,8 @@ use crate::logger::Logger; use heapless::String; -enum ControlMode { +#[derive(Copy, Clone)] +pub enum ControlMode { MPPT, ConstantVoltage } @@ -289,4 +290,9 @@ impl SwitchControl logger.log(b"\r\n").unwrap(); } + + pub fn get_control_mode(&self) -> ControlMode + { + self.mode + } }