retrieving timer ID

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: retrieving timer ID

Post by pythoncoder » Fri Jul 14, 2017 12:02 pm

v923z wrote:...I think I had a pull request in mind...
OK!
v923z wrote:...I think any single-shot, non-blocking timer would require such a feature.
I'm unconvinced. Your application requires it because you are dynamically allocating timers, not because it's single-shot:

Code: Select all

def callback(t):
	t.deinit()
	t.callback(None)

timer = pyb.Timer(2)
timer.init(freq=1)
timer.callback(callback)
Peter Hinch
Index to my micropython libraries.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: retrieving timer ID

Post by pfalcon » Fri Aug 04, 2017 3:26 pm

In Python (not just MicroPython), to find out ID of any object (not just Timer), use id() function. To map arbitrary values to any object (not just Timer), use dictionary:

Code: Select all

TIMER_MAP = {
timer1: "this is my blue timer",
timer2: "this is my red timer",
timer3: 4,
}
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: retrieving timer ID

Post by deshipu » Fri Aug 04, 2017 5:20 pm

The id() function gives you a pretty much arbitrary number, not the timer's id. The only thing that is guaranteed for it is that it will be different for different objects in memory at a given point (but it can be re-used once an object disappears). That doesn't really help much in figuring out which timer is which (and same for all the other peripherals too).

Post Reply