Latest Arduino and esp32p4-m3. lvgl 9.5
The .ino file contains the following function running in a task in core 1
void MacroStack (void * pvParameters) {
USBSerial.println("MacroStack starting.");
while(true) {
if (Stacking)
{
USBSerial.printf("Steps = %i StepIncs = %i Stacking %i\n", StepsFS, StepIncs, Stacking);
if (StepsFS < 0)
digitalWrite(DIR_PIN, LOW);
else
digitalWrite(DIR_PIN, HIGH);
if (Photo) TakePhoto();
for (int i = 0; i < abs(StepsFS); i++ )
{
USBSerial.printf("Step = %i\n", i);
for(int j = 0; j < StepIncs; j++)
{
if (!Stacking) break;
digitalWrite(Step_PIN, HIGH);
esp_rom_delay_us(100);
digitalWrite(Step_PIN, LOW);
esp_rom_delay_us(300);
}
if (!Stacking) break;
if (Photo) TakePhoto();
IncProgress(i + 1);
}
if (Stacking == false)
USBSerial.println("Aborting- Stack");
else
{
Stacking = false;
//ResetStackBtn ();
USBSerial.println("Stopped Stacking");
}
}
delay(200);
}
USBSerial.println("Stacking crashed");
vTaskDelete(NULL); // Clean up task if loop breaks
}
In a separate lvgl c file I have the callback for a button as follows
static void btn_Stack_cb(lv_event_t * e) {
if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
delay(100);
if (!Stacking) {
lv_label_set_text(label_btn_stack, "Stop");
lv_obj_set_style_bg_color(btn_Run_Stack, lv_palette_main(LV_PALETTE_RED), 0);
lv_refr_now(NULL);
StepsFS = (ta_Steps != NULL) ? atoi(lv_textarea_get_text(ta_Steps)) : 0;
float step_sz = (ta_StepSize != NULL) ? atof(lv_textarea_get_text(ta_StepSize)) : 0.0f;
if (StepsFS != 0) Run_Macro_Stack( StepsFS, step_sz, 16, lv_obj_has_state(sw_Photo, LV_STATE_CHECKED));
} else {
Stacking = false;
// ResetStackBtn();
// lv_refr_now(NULL);
}
}
}
and a function
void ResetStackBtn()
{
//Stacking = false;
if(label_btn_stack != NULL) lv_label_set_text(label_btn_stack, "Run");
if(btn_Run_Stack != NULL) lv_obj_set_style_bg_color(btn_Run_Stack, lv_palette_main(LV_PALETTE_GREEN), 0);
IncProgress(0);
}
If the call to ResetStackBtn is commented out the code runs as expected, but as soon as I try to make ResetStackBtn change the colour and label the code blocks.
likewise if I use the progress bar it will (sometimes) block.
void IncProgress(int Steps)
{
// return; //(if not commented out the function doesn't block)
int percentage = (Steps * 100) / StepsFS;
lv_bar_set_value(stack_progress_bar, percentage, LV_ANIM_ON);
char buf[12];
lv_snprintf(buf, sizeof(buf), "%d%%", percentage);
lv_label_set_text(label_progress_pct, buf);
}
I know ESP32-P4 can be fickle with blocking but if any enlighten me as to why the code doesn’t work (Note the many things commented out/in) it would be much appreciated. I’ll admit the code looks scrappy, but that is because I’ve tried so many things to try and get it to work.
My fallback solution will be to have two buttons, one to run, and one to abort and forget the progress bar.

