T-vK / ESP32-BLE-Keyboard

Bluetooth LE Keyboard library for the ESP32 (Arduino IDE compatible)
2.4k stars 402 forks source link

Press / release not working after delay statements? #154

Open ismenc opened 2 years ago

ismenc commented 2 years ago

Hi, I'm getting a curious issue when using press and release functions inside a FreeRTOS task. For user experience purposes, one task is pressing a key instantly while the release task is being delayed. The issue is that vTaskDelay is causing the .release funcion not to work. Also tested with press and same result. A vTaskDelay greater than 0 before the functions makes them useless. But this issue is not happening with ReleaseAll (which is the workaround i will take by the moment). Also it isn't a problem of vTaskDelay because while raw testing in setup function, it doesn't show any problem.

Tasks codes:

void blePressKeyTask(void * params) {
  xSemaphoreTake(keyboardMutex, portMAX_DELAY);
  if(bleKeyboard.isConnected()) {
    vTaskDelay((((KeyParams *)params)->delaySeconds*1000)/portTICK_PERIOD_MS);
    bleKeyboard.press(((KeyParams *)params)->key); // This is working because delaySeconds = 0 here
  }
  else {
    xTaskCreatePinnedToCore(displayErrorTask, "display bt notcon", 1024, NULL, 1, NULL, APP_CPU_NUM);
  }
  xSemaphoreGive(keyboardMutex);
  vTaskDelete(NULL);
}

void bleReleaseKeyTask(void * params) {
  xSemaphoreTake(keyboardMutex, portMAX_DELAY);
  if(bleKeyboard.isConnected()) {
    vTaskDelay((((KeyParams *)params)->delaySeconds*1000)/portTICK_PERIOD_MS);
    bleKeyboard.release(((KeyParams *)params)->key); // This is not working
    // bleKeyboard.releaseAll(); // This is working
  }
  else {
    xTaskCreatePinnedToCore(displayErrorTask, "display bt notcon", 1024, NULL, 1, NULL, APP_CPU_NUM);
  }
  xSemaphoreGive(keyboardMutex);
  vTaskDelete(NULL);
}

As you can see both tasks codes contains the vTaskDelay. To the press function it's always reaching with 0, and therefore, working. Any idea on why could this happen? Already tried to pin the tasks to the app core, increasing stack size, but no success...

Thank you!