Timer in Unix port
-
- Posts: 12
- Joined: Tue Apr 14, 2020 6:08 pm
Timer in Unix port
Hi!
I was trying to port my code that I wrote for esp8266/esp32 to Unix, so that I can scale test with micropython running in Docker containers.
I hit a wall with the use of Timer, since it is not available in the Unix port, which I understand, since it an hardware controlled timer library.
Is there a way to port this easily, or do I have to code the intended behavior somehow? From my basic understanding, the network logic is handled by the OS, so I was wondering if that is possible with timers.
I was trying to port my code that I wrote for esp8266/esp32 to Unix, so that I can scale test with micropython running in Docker containers.
I hit a wall with the use of Timer, since it is not available in the Unix port, which I understand, since it an hardware controlled timer library.
Is there a way to port this easily, or do I have to code the intended behavior somehow? From my basic understanding, the network logic is handled by the OS, so I was wondering if that is possible with timers.
Re: Timer in Unix port
I'm not aware of the limitation you describe. Can you be more specific about which modules/classes/functions you are missing?
Re: Timer in Unix port
The unix port indeed does not have the machine.Timer class which I assume is what you mean. It shouldn't be very hard to add something with a similar interface, but using actual interrupts is something else. What do you want to do with it?
edit forgot about it, but there's https://github.com/micropython/micropyt ... e/timer.py. Requires FFI though. IIRC you should be able to do 'upip install machine'. Else clone the repository and copy the machine directory to a directory which in in MicroPython's module search path a.k.a. sys.path.
I don't understand what you mean here, what is the relationship between network and timers?From my basic understanding, the network logic is handled by the OS, so I was wondering if that is possible with timers.
edit forgot about it, but there's https://github.com/micropython/micropyt ... e/timer.py. Requires FFI though. IIRC you should be able to do 'upip install machine'. Else clone the repository and copy the machine directory to a directory which in in MicroPython's module search path a.k.a. sys.path.
-
- Posts: 12
- Joined: Tue Apr 14, 2020 6:08 pm
Re: Timer in Unix port
That's it! Thank you! I was using the machine library Timer in the ESPs, but the Unix port didn't have it.
Basically I was reading the temperature from a DHT in intervals of a given time, using the timer. For the Unix, I was simulating the DHT library to return a predefined value.
I gave the network module as an example of changes in the logic between the ESPs and the Unix port.
However, I notice that this Timer library is very different from the ESPs one. For once, I can't understand how to make a periodic timer, like trigger the callback in intervals of 5 seconds. I setup the timer with
but it returns segmentation fault. Is there something I'm missing?
Basically I was reading the temperature from a DHT in intervals of a given time, using the timer. For the Unix, I was simulating the DHT library to return a predefined value.
I gave the network module as an example of changes in the logic between the ESPs and the Unix port.
However, I notice that this Timer library is very different from the ESPs one. For once, I can't understand how to make a periodic timer, like trigger the callback in intervals of 5 seconds. I setup the timer with
Code: Select all
timer = machine.Timer(0, 5)
timer.callback(lambda t: measure(t))
Re: Timer in Unix port
The second argument is frequency in Hz, not period, so every 5 seconds would be 0.2. However floating point doesn't seem to be supported by the class, so not possible.
Anyway if I try
that starts printing 'cb' every 500mSec.
So either your measure() function does something wrong, or there's some bug which doesn't happen on my machine.
Anyway if I try
Code: Select all
t = machine.timer.Timer(0, 2)
t.callback(lambda t: print(t))
So either your measure() function does something wrong, or there's some bug which doesn't happen on my machine.
-
- Posts: 12
- Joined: Tue Apr 14, 2020 6:08 pm
Re: Timer in Unix port
Thank you for the help!
I realized this was very limiting for what I need, so I ended up doing a makeshift timer with the uasyncio library.
I realized this was very limiting for what I need, so I ended up doing a makeshift timer with the uasyncio library.
Re: Timer in Unix port
If it implements the complete Timer interface and functions like the ones on the other platforms, it might be worth making a PR for this: a Timer implemented using MicroPython features is imo more usable and interesting than one which doesn't fully work and is implemented with FFI.
-
- Posts: 12
- Joined: Tue Apr 14, 2020 6:08 pm
Re: Timer in Unix port
Even something similar to this:
I'm fairly new to this micropython business, therefore I'm not sure if this solution is PR worthy.
Code: Select all
async def timer_exec(callback, interval):
if stop_repeat:
return
callback(None)
await asyncio.sleep_ms(interval)
loop = asyncio.get_event_loop()
loop.create_task(timer_exec(callback, interval))
def start_timer():
loop = asyncio.get_event_loop()
loop.create_task(timer_exec(cb, 5000))
Re: Timer in Unix port
Here is my attempt to implement a self contained "Timer" class for the unix port, that matches Micropython Timer API:
https://github.com/lvgl/lv_binding_micr ... v_timer.py
https://github.com/lvgl/lv_binding_micr ... v_timer.py