https://toitlang.org/ logo
Pause instead of sleep?
# help
a
Is there a way to sleep, but not allowed other tasks to run during that period?
k
For a specific container? Not the entire system?
a
So, we are doing some low level stepper motor stuff (and debugging things). We are manually controlling steps with GPIo pins currently, pulsing one of them to control the steps. And I want consistent pauses between them, but I'm guessing my use of sleep is causing other containers to do stuff as I'm currently running jaguar?
k
How long do you need to pause?
a
We would be happy with a second for now 🙂
k
So you could do something like this: Change the priority of your running process to be higher than other things and do a busy-wait loop until enough time has passed ideally using
Time.monotonic-us --since-wakeup
.
You can change the priority of your process using something like
Process.current.priority = Process.PRIORITY-HIGH
.
You'll be fairly unresponsive while you loop.
a
Just realized that just running the thing with Jag disable makes a hell of a difference already!
k
So maybe just running your container with a higher priority would help?
When your sleep times out, you'll be more important than Jaguar, so it probably would improve things considerably.
Copy code
fancy-sleep duration/Duration -> none:
  deadline-us := (Time.monotonic-us --since-wakeup) + duration.in-us
  while true:
    remaining-us := deadline-us - (Time.monotonic-us --since-wakeup)
    if remaining-us <= 0: return
    // Don't sleep when too close to the deadline.
    sleep-us := remaining-us - 100_000 
    // Don't sleep for too little time.
    if sleep-us >= 20_000: sleep --ms=(sleep-us / 1_000)