FEATURE_CAN vs FEATURE_SERIAL

This commit is contained in:
2021-06-28 10:57:39 +02:00
parent f93076c97b
commit 67ad568de1
9 changed files with 31 additions and 22 deletions

View File

@ -63,6 +63,7 @@ build_flags =
-DTFT_RST=2
-DSPI_FREQUENCY=27000000
-DDEFAULT_SWAPSCREENBYTES=false
-DFEATURE_SERIAL
-DPINS_RX1=4
-DPINS_TX1=5
-DPINS_RX2=25
@ -115,6 +116,8 @@ build_flags =
-DTFT_RST=2
-DSPI_FREQUENCY=27000000
-DDEFAULT_SWAPSCREENBYTES=false
-DFEATURE_CAN
; -DFEATURE_SERIAL
; -DPINS_RX1=4
; -DPINS_TX1=5
; -DPINS_RX2=22
@ -163,7 +166,6 @@ build_flags =
; -DDEFAULT_GAMETRAKDISTMIN=0
; -DDEFAULT_GAMETRAKDISTMAX=4095
; -DFEATURE_CLOUD
-DFEATURE_CAN
[env:feedc0de_usb]
platform = ${feedc0de.platform}
@ -332,6 +334,7 @@ build_flags =
${default_wheels_inverted.build_flags}
-DDEFAULT_WHEELDIAMETER=165
; TODO: actually assign pins
-DFEATURE_SERIAL
-DPINS_RX1=22
-DPINS_TX1=25
-DPINS_RX2=23
@ -421,6 +424,7 @@ build_flags =
-DTFT_RST=2
-DSPI_FREQUENCY=27000000
-DDEFAULT_SWAPSCREENBYTES=false
-DFEATURE_SERIAL
-DPINS_RX1=4
-DPINS_TX1=5
-DPINS_RX2=25

View File

@ -7,22 +7,22 @@
#include "types.h"
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
#include "feedbackparser.h"
#endif
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
class HardwareSerial;
#endif
namespace {
struct Controller {
Controller(
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
HardwareSerial &serial,
#endif
bool &enableLeft, bool &enableRight, bool &invertLeft, bool &invertRight) :
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
serial{serial},
#endif
enableLeft{enableLeft}, enableRight{enableRight}, invertLeft{invertLeft}, invertRight{invertRight}
@ -31,7 +31,7 @@ struct Controller {
// Controller(const Controller &) = delete;
// Controller &operator=(const Controller &) = delete;
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
std::reference_wrapper<HardwareSerial> serial;
#endif
bool &enableLeft, &enableRight, &invertLeft, &invertRight;
@ -44,7 +44,7 @@ struct Controller {
bool feedbackValid{};
bobbycar::protocol::serial::Feedback feedback{};
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
FeedbackParser parser{serial, feedbackValid, feedback};
#endif
};

View File

@ -60,7 +60,7 @@ public:
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_WHEELDIAMETERMM>, SwitchScreenAction<WheelDiameterMmChangeScreen>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_WHEELDIAMETERINCH>, SwitchScreenAction<WheelDiameterInchChangeScreen>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_NUMMAGNETPOLES>, SwitchScreenAction<NumMagnetPolesChangeScreen>>>();
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_SWAPFRONTBACK>, ToggleBoolAction, CheckboxIcon, SwapFrontBackAccessor>>();
#endif
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<SettingsMenu>, StaticMenuItemIcon<&icons::back>>>();

View File

@ -42,13 +42,13 @@ public:
explicit Controllers() :
std::array<Controller, 2>{{
Controller {
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
Serial1,
#endif
settings.controllerHardware.enableFrontLeft, settings.controllerHardware.enableFrontRight, settings.controllerHardware.invertFrontLeft, settings.controllerHardware.invertFrontRight
},
Controller {
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
Serial2,
#endif
settings.controllerHardware.enableBackLeft, settings.controllerHardware.enableBackRight, settings.controllerHardware.invertBackLeft, settings.controllerHardware.invertBackRight

View File

@ -243,7 +243,7 @@ void setup()
}
printMemoryStats("loadSettings()");
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
bootLabel.redraw("swap front back");
updateSwapFrontBack();
printMemoryStats("swapFronBack()");
@ -309,7 +309,9 @@ void setup()
#ifdef FEATURE_CAN
initCan();
#else
#endif
#ifdef FEATURE_SERIAL
bootLabel.redraw("front Serial begin");
controllers.front.serial.get().begin(38400, SERIAL_8N1, PINS_RX1, PINS_TX1);
@ -440,7 +442,9 @@ void loop()
for (int i = 0; i < 4; i++)
if (!parseCanInput())
break;
#else
#endif
#ifdef FEATURE_SERIAL
for (Controller &controller : controllers)
controller.parser.update();
#endif

View File

@ -32,7 +32,7 @@ constexpr Settings::ControllerHardware defaultControllerHardware {
.wheelDiameter = DEFAULT_WHEELDIAMETER,
.numMagnetPoles = 15,
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
.swapFrontBack = false
#endif
};
@ -50,7 +50,7 @@ constexpr Settings::ControllerHardware mosfetsOffControllerHardware {
.wheelDiameter = 165,
.numMagnetPoles = 15,
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
.swapFrontBack = false
#endif
};
@ -79,7 +79,7 @@ constexpr Settings::ControllerHardware spinnerControllerHardware {
.wheelDiameter = 165,
.numMagnetPoles = 15,
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
.swapFrontBack = false
#endif
};

View File

@ -51,7 +51,7 @@ struct Settings
int16_t wheelDiameter; // in mm
int16_t numMagnetPoles; // virtual RPM per one real RPM
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
bool swapFrontBack;
#endif
} controllerHardware;
@ -144,7 +144,7 @@ void Settings::executeForEverySetting(T &&callable)
callable("wheelDiameter", controllerHardware.wheelDiameter);
callable("numMagnetPoles", controllerHardware.numMagnetPoles);
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
callable("swapFrontBack", controllerHardware.swapFrontBack);
#endif

View File

@ -56,7 +56,7 @@ struct WheelDiameterInchAccessor : public virtual AccessorInterface<float>
void setValue(float value) override { settings.controllerHardware.wheelDiameter = convertFromInch(value); saveSettings(); }
};
struct NumMagnetPolesAccessor : public RefAccessorSaveSettings<int16_t> { int16_t &getRef() const override { return settings.controllerHardware.numMagnetPoles; } };
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
struct SwapFrontBackAccessor : public RefAccessorSaveSettings<bool> {
bool &getRef() const override { return settings.controllerHardware.swapFrontBack; }
void setValue(bool value) override { RefAccessorSaveSettings<bool>::setValue(value); updateSwapFrontBack(); };

View File

@ -256,14 +256,15 @@ void fixCommonParams()
void sendCommands()
{
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
for (Controller &controller : controllers)
{
controller.command.start = Command::VALID_HEADER;
controller.command.checksum = calculateChecksum(controller.command);
controller.serial.get().write((uint8_t *) &controller.command, sizeof(controller.command));
}
#else
#endif
#ifdef FEATURE_CAN
sendCanCommands();
#endif
}
@ -271,7 +272,7 @@ void sendCommands()
template<typename T, typename... Args>
void switchScreen(Args&&... args);
#ifndef FEATURE_CAN
#ifdef FEATURE_SERIAL
void updateSwapFrontBack()
{
controllers.front.serial = settings.controllerHardware.swapFrontBack ? Serial2 : Serial1;