Skip to content

Commit

Permalink
esp_peripherals: Add an API for alloc periph ID
Browse files Browse the repository at this point in the history
Closes #1201
  • Loading branch information
xutao committed Jul 25, 2024
1 parent faabc34 commit 08c0edb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
12 changes: 12 additions & 0 deletions components/esp_peripherals/esp_peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
50 changes: 33 additions & 17 deletions components/esp_peripherals/include/esp_peripherals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 08c0edb

Please sign in to comment.