BLE settings added to enable/disable BLE

This commit is contained in:
2021-08-10 04:10:35 +02:00
parent e559fa8b82
commit 8e3c0b4204
13 changed files with 255 additions and 108 deletions

View File

@ -31,6 +31,7 @@ set(headers
actions/updateswapfrontbackaction.h
bluetoothmode.h
ble.h
bletexthelpers.h
bmsutils.h
changevaluedisplay_bluetoothmode.h
changevaluedisplay_bool.h
@ -46,6 +47,7 @@ set(headers
displays/graphdisplay.h
displays/menus/aboutmenu.h
displays/menus/accesspointwifisettingsmenu.h
displays/menus/blesettingsmenu.h
displays/menus/bluetoothsettingsmenu.h
displays/menus/bmsmenu.h
displays/menus/buzzermenu.h

View File

@ -38,6 +38,10 @@ struct WifiEnabledAccessor : public RefAccessorSaveSettings<bool> { bool &getRef
struct AutoBluetoothModeAccessor : public RefAccessorSaveSettings<BluetoothMode> { BluetoothMode &getRef() const override { return settings.bluetoothSettings.autoBluetoothMode; } };
#endif
#ifdef FEATURE_BLE
struct BleEnabledAccessor : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.bleSettings.bleEnabled; } };
#endif
struct FrontLeftEnabledAccessor : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.controllerHardware.enableFrontLeft; } };
struct FrontRightEnabledAccessor : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.controllerHardware.enableFrontRight; } };
struct BackLeftEnabledAccessor : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.controllerHardware.enableBackLeft; } };

View File

@ -1,5 +1,8 @@
#pragma once
// esp-idf includes
#include <esp_log.h>
// 3rdparty lib includes
#include <ArduinoJson.h>
#ifdef FEATURE_BLE
@ -18,6 +21,9 @@ BLEService *pService{};
BLECharacteristic *livestatsCharacteristic{};
BLECharacteristic *remotecontrolCharacteristic{};
void createBle();
void destroyBle();
class RemoteControlCallbacks : public NimBLECharacteristicCallbacks
{
public:
@ -28,6 +34,133 @@ RemoteControlCallbacks bleRemoteCallbacks;
void initBle()
{
if (settings.bleSettings.bleEnabled)
createBle();
}
void handleBle()
{
if (settings.bleSettings.bleEnabled)
{
if (!pServer)
createBle();
if (livestatsCharacteristic->getSubscribedCount())
{
StaticJsonDocument<1024> doc;
{
auto arr = doc.createNestedArray("v");
if (controllers.front.feedbackValid)
arr.add(fixBatVoltage(controllers.front.feedback.batVoltage));
else
arr.add(nullptr);
if (controllers.back.feedbackValid)
arr.add(fixBatVoltage(controllers.back.feedback.batVoltage));
else
arr.add(nullptr);
}
{
auto arr = doc.createNestedArray("t");
if (controllers.front.feedbackValid)
arr.add(fixBoardTemp(controllers.front.feedback.boardTemp));
else
arr.add(nullptr);
if (controllers.back.feedbackValid)
arr.add(fixBoardTemp(controllers.back.feedback.boardTemp));
else
arr.add(nullptr);
}
{
auto arr = doc.createNestedArray("e");
if (controllers.front.feedbackValid)
{
arr.add(controllers.front.feedback.left.error);
arr.add(controllers.front.feedback.right.error);
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
if (controllers.back.feedbackValid)
{
arr.add(controllers.back.feedback.left.error);
arr.add(controllers.back.feedback.right.error);
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
}
{
auto arr = doc.createNestedArray("s");
if (controllers.front.feedbackValid)
{
arr.add(convertToKmh(controllers.front.feedback.left.speed * (settings.controllerHardware.invertFrontLeft ? -1 : 1)));
arr.add(convertToKmh(controllers.front.feedback.right.speed * (settings.controllerHardware.invertFrontRight ? -1 : 1)));
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
if (controllers.back.feedbackValid)
{
arr.add(convertToKmh(controllers.back.feedback.left.speed * (settings.controllerHardware.invertBackLeft ? -1 : 1)));
arr.add(convertToKmh(controllers.back.feedback.right.speed * (settings.controllerHardware.invertBackRight ? -1 : 1)));
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
}
{
auto arr = doc.createNestedArray("a");
if (controllers.front.feedbackValid)
{
arr.add(fixCurrent(controllers.front.feedback.left.dcLink) * 2);
arr.add(fixCurrent(controllers.front.feedback.right.dcLink) * 2);
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
if (controllers.back.feedbackValid)
{
arr.add(fixCurrent(controllers.back.feedback.left.dcLink) * 2);
arr.add(fixCurrent(controllers.back.feedback.right.dcLink) * 2);
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
}
std::string json;
serializeJson(doc, json);
livestatsCharacteristic->setValue(json);
livestatsCharacteristic->notify();
}
}
else if (pServer)
{
destroyBle();
}
}
void createBle()
{
ESP_LOGI("BOBBY", "called");
BLEDevice::init(deviceName);
const auto serviceUuid{"0335e46c-f355-4ce6-8076-017de08cee98"};
@ -48,113 +181,16 @@ void initBle()
BLEDevice::startAdvertising();
}
void handleBle()
void destroyBle()
{
if (livestatsCharacteristic->getSubscribedCount())
{
StaticJsonDocument<1024> doc;
{
auto arr = doc.createNestedArray("v");
if (controllers.front.feedbackValid)
arr.add(fixBatVoltage(controllers.front.feedback.batVoltage));
else
arr.add(nullptr);
if (controllers.back.feedbackValid)
arr.add(fixBatVoltage(controllers.back.feedback.batVoltage));
else
arr.add(nullptr);
}
ESP_LOGI("BOBBY", "called");
{
auto arr = doc.createNestedArray("t");
if (controllers.front.feedbackValid)
arr.add(fixBoardTemp(controllers.front.feedback.boardTemp));
else
arr.add(nullptr);
if (controllers.back.feedbackValid)
arr.add(fixBoardTemp(controllers.back.feedback.boardTemp));
else
arr.add(nullptr);
}
BLEDevice::deinit(true);
{
auto arr = doc.createNestedArray("e");
if (controllers.front.feedbackValid)
{
arr.add(controllers.front.feedback.left.error);
arr.add(controllers.front.feedback.right.error);
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
if (controllers.back.feedbackValid)
{
arr.add(controllers.back.feedback.left.error);
arr.add(controllers.back.feedback.right.error);
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
}
{
auto arr = doc.createNestedArray("s");
if (controllers.front.feedbackValid)
{
arr.add(convertToKmh(controllers.front.feedback.left.speed * (settings.controllerHardware.invertFrontLeft ? -1 : 1)));
arr.add(convertToKmh(controllers.front.feedback.right.speed * (settings.controllerHardware.invertFrontRight ? -1 : 1)));
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
if (controllers.back.feedbackValid)
{
arr.add(convertToKmh(controllers.back.feedback.left.speed * (settings.controllerHardware.invertBackLeft ? -1 : 1)));
arr.add(convertToKmh(controllers.back.feedback.right.speed * (settings.controllerHardware.invertBackRight ? -1 : 1)));
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
}
{
auto arr = doc.createNestedArray("a");
if (controllers.front.feedbackValid)
{
arr.add(fixCurrent(controllers.front.feedback.left.dcLink) * 2);
arr.add(fixCurrent(controllers.front.feedback.right.dcLink) * 2);
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
if (controllers.back.feedbackValid)
{
arr.add(fixCurrent(controllers.back.feedback.left.dcLink) * 2);
arr.add(fixCurrent(controllers.back.feedback.right.dcLink) * 2);
}
else
{
arr.add(nullptr);
arr.add(nullptr);
}
}
std::string json;
serializeJson(doc, json);
livestatsCharacteristic->setValue(json);
livestatsCharacteristic->notify();
}
pServer = {};
pService = {};
livestatsCharacteristic = {};
remotecontrolCharacteristic = {};
}
void RemoteControlCallbacks::onWrite(NimBLECharacteristic* pCharacteristic)

34
main/bletexthelpers.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
// 3rdparty lib includes
#include <fmt/core.h>
// local includes
#include "textinterface.h"
#include "ble.h"
namespace {
#ifdef FEATURE_BLE
struct BleServerPeerDevicesText : public virtual TextInterface {
public:
std::string text() const override
{
std::string text = "peerDevices: ";
if (pServer)
text += std::to_string(pServer->getPeerDevices().size());
return text;
}
};
struct BleCharacSubscribedText : public virtual TextInterface {
public:
std::string text() const override
{
std::string text = "subscribed: ";
if (livestatsCharacteristic)
text += std::to_string(livestatsCharacteristic->getSubscribedCount());
return text;
}
};
#endif
}

View File

@ -1,5 +1,6 @@
#pragma once
// local includes
#include "textinterface.h"
#include "globals.h"
#include "utils.h"

View File

@ -0,0 +1,36 @@
#pragma once
// local includes
#include "menudisplay.h"
#include "menuitem.h"
#include "actions/switchscreenaction.h"
#include "actions/toggleboolaction.h"
#include "checkboxicon.h"
#include "bletexthelpers.h"
#include "accessors/settingsaccessors.h"
#include "icons/back.h"
#include "texts.h"
// forward declares
namespace {
class SettingsMenu;
} // namespace
namespace {
#ifdef FEATURE_BLE
class BleSettingsMenu :
public MenuDisplay,
public StaticText<TEXT_BLESETTINGS>,
public BackActionInterface<SwitchScreenAction<SettingsMenu>>
{
public:
BleSettingsMenu()
{
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BLEENABLED>, ToggleBoolAction, CheckboxIcon, BleEnabledAccessor>>();
constructMenuItem<makeComponent<MenuItem, BleServerPeerDevicesText, DisabledColor, DummyAction>>();
constructMenuItem<makeComponent<MenuItem, BleCharacSubscribedText, DisabledColor, DummyAction>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<SettingsMenu>, StaticMenuItemIcon<&icons::back>>>();
}
};
#endif
} // namespace

View File

@ -7,7 +7,7 @@
#include "actions/switchscreenaction.h"
#include "checkboxicon.h"
#include "icons/wifi.h"
#ifdef FEATURE_BLUETOOTH
#if defined(FEATURE_BLUETOOTH) || defined(FEATURE_BLE)
#include "icons/bluetooth.h"
#endif
#include "icons/hardware.h"
@ -23,6 +23,7 @@ namespace {
class LimitsSettingsMenu;
class WifiSettingsMenu;
class BluetoothSettingsMenu;
class BleSettingsMenu;
class ModesSettingsMenu;
class ControllerHardwareSettingsMenu;
class BoardcomputerHardwareSettingsMenu;
@ -57,6 +58,9 @@ public:
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_WIFISETTINGS>, SwitchScreenAction<WifiSettingsMenu>, StaticMenuItemIcon<&icons::wifi>>>();
#ifdef FEATURE_BLUETOOTH
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BLUETOOTHSETTINGS>, SwitchScreenAction<BluetoothSettingsMenu>, StaticMenuItemIcon<&icons::bluetooth>>>();
#endif
#ifdef FEATURE_BLE
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BLESETTINGS>, SwitchScreenAction<BleSettingsMenu>, StaticMenuItemIcon<&icons::bluetooth>>>();
#endif
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_MODESSETTINGS>, SwitchScreenAction<ModesSettingsMenu>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_CONTROLLERHARDWARESETTINGS>, SwitchScreenAction<ControllerHardwareSettingsMenu>, StaticMenuItemIcon<&icons::hardware>>>();

View File

@ -100,7 +100,7 @@ private:
Label m_labelWifiStatus{35, bottomLines[0]}; // 120, 15
Label m_labelLimit0{205, bottomLines[0]}; // 35, 15
Label m_labelIpAddress{25, bottomLines[1]}; // 130, 15
Label m_labelSignal{125, bottomLines[1]}; // 130, 15
Label m_labelSignal{120, bottomLines[1]}; // 130, 15
Label m_labelLimit1{205, bottomLines[1]}; // 35, 15
Label m_labelPerformance{40, bottomLines[2]}; // 40, 15
Label m_labelFreeMem{70, bottomLines[2]}; // 40, 15

View File

@ -4,7 +4,7 @@
namespace {
namespace icons {
#ifdef FEATURE_BLUETOOTH
#if defined(FEATURE_BLUETOOTH) || defined(FEATURE_BLE)
const Icon<24, 24> bluetooth{{
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x120F, 0x1A50, 0x0000, 0x01CF, 0x2AB1, 0x3B12, 0x4333, 0x4333, 0x3AF2, 0x2AB1, 0x01EF, // 0x0010 (16) pixels
0x0000, 0x1A50, 0x1210, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1210, 0x018E, 0x0007, 0x2AB1, // 0x0020 (32) pixels

View File

@ -27,6 +27,9 @@ using namespace std::chrono_literals;
#ifdef FEATURE_BLUETOOTH
#include "displays/menus/bluetoothsettingsmenu.h"
#endif
#ifdef FEATURE_BLE
#include "displays/menus/blesettingsmenu.h"
#endif
#include "displays/menus/bmsmenu.h"
#include "displays/menus/buzzermenu.h"
#include "displays/menus/commanddebugmenu.h"

View File

@ -75,6 +75,12 @@ constexpr Settings::BluetoothSettings defaultBluetoothSettings {
};
#endif
#ifdef FEATURE_BLE
constexpr Settings::BleSettings defaultBleSettings {
.bleEnabled = true
};
#endif
constexpr Settings::ControllerHardware spinnerControllerHardware {
.enableFrontLeft = true,
.enableFrontRight = true,
@ -182,6 +188,9 @@ constexpr Settings defaultSettings {
.wifiSettings = defaultWifiSettings,
#ifdef FEATURE_BLUETOOTH
.bluetoothSettings = defaultBluetoothSettings,
#endif
#ifdef FEATURE_BLE
.bleSettings = defaultBleSettings,
#endif
.controllerHardware = defaultControllerHardware,
.boardcomputerHardware = defaultBoardcomputerHardware,

View File

@ -47,6 +47,12 @@ struct Settings
} bluetoothSettings;
#endif
#ifdef FEATURE_BLE
struct BleSettings {
bool bleEnabled;
} bleSettings;
#endif
struct ControllerHardware {
bool enableFrontLeft, enableFrontRight, enableBackLeft, enableBackRight;
bool invertFrontLeft, invertFrontRight, invertBackLeft, invertBackRight;
@ -127,11 +133,15 @@ void Settings::executeForEveryCommonSetting(T &&callable)
callable("autoConnectBms", autoConnectBms);
#endif
callable("wifiEnabled", wifiSettings.wifiEnabled);
#ifdef FEATURE_BLUETOOTH
callable("autoBluetoothMo", bluetoothSettings.autoBluetoothMode);
#endif
callable("wifiEnabled", wifiSettings.wifiEnabled);
#ifdef FEATURE_BLE
callable("bleEnabled", bleSettings.bleEnabled);
#endif
callable("wheelDiameter", controllerHardware.wheelDiameter);
callable("numMagnetPoles", controllerHardware.numMagnetPoles);

View File

@ -33,6 +33,13 @@ constexpr char TEXT_TURNOFFDISCHARGE[] = "Turn off discharge";
#endif
#endif
#ifdef FEATURE_BLE
//BleSettingsMenu
constexpr char TEXT_BLESETTINGS[] = "BLE settings";
constexpr char TEXT_BLEENABLED[] = "BLE enabled";
//constexpr char TEXT_BACK[] = "Back";
#endif
//DebugMenu
constexpr char TEXT_LOADSETTINGS[] = "Load settings";
constexpr char TEXT_SAVESETTINGS[] = "Save settings";
@ -74,6 +81,7 @@ constexpr char TEXT_BACKLIGHT[] = "Backlight";
constexpr char TEXT_LIMITSSETTINGS[] = "Limits settings";
constexpr char TEXT_WIFISETTINGS[] = "WiFi settings";
//constexpr char TEXT_BLUETOOTHSETTINGS[] = "Bluetooth settings";
//constexpr char TEXT_BLESETTINGS[] = "BLE settings";
constexpr char TEXT_MODESSETTINGS[] = "Modes settings";
constexpr char TEXT_CONTROLLERHARDWARESETTINGS[] = "Controller H/W settings";
constexpr char TEXT_BOARDCOMPUTERHARDWARESETTINGS[] = "Boardcomputer H/W settings";