Files
MeshCore/src/helpers/esp32/SerialBLEInterface.h
T
shade 8d41f01ea5 версия 0.2.1beta: перенос строк, кривая батареи, RSSI на V4, быстрое BLE, отложенная запись на nRF52
Длинные сообщения переносятся по словам (базовый DisplayDriver + SSD1306/SH1106/ST7735/ST7789LCD/GxEPD).
Процент батареи по кривой разряда Li-ion вместо линейной формулы (обе формулы UI унифицированы).
RSSI_OFFSET для Heltec V4 (компенсация усиления LNA GC1109).
BLE: MTU 512, 2M PHY, интервал 15 мс, GAP-вызовы вне колбэка (фриз ~10 с), fast mode с форвардером и авто-возвратом в экономный режим.
nRF52: отложенная запись prefs/контактов/каналов, пока подключено приложение; reboot/powerOff через MyMesh (в т.ч. из UITask::shutdown).
E-ink: периодический полный refresh (EINK_MAX_PARTIAL_REFRESH=60), квадратные иконки в drawXbm.
Платы: таблица разделов partitions/esp32s3_16mb.csv для Station G2 / G3 / T-Beam 1W (spiffs на прежних адресах).
Ядро: num_channels в setChannel, guard перед data[len]=0, Packet::_rssi, uf2conv на пустой строке, предупреждение в build.sh.
2026-10-16 08:45:33 +03:00

111 lines
3.6 KiB
C++

#pragma once
#include "../BaseSerialInterface.h"
#include <esp_gap_ble_api.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
class SerialBLEInterface : public BaseSerialInterface, BLESecurityCallbacks, BLEServerCallbacks, BLECharacteristicCallbacks {
BLEServer *pServer;
BLEService *pService;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected;
bool oldDeviceConnected;
bool _isEnabled;
uint16_t last_conn_id;
uint32_t _pin_code;
unsigned long _last_write;
unsigned long adv_restart_time;
unsigned long _conn_params_update_time; // 0 = no pending connection parameter update
unsigned long _phy_update_time; // 0 = no pending PHY update (staggered after conn params)
unsigned long _mode_change_time; // 0 = no pending fast/idle mode change
bool _fast_mode; // true = sync interval, false = idle interval
esp_bd_addr_t _pending_conn_bda; // peer address the pending updates are meant for
struct Frame {
uint8_t len;
uint8_t buf[MAX_FRAME_SIZE];
};
#define FRAME_QUEUE_SIZE 4
StaticQueue_t recv_queue_state;
uint8_t recv_queue_storage[FRAME_QUEUE_SIZE * sizeof(Frame)];
QueueHandle_t recv_queue;
int send_queue_len;
Frame send_queue[FRAME_QUEUE_SIZE];
void clearBuffers();
protected:
// BLESecurityCallbacks methods
uint32_t onPassKeyRequest() override;
void onPassKeyNotify(uint32_t pass_key) override;
bool onConfirmPIN(uint32_t pass_key) override;
bool onSecurityRequest() override;
void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl) override;
// BLEServerCallbacks methods
void onConnect(BLEServer* pServer) override;
void onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param) override;
void onMtuChanged(BLEServer* pServer, esp_ble_gatts_cb_param_t* param) override;
void onDisconnect(BLEServer* pServer) override;
// BLECharacteristicCallbacks methods
void onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) override;
public:
SerialBLEInterface() {
pServer = NULL;
pService = NULL;
deviceConnected = false;
oldDeviceConnected = false;
adv_restart_time = 0;
_isEnabled = false;
_last_write = 0;
last_conn_id = 0;
recv_queue = xQueueCreateStatic(
FRAME_QUEUE_SIZE, sizeof(Frame), recv_queue_storage, &recv_queue_state
);
send_queue_len = 0;
_conn_params_update_time = 0;
_phy_update_time = 0;
_mode_change_time = 0;
_fast_mode = true;
memset(_pending_conn_bda, 0, sizeof(_pending_conn_bda));
}
/**
* init the BLE interface.
* @param prefix a prefix for the device name
* @param name IN/OUT - a name for the device (combined with prefix). If "@@MAC", is modified and returned
* @param pin_code the BLE security pin
*/
void begin(const char* prefix, char* name, uint32_t pin_code);
// BaseSerialInterface methods
void enable() override;
void disable() override;
bool isEnabled() const override { return _isEnabled; }
bool isConnected() const override;
void setFastMode(bool fast) override;
bool isWriteBusy() const override;
size_t writeFrame(const uint8_t src[], size_t len) override;
size_t checkRecvFrame(uint8_t dest[]) override;
};
#if BLE_DEBUG_LOGGING && ARDUINO
#include <Arduino.h>
#define BLE_DEBUG_PRINT(F, ...) Serial.printf("BLE: " F, ##__VA_ARGS__)
#define BLE_DEBUG_PRINTLN(F, ...) Serial.printf("BLE: " F "\n", ##__VA_ARGS__)
#else
#define BLE_DEBUG_PRINT(...) {}
#define BLE_DEBUG_PRINTLN(...) {}
#endif