BLE_matrix_button/src/main.c
2025-12-08 20:08:32 +01:00

102 lines
2.1 KiB
C
Executable File

#include <esp_log.h>
#include <nvs_flash.h>
#include <freertos/FreeRTOSConfig.h>
/* BLE */
#include <nimble/nimble_port.h>
#include <nimble/nimble_port_freertos.h>
/* Pas bien clair encore */
#include <host/ble_hs.h> // Host Event
#include <services/gap/ble_svc_gap.h> // Device macros/functions
#include "utils.h"
#include "hid_keyboard.h"
#include "ble_stream_deck.h"
#include "gatt_server.h"
void ble_store_config_init(void);
static uint8_t blehr_addr_type;
void app_main(void)
{
int result;
setbuf(stdout, NULL);
result = init_nvs();
ESP_ERROR_CHECK(result);
result = nimble_port_init();
ESP_ERROR_CHECK(result);
/* NimBLE host config */
ble_hs_cfg.sync_cb = on_sync;
ble_hs_cfg.reset_cb = on_reset;
ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_NO_IO;
result = ble_att_set_preferred_mtu(23);
ESP_ERROR_CHECK(result);
result = init_gatt_server();
ESP_ERROR_CHECK(result);
result = init_keyboard_service();
ESP_ERROR_CHECK(result);
result = ble_svc_gap_device_name_set(GAP_DEVICE_NAME);
ESP_ERROR_CHECK(result);
result = ble_svc_gap_device_appearance_set(GAP_ADV_APPAREANCE);
ESP_ERROR_CHECK(result);
ble_store_config_init();
nimble_port_freertos_init(host_task);
//ble_gatts_show_local();
}
int init_nvs()
{
int result;
/* Init NVS */
result = nvs_flash_init();
if (result == ESP_ERR_NVS_NO_FREE_PAGES || result == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
result = nvs_flash_init();
}
return result;
}
void host_task(void *param)
{
ESP_LOGI(TAG, "BLE Host Task Started");
nimble_port_run();
nimble_port_freertos_deinit();
}
void on_sync(void)
{
int rc;
rc = ble_hs_id_infer_auto(0, &blehr_addr_type);
assert(rc == 0);
uint8_t addr_val[7] = {0};
rc = ble_hs_id_copy_addr(blehr_addr_type, addr_val, NULL);
ESP_LOGI(TAG, "Device Address: ");
print_uint8(addr_val);
ESP_LOGI(TAG, "\n");
/* Begin advertising */
gatt_advertise();
}
void on_reset(int reason)
{
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
}