Timer in Unix port

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
lowkey_daisy
Posts: 12
Joined: Tue Apr 14, 2020 6:08 pm

Timer in Unix port

Post by lowkey_daisy » Sat May 09, 2020 10:23 pm

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.

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: Timer in Unix port

Post by tve » Sat May 09, 2020 10:48 pm

I'm not aware of the limitation you describe. Can you be more specific about which modules/classes/functions you are missing?

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Timer in Unix port

Post by stijn » Sun May 10, 2020 7:28 am

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?
From my basic understanding, the network logic is handled by the OS, so I was wondering if that is possible with timers.
I don't understand what you mean here, what is the relationship between network and 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.

lowkey_daisy
Posts: 12
Joined: Tue Apr 14, 2020 6:08 pm

Re: Timer in Unix port

Post by lowkey_daisy » Sun May 10, 2020 10:05 am

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

Code: Select all

timer = machine.Timer(0, 5)
timer.callback(lambda t: measure(t))
but it returns segmentation fault. Is there something I'm missing?

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Timer in Unix port

Post by stijn » Sun May 10, 2020 11:15 am

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

Code: Select all

t = machine.timer.Timer(0, 2)
t.callback(lambda t: print(t))
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.

lowkey_daisy
Posts: 12
Joined: Tue Apr 14, 2020 6:08 pm

Re: Timer in Unix port

Post by lowkey_daisy » Sun May 10, 2020 1:14 pm

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.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Timer in Unix port

Post by stijn » Sun May 10, 2020 2:37 pm

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.

lowkey_daisy
Posts: 12
Joined: Tue Apr 14, 2020 6:08 pm

Re: Timer in Unix port

Post by lowkey_daisy » Sun May 10, 2020 3:23 pm

Even something similar to this:

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))
I'm fairly new to this micropython business, therefore I'm not sure if this solution is PR worthy.

agonnen
Posts: 27
Joined: Sat Oct 13, 2018 7:52 pm

Re: Timer in Unix port

Post by agonnen » Thu Jun 03, 2021 10:04 pm

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

Post Reply