Merge pull request #64 from bobbycar-graz/bluetooth-wifi-improvements

Added persistent settings for bluetooth and wifi
This commit is contained in:
2020-06-07 21:49:27 +02:00
committed by GitHub
10 changed files with 89 additions and 25 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include "changevaluedisplay.h"
#include "menudisplay.h"
#include "staticmenudefinition.h"
#include "menuitem.h"
@ -11,6 +12,7 @@
#include "actions/bluetoothdisconnectaction.h"
#include "actions/switchscreenaction.h"
#include "bluetoothtexthelpers.h"
#include "settingsaccessors.h"
#include "icons/back.h"
#include "texts.h"
@ -19,6 +21,16 @@ class SettingsMenu;
}
namespace {
class BluetoothSettingsMenu;
using AutoBluetoothModeChangeDisplay = makeComponent<
ChangeValueDisplay<BluetoothMode>,
StaticText<TEXT_AUTOBLUETOOTHMODE>,
AutoBluetoothModeAccessor,
BackActionInterface<SwitchScreenAction<BluetoothSettingsMenu>>,
SwitchScreenAction<BluetoothSettingsMenu>
>;
class BluetoothSettingsMenu :
public MenuDisplay,
public StaticText<TEXT_BLUETOOTHSETTINGS>,
@ -34,6 +46,7 @@ class BluetoothSettingsMenu :
makeComponent<MenuItem, StaticText<TEXT_BLUETOOTHFLUSH>, BluetoothFlushAction>,
makeComponent<MenuItem, StaticText<TEXT_BLUETOOTHEND>, BluetoothEndAction>,
makeComponent<MenuItem, StaticText<TEXT_BLUETOOTHDISCONNECT>, BluetoothDisconnectAction>,
makeComponent<MenuItem, StaticText<TEXT_AUTOBLUETOOTHMODE>, SwitchScreenAction<AutoBluetoothModeChangeDisplay>>,
makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<SettingsMenu>, StaticMenuItemIcon<&icons::back>>
>
{};

View File

@ -5,7 +5,6 @@
#include "menudisplay.h"
#include "staticmenudefinition.h"
#include "utils.h"
#include "changevaluedisplay.h"
#include "actions/toggleboolaction.h"
#include "actions/switchscreenaction.h"
#include "checkboxicon.h"
@ -17,7 +16,6 @@
#include "icons/back.h"
#include "texts.h"
#include "globals.h"
#include "settingsaccessors.h"
namespace {
class LimitsSettingsMenu;
@ -32,15 +30,6 @@ class MainMenu;
}
namespace {
class SettingsMenu;
using BluetoothModeChangeDisplay = makeComponent<
ChangeValueDisplay<BluetoothMode>,
StaticText<TEXT_BLUETOOTHMODE>,
BluetoothModeAccessor,
BackActionInterface<SwitchScreenAction<SettingsMenu>>,
SwitchScreenAction<SettingsMenu>
>;
struct FrontLedAccessor : public RefAccessor<bool> { bool &getRef() const override { return front.command.led; } };
struct BackLedAccessor : public RefAccessor<bool> { bool &getRef() const override { return back.command.led; } };
@ -55,7 +44,6 @@ class SettingsMenu :
makeComponent<MenuItem, StaticText<TEXT_MODESSETTINGS>, SwitchScreenAction<ModesSettingsMenu>>,
makeComponent<MenuItem, StaticText<TEXT_CONTROLLERHARDWARESETTINGS>, SwitchScreenAction<ControllerHardwareSettingsMenu>, StaticMenuItemIcon<&icons::hardware>>,
makeComponent<MenuItem, StaticText<TEXT_BOARDCOMPUTERHARDWARESETTINGS>, SwitchScreenAction<BoardcomputerHardwareSettingsMenu>, StaticMenuItemIcon<&icons::hardware>>,
makeComponent<MenuItem, StaticText<TEXT_BLUETOOTHMODE>, SwitchScreenAction<BluetoothModeChangeDisplay>>,
#ifdef FEATURE_BMS
makeComponent<MenuItem, StaticText<TEXT_AUTOCONNECTBMS>, ToggleBoolAction, CheckboxIcon, AutoConnectBmsAccessor>,
#endif

View File

@ -1,12 +1,16 @@
#pragma once
#include "changevaluedisplay.h"
#include "menudisplay.h"
#include "staticmenudefinition.h"
#include "utils.h"
#include "actions/switchscreenaction.h"
#include "actions/toggleboolaction.h"
#include "checkboxicon.h"
#include "icons/scan.h"
#include "icons/back.h"
#include "texts.h"
#include "settingsaccessors.h"
namespace {
class GenericWifiSettingsMenu;
@ -17,11 +21,23 @@ class SettingsMenu;
}
namespace {
class WifiSettingsMenu;
using AutoWifiModeChangeDisplay = makeComponent<
ChangeValueDisplay<wifi_mode_t>,
StaticText<TEXT_AUTOWIFIMODE>,
AutoWifiModeAccessor,
BackActionInterface<SwitchScreenAction<WifiSettingsMenu>>,
SwitchScreenAction<WifiSettingsMenu>
>;
class WifiSettingsMenu :
public MenuDisplay,
public StaticText<TEXT_WIFISETTINGS>,
public BackActionInterface<SwitchScreenAction<SettingsMenu>>,
public StaticMenuDefinition<
makeComponent<MenuItem, StaticText<TEXT_AUTOWIFIMODE>, SwitchScreenAction<AutoWifiModeChangeDisplay>>,
makeComponent<MenuItem, StaticText<TEXT_AUTOENABLEAP>, ToggleBoolAction, CheckboxIcon, AutoEnableApAccessor>,
makeComponent<MenuItem, StaticText<TEXT_GENERICWIFISETTINGS>, SwitchScreenAction<GenericWifiSettingsMenu>>,
makeComponent<MenuItem, StaticText<TEXT_STATIONWIFISETTINGS>, SwitchScreenAction<StationWifiSettingsMenu>>,
makeComponent<MenuItem, StaticText<TEXT_WIFISCAN>, SwitchScreenAction<WifiScanMenu>, StaticMenuItemIcon<&icons::scan>>,

View File

@ -1,5 +1,7 @@
#include <cstdio>
#include <esp_wifi_types.h>
#include <Arduino.h>
#include <HardwareSerial.h>
#include <WiFi.h>
@ -88,22 +90,23 @@ void setup()
if (!WiFi.softAPsetHostname(deviceName))
Serial.println("Could not softAPsetHostname");
if (!WiFi.mode(WIFI_AP_STA))
if (!WiFi.mode(settings.wifiSettings.autoWifiMode))
Serial.println("Could not set mode to WIFI_AP_STA");
WifiSoftApAction{}.triggered();
if (settings.wifiSettings.autoEnableAp)
WifiSoftApAction{}.triggered();
if (!WiFi.begin("realraum", "r3alraum"))
Serial.println("Could not begin WiFi");
if (settings.bluetoothMode == BluetoothMode::Master)
if (settings.bluetoothSettings.autoBluetoothMode == BluetoothMode::Master)
{
BluetoothBeginMasterAction{}.triggered();
#ifdef FEATURE_BMS
if (settings.autoConnectBms)
BluetoothConnectBmsAction{}.triggered();
#endif
} else if (settings.bluetoothMode == BluetoothMode::Slave)
} else if (settings.bluetoothSettings.autoBluetoothMode == BluetoothMode::Slave)
BluetoothBeginAction{}.triggered();
front.serial.get().begin(38400, SERIAL_8N1, PINS_RX1, PINS_TX1);

View File

@ -51,6 +51,15 @@ constexpr Settings::ControllerHardware mosfetsOffControllerHardware {
.swapFrontBack = false
};
constexpr Settings::WifiSettings defaultWifiSettings {
.autoWifiMode = WIFI_MODE_APSTA,
.autoEnableAp = true
};
constexpr Settings::BluetoothSettings defaultBluetoothSettings {
.autoBluetoothMode = BluetoothMode::Off
};
constexpr Settings::ControllerHardware spinnerControllerHardware {
.enableFrontLeft = true,
.enableFrontRight = true,
@ -127,13 +136,14 @@ constexpr Settings defaultSettings{
#ifdef FEATURE_BMS
.autoConnectBms = false,
#endif
.bluetoothMode = BluetoothMode::Off,
.reverseBeep = false,
.reverseBeepFreq0 = 3,
.reverseBeepFreq1 = 0,
.reverseBeepDuration0 = 500,
.reverseBeepDuration1 = 500,
.limits = defaultLimits,
.wifiSettings = defaultWifiSettings,
.bluetoothSettings = defaultBluetoothSettings,
.controllerHardware = defaultControllerHardware,
.boardcomputerHardware = defaultBoardcomputerHardware,
.defaultMode = defaultDefaultMode,

View File

@ -122,7 +122,9 @@ union X {
UpdateDisplay updateDisplay;
#endif
BluetoothModeChangeDisplay bluetoothModeChangeDisplay;
AutoWifiModeChangeDisplay autoWifiModeChangeDisplay;
AutoBluetoothModeChangeDisplay autoBluetoothModeChangeDisplay;
FrontFreqChangeScreen changeFrontFreq;
FrontPatternChangeScreen changeFrontPattern;
@ -265,7 +267,9 @@ template<> decltype(displays.statusDisplay) &
template<> decltype(displays.updateDisplay) &getRefByType<decltype(displays.updateDisplay)>() { return displays.updateDisplay; }
#endif
template<> decltype(displays.bluetoothModeChangeDisplay) &getRefByType<decltype(displays.bluetoothModeChangeDisplay)>() { return displays.bluetoothModeChangeDisplay; }
template<> decltype(displays.autoWifiModeChangeDisplay) &getRefByType<decltype(displays.autoWifiModeChangeDisplay)>() { return displays.autoWifiModeChangeDisplay; }
template<> decltype(displays.autoBluetoothModeChangeDisplay) &getRefByType<decltype(displays.autoBluetoothModeChangeDisplay)>() { return displays.autoBluetoothModeChangeDisplay; }
template<> decltype(displays.changeFrontFreq) &getRefByType<decltype(displays.changeFrontFreq)>() { return displays.changeFrontFreq; }
template<> decltype(displays.changeFrontPattern) &getRefByType<decltype(displays.changeFrontPattern)>() { return displays.changeFrontPattern; }

View File

@ -2,6 +2,8 @@
#include <cstdint>
#include <esp_wifi_types.h>
#include "bobbycar-protocol/protocol.h"
#include "bluetoothmode.h"
@ -16,8 +18,6 @@ struct Settings
bool autoConnectBms;
#endif
BluetoothMode bluetoothMode;
bool reverseBeep;
uint8_t reverseBeepFreq0;
uint8_t reverseBeepFreq1;
@ -32,6 +32,15 @@ struct Settings
int16_t phaseAdvMax; // [deg] Maximum Phase Advance angle (only for SIN). Higher angle results in higher maximum speed.
} limits;
struct WifiSettings {
wifi_mode_t autoWifiMode;
bool autoEnableAp;
} wifiSettings;
struct BluetoothSettings {
BluetoothMode autoBluetoothMode;
} bluetoothSettings;
struct ControllerHardware {
bool enableFrontLeft, enableFrontRight, enableBackLeft, enableBackRight;
bool invertFrontLeft, invertFrontRight, invertBackLeft, invertBackRight;
@ -88,8 +97,6 @@ void Settings::executeForEverySetting(T &&callable)
callable("autoConnectBms", autoConnectBms);
#endif
callable("bluetoothMode", bluetoothMode);
callable("reverseBeep", reverseBeep);
callable("revBeepFreq0", reverseBeepFreq0);
callable("revBeepFreq1", reverseBeepFreq1);
@ -102,6 +109,11 @@ void Settings::executeForEverySetting(T &&callable)
callable("fieldWeakMax", limits.fieldWeakMax);
callable("phaseAdvMax", limits.phaseAdvMax);
callable("autoBluetoothMo", bluetoothSettings.autoBluetoothMode);
callable("autoWifiMode", wifiSettings.autoWifiMode);
callable("autoEnableAp", wifiSettings.autoEnableAp);
callable("enableFrontLeft", controllerHardware.enableFrontLeft);
callable("enableFrontRigh", controllerHardware.enableFrontRight);
callable("enableBackLeft", controllerHardware.enableBackLeft);

View File

@ -15,7 +15,6 @@ struct RefAccessorSaveSettings : public virtual RefAccessor<T>
#ifdef FEATURE_BMS
struct AutoConnectBmsAccessor : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.autoConnectBms; } };
#endif
struct BluetoothModeAccessor : public RefAccessorSaveSettings<BluetoothMode> { BluetoothMode &getRef() const override { return settings.bluetoothMode; } };
struct ReverseBeepAccessor : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.reverseBeep; } };
struct ReverseBeepFreq0Accessor : public RefAccessorSaveSettings<uint8_t> { uint8_t &getRef() const override { return settings.reverseBeepFreq0; } };
struct ReverseBeepFreq1Accessor : public RefAccessorSaveSettings<uint8_t> { uint8_t &getRef() const override { return settings.reverseBeepFreq1; } };
@ -33,6 +32,11 @@ struct NMotMaxRpmAccessor : public RefAccessorSaveSettings<int16_t> { int16_t &g
struct FieldWeakMaxAccessor : public RefAccessorSaveSettings<int16_t> { int16_t &getRef() const override { return settings.limits.fieldWeakMax; } };
struct PhaseAdvMaxAccessor : public RefAccessorSaveSettings<int16_t> { int16_t &getRef() const override { return settings.limits.phaseAdvMax; } };
struct AutoWifiModeAccessor : public RefAccessorSaveSettings<wifi_mode_t> { wifi_mode_t &getRef() const override { return settings.wifiSettings.autoWifiMode; } };
struct AutoEnableApAccessor : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.wifiSettings.autoEnableAp; } };
struct AutoBluetoothModeAccessor : public RefAccessorSaveSettings<BluetoothMode> { BluetoothMode &getRef() const override { return settings.bluetoothSettings.autoBluetoothMode; } };
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

@ -109,6 +109,14 @@ template<> struct nvsGetterHelper<UnifiedModelMode> { static esp_err_t nvs_get(n
*out_value = UnifiedModelMode(tempValue);
return err;
}};
template<> struct nvsGetterHelper<wifi_mode_t> { static esp_err_t nvs_get(nvs_handle handle, const char* key, wifi_mode_t* out_value)
{
uint8_t tempValue;
esp_err_t err = nvs_get_u8(handle, key, &tempValue);
if (err == ESP_OK)
*out_value = wifi_mode_t(tempValue);
return err;
}};
bool SettingsSaver::load(Settings &settings)
{
@ -155,6 +163,10 @@ template<> struct nvsSetterHelper<UnifiedModelMode> { static esp_err_t nvs_set(n
{
return nvs_set_u8(handle, key, uint8_t(value));
}};
template<> struct nvsSetterHelper<wifi_mode_t> { static esp_err_t nvs_set(nvs_handle handle, const char* key, wifi_mode_t value)
{
return nvs_set_u8(handle, key, uint8_t(value));
}};
bool SettingsSaver::save(Settings &settings)
{

View File

@ -20,6 +20,7 @@ constexpr char TEXT_BLUETOOTHBEGINMASTER[] = "begin() Master";
constexpr char TEXT_BLUETOOTHFLUSH[] = "flush()";
constexpr char TEXT_BLUETOOTHEND[] = "end()";
constexpr char TEXT_BLUETOOTHDISCONNECT[] = "disconnect()";
constexpr char TEXT_AUTOBLUETOOTHMODE[] = "Auto bluetooth mode";
//constexpr char TEXT_BACK[] = "Back";
//BmsMenu
@ -72,7 +73,6 @@ constexpr char TEXT_WIFISETTINGS[] = "WiFi settings";
constexpr char TEXT_MODESSETTINGS[] = "Modes settings";
constexpr char TEXT_CONTROLLERHARDWARESETTINGS[] = "Controller H/W settings";
constexpr char TEXT_BOARDCOMPUTERHARDWARESETTINGS[] = "Boardcomputer H/W settings";
constexpr char TEXT_BLUETOOTHMODE[] = "Bluetooth mode";
constexpr char TEXT_AUTOCONNECTBMS[] = "Auto connect BMS";
constexpr char TEXT_BUZZER[] = "Buzzer";
constexpr char TEXT_FRONTLED[] = "Front LED";
@ -92,6 +92,8 @@ constexpr char TEXT_SWAPFRONTBACK[] = "Swap front/back";
//StationWifiSettingsMenu
constexpr char TEXT_STATIONWIFISETTINGS[] = "Station WiFi settings";
constexpr char TEXT_AUTOWIFIMODE[] = "Auto wifi mode";
constexpr char TEXT_AUTOENABLEAP[] = "Auto enable AP";
constexpr char TEXT_WIFIRECONNECT[] = "reconnect()";
constexpr char TEXT_WIFIDISCONNECT[] = "disconnect()";
constexpr char TEXT_WIFICHANGEAUTOCONNECT[] = "Change auto connect";