Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esp32/machine_timer: Add find free timer id. #12155

Closed
wants to merge 1 commit into from

Conversation

IhorNehrutsa
Copy link
Contributor

@IhorNehrutsa IhorNehrutsa commented Aug 3, 2023

atimer = Timer(-2)
The constructor id of -2 selects the id of a free timer.
Check the timer id range.
Check if the timer is already in use.

This allows you to write separated library modules without worrying about the timer id. For example:

class DoSomethingByTymer():
    def init(self):
        self.timer = Timer(-2, mode=Timer.PERIODIC, period=5000, callback=self.cb)
    def cb(self):
        print('DoSomethingByTymer()')
        # etc

EDITED: Maybe id value None is a better arg?
atimer = Timer(None)

@IhorNehrutsa
Copy link
Contributor Author

IhorNehrutsa commented Aug 3, 2023

Check if the timer is already in use:

from machine import Timer

t0 = Timer(0)
t1 = Timer(0)

Traceback (most recent call last):
File "", line 4, in
RuntimeError: already used

Check the timer id range:

from machine import Timer

t = Timer(10)

Traceback (most recent call last):
File "", line 3, in
ValueError: id must be from 0 to 4

Timer() constructor id of -2 selects the id of a free timer:

from machine import Timer

t0 = Timer(-2)
print(t0)

t1 = Timer(-2)
print(t1)

t2 = Timer(-2)
print(t2)

t3 = Timer(-2)
print(t3)

t4 = Timer(-2)

Timer(3, mode=ONE_SHOT, period=0)
Timer(2, mode=ONE_SHOT, period=0)
Timer(1, mode=ONE_SHOT, period=0)
Timer(0, mode=ONE_SHOT, period=0)
Traceback (most recent call last):
File "", line 15, in
RuntimeError: out of Timers:4

@IhorNehrutsa IhorNehrutsa changed the title esp32/machine_timer.c: Add find free timer id. esp32/machine_timer: Add find free timer id. Aug 4, 2023
@IhorNehrutsa IhorNehrutsa force-pushed the machine_timer branch 3 times, most recently from 48e27e5 to 7432c58 Compare September 3, 2023 19:31
@IhorNehrutsa IhorNehrutsa force-pushed the machine_timer branch 2 times, most recently from 2704e28 to 32a1dcd Compare October 12, 2023 08:37
``id`` of -2 selects the ``id`` of a free timer.
Check the timer `id` range.
Check if the timer is already in use.

Signed-off-by: IhorNehrutsa <Ihor.Nehrutsa@gmail.com>
@projectgus
Copy link
Contributor

This is an automated heads-up that we've just merged a Pull Request
that removes the STATIC macro from MicroPython's C API.

See #13763

A search suggests this PR might apply the STATIC macro to some C code. If it
does, then next time you rebase the PR (or merge from master) then you should
please replace all the STATIC keywords with static.

Although this is an automated message, feel free to @-reply to me directly if
you have any questions about this.

@cnadler86
Copy link

Is this still ongoing? I think it might ve a very helpful feature, since sw timers are not supported on esp32 Boards

@projectgus
Copy link
Contributor

projectgus commented Aug 26, 2024

Is this still ongoing? I think it might ve a very helpful feature, since sw timers are not supported on esp32 Boards

Sorry @IhorNehrutsa that no one has gotten back to you about this, and thanks @cnadler86 for the heads up.

In general, it's better if MicroPython APIs behave the same across ports. If I understand correctly, the main reason for adding this special -2 value on esp32 port is precisely because software timers aren't supported on esp32. There's probably not really a need for it on the ports with software timers.

So the way forward for machine.Timer on the esp32 port is probably to implement software timers, so it works the same as other ports with software timer support. I don't think there's any technical barrier to doing this, the ESP-IDF framework has the esp_timer API which can create a microsecond precision timer callback, and the shared/runtime/softtimer.[ch] code has all the code to implement Python soft timers on top of a lower level primitive like that. Someone just needs to do that work and submit the PR (AFAIK there is not any open PR for this, yet).

@projectgus
Copy link
Contributor

I'm going to close this out, but please refer to comment above - a PR that implements softtimers on esp32 port would be very welcome (I think probably using esp_timer API).

@projectgus projectgus closed this Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants