From e5871d339ecff23e1c860fa7708d580b7083504c Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 16 May 2021 21:51:31 +0200 Subject: [PATCH] CAN implementation started --- platformio.ini | 1 + src/controller.h | 19 +++++++++++++++++-- .../menus/controllerhardwaresettingsmenu.h | 2 ++ src/globals.h | 14 ++++++++++++-- src/main.cpp | 6 ++++++ src/presets.h | 6 ++++++ src/settings.h | 4 ++++ src/settingsaccessors.h | 2 ++ src/utils.h | 4 ++++ 9 files changed, 54 insertions(+), 4 deletions(-) diff --git a/platformio.ini b/platformio.ini index ab9feff..ad50e9e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -162,6 +162,7 @@ build_flags = ; -DDEFAULT_GAMETRAKDISTMIN=0 ; -DDEFAULT_GAMETRAKDISTMAX=4095 -DFEATURE_CLOUD + -DFEATURE_CAN [env:feedc0de_usb] platform = ${feedc0de.platform} diff --git a/src/controller.h b/src/controller.h index 770b080..9d10d64 100644 --- a/src/controller.h +++ b/src/controller.h @@ -4,20 +4,33 @@ #include "bobbycar-protocol/protocol.h" +#ifndef FEATURE_CAN #include "feedbackparser.h" +#endif +#ifndef FEATURE_CAN class HardwareSerial; +#endif namespace { struct Controller { - Controller(HardwareSerial &serial, bool &enableLeft, bool &enableRight, bool &invertLeft, bool &invertRight) : - serial{serial}, enableLeft{enableLeft}, enableRight{enableRight}, invertLeft{invertLeft}, invertRight{invertRight} + Controller( +#ifndef FEATURE_CAN + HardwareSerial &serial, +#endif + bool &enableLeft, bool &enableRight, bool &invertLeft, bool &invertRight) : +#ifndef FEATURE_CAN + serial{serial}, +#endif + enableLeft{enableLeft}, enableRight{enableRight}, invertLeft{invertLeft}, invertRight{invertRight} { } // Controller(const Controller &) = delete; // Controller &operator=(const Controller &) = delete; +#ifndef FEATURE_CAN std::reference_wrapper serial; +#endif bool &enableLeft, &enableRight, &invertLeft, &invertRight; Command command{}; @@ -25,6 +38,8 @@ struct Controller { bool feedbackValid{}; Feedback feedback{}; +#ifndef FEATURE_CAN FeedbackParser parser{serial, feedbackValid, feedback}; +#endif }; } diff --git a/src/displays/menus/controllerhardwaresettingsmenu.h b/src/displays/menus/controllerhardwaresettingsmenu.h index 4199fa6..caaf32b 100644 --- a/src/displays/menus/controllerhardwaresettingsmenu.h +++ b/src/displays/menus/controllerhardwaresettingsmenu.h @@ -60,7 +60,9 @@ public: constructMenuItem, SwitchScreenAction>>(); constructMenuItem, SwitchScreenAction>>(); constructMenuItem, SwitchScreenAction>>(); +#ifndef FEATURE_CAN constructMenuItem, ToggleBoolAction, CheckboxIcon, SwapFrontBackAccessor>>(); +#endif constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&icons::back>>>(); } }; diff --git a/src/globals.h b/src/globals.h index 8baa4b0..81615c6 100644 --- a/src/globals.h +++ b/src/globals.h @@ -41,8 +41,18 @@ class Controllers : public std::array public: explicit Controllers() : std::array{{ - Controller{Serial1, settings.controllerHardware.enableFrontLeft, settings.controllerHardware.enableFrontRight, settings.controllerHardware.invertFrontLeft, settings.controllerHardware.invertFrontRight}, - Controller{Serial2, settings.controllerHardware.enableBackLeft, settings.controllerHardware.enableBackRight, settings.controllerHardware.invertBackLeft, settings.controllerHardware.invertBackRight} + Controller { +#ifndef FEATURE_CAN + Serial1, +#endif + settings.controllerHardware.enableFrontLeft, settings.controllerHardware.enableFrontRight, settings.controllerHardware.invertFrontLeft, settings.controllerHardware.invertFrontRight + }, + Controller { +#ifndef FEATURE_CAN + Serial2, +#endif + settings.controllerHardware.enableBackLeft, settings.controllerHardware.enableBackRight, settings.controllerHardware.invertBackLeft, settings.controllerHardware.invertBackRight + } }} {} diff --git a/src/main.cpp b/src/main.cpp index 67d0328..c329b58 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -233,9 +233,11 @@ void setup() } printMemoryStats("loadSettings()"); +#ifndef FEATURE_CAN bootLabel.redraw("swap front back"); updateSwapFrontBack(); printMemoryStats("swapFronBack()"); +#endif bootLabel.redraw("deviceName"); { @@ -295,11 +297,13 @@ void setup() } #endif +#ifndef FEATURE_CAN bootLabel.redraw("front Serial begin"); controllers.front.serial.get().begin(38400, SERIAL_8N1, PINS_RX1, PINS_TX1); bootLabel.redraw("back Serial begin"); controllers.back.serial.get().begin(38400, SERIAL_8N1, PINS_RX2, PINS_TX2); +#endif raw_gas = 0; raw_brems = 0; @@ -418,8 +422,10 @@ void loop() performance.lastTime = now; } +#ifndef FEATURE_CAN for (Controller &controller : controllers) controller.parser.update(); +#endif handleSerial(); diff --git a/src/presets.h b/src/presets.h index 351c860..1f8a4e0 100644 --- a/src/presets.h +++ b/src/presets.h @@ -32,7 +32,9 @@ constexpr Settings::ControllerHardware defaultControllerHardware { .wheelDiameter = DEFAULT_WHEELDIAMETER, .numMagnetPoles = 15, +#ifndef FEATURE_CAN .swapFrontBack = false +#endif }; constexpr Settings::ControllerHardware mosfetsOffControllerHardware { @@ -48,7 +50,9 @@ constexpr Settings::ControllerHardware mosfetsOffControllerHardware { .wheelDiameter = 165, .numMagnetPoles = 15, +#ifndef FEATURE_CAN .swapFrontBack = false +#endif }; constexpr Settings::WifiSettings defaultWifiSettings { @@ -75,7 +79,9 @@ constexpr Settings::ControllerHardware spinnerControllerHardware { .wheelDiameter = 165, .numMagnetPoles = 15, +#ifndef FEATURE_CAN .swapFrontBack = false +#endif }; constexpr Settings::BoardcomputerHardware::TimersSettings defaultTimersSettings { diff --git a/src/settings.h b/src/settings.h index 10cf3a0..daee213 100644 --- a/src/settings.h +++ b/src/settings.h @@ -51,7 +51,9 @@ struct Settings int16_t wheelDiameter; // in mm int16_t numMagnetPoles; // virtual RPM per one real RPM +#ifndef FEATURE_CAN bool swapFrontBack; +#endif } controllerHardware; struct BoardcomputerHardware { @@ -142,7 +144,9 @@ void Settings::executeForEverySetting(T &&callable) callable("wheelDiameter", controllerHardware.wheelDiameter); callable("numMagnetPoles", controllerHardware.numMagnetPoles); +#ifndef FEATURE_CAN callable("swapFrontBack", controllerHardware.swapFrontBack); +#endif callable("sampleCount", boardcomputerHardware.sampleCount); callable("gasMin", boardcomputerHardware.gasMin); diff --git a/src/settingsaccessors.h b/src/settingsaccessors.h index 7d2aa23..8c82d5d 100644 --- a/src/settingsaccessors.h +++ b/src/settingsaccessors.h @@ -56,10 +56,12 @@ struct WheelDiameterInchAccessor : public virtual AccessorInterface void setValue(float value) override { settings.controllerHardware.wheelDiameter = convertFromInch(value); saveSettings(); } }; struct NumMagnetPolesAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.controllerHardware.numMagnetPoles; } }; +#ifndef FEATURE_CAN struct SwapFrontBackAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.controllerHardware.swapFrontBack; } void setValue(bool value) override { RefAccessorSaveSettings::setValue(value); updateSwapFrontBack(); }; }; +#endif struct SampleCountAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.sampleCount; } }; struct GasMinAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.gasMin; } }; diff --git a/src/utils.h b/src/utils.h index dbfaffc..c1d70b8 100644 --- a/src/utils.h +++ b/src/utils.h @@ -251,18 +251,22 @@ void sendCommands() { controller.command.start = Command::VALID_HEADER; controller.command.checksum = calculateChecksum(controller.command); +#ifndef FEATURE_CAN controller.serial.get().write((uint8_t *) &controller.command, sizeof(controller.command)); +#endif } } template void switchScreen(Args&&... args); +#ifndef FEATURE_CAN void updateSwapFrontBack() { controllers.front.serial = settings.controllerHardware.swapFrontBack ? Serial2 : Serial1; controllers.back.serial = settings.controllerHardware.swapFrontBack ? Serial1 : Serial2; } +#endif void loadSettings() {