refactor(ble_mesh): remove LED hardware dependency from examples

(cherry picked from commit 4b7cb8cecc)

Co-authored-by: luoxu <luoxu@espressif.com>
This commit is contained in:
Luo Xu
2026-04-09 21:43:40 +08:00
parent db22058940
commit cdef3f9a83
38 changed files with 90 additions and 1141 deletions
@@ -2,14 +2,14 @@
/*
* SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2018-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <inttypes.h>
#include "driver/gpio.h"
#include "esp_log.h"
#include "iot_button.h"
@@ -17,45 +17,11 @@
#define TAG "BOARD"
#define BUTTON_IO_NUM 0
#define BUTTON_IO_NUM CONFIG_BUTTON_IO_NUM
#define BUTTON_ACTIVE_LEVEL 0
extern void example_ble_mesh_send_gen_onoff_set(void);
struct _led_state led_state[3] = {
{ LED_OFF, LED_OFF, LED_R, "red" },
{ LED_OFF, LED_OFF, LED_G, "green" },
{ LED_OFF, LED_OFF, LED_B, "blue" },
};
void board_led_operation(uint8_t pin, uint8_t onoff)
{
for (int i = 0; i < ARRAY_SIZE(led_state); i++) {
if (led_state[i].pin != pin) {
continue;
}
if (onoff == led_state[i].previous) {
ESP_LOGW(TAG, "led %s is already %s",
led_state[i].name, (onoff ? "on" : "off"));
return;
}
gpio_set_level(pin, onoff);
led_state[i].previous = onoff;
return;
}
ESP_LOGE(TAG, "LED is not found!");
}
static void board_led_init(void)
{
for (int i = 0; i < ARRAY_SIZE(led_state); i++) {
gpio_reset_pin(led_state[i].pin);
gpio_set_direction(led_state[i].pin, GPIO_MODE_OUTPUT);
gpio_set_level(led_state[i].pin, LED_OFF);
led_state[i].previous = LED_OFF;
}
}
static void button_tap_cb(void* arg)
{
ESP_LOGI(TAG, "tap cb (%s)", (char *)arg);
@@ -73,6 +39,5 @@ static void board_button_init(void)
void board_init(void)
{
board_led_init();
board_button_init();
}
@@ -2,7 +2,7 @@
/*
* SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2018-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -14,55 +14,6 @@
extern "C" {
#endif /**< __cplusplus */
#include "driver/gpio.h"
#include "esp_ble_mesh_defs.h"
#if defined(CONFIG_BLE_MESH_ESP_WROOM_32)
#define LED_R GPIO_NUM_25
#define LED_G GPIO_NUM_26
#define LED_B GPIO_NUM_27
#elif defined(CONFIG_BLE_MESH_ESP_WROVER)
#define LED_R GPIO_NUM_0
#define LED_G GPIO_NUM_2
#define LED_B GPIO_NUM_4
#elif defined(CONFIG_BLE_MESH_ESP32C3_DEV)
#define LED_R GPIO_NUM_8
#define LED_G GPIO_NUM_8
#define LED_B GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32S3_DEV)
#define LED_R GPIO_NUM_47
#define LED_G GPIO_NUM_47
#define LED_B GPIO_NUM_47
#elif defined(CONFIG_BLE_MESH_ESP32C6_DEV)
#define LED_R GPIO_NUM_8
#define LED_G GPIO_NUM_8
#define LED_B GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32C61_DEV)
#define LED_R GPIO_NUM_8
#define LED_G GPIO_NUM_8
#define LED_B GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32H2_DEV)
#define LED_R GPIO_NUM_8
#define LED_G GPIO_NUM_8
#define LED_B GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32C5_DEV)
#define LED_R GPIO_NUM_8
#define LED_G GPIO_NUM_8
#define LED_B GPIO_NUM_8
#endif
#define LED_ON 1
#define LED_OFF 0
struct _led_state {
uint8_t current;
uint8_t previous;
uint8_t pin;
char *name;
};
void board_led_operation(uint8_t pin, uint8_t onoff);
void board_init(void);
#ifdef __cplusplus
@@ -2,7 +2,7 @@
/*
* SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2018-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -38,7 +38,7 @@ static struct example_info_store {
} __attribute__((packed)) store = {
.net_idx = ESP_BLE_MESH_KEY_UNUSED,
.app_idx = ESP_BLE_MESH_KEY_UNUSED,
.onoff = LED_OFF,
.onoff = 0,
.tid = 0x0,
};
@@ -122,7 +122,6 @@ static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32
{
ESP_LOGI(TAG, "net_idx: 0x%04x, addr: 0x%04x", net_idx, addr);
ESP_LOGI(TAG, "flags: 0x%02x, iv_index: 0x%08" PRIx32, flags, iv_index);
board_led_operation(LED_G, LED_OFF);
store.net_idx = net_idx;
/* mesh_example_info_store() shall not be invoked here, because if the device
* is restarted and goes into a provisioned state, then the following events
@@ -287,8 +286,6 @@ static esp_err_t ble_mesh_init(void)
ESP_LOGI(TAG, "BLE Mesh Node initialized");
board_led_operation(LED_G, LED_ON);
return err;
}
@@ -2,55 +2,17 @@
/*
* SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2018-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include "driver/gpio.h"
#include "esp_log.h"
#include "board.h"
#define TAG "BOARD"
struct _led_state led_state[3] = {
{ LED_OFF, LED_OFF, LED_R, "red" },
{ LED_OFF, LED_OFF, LED_G, "green" },
{ LED_OFF, LED_OFF, LED_B, "blue" },
};
void board_led_operation(uint8_t pin, uint8_t onoff)
{
for (int i = 0; i < 3; i++) {
if (led_state[i].pin != pin) {
continue;
}
if (onoff == led_state[i].previous) {
ESP_LOGW(TAG, "led %s is already %s",
led_state[i].name, (onoff ? "on" : "off"));
return;
}
gpio_set_level(pin, onoff);
led_state[i].previous = onoff;
return;
}
ESP_LOGE(TAG, "LED is not found!");
}
static void board_led_init(void)
{
for (int i = 0; i < 3; i++) {
gpio_reset_pin(led_state[i].pin);
gpio_set_direction(led_state[i].pin, GPIO_MODE_OUTPUT);
gpio_set_level(led_state[i].pin, LED_OFF);
led_state[i].previous = LED_OFF;
}
}
void board_init(void)
{
board_led_init();
}
@@ -2,7 +2,7 @@
/*
* SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2018-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -12,60 +12,16 @@
#ifdef __cplusplus
extern "C" {
#endif /**< __cplusplus */
#endif
#include "driver/gpio.h"
#if defined(CONFIG_BLE_MESH_ESP_WROOM_32)
#define LED_R GPIO_NUM_25
#define LED_G GPIO_NUM_26
#define LED_B GPIO_NUM_27
#elif defined(CONFIG_BLE_MESH_ESP_WROVER)
#define LED_R GPIO_NUM_0
#define LED_G GPIO_NUM_2
#define LED_B GPIO_NUM_4
#elif defined(CONFIG_BLE_MESH_ESP32C3_DEV)
#define LED_R GPIO_NUM_8
#define LED_G GPIO_NUM_8
#define LED_B GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32S3_DEV)
#define LED_R GPIO_NUM_47
#define LED_G GPIO_NUM_47
#define LED_B GPIO_NUM_47
#elif defined(CONFIG_BLE_MESH_ESP32C6_DEV)
#define LED_R GPIO_NUM_8
#define LED_G GPIO_NUM_8
#define LED_B GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32C61_DEV)
#define LED_R GPIO_NUM_8
#define LED_G GPIO_NUM_8
#define LED_B GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32H2_DEV)
#define LED_R GPIO_NUM_8
#define LED_G GPIO_NUM_8
#define LED_B GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32C5_DEV)
#define LED_R GPIO_NUM_8
#define LED_G GPIO_NUM_8
#define LED_B GPIO_NUM_8
#endif
#define LED_ON 1
#define LED_OFF 0
struct _led_state {
uint8_t current;
uint8_t previous;
uint8_t pin;
char *name;
};
void board_led_operation(uint8_t pin, uint8_t onoff);
#define BUTTON_IO_NUM CONFIG_BUTTON_IO_NUM
void board_init(void);
#ifdef __cplusplus
}
#endif /**< __cplusplus */
#endif
#endif /* _BOARD_H_ */
@@ -2,7 +2,7 @@
/*
* SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2018-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -29,8 +29,6 @@
#define CID_ESP 0x02E5
extern struct _led_state led_state[3];
static uint8_t dev_uuid[16] = { 0xdd, 0xdd };
static esp_ble_mesh_cfg_srv_t config_server = {
@@ -119,7 +117,6 @@ static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32
{
ESP_LOGI(TAG, "net_idx: 0x%04x, addr: 0x%04x", net_idx, addr);
ESP_LOGI(TAG, "flags: 0x%02x, iv_index: 0x%08" PRIx32, flags, iv_index);
board_led_operation(LED_G, LED_OFF);
}
static void example_change_led_state(esp_ble_mesh_model_t *model,
@@ -127,24 +124,23 @@ static void example_change_led_state(esp_ble_mesh_model_t *model,
{
uint16_t primary_addr = esp_ble_mesh_get_primary_element_address();
uint8_t elem_count = esp_ble_mesh_get_element_count();
struct _led_state *led = NULL;
uint8_t i;
if (ESP_BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) {
for (i = 0; i < elem_count; i++) {
if (ctx->recv_dst == (primary_addr + i)) {
led = &led_state[i];
board_led_operation(led->pin, onoff);
ESP_LOGI(TAG, "Recv onoff message (unicast 0x%04x), element index %d, onoff %s",
ctx->recv_dst, i, onoff ? "ON" : "OFF");
}
}
} else if (ESP_BLE_MESH_ADDR_IS_GROUP(ctx->recv_dst)) {
if (esp_ble_mesh_is_model_subscribed_to_group(model, ctx->recv_dst)) {
led = &led_state[model->element->element_addr - primary_addr];
board_led_operation(led->pin, onoff);
ESP_LOGI(TAG, "Recv onoff message (group 0x%04x), element index %d, onoff %s",
ctx->recv_dst, (int)(model->element->element_addr - primary_addr), onoff ? "ON" : "OFF");
}
} else if (ctx->recv_dst == 0xFFFF) {
led = &led_state[model->element->element_addr - primary_addr];
board_led_operation(led->pin, onoff);
ESP_LOGI(TAG, "Recv onoff message (broadcast), element index %d, onoff %s",
(int)(model->element->element_addr - primary_addr), onoff ? "ON" : "OFF");
}
}
@@ -312,8 +308,6 @@ static esp_err_t ble_mesh_init(void)
ESP_LOGI(TAG, "BLE Mesh Node initialized");
board_led_operation(LED_G, LED_ON);
return err;
}