Task Handler and For ( ; ; ) loops


I am in the stage where i am satisfied with the layout of the GUI. The next step is to make it function with GRBL. GRBL is a firmware for CNC machines for those not familiar with it. Currently, we tether cnc machines to the PC to send commands to the cnc machine

I made a GUI for ESP32 that can be loaded with GRBL. The buttons I layed out thru littlevgl will be calling macro like text commands sent to the serial port of the microcontroller. I have tried with success sending texts made from the custom keyboard to the serial monitor so I believe the remaining buttons will just be easy.

The main loop of GRBL has about ten function calls plus lv_task_handler(). One of the functions called finally by GRBL is protocol_main_loop(). This GRBL function freezes the UI.
Based on the comment on the function it is an idle function which waits for the users input with the following loop condition:

for (;;) {

What MCU/Processor/Board and compiler are you using?


What do you want to achieve?

Make protocol_main_loop work without freezing the microcontroller.

What have you tried so far?

  1. I tried removing the for loop block which causes the GUI to freeze. I think having a GUI serves that purposed. Clicking buttons and sending commands thru the GUI is doing the same as waiting for the user input. But doing this makes the microcontroller make an endless loop of sending the welcome message at start up.
  2. Reading up on littlevgl’s task handler to make the calls on the ‘for’ loop work not freeze the GUI.
  3. tried running protocol_main_loop on core 0 of ESP. This blanks the screen display.

Code to reproduce

main program loop:

void loop() { 


/ Reset Grbl primary systems.
   serial_reset_read_buffer(CLIENT_ALL); // Clear serial read buffer
   gc_init(); // Set g-code parser to default state  
  // coolant_init();
   plan_reset(); // Clear block buffer and planner variables
   st_reset(); // Clear stepper subsystem variables
  // Sync cleared gcode and planner positions to current system position.
  // put your main code here, to run repeatedly:
  // Start Grbl main loop. Processes program inputs and executes them.  

mainGRBL() calls a series of functions the last of which calls protocol_main_loop(). protocol_main_loop() freezes the GUI. As mentioned above it uses a ( ; ; ) condition that makes it loop forever.

Please advice what i can do. Thanks.

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

This looks like a single-threaded system, so the infinite processing loop will prevent you from ever calling the . Do you have a way of creating multiple threads?

I created my lvgl application in the context of a C++ thread so that I could run it in parallel with my main application. I don’t know if you’ve got access to any sort of scheduler in your system.

If not, then can you perhaps call the task handler inside the protocol_main_loop()? I’m completely unfamiliar with GRBL (aside from having heard of it), so I may be asking for the impossible.

1 Like

Essentially, you need to ensure that lv_task_handler is repeatedly called every 10ms or so. By the sounds of it protocol_main_loop has it’s own loop inside and thus will never return. Like @tarvik suggested, you should try to call lv_task_handler inside that.

1 Like

On an Esp32 you have two cores. You could use freertos tasks to run the loops for GRBL and lvgl on different cores. Since lvgl only sends commands to GRBL there should be no trouble with the tasks running asynchronous.


1 Like

I tried @tarvik’s suggestion and seconded by @embeddedt. It drew lots of error which kind of overhelmed me. Seemed like a feat to remove all those errors.

Anyway, after lots of rethinking, tried it the whole day yesterday. Guess what? it now works!


1 Like