mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-02 11:10:54 +03:00
refactor(ble_mesh): remove LED hardware dependency from examples
This commit is contained in:
@@ -2,20 +2,17 @@
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "iot_button.h"
|
||||
#include "board.h"
|
||||
#include "esp_timer.h"
|
||||
#include "lightbulb.h"
|
||||
|
||||
#define TAG "BOARD"
|
||||
|
||||
@@ -24,59 +21,6 @@
|
||||
extern void example_ble_mesh_start_example_configuration(void);
|
||||
extern void example_ble_mesh_send_gen_onoff_set(bool by_df);
|
||||
extern uint8_t in_configuration_phase;
|
||||
esp_timer_handle_t led_timer_hdl;
|
||||
|
||||
void board_led_operation(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
ws2812_set_rgb_channel(r, g, b);
|
||||
}
|
||||
|
||||
static void led_timer_callback(void* arg)
|
||||
{
|
||||
board_led_operation(0,0,0);
|
||||
}
|
||||
|
||||
static void esp_led_timer_init(void)
|
||||
{
|
||||
const esp_timer_create_args_t led_timer_args = {
|
||||
.callback = &led_timer_callback,
|
||||
.arg = NULL,
|
||||
.name = "led timer",
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(esp_timer_create(&led_timer_args, &led_timer_hdl));
|
||||
}
|
||||
|
||||
void board_led_operation_auto_close(uint8_t r, uint8_t g, uint8_t b, uint32_t ms)
|
||||
{
|
||||
esp_timer_stop(led_timer_hdl);
|
||||
board_led_operation(r,g,b);
|
||||
esp_timer_start_once(led_timer_hdl, ms * 1000);
|
||||
}
|
||||
|
||||
|
||||
static void board_led_init(void)
|
||||
{
|
||||
lightbulb_config_t config = {
|
||||
.type = DRIVER_WS2812,
|
||||
.driver_conf.ws2812.led_num = 3,
|
||||
.driver_conf.ws2812.ctrl_io = 8,
|
||||
.capability.enable_fade = true,
|
||||
.capability.fade_time_ms = 800,
|
||||
.capability.enable_status_storage = false,
|
||||
.capability.led_beads = LED_BEADS_3CH_RGB,
|
||||
.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);
|
||||
esp_led_timer_init();
|
||||
}
|
||||
|
||||
static void button_tap_cb(void* arg)
|
||||
{
|
||||
@@ -100,6 +44,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
|
||||
*/
|
||||
@@ -15,49 +15,8 @@ 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 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 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 BLE_MESH_LED_STRIP_IO GPIO_NUM_8
|
||||
#endif
|
||||
|
||||
#define LED_ON 1
|
||||
#define LED_OFF 0
|
||||
|
||||
#define BUTTON_IO_NUM GPIO_NUM_9
|
||||
|
||||
struct _led_state {
|
||||
uint8_t current;
|
||||
uint8_t previous;
|
||||
uint8_t pin;
|
||||
char *name;
|
||||
};
|
||||
|
||||
void board_led_operation(uint8_t r, uint8_t g, uint8_t b);
|
||||
#define BUTTON_IO_NUM CONFIG_BUTTON_IO_NUM
|
||||
|
||||
void board_init(void);
|
||||
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
@@ -338,7 +338,7 @@ void example_ble_mesh_send_gen_onoff_set(bool by_df)
|
||||
}
|
||||
|
||||
node->onoff = !node->onoff;
|
||||
board_led_operation(0, 100, 0);
|
||||
ESP_LOGI(TAG, "GenOnOff Set sent to node 0x%04x", node->unicast);
|
||||
}
|
||||
|
||||
void example_ble_mesh_start_example_configuration(void)
|
||||
@@ -388,7 +388,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;
|
||||
@@ -438,7 +438,7 @@ static void recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[BD_ADDR_LEN],
|
||||
ESP_LOGE(TAG, "%s: Add unprovisioned device into queue failed", __func__);
|
||||
}
|
||||
|
||||
board_led_operation(100, 100, 100);
|
||||
ESP_LOGI(TAG, "Unprovisioned device added to queue, starting provisioning");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -468,7 +468,6 @@ static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event,
|
||||
prov_complete(param->provisioner_prov_complete.node_idx, param->provisioner_prov_complete.device_uuid,
|
||||
param->provisioner_prov_complete.unicast_addr, param->provisioner_prov_complete.element_num,
|
||||
param->provisioner_prov_complete.netkey_idx);
|
||||
board_led_operation(0, 0, 0);
|
||||
break;
|
||||
case ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT:
|
||||
ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT, err_code %d", param->provisioner_add_unprov_dev_comp.err_code);
|
||||
@@ -694,7 +693,6 @@ static void example_ble_mesh_generic_client_cb(esp_ble_mesh_generic_client_cb_ev
|
||||
node->onoff = param->status_cb.onoff_status.present_onoff;
|
||||
ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET onoff: 0x%02x", node->onoff);
|
||||
node->onoff = !node->onoff;
|
||||
board_led_operation(0,0,0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -876,7 +874,6 @@ static esp_err_t ble_mesh_init(void)
|
||||
example_ble_mesh_store_node_info(dev_uuid, 0x01, 1, 0);
|
||||
|
||||
ESP_LOGI(TAG, "BLE Mesh Provisioner initialized");
|
||||
board_led_operation(0,0,0);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,79 +2,20 @@
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include "board.h"
|
||||
#include "esp_timer.h"
|
||||
#include "lightbulb.h"
|
||||
#include "iot_button.h"
|
||||
|
||||
#include "mesh/adapter.h"
|
||||
#include "board.h"
|
||||
|
||||
#define TAG "BOARD"
|
||||
#define BUTTON_ACTIVE_LEVEL 0
|
||||
|
||||
esp_timer_handle_t led_timer_hdl;
|
||||
|
||||
void board_led_operation(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
ws2812_set_rgb_channel(r, g, b);
|
||||
}
|
||||
|
||||
static void led_timer_callback(void* arg)
|
||||
{
|
||||
board_led_operation(0,0,0);
|
||||
}
|
||||
|
||||
static void esp_led_timer_init(void)
|
||||
{
|
||||
const esp_timer_create_args_t led_timer_args = {
|
||||
.callback = &led_timer_callback,
|
||||
.arg = NULL,
|
||||
.name = "led timer",
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(esp_timer_create(&led_timer_args, &led_timer_hdl));
|
||||
}
|
||||
|
||||
void board_led_operation_auto_close(uint8_t r, uint8_t g, uint8_t b, uint32_t ms)
|
||||
{
|
||||
esp_timer_stop(led_timer_hdl);
|
||||
board_led_operation(r,g,b);
|
||||
esp_timer_start_once(led_timer_hdl, ms * 1000);
|
||||
}
|
||||
|
||||
static void board_led_init(void)
|
||||
{
|
||||
lightbulb_config_t config = {
|
||||
.type = DRIVER_WS2812,
|
||||
.driver_conf.ws2812.led_num = 3,
|
||||
.driver_conf.ws2812.ctrl_io = 8,
|
||||
.capability.enable_fade = true,
|
||||
.capability.fade_time_ms = 800,
|
||||
.capability.enable_status_storage = false,
|
||||
.capability.led_beads = LED_BEADS_3CH_RGB,
|
||||
.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);
|
||||
esp_led_timer_init();
|
||||
}
|
||||
|
||||
static void button_tap_cb(void* arg)
|
||||
{
|
||||
ESP_LOGI(TAG, "tap cb (%s)", (char *)arg);
|
||||
@@ -90,6 +31,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
|
||||
*/
|
||||
@@ -16,40 +16,8 @@ extern "C" {
|
||||
|
||||
#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 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 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 BLE_MESH_LED_STRIP_IO GPIO_NUM_8
|
||||
#endif
|
||||
#define BUTTON_IO_NUM CONFIG_BUTTON_IO_NUM
|
||||
|
||||
#define LED_ON 1
|
||||
#define LED_OFF 0
|
||||
#define BUTTON_IO_NUM GPIO_NUM_9
|
||||
|
||||
void board_led_operation(uint8_t r, uint8_t g, uint8_t b);
|
||||
void board_led_operation_auto_close(uint8_t r, uint8_t g, uint8_t b, uint32_t ms);
|
||||
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
|
||||
*/
|
||||
@@ -149,7 +149,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);
|
||||
esp_ble_mesh_node_add_local_app_key(app_key, net_idx, APP_KEY_IDX);
|
||||
board_led_operation(0, 0,0);
|
||||
}
|
||||
|
||||
static void example_change_led_state(esp_ble_mesh_model_t *model,
|
||||
@@ -164,11 +163,9 @@ static void example_change_led_state(esp_ble_mesh_model_t *model,
|
||||
if (ctx->recv_dst == (primary_addr + i)) {
|
||||
if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) {
|
||||
if (ctx->recv_cred == ESP_BLE_MESH_DIRECTED_CRED) {
|
||||
// Receive a message send by directed forwarding.
|
||||
board_led_operation_auto_close(0,0,100,2000);
|
||||
ESP_LOGI(TAG, "Recv OnOff %s via directed forwarding", onoff ? "ON" : "OFF");
|
||||
} else {
|
||||
// Receive a message send by flooding.
|
||||
board_led_operation_auto_close(100,0,0,2000);
|
||||
ESP_LOGI(TAG, "Recv OnOff %s via flooding", onoff ? "ON" : "OFF");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,22 +174,18 @@ static void example_change_led_state(esp_ble_mesh_model_t *model,
|
||||
if (esp_ble_mesh_is_model_subscribed_to_group(model, ctx->recv_dst)) {
|
||||
if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) {
|
||||
if (ctx->recv_cred == ESP_BLE_MESH_DIRECTED_CRED) {
|
||||
/* Receive a message send by directed forwarding. */
|
||||
board_led_operation_auto_close(0,0,100,2000);
|
||||
ESP_LOGI(TAG, "Recv OnOff %s via directed forwarding", onoff ? "ON" : "OFF");
|
||||
} else {
|
||||
/* Receive a message send by flooding. */
|
||||
board_led_operation_auto_close(100,0,0,2000);
|
||||
ESP_LOGI(TAG, "Recv OnOff %s via flooding", onoff ? "ON" : "OFF");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (ctx->recv_dst == 0xFFFF) {
|
||||
if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) {
|
||||
if (ctx->recv_cred == ESP_BLE_MESH_DIRECTED_CRED) {
|
||||
/* Receive a message send by directed forwarding. */
|
||||
board_led_operation_auto_close(0,0,100,2000);
|
||||
ESP_LOGI(TAG, "Recv OnOff %s via directed forwarding", onoff ? "ON" : "OFF");
|
||||
} else {
|
||||
/* Receive a message send by flooding. */
|
||||
board_led_operation_auto_close(100,0,0,2000);
|
||||
ESP_LOGI(TAG, "Recv OnOff %s via flooding", onoff ? "ON" : "OFF");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -279,7 +272,7 @@ static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event,
|
||||
}
|
||||
}
|
||||
}
|
||||
board_led_operation_auto_close(50,50,50, 1000);
|
||||
ESP_LOGI(TAG, "AppKey bound to local model successfully");
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -380,7 +373,6 @@ static void example_ble_mesh_directed_forwarding_server_cb(esp_ble_mesh_df_serve
|
||||
memcpy(&path_origin, &change.df_table_info.df_table_entry_add_remove.path_origin, sizeof(path_origin));
|
||||
memcpy(&path_target, &change.df_table_info.df_table_entry_add_remove.path_target, sizeof(path_target));
|
||||
ESP_LOGI(TAG, "Established a path from 0x%04x to 0x%04x", path_origin.range_start, path_target.range_start);
|
||||
// board_led_operation(0, 0, 100);
|
||||
|
||||
}
|
||||
break;
|
||||
@@ -388,7 +380,6 @@ static void example_ble_mesh_directed_forwarding_server_cb(esp_ble_mesh_df_serve
|
||||
memcpy(&path_origin, &change.df_table_info.df_table_entry_add_remove.path_origin, sizeof(path_origin));
|
||||
memcpy(&path_target, &change.df_table_info.df_table_entry_add_remove.path_target, sizeof(path_target));
|
||||
ESP_LOGI(TAG, "Remove a path from 0x%04x to 0x%04x", path_origin.range_start, path_target.range_start);
|
||||
board_led_operation(0, 0, 0);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -467,8 +458,6 @@ static esp_err_t ble_mesh_init(void)
|
||||
|
||||
ESP_LOGI(TAG, "BLE Mesh Node initialized");
|
||||
|
||||
board_led_operation(0, 0, 0);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user