Open
Description
The microcontrollers support counting rising/falling edges at rather fast pace (1-10kHz), eg to measure RPM of a motor with hall sensor or disc encoder. It would be nice to have the interrupt handling implemented in C code and export something easier to use into Python:
from machine import Counter, Pin
from time import sleep
counter = Counter(Pin(34))
while True:
count, duration = counter.poll()
print("%d RPM" % (count * 60 / duration))
sleep(1)
In this pseudo-code instantiating a counter object sets up interrupt handlers and an integer for storing the count of events and another integer for storing current timestamp. When poll() function is executed, count and timestamp delta are returned and variables are reset.
The main benefit of such approach is accuracy and this probably hogs the Python runtime less as well especially at higher frequecies (1kHz and above)