Now the timer goes off every second . This is normal, but if the auto-off is 5 minutes (300 seconds), then 300 calls for one action is excessive.
Instead of: period=1000, callback=seconds_counter
Can: Start the timer for the entire time .time_out * 1000 On any action ( ) - stop and restart feed () This reduces the load on the CPU (fewer interrupts), saves energy .
Example: def start_timer(self): if self.shutdown_time > 0: self.stop_timer() self.auto_shutdown_timer = Timer(Timer.TIMER1, Timer.CHANNEL0) self.auto_shutdown_timer.init( mode=Timer.ONE_SHOT, period=self.time_out * 1000, callback=self.on_timeout )
def on_timeout(self, _): if self.ctx: self.ctx.clear() power_manager.shutdown()
def feed(self): self.time_out = self.shutdown_time self.start_timer() #
Advantages: No 300 calls per second - only one timer for the entire waiting time . Less load on the system. Faster reaction time - no need to wait for the next second