From 93a06906cb8cb15d99434bb09f1e73fe8416c29e Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 10 Apr 2020 12:15:00 -0700 Subject: [PATCH] begin splitting up source files, so we can have a tree of sources... unique to each architecture. For now, we have "esp32" and "bare" esp32 is the old esp stuff bare is an target suitable for emulation that doesn't require any particular hardware to run (no bluetooth, no i2c devices, no spi devices) --- platformio.ini | 20 ++++++++++++--- src/bare/main-bare.cpp | 6 +++++ src/configuration.h | 2 ++ src/esp32/main-esp32.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ src/target_specific.h | 6 +++++ 5 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/bare/main-bare.cpp create mode 100644 src/esp32/main-esp32.cpp create mode 100644 src/target_specific.h diff --git a/platformio.ini b/platformio.ini index 093a8d477..cd1755adf 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,6 +15,11 @@ default_envs = tbeam ; default to a US frequency range, change it as needed for your region and hardware (CN, JP, EU433, EU865) hw_version = US +; Common settings for ESP targes, mixin with extends = esp32_base +[esp32_base] +src_filter = + ${env.src_filter} - + [env] platform = espressif32 framework = arduino @@ -70,7 +75,8 @@ lib_deps = https://github.com/meshtastic/SparkFun_Ublox_Arduino_Library.git ; The 1.0 release of the TBEAM board -[env:tbeam] +[env:tbeam] +extends = esp32_base board = ttgo-t-beam lib_deps = ${env.lib_deps} @@ -79,22 +85,26 @@ build_flags = ${env.build_flags} -D TBEAM_V10 ; The original TBEAM board without the AXP power chip and a few other changes -[env:tbeam0.7] +[env:tbeam0.7] +extends = esp32_base board = ttgo-t-beam build_flags = ${env.build_flags} -D TBEAM_V07 [env:heltec] ;build_type = debug ; to make it possible to step through our jtag debugger +extends = esp32_base board = heltec_wifi_lora_32_V2 [env:ttgo-lora32-v1] +extends = esp32_base board = ttgo-lora32-v1 build_flags = ${env.build_flags} -D TTGO_LORA_V1 ; note: the platformio definition for lora32-v2 seems stale, it is missing a pins_arduino.h file, therefore I don't think it works [env:ttgo-lora32-v2] +extends = esp32_base board = ttgo-lora32-v1 build_flags = ${env.build_flags} -D TTGO_LORA_V2 @@ -104,4 +114,8 @@ build_flags = [env:bare] board = ttgo-lora32-v1 build_flags = - ${env.build_flags} -D BARE_BOARD \ No newline at end of file + ${env.build_flags} -D BARE_BOARD +src_filter = + ${env.src_filter} - +lib_ignore = + BluetoothOTA \ No newline at end of file diff --git a/src/bare/main-bare.cpp b/src/bare/main-bare.cpp new file mode 100644 index 000000000..29532a639 --- /dev/null +++ b/src/bare/main-bare.cpp @@ -0,0 +1,6 @@ +#include "target_specific.h" + +void setBluetoothEnable(bool on) +{ + // Do nothing +} \ No newline at end of file diff --git a/src/configuration.h b/src/configuration.h index a8128882d..862aa0f12 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -202,6 +202,8 @@ along with this program. If not, see . // This string must exactly match the case used in release file names or the android updater won't work #define HW_VENDOR "bare" +#define NO_ESP32 // Don't use ESP32 libs (mainly bluetooth) + #endif // ----------------------------------------------------------------------------- diff --git a/src/esp32/main-esp32.cpp b/src/esp32/main-esp32.cpp new file mode 100644 index 000000000..baf0d85b7 --- /dev/null +++ b/src/esp32/main-esp32.cpp @@ -0,0 +1,53 @@ +#include "BluetoothUtil.h" +#include "MeshBluetoothService.h" +#include "PowerFSM.h" +#include "configuration.h" +#include "main.h" +#include "target_specific.h" + +bool bluetoothOn; + +// This routine is called multiple times, once each time we come back from sleep +void reinitBluetooth() +{ + 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(); +} + +// Enable/disable bluetooth. +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) ); + reinitBluetooth(); + } 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(); + } + } +} \ No newline at end of file diff --git a/src/target_specific.h b/src/target_specific.h new file mode 100644 index 000000000..6acca364a --- /dev/null +++ b/src/target_specific.h @@ -0,0 +1,6 @@ +#pragma once + +// Functions that are unique to particular target types (esp32, bare, nrf52 etc...) + +// Enable/disable bluetooth. +void setBluetoothEnable(bool on); \ No newline at end of file