Timer issues

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

Timer issues

Post by warren » Thu Sep 15, 2016 8:48 pm

The code is from the docs here:
http://docs.micropython.org/en/latest/e ... tml#timers

Code: Select all

from machine import Timer
tim = Timer(-1)
tim.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:print(2))
Depending on the values chosen (and the wind direction!), I sometimes get warnings about objects being non-callable..

Two questions:

1) How do i completely kill the timer?

2) How do I 'feed' it to prevent it timing out?
#

If I repeat the code, I get ANOTHER timer!!

Thanks

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Timer issues

Post by pythoncoder » Fri Sep 16, 2016 8:30 am

To kill it, issue tim.deinit() http://docs.micropython.org/en/latest/e ... Timer.html

As for feeding it, I think you're talking about watchdog timers (WDT) http://docs.micropython.org/en/latest/e ... e.WDT.html. You created a virtual timer and issued mode=Timer.PERIODIC which will cause it to run until deinit() is called (or a software reset is issued).

"If I repeat the code, I get ANOTHER timer!! "

Well that's how Python works ;) Each time you call a constructor an object is created. Say I have a class MyClass and issue the following:

Code: Select all

a = MyClass()
a.bamboozle() # call some method
a = MyClass()
A second instance of MyClass is created and assigned to a. The first instance is "out of scope": it cannot now be accessed and will eventually be garbage collected. However this may not apply to virtual timer objects: I don't know the implementation but it's possible that an underlying object reference still exists causing them to live on indefinitely. I would issue deinit before letting a virtual timer go out of scope.
Peter Hinch
Index to my micropython libraries.

ihornehrutsa
Posts: 35
Joined: Sat Oct 26, 2019 8:38 pm

Re: Timer issues

Post by ihornehrutsa » Thu Sep 09, 2021 5:06 pm

Add WDT.deinit() method #7766
https://github.com/micropython/micropython/issues/7766

ESP32/machine_wdt.c: Add WDT.deinit() method. #6631
https://github.com/micropython/micropython/pull/6631

Post Reply