move bluetooth code into something that is architecture specific...

because the ESP32 implementation will be different from NRF52
to make this possible I needed to decouple knowlege about bluetooth from
our mesh service.  Instead mesh service now uses the Obserable pattern
to let any interested consumer get notified of important mesh changes
(currently that is only bluetooth, but really we should do the same
thing for decoupling the GUI 'app' from the mesh service)

@girtsf would you mind reviewing my Observer changes? I haven't written
C++ code in a long time ;-)
This commit is contained in:
geeksville
2020-04-10 12:18:48 -07:00
parent 93a06906cb
commit 6ad451eb5f
13 changed files with 104 additions and 108 deletions

View File

@@ -21,9 +21,7 @@
*/
#include "BluetoothUtil.h"
#include "GPS.h"
#include "MeshBluetoothService.h"
#include "MeshRadio.h"
#include "MeshService.h"
#include "NodeDB.h"
@@ -39,6 +37,10 @@
#include <Wire.h>
#include <driver/rtc_io.h>
#ifndef NO_ESP32
#include "BluetoothUtil.h"
#endif
#ifdef TBEAM_V10
#include "axp20x.h"
AXP20X_Class axp;
@@ -59,8 +61,6 @@ static meshtastic::PowerStatus powerStatus;
bool ssd1306_found;
bool axp192_found;
bool bluetoothOn;
// -----------------------------------------------------------------------------
// Application
// -----------------------------------------------------------------------------
@@ -266,49 +266,6 @@ void setup()
setCPUFast(false); // 80MHz is fine for our slow peripherals
}
void initBluetooth()
{
DEBUG_MSG("Starting bluetooth\n");
// FIXME - we are leaking like crazy
// AllocatorScope scope(btPool);
// Note: these callbacks might be coming in from a different thread.
BLEServer *serve = initBLE(
[](uint32_t pin) {
powerFSM.trigger(EVENT_BLUETOOTH_PAIR);
screen.startBluetoothPinScreen(pin);
},
[]() { screen.stopBluetoothPinScreen(); }, getDeviceName(), HW_VENDOR, xstr(APP_VERSION),
xstr(HW_VERSION)); // FIXME, use a real name based on the macaddr
createMeshBluetoothService(serve);
// Start advertising - this must be done _after_ creating all services
serve->getAdvertising()->start();
}
void setBluetoothEnable(bool on)
{
if (on != bluetoothOn) {
DEBUG_MSG("Setting bluetooth enable=%d\n", on);
bluetoothOn = on;
if (on) {
Serial.printf("Pre BT: %u heap size\n", ESP.getFreeHeap());
// ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) );
initBluetooth();
} else {
// We have to totally teardown our bluetooth objects to prevent leaks
stopMeshBluetoothService(); // Must do before shutting down bluetooth
deinitBLE();
destroyMeshBluetoothService(); // must do after deinit, because it frees our service
Serial.printf("Shutdown BT: %u heap size\n", ESP.getFreeHeap());
// ESP_ERROR_CHECK( heap_trace_stop() );
// heap_trace_dump();
}
}
}
uint32_t ledBlinker()
{
static bool ledOn;
@@ -352,7 +309,10 @@ void loop()
ledPeriodic.loop();
// axpDebugOutput.loop();
#ifndef NO_ESP32
loopBLE();
#endif
// for debug printing
// service.radio.radioIf.canSleep();