refactor(ble_mesh): remove LED hardware dependency from examples

This commit is contained in:
luoxu
2026-04-10 19:37:26 +08:00
parent 200e80503f
commit b72a0be6ea
38 changed files with 90 additions and 1141 deletions
@@ -2,67 +2,25 @@
/*
* 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 "iot_button.h"
#include "driver/gpio.h"
#include "lightbulb.h"
#include "esp_log.h"
#include "board.h"
#define TAG "BOARD"
#define BUTTON_IO_NUM CONFIG_BUTTON_IO_NUM
#define BUTTON_ACTIVE_LEVEL 0
extern uint8_t click_to_send_onoff_set;
extern void example_ble_mesh_send_gen_onoff_set(uint8_t onoff);
extern void example_ble_mesh_send_remote_provisioning_scan_start(void);
void board_led_operation(uint8_t r, uint8_t g, uint8_t b)
{
#ifdef BLE_MESH_LED_STRIP_IO
ws2812_set_rgb_channel(r, g, b);
#else
gpio_set_level(LED_R, r);
gpio_set_level(LED_G, g);
gpio_set_level(LED_B, b);
#endif
}
static void board_led_init(void)
{
#ifdef BLE_MESH_LED_STRIP_IO
lightbulb_config_t config = {
.type = DRIVER_WS2812,
.driver_conf.ws2812.led_num = 3,
.driver_conf.ws2812.ctrl_io = 8,
.capability.enable_fades = true,
.capability.fades_ms = 800,
.capability.enable_status_storage = false,
.capability.mode_mask = COLOR_MODE,
.capability.storage_cb = NULL,
.external_limit = NULL,
.gamma_conf = NULL,
.init_status.mode = WORK_COLOR,
.init_status.on = false,
.init_status.hue = 0,
.init_status.saturation = 100,
.init_status.value = 100,
};
lightbulb_init(&config);
ws2812_set_rgb_channel(LED_OFF, LED_OFF, LED_OFF);
#else
gpio_set_level(LED_R, LED_OFF);
gpio_set_level(LED_G, LED_OFF);
gpio_set_level(LED_B, LED_OFF);
#endif
}
static void button_tap_cb(void* arg)
{
static uint8_t onoff = 1;
@@ -75,7 +33,6 @@ static void button_tap_cb(void* arg)
}
}
static void board_button_init(void)
{
button_handle_t btn_handle = iot_button_create(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL);
@@ -86,6 +43,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,45 +14,6 @@
extern "C" {
#endif /**< __cplusplus */
#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 BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32S3_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_47
#elif defined(CONFIG_BLE_MESH_ESP32C6_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32C61_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32H2_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32C5_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#endif
#define BUTTON_IO_NUM GPIO_NUM_9
#ifndef BLE_MESH_LED_STRIP_IO
#define LED_ON 1
#else
#define LED_R 0
#define LED_G 1
#define LED_B 2
#define LED_ON 100
#endif
#define LED_OFF 0
void board_led_operation(uint8_t r, uint8_t g, uint8_t b);
void board_init(void);
#ifdef __cplusplus
@@ -2,7 +2,7 @@
/*
* SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2018-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -220,7 +220,7 @@ static esp_err_t prov_complete(int node_idx, const esp_ble_mesh_octet16_t uuid,
return ESP_FAIL;
}
err = example_ble_mesh_store_node_info(uuid, unicast, elem_num, LED_OFF);
err = example_ble_mesh_store_node_info(uuid, unicast, elem_num, 0);
if (err) {
ESP_LOGE(TAG, "%s: Store node info failed", __func__);
return ESP_FAIL;
@@ -249,7 +249,6 @@ static esp_err_t prov_complete(int node_idx, const esp_ble_mesh_octet16_t uuid,
static void prov_link_open(esp_ble_mesh_prov_bearer_t bearer)
{
ESP_LOGI(TAG, "%s link open", bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT");
board_led_operation(LED_OFF, LED_ON, LED_OFF);
}
static void prov_link_close(esp_ble_mesh_prov_bearer_t bearer, uint8_t reason)
@@ -634,7 +633,6 @@ static void example_ble_mesh_config_client_cb(esp_ble_mesh_cfg_client_cb_event_t
ESP_LOGI(TAG, "The Remote Provisioning Server have been provisioned, You could click button to start remote provisioning");
remote_rpr_srv_addr = addr;
}
board_led_operation(LED_OFF, LED_OFF, LED_OFF);
break;
}
default:
@@ -997,7 +995,6 @@ static void example_ble_mesh_remote_prov_client_callback(esp_ble_mesh_rpr_client
if (err) {
ESP_LOGE(TAG, "Failed to perform Remote Provisioning Client action: Start Prov");
}
board_led_operation(LED_OFF, LED_ON, LED_OFF);
break;
}
default:
@@ -2,61 +2,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 "driver/gpio.h"
#include "lightbulb.h"
#include "esp_log.h"
#include "board.h"
#define TAG "BOARD"
void board_led_operation(uint8_t r, uint8_t g, uint8_t b)
{
#ifdef BLE_MESH_LED_STRIP_IO
ws2812_set_rgb_channel(r, g, b);
#else
gpio_set_level(LED_R, r);
gpio_set_level(LED_G, g);
gpio_set_level(LED_B, b);
#endif
}
static void board_led_init(void)
{
#ifdef BLE_MESH_LED_STRIP_IO
lightbulb_config_t config = {
.type = DRIVER_WS2812,
.driver_conf.ws2812.led_num = 3,
.driver_conf.ws2812.ctrl_io = 8,
.capability.enable_fades = true,
.capability.fades_ms = 800,
.capability.enable_status_storage = false,
.capability.mode_mask = COLOR_MODE,
.capability.storage_cb = NULL,
.external_limit = NULL,
.gamma_conf = NULL,
.init_status.mode = WORK_COLOR,
.init_status.on = false,
.init_status.hue = 0,
.init_status.saturation = 100,
.init_status.value = 100,
};
lightbulb_init(&config);
ws2812_set_rgb_channel(LED_OFF, LED_OFF, LED_OFF);
#else
gpio_set_level(LED_R, LED_OFF);
gpio_set_level(LED_G, LED_OFF);
gpio_set_level(LED_B, LED_OFF);
#endif
}
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,51 +12,12 @@
#ifdef __cplusplus
extern "C" {
#endif /**< __cplusplus */
#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 BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32S3_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_47
#elif defined(CONFIG_BLE_MESH_ESP32C6_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32C61_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32H2_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32C5_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#endif
#define BUTTON_IO_NUM GPIO_NUM_9
#ifndef BLE_MESH_LED_STRIP_IO
#define LED_ON 1
#else
#define LED_R 0
#define LED_G 1
#define LED_B 2
#define LED_ON 100
#endif
#define LED_OFF 0
void board_led_operation(uint8_t r, uint8_t g, uint8_t b);
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
*/
@@ -128,28 +128,8 @@ static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32
static void example_change_led_state(esp_ble_mesh_model_t *model,
esp_ble_mesh_msg_ctx_t *ctx, uint8_t onoff)
{
uint16_t primary_addr = esp_ble_mesh_get_primary_element_address();
uint8_t elem_count = esp_ble_mesh_get_element_count();
uint8_t rgb[3] = {0};
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)) {
rgb[i] = onoff ? LED_ON : LED_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)) {
rgb[model->element->element_addr - primary_addr] = onoff ? LED_ON : LED_OFF;
}
} else if (ctx->recv_dst == 0xFFFF) {
rgb[0] = onoff ? LED_ON : LED_OFF;
rgb[1] = onoff ? LED_ON : LED_OFF;
rgb[2] = onoff ? LED_ON : LED_OFF;
}
board_led_operation(rgb[0], rgb[1], rgb[2]);
ESP_LOGI(TAG, "Recv onoff message, element addr 0x%04x, onoff %s",
ctx->recv_dst, onoff ? "ON" : "OFF");
}
static void example_handle_gen_onoff_msg(esp_ble_mesh_model_t *model,
@@ -197,7 +177,6 @@ static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event,
case ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT:
ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT, bearer %s",
param->node_prov_link_open.bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT");
board_led_operation(LED_OFF, LED_ON, LED_OFF);
break;
case ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT:
ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT, bearer %s",
@@ -280,7 +259,6 @@ static void example_ble_mesh_config_server_cb(esp_ble_mesh_cfg_server_cb_event_t
param->value.state_change.mod_app_bind.app_idx,
param->value.state_change.mod_app_bind.company_id,
param->value.state_change.mod_app_bind.model_id);
board_led_operation(LED_OFF, LED_OFF, LED_OFF);
break;
case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD:
ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD");
@@ -400,11 +378,9 @@ static void example_remote_prov_server_callback(esp_ble_mesh_rpr_server_cb_event
case ESP_BLE_MESH_RPR_SERVER_LINK_OPEN_EVT:
ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_LINK_OPEN_EVT");
print_link_open_evt(param);
board_led_operation(LED_OFF, LED_OFF, LED_ON);
break;
case ESP_BLE_MESH_RPR_SERVER_LINK_CLOSE_EVT:
ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_LINK_CLOSE_EVT");
board_led_operation(LED_OFF, LED_OFF, LED_OFF);
print_link_close_evt(param);
break;
case ESP_BLE_MESH_RPR_SERVER_PROV_COMP_EVT:
@@ -439,8 +415,6 @@ static esp_err_t ble_mesh_init(void)
ESP_LOGI(TAG, "BLE Mesh Node initialized");
board_led_operation(LED_ON, LED_OFF, LED_OFF);
return err;
}
@@ -2,61 +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 "lightbulb.h"
#include "esp_log.h"
#include "board.h"
#define TAG "BOARD"
void board_led_operation(uint8_t r, uint8_t g, uint8_t b)
{
#ifdef BLE_MESH_LED_STRIP_IO
ws2812_set_rgb_channel(r, g, b);
#else
gpio_set_level(LED_R, r);
gpio_set_level(LED_G, g);
gpio_set_level(LED_B, b);
#endif
}
static void board_led_init(void)
{
#ifdef BLE_MESH_LED_STRIP_IO
lightbulb_config_t config = {
.type = DRIVER_WS2812,
.driver_conf.ws2812.led_num = 3,
.driver_conf.ws2812.ctrl_io = 8,
.capability.enable_fades = true,
.capability.fades_ms = 800,
.capability.enable_status_storage = false,
.capability.mode_mask = COLOR_MODE,
.capability.storage_cb = NULL,
.external_limit = NULL,
.gamma_conf = NULL,
.init_status.mode = WORK_COLOR,
.init_status.on = false,
.init_status.hue = 0,
.init_status.saturation = 100,
.init_status.value = 100,
};
lightbulb_init(&config);
ws2812_set_rgb_channel(LED_OFF, LED_OFF, LED_OFF);
#else
gpio_set_level(LED_R, LED_OFF);
gpio_set_level(LED_G, LED_OFF);
gpio_set_level(LED_B, LED_OFF);
#endif
}
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,51 +12,12 @@
#ifdef __cplusplus
extern "C" {
#endif /**< __cplusplus */
#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 BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32S3_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_47
#elif defined(CONFIG_BLE_MESH_ESP32C6_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32C61_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32H2_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#elif defined(CONFIG_BLE_MESH_ESP32C5_DEV)
#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8
#endif
#define BUTTON_IO_NUM GPIO_NUM_9
#ifndef BLE_MESH_LED_STRIP_IO
#define LED_ON 1
#else
#define LED_R 0
#define LED_G 1
#define LED_B 2
#define LED_ON 100
#endif
#define LED_OFF 0
void board_led_operation(uint8_t r, uint8_t g, uint8_t b);
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
*/
@@ -122,28 +122,8 @@ static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32
static void example_change_led_state(esp_ble_mesh_model_t *model,
esp_ble_mesh_msg_ctx_t *ctx, uint8_t onoff)
{
uint16_t primary_addr = esp_ble_mesh_get_primary_element_address();
uint8_t elem_count = esp_ble_mesh_get_element_count();
uint8_t rgb[3] = {0};
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)) {
rgb[i] = onoff ? LED_ON : LED_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)) {
rgb[model->element->element_addr - primary_addr] = onoff ? LED_ON : LED_OFF;
}
} else if (ctx->recv_dst == 0xFFFF) {
rgb[0] = onoff ? LED_ON : LED_OFF;
rgb[1] = onoff ? LED_ON : LED_OFF;
rgb[2] = onoff ? LED_ON : LED_OFF;
}
board_led_operation(rgb[0], rgb[1], rgb[2]);
ESP_LOGI(TAG, "Recv onoff message, element addr 0x%04x, onoff %s",
ctx->recv_dst, onoff ? "ON" : "OFF");
}
static void example_handle_gen_onoff_msg(esp_ble_mesh_model_t *model,
@@ -191,7 +171,6 @@ static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event,
case ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT:
ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT, bearer %s",
param->node_prov_link_open.bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT");
board_led_operation(LED_OFF, LED_ON, LED_OFF);
break;
case ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT:
ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT, bearer %s",
@@ -275,7 +254,6 @@ static void example_ble_mesh_config_server_cb(esp_ble_mesh_cfg_server_cb_event_t
param->value.state_change.mod_app_bind.company_id,
param->value.state_change.mod_app_bind.model_id);
board_led_operation(LED_OFF, LED_OFF, LED_OFF);
break;
case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD:
ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD");
@@ -313,8 +291,6 @@ static esp_err_t ble_mesh_init(void)
ESP_LOGI(TAG, "BLE Mesh Node initialized");
board_led_operation(LED_ON, LED_OFF, LED_OFF);
return err;
}