Arubinu
04/25/2025, 7:29 PM(gpio.Pin MY_PIN).do:, I don't get reliable results.
But I suspect the issue comes from how .do: behaves inside a task, since it appears to be blocking...floitsch
04/26/2025, 8:10 AMfloitsch
04/26/2025, 8:12 AMpulse-counter. It counts in hardware for you, which makes it independent of the speed of your program and uses less CPU:
https://libs.toit.io/pulse-counter/library-summaryfloitsch
04/26/2025, 8:13 AMArubinu
04/26/2025, 8:16 AMfloitsch
04/26/2025, 8:19 AMdo internally uses wait-for which isn't really high performance. It uses an event queue and signals to wake up your task. That's efficient when nothing happens but adds a bit of overhead when a change is finally detected.
- a tight loop around pin.get will be faster, but you need to make sure that other tasks can run (assuming you have them). So adding a yield inside the loop might be necessary.
- do is blocking. If you want to run out code concurrently you either have to start a different process (a new container or spawn), but then the communication with it gets difficult. Alternatively you usually use tasks (using task). These are cooperative threads. You need to yield to allow other tasks to run. Some of the library functions do that automatically.Arubinu
04/26/2025, 9:01 AMfloitsch
04/26/2025, 9:22 AMwait-forArubinu
04/28/2025, 3:33 PMimport gpio
import pulse-counter
main:
pin/gpio.Pin := gpio.Pin in-pin --input --pull-up
pulse-count/pulse-counter.Unit := pulse-counter.Unit pin
task::get-task
get-task:
while true:
pulse-count.clear
start := Time.now.ms-since-epoch
sleep --ms=3000
pulse-total := pulse-count.value
now := Time.now.ms-since-epoch
print "pulse-total: " + pulse-total.stringify // <-- 30.000
last-speed = ((pulse-total.to-float / 2.0) * 60000.0 / (now - start).to-float).round
print "last-speed: " + last-speed.stringify // <-- 300.000floitsch
04/28/2025, 3:35 PMglitch-filter-ns of the Unit constructor.floitsch
04/28/2025, 3:36 PMfloitsch
04/28/2025, 3:36 PMfloitsch
04/28/2025, 3:37 PMprint "pulse-total: $pulse-total".floitsch
04/28/2025, 3:38 PMArubinu
04/28/2025, 3:40 PMfloitsch
04/28/2025, 3:41 PMfloitsch
04/28/2025, 3:43 PMArubinu
04/28/2025, 3:47 PMfloitsch
04/28/2025, 3:49 PMArubinu
04/28/2025, 4:40 PM