refactor(ble_mesh): remove LED hardware dependency from examples

This commit is contained in:
luoxu
2026-04-08 15:08:21 +08:00
parent 6ab1721056
commit 4b7cb8cecc
38 changed files with 90 additions and 1141 deletions
@@ -149,7 +149,7 @@ static void provisioner_prov_complete(int node_index, const uint8_t uuid[16], ui
/* Sets node info */
err = example_store_node_info(uuid, unicast_addr, elem_num, prov_info.net_idx,
prov_info.app_idx, LED_OFF);
prov_info.app_idx, 0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: Failed to set node info", __func__);
return;
@@ -1,66 +1,18 @@
/*
* 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 "board.h"
#include "ble_mesh_fast_prov_common.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_output_number(esp_ble_mesh_output_action_t action, uint32_t number)
{
ESP_LOGI(TAG, "Board output number %" PRIu32, number);
}
void board_prov_complete(void)
{
board_led_operation(LED_B, LED_OFF);
}
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;
}
}
esp_err_t board_init(void)
{
board_led_init();
return ESP_OK;
}
@@ -1,6 +1,6 @@
/*
* 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,56 +12,7 @@
extern "C" {
#endif /**< __cplusplus */
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_ble_mesh_defs.h"
#ifdef 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
struct _led_state {
uint8_t current;
uint8_t previous;
uint8_t pin;
char *name;
};
void board_output_number(esp_ble_mesh_output_action_t action, uint32_t number);
void board_prov_complete(void);
void board_led_operation(uint8_t pin, uint8_t onoff);
#include "esp_err.h"
esp_err_t board_init(void);
@@ -1,6 +1,6 @@
/*
* 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
*/
@@ -28,7 +28,6 @@
#define TAG "EXAMPLE"
extern struct _led_state led_state[3];
extern struct k_delayed_work send_self_prov_node_addr_timer;
extern bt_mesh_atomic_t fast_prov_cli_flags;
@@ -155,9 +154,7 @@ static esp_ble_mesh_prov_t prov = {
static void example_change_led_state(uint8_t onoff)
{
struct _led_state *led = &led_state[1];
board_led_operation(led->pin, onoff);
ESP_LOGI(TAG, "Recv GenOnOff message, state: %s", onoff ? "ON" : "OFF");
/* When the node receives the first Generic OnOff Get/Set/Set Unack message, it will
* start the timer used to disable fast provisioning functionality.
@@ -177,7 +174,7 @@ static void node_prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, u
{
ESP_LOGI(TAG, "net_idx: 0x%04x, unicast_addr: 0x%04x", net_idx, addr);
ESP_LOGI(TAG, "flags: 0x%02x, iv_index: 0x%08" PRIx32, flags, iv_index);
board_prov_complete();
ESP_LOGI(TAG, "Provisioning complete");
/* Updates the net_idx used by Fast Prov Server model, and it can also
* be updated if the Fast Prov Info Set message contains a valid one.
*/
@@ -213,7 +210,7 @@ static void provisioner_prov_complete(int node_idx, const uint8_t uuid[16], uint
/* Sets node info */
err = example_store_node_info(uuid, unicast_addr, element_num, net_idx,
fast_prov_server.app_idx, LED_OFF);
fast_prov_server.app_idx, 0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: Failed to set node info", __func__);
return;
@@ -522,7 +519,7 @@ static void example_ble_mesh_custom_model_cb(esp_ble_mesh_model_cb_event_t event
param->client_send_timeout.model,
param->client_send_timeout.ctx);
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: Faield to resend fast prov client message", __func__);
ESP_LOGE(TAG, "%s: Failed to resend fast prov client message", __func__);
return;
}
break;
@@ -743,8 +740,6 @@ static esp_err_t ble_mesh_init(void)
ESP_LOGI(TAG, "BLE Mesh Fast Prov Node initialized");
board_led_operation(LED_B, LED_ON);
return ESP_OK;
}