Git Product home page Git Product logo

Comments (2)

shootao avatar shootao commented on August 28, 2024

Hi @cskilbeck
I thinks the follow patch can meet you need

diff --git a/components/esp_peripherals/esp_peripherals.c b/components/esp_peripherals/esp_peripherals.c
index 5e141123..b6cb75c4 100644
--- a/components/esp_peripherals/esp_peripherals.c
+++ b/components/esp_peripherals/esp_peripherals.c
@@ -35,6 +35,7 @@

 static const char *TAG = "ESP_PERIPH";

+#define DEFAULT_CUS_PERIPH_ID_MAX_NUM      (5)
 #define DEFAULT_ESP_PERIPH_WAIT_TICK       (10/portTICK_RATE_MS)

 struct esp_periph {
@@ -62,6 +63,7 @@ typedef struct esp_periph_sets {
     bool                                            ext_stack;
     bool                                            run;
     esp_periph_event_t                              event_handle;
+    int                                             periph_id[DEFAULT_CUS_PERIPH_ID_MAX_NUM];
     STAILQ_HEAD(esp_periph_list_item, esp_periph)   periph_list;
 } esp_periph_set_t;

@@ -199,6 +201,24 @@ esp_err_t esp_periph_remove_from_set(esp_periph_set_handle_t periph_set_handle,
     return ESP_OK;
 }

+esp_err_t esp_periph_allocated_periph_id(esp_periph_set_handle_t periph_set_handle, int *periph_id)
+{
+    if (periph_set_handle == NULL) {
+        AUDIO_ERROR(TAG, "Peripherals have not been initialized");
+        return ESP_FAIL;
+    }
+    for (int i = 0; i < DEFAULT_CUS_PERIPH_ID_MAX_NUM; i++) {
+        if (periph_set_handle->periph_id[i] == 0) {
+            *periph_id = i + PERIPH_ID_MAX;
+            periph_set_handle->periph_id[i] = *periph_id;
+            return ESP_OK;
+        }
+    }
+    ESP_LOGE(TAG, "Have no enough periph id space");
+    *periph_id = -1;
+    return ESP_FAIL;
+}
+
 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 0ea9dbad..ed1fb3a5 100644
--- a/components/esp_peripherals/include/esp_peripherals.h
+++ b/components/esp_peripherals/include/esp_peripherals.h
@@ -54,7 +54,8 @@ typedef enum {
     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_LCD        = AUDIO_ELEMENT_TYPE_PERIPH + 17,
+    PERIPH_ID_MAX        = AUDIO_ELEMENT_TYPE_PERIPH + 18
 } esp_periph_id_t;

 /**
@@ -239,6 +240,20 @@ 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 is used to get a peripheral id from the periph_set, and this periph id not include in the esp_periph_id_t
+ *
+ * @param      periph_set_handle    The esp_periph_set_handle_t instance
+ * @param      periph_id            The peripheral identifier
+ *
+ * @return
+ *     - ESP_OK
+ *     - ESP_FAIL
+ */
+esp_err_t esp_periph_allocated_periph_id(esp_periph_set_handle_t periph_set_handle, int *periph_id);
+
 /**
  * @brief      Call this function to change periph_set waiting time.
  *

from esp-adf.

cskilbeck avatar cskilbeck commented on August 28, 2024

Looks good, thanks!

from esp-adf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.