diff --git a/components/esp_peripherals/esp_peripherals.c b/components/esp_peripherals/esp_peripherals.c index 5e1411238..0f85c53e8 100644 --- a/components/esp_peripherals/esp_peripherals.c +++ b/components/esp_peripherals/esp_peripherals.c @@ -62,6 +62,7 @@ typedef struct esp_periph_sets { bool ext_stack; bool run; esp_periph_event_t event_handle; + int periph_dynamic_id; STAILQ_HEAD(esp_periph_list_item, esp_periph) periph_list; } esp_periph_set_t; @@ -172,6 +173,7 @@ esp_periph_set_handle_t esp_periph_set_init(esp_periph_config_t *config) event_cfg.context = periph_sets; event_cfg.on_cmd = process_peripheral_event; periph_sets->event_handle.iface = audio_event_iface_init(&event_cfg); + periph_sets->periph_dynamic_id = PERIPH_ID_CUSTOM_BASE; AUDIO_MEM_CHECK(TAG, periph_sets->event_handle.iface, goto _periph_init_failed); audio_event_iface_set_cmd_waiting_timeout(periph_sets->event_handle.iface, DEFAULT_ESP_PERIPH_WAIT_TICK); @@ -199,6 +201,16 @@ esp_err_t esp_periph_remove_from_set(esp_periph_set_handle_t periph_set_handle, return ESP_OK; } +esp_err_t esp_periph_alloc_periph_id(esp_periph_set_handle_t periph_set_handle, int *periph_id) +{ + if ((periph_set_handle == NULL) || (periph_id == NULL)) { + AUDIO_ERROR(TAG, "Invalid parameters: periph_set_handle or periph_id is NULL"); + return ESP_FAIL; + } + *periph_id = periph_set_handle->periph_dynamic_id++; + return ESP_OK; +} + esp_err_t esp_periph_set_destroy(esp_periph_set_handle_t periph_set_handle) { if (periph_set_handle == NULL) { diff --git a/components/esp_peripherals/include/esp_peripherals.h b/components/esp_peripherals/include/esp_peripherals.h index 0ea9dbade..ad4f9fe9f 100644 --- a/components/esp_peripherals/include/esp_peripherals.h +++ b/components/esp_peripherals/include/esp_peripherals.h @@ -38,23 +38,24 @@ extern "C" { * @brief Peripheral Identify, this must be unique for each peripheral added to the peripherals list */ typedef enum { - PERIPH_ID_BUTTON = AUDIO_ELEMENT_TYPE_PERIPH + 1, - PERIPH_ID_TOUCH = AUDIO_ELEMENT_TYPE_PERIPH + 2, - PERIPH_ID_SDCARD = AUDIO_ELEMENT_TYPE_PERIPH + 3, - PERIPH_ID_WIFI = AUDIO_ELEMENT_TYPE_PERIPH + 4, - PERIPH_ID_FLASH = AUDIO_ELEMENT_TYPE_PERIPH + 5, - PERIPH_ID_AUXIN = AUDIO_ELEMENT_TYPE_PERIPH + 6, - PERIPH_ID_ADC = AUDIO_ELEMENT_TYPE_PERIPH + 7, - PERIPH_ID_CONSOLE = AUDIO_ELEMENT_TYPE_PERIPH + 8, - PERIPH_ID_BLUETOOTH = AUDIO_ELEMENT_TYPE_PERIPH + 9, - PERIPH_ID_LED = AUDIO_ELEMENT_TYPE_PERIPH + 10, - PERIPH_ID_SPIFFS = AUDIO_ELEMENT_TYPE_PERIPH + 11, - PERIPH_ID_ADC_BTN = AUDIO_ELEMENT_TYPE_PERIPH + 12, - PERIPH_ID_IS31FL3216 = AUDIO_ELEMENT_TYPE_PERIPH + 13, - PERIPH_ID_GPIO_ISR = AUDIO_ELEMENT_TYPE_PERIPH + 14, - PERIPH_ID_WS2812 = AUDIO_ELEMENT_TYPE_PERIPH + 15, - PERIPH_ID_AW2013 = AUDIO_ELEMENT_TYPE_PERIPH + 16, - PERIPH_ID_LCD = AUDIO_ELEMENT_TYPE_PERIPH + 17 + PERIPH_ID_BUTTON = AUDIO_ELEMENT_TYPE_PERIPH + 1, + PERIPH_ID_TOUCH = AUDIO_ELEMENT_TYPE_PERIPH + 2, + PERIPH_ID_SDCARD = AUDIO_ELEMENT_TYPE_PERIPH + 3, + PERIPH_ID_WIFI = AUDIO_ELEMENT_TYPE_PERIPH + 4, + PERIPH_ID_FLASH = AUDIO_ELEMENT_TYPE_PERIPH + 5, + PERIPH_ID_AUXIN = AUDIO_ELEMENT_TYPE_PERIPH + 6, + PERIPH_ID_ADC = AUDIO_ELEMENT_TYPE_PERIPH + 7, + PERIPH_ID_CONSOLE = AUDIO_ELEMENT_TYPE_PERIPH + 8, + PERIPH_ID_BLUETOOTH = AUDIO_ELEMENT_TYPE_PERIPH + 9, + PERIPH_ID_LED = AUDIO_ELEMENT_TYPE_PERIPH + 10, + PERIPH_ID_SPIFFS = AUDIO_ELEMENT_TYPE_PERIPH + 11, + PERIPH_ID_ADC_BTN = AUDIO_ELEMENT_TYPE_PERIPH + 12, + PERIPH_ID_IS31FL3216 = AUDIO_ELEMENT_TYPE_PERIPH + 13, + PERIPH_ID_GPIO_ISR = AUDIO_ELEMENT_TYPE_PERIPH + 14, + PERIPH_ID_WS2812 = AUDIO_ELEMENT_TYPE_PERIPH + 15, + PERIPH_ID_AW2013 = AUDIO_ELEMENT_TYPE_PERIPH + 16, + PERIPH_ID_LCD = AUDIO_ELEMENT_TYPE_PERIPH + 17, + PERIPH_ID_CUSTOM_BASE = AUDIO_ELEMENT_TYPE_PERIPH + 18 } esp_periph_id_t; /** @@ -239,6 +240,21 @@ esp_err_t esp_periph_set_list_destroy(esp_periph_set_handle_t periph_set_handle) */ esp_err_t esp_periph_remove_from_set(esp_periph_set_handle_t periph_set_handle, esp_periph_handle_t periph); +/** + * @brief Allocated a peripheral ID from the periph_set. + * + * @note This function can apply for a peripheral ID to realize a customized peripheral module. + * The requested peripheral ID will not be included in `esp_periph_id_t` + * + * @param[in] periph_set_handle The esp_periph_set_handle_t instance + * @param[out] periph_id The peripheral identifier + * + * @return + * - ESP_OK + * - ESP_FAIL + */ +esp_err_t esp_periph_alloc_periph_id(esp_periph_set_handle_t periph_set_handle, int *periph_id); + /** * @brief Call this function to change periph_set waiting time. *