Conflict between screen and rfid reader

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

ESP 32-S3 / Screen is 3.5”480x320 TFT LCD Capacitive Touchscreen Breakout Wiki - DFRobot

with mfrc522

[env:esp32dev]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
monitor_speed = 9600
lib_deps = 
	lvgl/lvgl@^8.3.4
	lovyan03/LovyanGFX@^1.1.5
	miguelbalboa/MFRC522@^1.4.10
	bblanchon/ArduinoJson@^6.21.2
	SPI
	Wire

What LVGL version are you using?

8.3.4

What do you want to achieve?

I’m trying to use the reader and screen at the same time
Both are working but it seems that when i use the reader, the screen goes off due to data being sent at the same time ?
I tried setting a timer / boolean but the result is the same when i scan a tag, the screen shut down.
Any idea ?


static void display_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
  #ifdef DISABLE_FLUSH_DURING_BENCHMARK
  if (disable_flush) {
    lv_disp_flush_ready(disp);
    return;
  }
  #endif 

  if (spiState) {
   uint32_t w = (area->x2 - area->x1 + 1);
    uint32_t h = (area->y2 - area->y1 + 1);
    Serial.println("Calling display flush");
    display.startWrite();
    display.setAddrWindow(area->x1, area->y1, w, h);
    display.writePixels((uint16_t *)&color_p->full, w * h, true);
    display.endWrite();
  }
    lv_disp_flush_ready(disp);
}

static void touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) {
  uint16_t touchX, touchY;
  bool touched = display.getTouch(&touchX, &touchY);

  //
  if (!touched) {
    data->state = LV_INDEV_STATE_REL;
  } else {
    data->state = LV_INDEV_STATE_PR;

    data->point.x = touchX;
    data->point.y = touchY;
  }
}

static void lv_tick_task(void *arg) {
  (void)arg;
  lv_tick_inc(LV_TICK_PERIOD_MS);
}

static void radio_event_handler(lv_event_t * e)
{
    uint32_t * active_id = (uint32_t*)lv_event_get_user_data(e);
    lv_obj_t * cont = lv_event_get_current_target(e);
    lv_obj_t * act_cb = lv_event_get_target(e);
    lv_obj_t * old_cb = lv_obj_get_child(cont, *active_id);

    /*Do nothing if the container was clicked*/
    if(act_cb == cont) return;

    lv_obj_clear_state(old_cb, LV_STATE_CHECKED);
    lv_obj_add_state(act_cb, LV_STATE_CHECKED);

    *active_id = lv_obj_get_index(act_cb);

    LV_LOG_USER("Selected radio buttons: %d, %d", (int)active_index_1, (int)active_index_2);
}


static void radiobutton_create(lv_obj_t * parent, const char * txt)
{
    lv_obj_t * obj = lv_checkbox_create(parent);
    lv_checkbox_set_text(obj, txt);
    lv_obj_add_flag(obj, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_obj_add_style(obj, &style_radio, LV_PART_INDICATOR);
    lv_obj_add_style(obj, &style_radio_chk, LV_PART_INDICATOR | LV_STATE_CHECKED);
}

/**
 * Checkboxes as radio buttons
 */
void checkbox_state(void)
{
    lv_style_init(&style_radio);
    lv_style_set_radius(&style_radio, LV_RADIUS_CIRCLE);

    lv_style_init(&style_radio_chk);
    lv_style_set_bg_img_src(&style_radio_chk, NULL);

    uint32_t i;
    char buf[32];

    lv_obj_t * cont1 = lv_obj_create(lv_scr_act());
    lv_obj_set_flex_flow(cont1, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_size(cont1, lv_pct(40), lv_pct(80));
    lv_obj_align(cont1, LV_ALIGN_CENTER, 70, 0);
    lv_obj_add_event_cb(cont1, radio_event_handler, LV_EVENT_CLICKED, &active_index_1);

    lv_snprintf(buf, sizeof(buf), "%s", "Entree");
        radiobutton_create(cont1, buf);
        lv_snprintf(buf, sizeof(buf), "%s", "Sortie");
        radiobutton_create(cont1, buf);
    /*for (i = 0;i < 5;i++) {
        lv_snprintf(buf, sizeof(buf), "A %d", (int)i + 1);
        radiobutton_create(cont1, buf);

    }*/
    /*Make the first checkbox checked*/
    lv_obj_add_state(lv_obj_get_child(cont1, 0), LV_STATE_CHECKED);
}


void initLvglLgfx() {

  display.begin();
  #ifdef LANDSCAPE
    display.setRotation( 1 ); 
    #endif
    display.setBrightness(255);

  lv_init();
  

  #ifdef LV_DOUBLE_BUFFER
  lv_disp_draw_buf_init(&draw_buf, buf, buf2, screenWidth * LV_BUFFER_SIZE);
  #else
  lv_disp_draw_buf_init(&draw_buf, buf, NULL, screenWidth * LV_BUFFER_SIZE * 2);
  #endif
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init( &disp_drv );
   
    disp_drv.hor_res = screenWidth;
    disp_drv.ver_res = screenHeight;
    disp_drv.flush_cb = display_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register( &disp_drv );

// Input device driver
    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init( &indev_drv );
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = touchpad_read;
    lv_indev_drv_register( &indev_drv );


// periodic timer interrupt
const esp_timer_create_args_t periodic_timer_args = {
  .callback = &lv_tick_task, .name = "periodic_gui"
};
esp_timer_handle_t periodic_timer;
ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, LV_TICK_PERIOD_MS * 1000));


/// OBJSSS
//calendar();
//anim_bar();
checkbox_state();

}

byte nuidPICC[4];
void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}


void playSound() {
  //tone(BUZZZER_PIN, NOTE_E5, 16);
  //delay(16);
  //noTone(BUZZZER_PIN);

  ledcWriteTone(7, 1000);
  delay(500); 
  ledcWriteTone(7, 0);
}

void initBuzzer() {
  ledcSetup(7, 1000, BUZZZER_PIN);
  ledcAttachPin(BUZZZER_PIN, 7);
}

void rfidCheck() {
 
 if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;

  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));


  if (rfid.uid.uidByte[0] != nuidPICC[0] || 
    rfid.uid.uidByte[1] != nuidPICC[1] || 
    rfid.uid.uidByte[2] != nuidPICC[2] || 
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    Serial.println(F("A new card has been detected."));


    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   
    Serial.println(F("The NUID tag is:"));
    Serial.print(F("In hex: "));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    playSound();
  }
  else Serial.println(F("Card read previously."));

  rfid.PICC_HaltA();

  rfid.PCD_StopCrypto1();
}

void initRfid() {
 // SPI.begin(12, 13, 11, 5);
  rfid.PCD_Init();
  rfid.PCD_WriteRegister(rfid.RFCfgReg, (0x07<<4));
  rfid.PCD_AntennaOn();
  delay(4);
  rfid.PCD_DumpVersionToSerial();
  Serial.println("RFID Initialized");
}

void setup() {
  Serial.begin(115200);


// INIT LGFX

  initLvglLgfx();
  initRfid();

  ESP_LOGI(TAG, "Ready to start a demo. Tap a button on screen. Reset the board with the reset button or Ctrl+T Ctrl+R to pick a new one.");
}



void loop()
{
  lv_timer_handler();
  vTaskDelay(pdMS_TO_TICKS(TASK_SLEEP_PERIOD_MS));

  unsigned long currentMillis = millis();

  unsigned long elapsedMillis = currentMillis - StartOfInterval;

  if (elapsedMillis >= ResetInterval && !spiState)
  {
    StartOfInterval = currentMillis;
    elapsedMillis = 0;
    rfid.PCD_Reset();
    initRfid(); 
    Serial.println("Resetting reader");
  }
  
  if (!spiState) {
    rfidCheck();
  }
  spiState = !spiState;
}