Why doesn't Timer() have .start()? #18703
Unanswered
PsuFan
asked this question in
Core Development
Replies: 1 comment 2 replies
-
|
You could make your own class to get this functionality. Adding functions/methods increases the firmware size. The timer is started with the import time
from machine import Timer
class OneShotTimer:
def __init__(self, id, delay, callback):
self.timer = Timer(id)
self.delay = delay
self.callback = callback
def start(self):
self.timer.init(mode=Timer.ONE_SHOT, period=self.delay, callback=self.callback)
def stop(self):
self.timer.deinit()
class Callback:
def __init__(self):
self.count = 0
def __call__(self, timer: Timer):
self.count += 1
print(f"Callabck {self.count}")
my_timer = OneShotTimer(1, 1000, Callback())
my_timer.start()You could test it here: https://wokwi.com |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Why can't I initialize my period, mode, and callback parameters and then start() and stop() it when needed?
Beta Was this translation helpful? Give feedback.
All reactions