ESP vs Pico Timer Declaration?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

ESP vs Pico Timer Declaration?

Post by Jibun no kage » Wed Aug 17, 2022 1:48 am

Recently was doing some testing across micropython supported controllers, i.e. ports. Pico versus ESP variants. And I discovered the following was required:

Code: Select all

        from machine import Timer
        if (IsPico() or IsPicoW()):
            theTimer=Timer(-1, period=1000*60, mode=Timer.PERIODIC, callback=Ping)
        elif (IsESP01() or IsESP8266 or IsESP32()):
            theTimer=Timer(-1)
            theTimer.init(period=1000*60, mode=Timer.PERIODIC, callback=Ping)
The methods IsPico, IsPicoW, IsESP01, etc., actually are my creation, they reference a static dictionary that qualifies what a device is, based on what values are available, unique_id from machine, MAC address from network.WLAN, etc. The issue is how the virtual timer has to be declared.

Here is the interesting part... "theTimer=Timer(-ONE, period=1000*60, mode=Timer.PERIODIC, callback=Ping)" Fails on ESP modules, but works on Picos? Is this by design. intentional? On ESP module, the error returned is "TypeError: function takes 1 positional arguments but 4 were given." So the ESP Timer() invocation can't mirror the Pico invocation at all.

If so, why? Would you not want the same declaration to work on different ports, so make documentation streamline and consistent? Development more universal? Not sure I see the logic of the ESP port working one way versus the Pico port another way.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ESP vs Pico Timer Declaration?

Post by Roberthh » Wed Aug 17, 2022 6:25 am

I would say that the ESP implementation misses the 'common' style, in that additional arguments can be given either at instantiating the object of in an init call. Then, it should be able to change individual properties of an object trough an init call. But there is a wild mix of behavior. For some ports, even the init method does not exist for devices. Cleaning that up would first require to define a common interface policy, and then fix all the ports and modules. That is quite a task.

In case of ESP32 it's strange, since it is a single function call that is missing in machine_timer_make_new(). Everything else is already prepared.

P.S.: Since the change is rather small, i could make a PR for that, being open PR no. 54 for ESP32. Due to Damien's work load, the chance for approvae is minor, but at least that does not get lost.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ESP vs Pico Timer Declaration?

Post by Roberthh » Wed Aug 17, 2022 7:01 am

OK. so I made a PR for it, #9063, open PR no. 276.

Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: ESP vs Pico Timer Declaration?

Post by Jibun no kage » Wed Aug 17, 2022 7:28 pm

That is cool. Thanks for the PR. It will likely be addressed when some other issue is being addressed, and this PR gets some TLC. For now I can get my code as consistent as possible, and leave it at that. Just could not see why the delta existed, when I asked. It seemed like an 'oops' more than an underlying technical issue, across devices.

As you may gather, I am building up a code base that is as hardware and MP image agnostic as possible. So when I want to use Pico, I use Pico, and when I want to use ESP, I use ESP. And I don't have to revisit code variances most of the time.

Post Reply