gurgleapps/rotary-encoder

Runtime Errors

Opened this issue · 1 comments

Pretty new here and I apologize if I'm missing something basic. The code works with my encoder (same KY040). However, I keep getting the following error:
Traceback (most recent call last):
File "Rotary.py", line 32, in rotary_change
RuntimeError: schedule queue full

In micropython, we get the following error in the rotary class:

line 30: micropython.schedule(self.call_handlers, Rotary.ROT_CW)

Traceback (most recent call last):
File "rotary.py", line 30, in rotary_change
RuntimeError: schedule queue full

In MicroPython, the micropython.schedule() function is used to schedule a function to be called later in the context of the MicroPython scheduler. It's especially useful when you need to call a function in response to an interrupt, but the function might not be safe to call directly from an interrupt context (for example, it may allocate memory).

The error "RuntimeError: schedule queue full" indicates that the internal queue MicroPython uses to keep track of scheduled functions is full. There's only a limited number of slots in this queue.

Here are some reasons why you might be encountering this error:

High Interrupt Rate: If the rotary_change function is getting called very frequently (because of rapid changes in the rotary encoder), it might be trying to schedule more functions than the queue can handle.

Scheduled Function Duration: If the scheduled function (self.call_handlers) takes a long time to execute, it can prevent the queue from being emptied in a timely manner, which means new functions can't be scheduled until the old ones are executed.

Other Scheduled Functions: If other parts of your code are also scheduling functions using micropython.schedule(), they could be filling up the queue, leaving no space for the rotary_change function to schedule its handler.

To solve the problem:

Debounce: If the interrupt from the rotary encoder isn't debounced, you might get multiple interrupts for a single "event". Consider adding a debounce mechanism.

Optimize the Handler: Ensure that the function you're scheduling (self.call_handlers) is as fast as possible. If you can optimize it, you'll free up the queue more quickly.

Check for Other Schedulers: Look through your code to see if other parts of your application are also scheduling functions frequently, and optimize or reduce their frequency if possible.

Direct Handling: If the operation in the scheduled function is simple and doesn't require memory allocation or other operations that aren't safe in an interrupt context, you might consider handling it directly in the interrupt handler rather than scheduling it.

Increase the Queue Size (Advanced): If you're building your own MicroPython firmware, you might be able to increase the size of the schedule queue, though this will consume more RAM.

Remember, the goal is to balance the rate at which functions are added to the schedule queue with the rate at which they're removed (executed). If you can achieve this balance, you won't run into the "schedule queue full" error.