@@ -51,6 +51,9 @@ BLECharacteristic *pTxCharacteristic;
|
||||
BLECharacteristic *pRxCharacteristic;
|
||||
uint8_t txValue = 0;
|
||||
|
||||
BLECharacteristic *pBatteryLevelCharacteristic;
|
||||
BLECharacteristic *pBatteryPowerStateCharacteristic;
|
||||
|
||||
char *gadgetbridge_msg = NULL;
|
||||
uint32_t gadgetbridge_msg_size = 0;
|
||||
|
||||
@@ -261,6 +264,49 @@ void blectl_setup( void ) {
|
||||
|
||||
// Start advertising
|
||||
pServer->getAdvertising()->addServiceUUID( pService->getUUID() );
|
||||
|
||||
|
||||
// Create device information service
|
||||
BLEService *pDeviceInformationService = pServer->createService(DEVICE_INFORMATION_SERVICE_UUID);
|
||||
|
||||
// Create manufacturer name string Characteristic -
|
||||
BLECharacteristic* pManufacturerNameStringCharacteristic = pDeviceInformationService->createCharacteristic( MANUFACTURER_NAME_STRING_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ );
|
||||
pManufacturerNameStringCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
|
||||
pManufacturerNameStringCharacteristic->addDescriptor( new BLE2902() );
|
||||
pManufacturerNameStringCharacteristic->setValue("Lily Go");
|
||||
|
||||
// Create manufacturer name string Characteristic -
|
||||
BLECharacteristic* pFirmwareRevisionStringCharacteristic = pDeviceInformationService->createCharacteristic( FIRMWARE_REVISION_STRING_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ );
|
||||
pFirmwareRevisionStringCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
|
||||
pFirmwareRevisionStringCharacteristic->addDescriptor( new BLE2902() );
|
||||
pFirmwareRevisionStringCharacteristic->setValue(__FIRMWARE__);
|
||||
|
||||
// Start battery service
|
||||
pDeviceInformationService->start();
|
||||
|
||||
// Start advertising battery service
|
||||
pServer->getAdvertising()->addServiceUUID( pDeviceInformationService->getUUID() );
|
||||
|
||||
|
||||
// Create battery service
|
||||
BLEService *pBatteryService = pServer->createService(BATTERY_SERVICE_UUID);
|
||||
|
||||
// Create a BLE battery service, batttery level Characteristic -
|
||||
pBatteryLevelCharacteristic = pBatteryService->createCharacteristic( BATTERY_LEVEL_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY );
|
||||
pBatteryLevelCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
|
||||
pBatteryLevelCharacteristic->addDescriptor( new BLEDescriptor(BATTERY_LEVEL_DESCRIPTOR_UUID) );
|
||||
pBatteryLevelCharacteristic->addDescriptor( new BLE2902() );
|
||||
|
||||
pBatteryPowerStateCharacteristic = pBatteryService->createCharacteristic( BATTERY_POWER_STATE_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY );
|
||||
pBatteryPowerStateCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
|
||||
pBatteryPowerStateCharacteristic->addDescriptor( new BLE2902() );
|
||||
|
||||
// Start battery service
|
||||
pBatteryService->start();
|
||||
|
||||
// Start advertising battery service
|
||||
pServer->getAdvertising()->addServiceUUID( pBatteryService->getUUID() );
|
||||
|
||||
// Slow advertising interval for battery life
|
||||
// The maximum 0x4000 interval of ~16 sec was too slow, I could not reliably connect
|
||||
pServer->getAdvertising()->setMinInterval( 100 );
|
||||
@@ -433,4 +479,22 @@ void blectl_read_config( void ) {
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void blectl_update_battery( int32_t percent, bool charging, bool plug )
|
||||
{
|
||||
uint8_t level = (uint8_t)percent;
|
||||
if (level > 100) level = 100;
|
||||
|
||||
pBatteryLevelCharacteristic->setValue(&level, 1);
|
||||
pBatteryLevelCharacteristic->notify();
|
||||
|
||||
uint8_t batteryPowerState = BATTERY_POWER_STATE_BATTERY_PRESENT |
|
||||
(plug ? BATTERY_POWER_STATE_DISCHARGE_NOT_DISCHARING : BATTERY_POWER_STATE_DISCHARGE_DISCHARING) |
|
||||
(charging? BATTERY_POWER_STATE_CHARGE_CHARING : BATTERY_POWER_STATE_CHARGE_NOT_CHARING) |
|
||||
(percent > 10 ? BATTERY_POWER_STATE_LEVEL_GOOD : BATTERY_POWER_STATE_LEVEL_CRITICALLY_LOW );
|
||||
pBatteryPowerStateCharacteristic->setValue(&batteryPowerState, 1);
|
||||
pBatteryPowerStateCharacteristic->notify();
|
||||
}
|
||||
@@ -30,6 +30,39 @@
|
||||
#define CHARACTERISTIC_UUID_RX BLEUUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
|
||||
#define CHARACTERISTIC_UUID_TX BLEUUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")
|
||||
|
||||
|
||||
#define DEVICE_INFORMATION_SERVICE_UUID BLEUUID((uint16_t)0x180A) // Device Information server UUID
|
||||
#define MANUFACTURER_NAME_STRING_CHARACTERISTIC_UUID BLEUUID((uint16_t)0x2A29) // Device Information - manufacturer name string UUID
|
||||
#define FIRMWARE_REVISION_STRING_CHARACTERISTIC_UUID BLEUUID((uint16_t)0x2A26) // Device Information - firmware revision UUID
|
||||
|
||||
#define BATTERY_SERVICE_UUID BLEUUID((uint16_t)0x180F) // Battery service UUID
|
||||
#define BATTERY_LEVEL_CHARACTERISTIC_UUID BLEUUID((uint16_t)0x2A19) // battery level characteristic UUID
|
||||
#define BATTERY_LEVEL_DESCRIPTOR_UUID BLEUUID((uint16_t)0x2901) // battery level descriptor UUID
|
||||
#define BATTERY_POWER_STATE_CHARACTERISTIC_UUID BLEUUID((uint16_t)0x2A1A) // battery power state characteristic UUID
|
||||
|
||||
|
||||
#define BATTERY_POWER_STATE_BATTERY_UNKNOWN 0x0
|
||||
#define BATTERY_POWER_STATE_BATTERY_NOT_SUPPORTED 0x1
|
||||
#define BATTERY_POWER_STATE_BATTERY_NOT_PRESENT 0x2
|
||||
#define BATTERY_POWER_STATE_BATTERY_PRESENT 0x3
|
||||
|
||||
#define BATTERY_POWER_STATE_DISCHARGE_UNKNOWN 0x0
|
||||
#define BATTERY_POWER_STATE_DISCHARGE_NOT_SUPPORTED 0x4
|
||||
#define BATTERY_POWER_STATE_DISCHARGE_NOT_DISCHARING 0x8
|
||||
#define BATTERY_POWER_STATE_DISCHARGE_DISCHARING 0xc
|
||||
|
||||
#define BATTERY_POWER_STATE_CHARGE_UNKNOWN 0x0
|
||||
#define BATTERY_POWER_STATE_CHARGE_NOT_CHARGEABLE 0x10
|
||||
#define BATTERY_POWER_STATE_CHARGE_NOT_CHARING 0x20
|
||||
#define BATTERY_POWER_STATE_CHARGE_CHARING 0x30
|
||||
|
||||
#define BATTERY_POWER_STATE_LEVEL_UNKNOWN 0x0
|
||||
#define BATTERY_POWER_STATE_LEVEL_NOT_SUPPORTED 0x40
|
||||
#define BATTERY_POWER_STATE_LEVEL_GOOD 0x80
|
||||
#define BATTERY_POWER_STATE_LEVEL_CRITICALLY_LOW 0xC0
|
||||
|
||||
|
||||
|
||||
#define BLECTL_JSON_COFIG_FILE "/blectl.json"
|
||||
|
||||
#define EndofText 0x03
|
||||
@@ -91,4 +124,6 @@
|
||||
void blectl_save_config( void );
|
||||
void blectl_read_config( void );
|
||||
|
||||
void blectl_update_battery( int32_t percent, bool charging, bool plug );
|
||||
|
||||
#endif // _BLECTL_H
|
||||
@@ -7,6 +7,8 @@
|
||||
#include "pmu.h"
|
||||
#include "powermgm.h"
|
||||
#include "motor.h"
|
||||
#include "blectl.h"
|
||||
|
||||
|
||||
#include "gui/statusbar.h"
|
||||
|
||||
@@ -283,6 +285,7 @@ void pmu_loop( TTGOClass *ttgo ) {
|
||||
if ( nextmillis < millis() || updatetrigger == true ) {
|
||||
nextmillis = millis() + 1000;
|
||||
statusbar_update_battery( pmu_get_battery_percent( ttgo ), ttgo->power->isChargeing(), ttgo->power->isVBUSPlug() );
|
||||
blectl_update_battery( pmu_get_battery_percent( ttgo ), ttgo->power->isChargeing(), ttgo->power->isVBUSPlug() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user