initial commit

This commit is contained in:
Mikkeli Matlock
2026-02-15 02:48:59 +09:00
commit 19db125619
258 changed files with 345581 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
idf_component_register(
SRCS
"lvgl_bsp.cpp"
"ble_scan_bsp.c"
"esp_wifi_bsp.c"
PRIV_REQUIRES
esp_wifi
esp_event
nvs_flash
ui_bsp
espressif__avi_player
espressif__esp_new_jpeg
port_bsp
esp_timer
REQUIRES
bt
lvgl__lvgl
INCLUDE_DIRS
"./")

View File

@@ -0,0 +1,257 @@
#include <stdio.h>
#include "ble_scan_bsp.h"
#include "freertos/FreeRTOS.h"
#include "esp_log.h"
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#define GATTC_TAG "GATTC_DEMO"
QueueHandle_t ble_queue;
#define REMOTE_SERVICE_UUID 0x00FF
#define REMOTE_NOTIFY_CHAR_UUID 0xFF01
#define PROFILE_NUM 1
#define PROFILE_A_APP_ID 0
#define INVALID_HANDLE 0
/* Declare static functions */
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
static esp_ble_scan_params_t ble_scan_params = { //ble scan parameter Settings
.scan_type = BLE_SCAN_TYPE_ACTIVE,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
.scan_interval = 0x50,
.scan_window = 0x30,
.scan_duplicate = BLE_SCAN_DUPLICATE_ENABLE //Filter duplicate ads
};
struct gattc_profile_inst {
esp_gattc_cb_t gattc_cb;
uint16_t gattc_if;
uint16_t app_id;
uint16_t conn_id;
uint16_t service_start_handle;
uint16_t service_end_handle;
uint16_t char_handle;
esp_bd_addr_t remote_bda;
};
/* One gatt-based profile one app_id and one gattc_if, this array will store the gattc_if returned by ESP_GATTS_REG_EVT */
static struct gattc_profile_inst gl_profile_tab[PROFILE_NUM] = {
[PROFILE_A_APP_ID] = {
.gattc_cb = gattc_profile_event_handler,
.gattc_if = ESP_GATT_IF_NONE, /* Didn't get the initial value, so it's ESP GATT IF NONE */
},
};
static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) // This function is called by the esp gattc cb callback function and has the opportunity to be executed
{
//esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param;
switch (event)
{
case ESP_GATTC_REG_EVT: //This callback function, executed by the esp gattc cb callback function, passes in the event parameters
ESP_LOGI(GATTC_TAG, "REG_EVT");
//esp_err_t scan_ret = esp_ble_gap_set_scan_params(&ble_scan_params); //Set scan parameters
//if (scan_ret)
//{
// ESP_LOGE(GATTC_TAG, "set scan params error, error code = %x", scan_ret);
//}
break;
case ESP_GATTC_CFG_MTU_EVT: //This event is triggered when the GATT client successfully configures the mtu by calling esp ble gattc set mtu()
if (param->cfg_mtu.status != ESP_GATT_OK)
{
ESP_LOGE(GATTC_TAG,"config mtu failed, error status = %x", param->cfg_mtu.status);
}
ESP_LOGI(GATTC_TAG, "ESP_GATTC_CFG_MTU_EVT, Status %d, MTU %d, conn_id %d", param->cfg_mtu.status, param->cfg_mtu.mtu, param->cfg_mtu.conn_id);
break;
default:
break;
}
}
static uint8_t value = 0;
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
//uint8_t *adv_name = NULL;
//uint8_t adv_name_len = 0;
switch (event)
{
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: //This event is triggered after the esp ble gap set scan params() function is successfully invoked to set scan parameters
{
//esp_ble_gap_start_scanning(duration); //Scan time of 30 seconds
break;
}
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT: //This event is triggered after the esp ble gap start scanning() function is successfully invoked to start scanning
{
if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS) //To check whether the ESP BT is successfully started, the value must equal to ESP BT STATUS SUCCESS
{
ESP_LOGE(GATTC_TAG, "scan start failed, error status = %x", param->scan_start_cmpl.status);
break;
}
ESP_LOGI(GATTC_TAG, "scan start success");
break;
}
case ESP_GAP_BLE_SCAN_RESULT_EVT: //Enter once every scan, not wait for the scan to finish before entering
{
esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
switch (scan_result->scan_rst.search_evt) // Search event type
{
case ESP_GAP_SEARCH_INQ_RES_EVT:
////esp_log_buffer_hex(GATTC_TAG, scan_result->scan_rst.bda, 6); //Bluetooth device address adv data len is the length of the AD data for the scanned device.
//Purpose: Advertising data is the message that the device sends when it broadcasts. This data typically includes the name of the device, service UUID, manufacturer data, and so on.
//The length of the AD data is specified by adv data len Scan response data is additional information returned by the device after receiving a request from the scan device. Typically, it contains more information about the device, such as the full device name or other service information.
//Scenario: When a device (such as a phone or other BLE device) scans an AD pack, it can send a scan request to request more information. The target device will respond to this request and send a scan of the response data
////ESP_LOGI(GATTC_TAG, "searched Adv Data Len %d, Scan Response Len %d", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len);
//adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len); //NULL is returned if the name is not scanned
if(xQueueSend(ble_queue,scan_result->scan_rst.bda,0) == pdTRUE)
{
value++;
if(value == 20)
{
value = 0;
esp_ble_gap_stop_scanning(); //Stop scanning
}
}
/*if (adv_name != NULL)
{
//ESP_LOGI(GATTC_TAG, "adv_name: %s", adv_name);
//esp_ble_gap_stop_scanning(); //Stop scanning
//esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true); //Open gattc and start the connection
}*/
break;
case ESP_GAP_SEARCH_INQ_CMPL_EVT: //The event is used to notify the scanning time of the BLE device when the scanning operation is completed
value = 0;
break;
default:
break;
}
break;
}
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT: //Usually triggered after you call esp ble gap stop scanning() function to stop scanning
if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS)
{
ESP_LOGE(GATTC_TAG, "scan stop failed, error status = %x", param->scan_stop_cmpl.status);
break;
}
ESP_LOGI(GATTC_TAG, "stop scan successfully");
break;
default:
break;
}
}
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
{
/* If event is register event, store the gattc_if for each profile */
if (event == ESP_GATTC_REG_EVT) // Description The event occurred when the GATT client was registered
{
if (param->reg.status == ESP_GATT_OK) // Get running status
{
gl_profile_tab[param->reg.app_id].gattc_if = gattc_if; // param->reg.app id Get the ID, which is the gattc if generated automatically by the system
printf("gattc_if:%d\n",gattc_if);
}
else
{
ESP_LOGI(GATTC_TAG, "reg app failed, app_id %04x, status %d",param->reg.app_id,param->reg.status); // Otherwise, the corresponding error structure is displayed
return; //Returns, does not execute the following
}
}
do
{
int idx;
for (idx = 0; idx < PROFILE_NUM; idx++)
{
if (gattc_if == ESP_GATT_IF_NONE || gattc_if == gl_profile_tab[idx].gattc_if) //if the callback reports gattc if/gatts if as an ESP GATT IF NONE macro, this event does not correspond to any application
{
if (gl_profile_tab[idx].gattc_cb)
{
gl_profile_tab[idx].gattc_cb(event, gattc_if, param); //Call the callback function
}
}
}
} while (0);
}
static void ble_scan_resources_init(void)
{
ble_queue = xQueueCreate( 60 , sizeof(uint8_t) * 6);
}
void ble_scan_prepare(void)
{
ble_scan_resources_init();
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
}
void ble_stack_init(void)
{
esp_err_t ret;
//ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bluedroid_init();
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bluedroid_enable();
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_ble_gap_register_callback(esp_gap_cb);
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s gap register failed, error code = %x\n", __func__, ret);
return;
}
ret = esp_ble_gattc_register_callback(esp_gattc_cb);
if(ret)
{
ESP_LOGE(GATTC_TAG, "%s gattc register failed, error code = %x\n", __func__, ret);
return;
}
ret = esp_ble_gattc_app_register(PROFILE_A_APP_ID);
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s gattc app register failed, error code = %x\n", __func__, ret);
}
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
if (local_mtu_ret)
{
ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
}
}
void ble_scan_start(void)
{
esp_ble_gap_set_scan_params(&ble_scan_params);//set scan
esp_ble_gap_start_scanning(3); //Scan time of 3 seconds
}
void ble_stack_deinit(void)
{
if(value != 0)esp_ble_gap_stop_scanning();
esp_ble_gattc_app_unregister(gl_profile_tab[0].gattc_if);
esp_bluedroid_disable();
esp_bluedroid_deinit();
esp_bt_controller_disable();
esp_bt_controller_deinit();
}

View File

@@ -0,0 +1,31 @@
#ifndef BLE_SCAN_BSP_H
#define BLE_SCAN_BSP_H
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gattc_api.h"
#include "esp_gatt_defs.h"
#include "esp_bt_main.h"
#include "esp_gatt_common_api.h"
#include "freertos/event_groups.h"
#include "freertos/queue.h"
#ifdef __cplusplus
extern "C" {
#endif
void ble_scan_prepare(void); //初始化之前的释放内存
void ble_stack_init(void); //ble初始化
void ble_scan_start(void); //ble扫描开始
void ble_stack_deinit(void); //ble反初始化
extern QueueHandle_t ble_queue;
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,89 @@
#include <stdio.h>
#include "esp_wifi_bsp.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "string.h"
EventGroupHandle_t wifi_even_ = NULL;
esp_bsp_t user_esp_bsp;
static esp_netif_t *net = NULL;
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
static void example_scan_wifi_task(void *arg);
void espwifi_init(void)
{
memset(&user_esp_bsp,0,sizeof(esp_bsp_t));
wifi_even_ = xEventGroupCreate();
nvs_flash_init(); // Initialize default NVS storage
esp_netif_init(); // Initialize TCP/IP stack
esp_event_loop_create_default(); // Create default event loop
net = esp_netif_create_default_wifi_sta(); // Add TCP/IP stack to the default event loop
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); // Default configuration
esp_wifi_init(&cfg); // Initialize WiFi
esp_event_handler_instance_t Instance_WIFI_IP;
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &Instance_WIFI_IP);
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &Instance_WIFI_IP);
wifi_config_t wifi_config = {
.sta = {
.ssid = "PDCN",
.password = "1234567890",
},
};
esp_wifi_set_mode(WIFI_MODE_STA); // Set mode to STA
esp_wifi_set_config(WIFI_IF_STA, &wifi_config); // Configure WiFi
esp_wifi_start(); // Start WiFi
xTaskCreatePinnedToCore(example_scan_wifi_task, "example_scan_wifi_task", 3000, NULL, 2, NULL,0);
}
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
if (event_id == WIFI_EVENT_STA_START)
{
xEventGroupSetBits(wifi_even_,0x01);
}
else if (event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
char ip[25];
uint32_t pxip = event->ip_info.ip.addr;
sprintf(ip, "%d.%d.%d.%d", (uint8_t)(pxip), (uint8_t)(pxip >> 8), (uint8_t)(pxip >> 16), (uint8_t)(pxip >> 24));
ESP_LOGI("wifiSta","%s",ip);
}
else if(event_id == WIFI_EVENT_STA_DISCONNECTED)
{
ESP_LOGD("wifiSta","Disconnected");
}
}
void espwifi_deinit(void)
{
esp_wifi_stop();
esp_wifi_deinit();
esp_netif_destroy_default_wifi(net);
esp_event_loop_delete_default();
//esp_netif_deinit();
//nvs_flash_deinit();
}
static void example_scan_wifi_task(void *arg)
{
uint16_t rec = 0;
EventBits_t even = xEventGroupWaitBits(wifi_even_,0x01,pdTRUE,pdTRUE,pdMS_TO_TICKS(15000));
if( even & 0x01 ) {
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_scan_start(NULL,true));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_scan_get_ap_num(&rec));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_clear_ap_list());
}
user_esp_bsp.apNum = rec;
xEventGroupSetBits(wifi_even_,0x02);
vTaskDelete(NULL);
}

View File

@@ -0,0 +1,29 @@
#ifndef ESP_WIFI_BSP_H
#define ESP_WIFI_BSP_H
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
extern EventGroupHandle_t wifi_even_;
typedef struct
{
char _ip[25];
int8_t rssi;
int8_t apNum;
}esp_bsp_t;
extern esp_bsp_t user_esp_bsp;
#ifdef __cplusplus
extern "C" {
#endif
void espwifi_init(void);
void espwifi_deinit(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,82 @@
#include <stdio.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <esp_log.h>
#include <esp_timer.h>
#include "lvgl_bsp.h"
static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
static lv_disp_drv_t disp_drv; // contains callback functions
static SemaphoreHandle_t lvgl_mux = NULL;
static const char *TAG = "LvglPort";
static void Increase_lvgl_tick(void *arg)
{
lv_tick_inc(LVGL_TICK_PERIOD_MS);
}
bool Lvgl_lock(int timeout_ms)
{
const TickType_t timeout_ticks = (timeout_ms == -1) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
return xSemaphoreTake(lvgl_mux, timeout_ticks) == pdTRUE;
}
void Lvgl_unlock(void)
{
assert(lvgl_mux && "bsp_display_start must be called first");
xSemaphoreGive(lvgl_mux);
}
static void Lvgl_port_task(void *arg)
{
uint32_t task_delay_ms = LVGL_TASK_MAX_DELAY_MS;
for(;;)
{
if (Lvgl_lock(-1))
{
task_delay_ms = lv_timer_handler();
//Release the mutex
Lvgl_unlock();
}
if (task_delay_ms > LVGL_TASK_MAX_DELAY_MS)
{
task_delay_ms = LVGL_TASK_MAX_DELAY_MS;
} else if (task_delay_ms < LVGL_TASK_MIN_DELAY_MS)
{
task_delay_ms = LVGL_TASK_MIN_DELAY_MS;
}
vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
}
}
void Lvgl_PortInit(int width, int height,DispFlushCb flush_cb) {
lvgl_mux = xSemaphoreCreateMutex();
lv_init();
lv_color_t *buffer1 = (lv_color_t *)heap_caps_malloc(width * height * sizeof(lv_color_t) , MALLOC_CAP_SPIRAM);
assert(buffer1);
lv_color_t *buffer2 = (lv_color_t *)heap_caps_malloc(width * height * sizeof(lv_color_t) , MALLOC_CAP_SPIRAM);
assert(buffer2);
lv_disp_draw_buf_init(&disp_buf, buffer1, buffer2, width * height);
ESP_LOGI(TAG, "Register display driver to LVGL");
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = width;
disp_drv.ver_res = height;
disp_drv.flush_cb = flush_cb;
disp_drv.full_refresh = 1;
disp_drv.draw_buf = &disp_buf;
lv_disp_drv_register(&disp_drv);
ESP_LOGI(TAG, "Install LVGL tick timer");
esp_timer_create_args_t lvgl_tick_timer_args = {};
lvgl_tick_timer_args.callback = &Increase_lvgl_tick;
lvgl_tick_timer_args.name = "lvgl_tick";
esp_timer_handle_t lvgl_tick_timer = NULL;
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer,LVGL_TICK_PERIOD_MS * 1000));
xTaskCreatePinnedToCore(Lvgl_port_task, "LVGL", 8 * 1024, NULL, 5, NULL, 0);
}

View File

@@ -0,0 +1,13 @@
#pragma once
#include "lvgl.h"
#define LVGL_TICK_PERIOD_MS 5
#define LVGL_TASK_MAX_DELAY_MS 500
#define LVGL_TASK_MIN_DELAY_MS 50
typedef void (*DispFlushCb)(struct _lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p);
void Lvgl_PortInit(int width, int height,DispFlushCb flush_cb);
bool Lvgl_lock(int timeout_ms);
void Lvgl_unlock(void);