add beta version bluetooth setup

This commit is contained in:
sharandac
2020-08-13 18:53:13 +02:00
parent bfa8e287ad
commit 174ab77d2f
12 changed files with 335 additions and 26 deletions

View File

@@ -33,12 +33,15 @@
#include <BLE2902.h>
#include "blectl.h"
#include "json_psram_allocator.h"
#include "gui/statusbar.h"
EventGroupHandle_t blectl_status = NULL;
portMUX_TYPE blectlMux = portMUX_INITIALIZER_UNLOCKED;
blectl_config_t blectl_config;
BLEServer *pServer = NULL;
BLECharacteristic *pTxCharacteristic;
uint8_t txValue = 0;
@@ -61,8 +64,10 @@ class BleCtlServerCallbacks: public BLEServerCallbacks {
statusbar_style_icon( STATUSBAR_BLUETOOTH, STATUSBAR_STYLE_GRAY );
log_i("BLE disconnected");
delay(500);
pServer->getAdvertising()->start();
log_i("BLE advertising...");
if ( blectl_get_advertising() ) {
pServer->getAdvertising()->start();
log_i("BLE advertising...");
}
}
};
@@ -151,6 +156,7 @@ class BleCtlCallbacks : public BLECharacteristicCallbacks
*
*/
void blectl_setup( void ) {
blectl_status = xEventGroupCreate();
blectl_set_event( BLECTL_CONNECT | BLECTL_OFF_REQUEST | BLECTL_ON_REQUEST | BLECTL_PAIRING | BLECTL_STANDBY_REQUEST | BLECTL_ACTIVE | BLECTL_SCAN );
@@ -205,8 +211,11 @@ void blectl_setup( void ) {
// The maximum 0x4000 interval of ~16 sec was too slow, I could not reliably connect
pServer->getAdvertising()->setMinInterval( 100 );
pServer->getAdvertising()->setMaxInterval( 200 );
pServer->getAdvertising()->start();
log_i("BLE advertising...");
if ( blectl_get_advertising() ) {
pServer->getAdvertising()->start();
log_i("BLE advertising...");
}
}
/*
@@ -243,4 +252,80 @@ void blectl_standby( void ) {
void blectl_wakeup( void ) {
statusbar_style_icon( STATUSBAR_BLUETOOTH, STATUSBAR_STYLE_GRAY );
}
void blectl_set_enable_on_standby( bool enable_on_standby ) {
blectl_config.enable_on_standby = enable_on_standby;
blectl_save_config();
}
void blectl_set_advertising( bool advertising ) {
blectl_config.advertising = advertising;
blectl_save_config();
if ( blectl_get_event( BLECTL_CONNECT ) )
return;
if ( advertising ) {
pServer->getAdvertising()->start();
}
else {
pServer->getAdvertising()->stop();
}
}
bool blectl_get_enable_on_standby( void ) {
return( blectl_config.enable_on_standby );
}
bool blectl_get_advertising( void ) {
return( blectl_config.enable_on_standby );
}
/*
*
*/
void blectl_save_config( void ) {
fs::File file = SPIFFS.open( BLECTL_JSON_COFIG_FILE, FILE_WRITE );
if (!file) {
log_e("Can't open file: %s!", BLECTL_JSON_COFIG_FILE );
}
else {
SpiRamJsonDocument doc( 1000 );
doc["advertising"] = blectl_config.advertising;
doc["enable_on_standby"] = blectl_config.enable_on_standby;
if ( serializeJsonPretty( doc, file ) == 0) {
log_e("Failed to write config file");
}
doc.clear();
}
file.close();
}
/*
*
*/
void blectl_read_config( void ) {
if ( SPIFFS.exists( BLECTL_JSON_COFIG_FILE ) ) {
fs::File file = SPIFFS.open( BLECTL_JSON_COFIG_FILE, FILE_READ );
if (!file) {
log_e("Can't open file: %s!", BLECTL_JSON_COFIG_FILE );
}
else {
int filesize = file.size();
SpiRamJsonDocument doc( filesize * 2 );
DeserializationError error = deserializeJson( doc, file );
if ( error ) {
log_e("blectl deserializeJson() failed: %s", error.c_str() );
}
else {
blectl_config.advertising = doc["advertising"].as<bool>();
blectl_config.enable_on_standby = doc["enable_on_standby"].as<bool>();
}
doc.clear();
}
file.close();
}
}

View File

@@ -30,6 +30,13 @@
#define CHARACTERISTIC_UUID_RX BLEUUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
#define CHARACTERISTIC_UUID_TX BLEUUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")
#define BLECTL_JSON_COFIG_FILE "/blectl.json"
typedef struct {
bool advertising = true;
bool enable_on_standby = false;
} blectl_config_t;
#define BLECTL_CONNECT _BV(0)
#define BLECTL_STANDBY_REQUEST _BV(1)
#define BLECTL_ON_REQUEST _BV(2)
@@ -60,5 +67,11 @@
EventBits_t blectl_get_event( EventBits_t bits );
void blectl_standby( void );
void blectl_wakeup( void );
void blectl_set_enable_on_standby( bool enable_on_standby );
void blectl_set_advertising( bool advertising );
bool blectl_get_enable_on_standby( void );
bool blectl_get_advertising( void );
void blectl_save_config( void );
void blectl_read_config( void );
#endif // _BLECTL_H

View File

@@ -124,9 +124,7 @@ void bma_loop( TTGOClass *ttgo ) {
/*
* handle IRQ event
*/
if ( xEventGroupGetBitsFromISR( bma_event_handle ) & BMA_EVENT_INT ) {
setCpuFrequencyMhz(240);
if ( xEventGroupGetBitsFromISR( bma_event_handle ) & BMA_EVENT_INT ) {
while( !ttgo->bma->readInterrupt() );
if ( ttgo->bma->isDoubleClick() ) {
powermgm_set_event( POWERMGM_BMA_WAKEUP );

View File

@@ -7,7 +7,6 @@ struct SpiRamAllocator {
void* allocate( size_t size ) {
void *psram = ps_calloc( size, 1 );
if ( psram ) {
log_i("allocate %d bytes (%p) json psram", size, psram );
return( psram );
}
else {
@@ -16,7 +15,6 @@ struct SpiRamAllocator {
}
}
void deallocate( void* pointer ) {
log_i("deallocate (%p) json psram", pointer );
free( pointer );
}
};

View File

@@ -36,7 +36,7 @@ void pmu_setup( TTGOClass *ttgo ) {
log_e("target voltage set failed!");
if ( ttgo->power->setChargeControlCur( 300 ) )
log_e("charge current set failed!");
if ( ttgo->power->setAdcSamplingRate( AXP_ADC_SAMPLING_RATE_200HZ ) )
if ( ttgo->power->setAdcSamplingRate( AXP_ADC_SAMPLING_RATE_25HZ ) )
log_e("adc sample set failed!");
// Turn off unused power
@@ -225,7 +225,6 @@ void pmu_loop( TTGOClass *ttgo ) {
* handle IRQ event
*/
if ( xEventGroupGetBitsFromISR( pmu_event_handle ) & PMU_EVENT_AXP_INT ) {
setCpuFrequencyMhz(240);
if ( powermgm_get_event( POWERMGM_PMU_BATTERY | POWERMGM_PMU_BUTTON | POWERMGM_STANDBY_REQUEST ) ) {
ttgo->power->clearIRQ();
xEventGroupClearBits( pmu_event_handle, PMU_EVENT_AXP_INT );

View File

@@ -54,6 +54,7 @@ void powermgm_setup( TTGOClass *ttgo ) {
pmu_setup( ttgo );
bma_setup( ttgo );
wifictl_setup();
blectl_read_config();
timesync_setup( ttgo );
touch_setup( ttgo );
sound_setup();
@@ -78,6 +79,8 @@ void powermgm_loop( TTGOClass *ttgo ) {
log_i("go wakeup");
setCpuFrequencyMhz(240);
pmu_wakeup();
bma_wakeup();
display_wakeup();
@@ -120,18 +123,24 @@ void powermgm_loop( TTGOClass *ttgo ) {
adc_power_off();
motor_vibe(1);
delay(50);
log_i("go standby");
setCpuFrequencyMhz( 80 );
gpio_wakeup_enable ( (gpio_num_t)AXP202_INT, GPIO_INTR_LOW_LEVEL );
gpio_wakeup_enable ( (gpio_num_t)BMA423_INT1, GPIO_INTR_HIGH_LEVEL );
esp_sleep_enable_gpio_wakeup ();
esp_light_sleep_start();
// from here, the consumption is round about 2.5mA
// total standby time is 152h (6days) without use?
if ( !blectl_get_enable_on_standby() ) {
motor_vibe(3);
delay(50);
log_i("go standby");
setCpuFrequencyMhz( 10 );
gpio_wakeup_enable ( (gpio_num_t)AXP202_INT, GPIO_INTR_LOW_LEVEL );
gpio_wakeup_enable ( (gpio_num_t)BMA423_INT1, GPIO_INTR_HIGH_LEVEL );
esp_sleep_enable_gpio_wakeup ();
esp_light_sleep_start();
// from here, the consumption is round about 2.5mA
// total standby time is 152h (6days) without use?
}
else {
log_i("standby block by bluetooth");
setCpuFrequencyMhz( 80 );
// from here, the consumption is round about 23mA
// total standby time is 19h without use?
}
}
// clear event
powermgm_clear_event( POWERMGM_PMU_BUTTON | POWERMGM_PMU_BATTERY | POWERMGM_BMA_WAKEUP | POWERMGM_STANDBY_REQUEST | POWERMGM_SILENCE_WAKEUP_REQUEST );