What can be done is to use a Timer to periodically trigger 'tick' a method. This method increments a counter and for e.g. regular blink it switches the LED on, off at 0->off, 100->on, 200->set counter to zero. So the counter is basically a 'state'. Combine this with a 'mode' for various patterns.
And wrap this into a class:
And wrap this into a class:
Code:
import machineimport timeclass Blink: MODE_TICK = 0 MODE_BLINK_2 = 1 def __init__(self, gpio:int): self.counter = 0 self.led=machine.Pin(gpio, machine.Pin.OUT) self.led.off() self.mode = Blink.MODE_TICK self.tim = machine.Timer() self.tim.init(freq=100, mode=machine.Timer.PERIODIC, callback=self.tick) def stop(self): self.tim.deinit() def tick(self, x): self.counter += 1 if self.mode == Blink.MODE_TICK: if self.counter == 0: self.led.on() if self.counter == 3: self.led.off() if self.counter > 200: self.counter = -1 if self.mode == Blink.MODE_BLINK_2: if self.counter == 0: self.led.on() if self.counter == 50: self.led.off() if self.counter == 100: self.led.on() if self.counter == 150: self.led.off() if self.counter >= 200: self.mode = Blink.MODE_TICK self.counter = -1 def set_mode(self, mode): self.mode = mode self.counter = -1 print("start")blink = Blink(25)print( "run in endless loop")while True: time.sleep(10) print("blink*2") blink.set_mode(Blink.MODE_BLINK_2) time.sleep(10)
Statistics: Posted by ghp — Tue Dec 17, 2024 11:11 pm