diff --git a/.travis.yml b/.travis.yml index ec78eb2..d2ab01f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,6 @@ env: - BUILD_CONFIG=feedc0de_usb - BUILD_CONFIG=greyhash_usb - BUILD_CONFIG=aveexy_usb - - BUILD_CONFIG=testbench_usb - BUILD_CONFIG=mickdermack_usb - BUILD_CONFIG=peter_usb diff --git a/README.md b/README.md index ee725b7..fc12886 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,11 @@ platformio run --environment bobbycar_usb --target upload ![Wiring diagram](/img/wiring.png) -### -DFEATURE_3WIRESW -![3 wire switches](/img/3wiresw.png) - ### -DFEATURE_DPAD ![dpad switches with 5 wires](/img/dpadsw.png) +### -DFEATURE_DPAD_3WIRESW +![3 wire switches](/img/3wiresw.png) + ### -DFEATURE_ROTARY ![rotary encoder](/img/rotary.png) diff --git a/platformio.ini b/platformio.ini index 2f4d29a..3ed6801 100644 --- a/platformio.ini +++ b/platformio.ini @@ -28,6 +28,7 @@ build_flags = -O3 -Werror -Wall + -Wno-unused-function -DUSER_SETUP_LOADED=1 -DLOAD_GLCD=1 -DLOAD_FONT2=1 @@ -51,8 +52,6 @@ build_flags = -DTFT_DC=12 -DTFT_RST=2 -DSPI_FREQUENCY=27000000 - -DSPI_READ_FREQUENCY=20000000 - -DSPI_TOUCH_FREQUENCY=2500000 -DDEFAULT_SWAPSCREENBYTES=false -DPINS_RX1=4 -DPINS_TX1=5 @@ -84,15 +83,10 @@ build_flags = ${default_limits.build_flags} -DDEVICE_PREFIX=bobbyquad -DFEATURE_WEBSERVER - -DFEATURE_3WIRESW - -DPINS_3WIRESW_OUT=0 - -DPINS_3WIRESW_IN1=16 - -DPINS_3WIRESW_IN2=27 -; -DFEATURE_DPAD -; -DPINS_DPAD_IN1=22 -; -DPINS_DPAD_IN2=23 -; -DPINS_DPAD_IN3=27 -; -DPINS_DPAD_IN4=32 + -DFEATURE_DPAD_3WIRESW + -DPINS_DPAD_3WIRESW_OUT=0 + -DPINS_DPAD_3WIRESW_IN1=16 + -DPINS_DPAD_3WIRESW_IN2=27 ; -DFEATURE_ROTARY ; -DPINS_ROTARY_CLK=16 ; -DPINS_ROTARY_DT=27 @@ -199,19 +193,11 @@ build_flags = ${default_limits.build_flags} -DDEVICE_PREFIX=bobbyquad -DFEATURE_WEBSERVER -; -DFEATURE_3WIRESW -; -DPINS_3WIRESW_OUT=0 -; -DPINS_3WIRESW_IN1=16 -; -DPINS_3WIRESW_IN2=27 -DFEATURE_DPAD -DPINS_DPAD_UP=32 -DPINS_DPAD_DOWN=22 -DPINS_DPAD_CONFIRM=23 -DPINS_DPAD_BACK=27 -; -DFEATURE_ROTARY -; -DPINS_ROTARY_CLK=16 -; -DPINS_ROTARY_DT=27 -; -DPINS_ROTARY_SW=0 -DDEFAULT_GASMIN=800 -DDEFAULT_GASMAX=3700 -DDEFAULT_BREMSMIN=1300 @@ -219,54 +205,6 @@ build_flags = -[env:testbench_usb] -platform = ${common_env_data.platform} -board = ${common_env_data.board} -framework = ${common_env_data.framework} -board_build.partitions = bobbycar_noota.csv -lib_deps = ${common_env_data.lib_deps} -lib_compat_mode = ${common_env_data.lib_compat_mode} -build_unflags = ${common_env_data.build_unflags} - -upload_port = /dev/ttyUSB* -upload_speed = 921600 - -build_flags = - ${common_env_data.build_flags} - -DDEVICE_PREFIX=testbench - -DFEATURE_WEBSERVER - -DILI9341_DRIVER=1 - -DTFT_MOSI=22 - -DTFT_SCLK=21 - -DTFT_CS=27 - -DTFT_DC=32 - -DTFT_RST=25 - -DSPI_FREQUENCY=27000000 - -DSPI_READ_FREQUENCY=20000000 - -DSPI_TOUCH_FREQUENCY=2500000 - -DDEFAULT_SWAPSCREENBYTES=false - -DPINS_RX1=18 - -DPINS_TX1=19 - -DPINS_RX2=23 - -DPINS_TX2=34 - -DPINS_GAS=35 - -DPINS_BREMS=33 -; -DFEATURE_3WIRESW -; -DPINS_3WIRESW_OUT=17 -; -DPINS_3WIRESW_IN1=4 -; -DPINS_3WIRESW_IN2=16 -; -DFEATURE_ROTARY -; -DPINS_ROTARY_CLK=4 -; -DPINS_ROTARY_DT=16 -; -DPINS_ROTARY_SW=17 - ${default_limits.build_flags} - -DDEFAULT_GASMIN=0 - -DDEFAULT_GASMAX=4095 - -DDEFAULT_BREMSMIN=0 - -DDEFAULT_BREMSMAX=4095 - - - [env:mickdermack_usb] platform = ${common_env_data.platform} board = ${common_env_data.board} @@ -284,7 +222,6 @@ build_flags = -DDEVICE_PREFIX=bobbyquad -DFEATURE_WEBSERVER ${default_limits.build_flags} - -DUSER_SETUP_LOADED=1 -DRPI_DISPLAY_TYPE -DILI9486_DRIVER ; TODO: TFT_MISO (touch MISO?) @@ -306,14 +243,6 @@ build_flags = -DPINS_TX2=34 -DPINS_GAS=35 -DPINS_BREMS=33 -; -DFEATURE_3WIRESW -; -DPINS_3WIRESW_OUT=17 -; -DPINS_3WIRESW_IN1=4 -; -DPINS_3WIRESW_IN2=16 -; -DFEATURE_ROTARY -; -DPINS_ROTARY_CLK=4 -; -DPINS_ROTARY_DT=16 -; -DPINS_ROTARY_SW=17 -DDEFAULT_GASMIN=0 -DDEFAULT_GASMAX=4095 -DDEFAULT_BREMSMIN=0 diff --git a/src/bmsutils.h b/src/bmsutils.h index aa6421f..06d9d60 100644 --- a/src/bmsutils.h +++ b/src/bmsutils.h @@ -1,6 +1,7 @@ #pragma once #include "globals.h" +#include "types.h" namespace { #ifdef FEATURE_BMS @@ -8,8 +9,8 @@ namespace bms { constexpr auto autoReconnect = false; // causes hangs when not available bool lastConnected; -unsigned long lastSend; -unsigned long lastReceive; +millis_t lastSend; +millis_t lastReceive; float voltage; float current; diff --git a/src/displays/menus/dynamicdebugmenu.h b/src/displays/menus/dynamicdebugmenu.h index 2013c29..265bb74 100644 --- a/src/displays/menus/dynamicdebugmenu.h +++ b/src/displays/menus/dynamicdebugmenu.h @@ -14,6 +14,7 @@ #include "checkboxicon.h" #include "icons/back.h" #include "texts.h" +#include "types.h" namespace { class DebugMenu; @@ -36,7 +37,7 @@ public: } private: - mutable unsigned long m_nextUpdate{}; + mutable millis_t m_nextUpdate{}; mutable String m_title; }; @@ -57,7 +58,7 @@ public: } private: - mutable unsigned long m_nextUpdate{}; + mutable millis_t m_nextUpdate{}; mutable int m_color; }; @@ -77,7 +78,7 @@ public: } private: - mutable unsigned long m_nextUpdate{}; + mutable millis_t m_nextUpdate{}; mutable int m_font; }; @@ -100,7 +101,7 @@ public: } private: - mutable unsigned long m_nextUpdate{}; + mutable millis_t m_nextUpdate{}; mutable const Icon<24, 24> *m_icon; }; diff --git a/src/displays/menus/mosfetsmenu.h b/src/displays/menus/mosfetsmenu.h index 61299a9..a574ecd 100644 --- a/src/displays/menus/mosfetsmenu.h +++ b/src/displays/menus/mosfetsmenu.h @@ -9,6 +9,7 @@ #include "checkboxicon.h" #include "icons/back.h" #include "texts.h" +#include "types.h" namespace { class MainMenu; diff --git a/src/displays/menus/wifiscanmenu.h b/src/displays/menus/wifiscanmenu.h index 9a151c8..09985ec 100644 --- a/src/displays/menus/wifiscanmenu.h +++ b/src/displays/menus/wifiscanmenu.h @@ -12,6 +12,7 @@ #include "actions/dummyaction.h" #include "icons/back.h" #include "texts.h" +#include "types.h" namespace { class WifiSettingsMenu; @@ -48,7 +49,7 @@ private: std::vector> vec; - unsigned long m_lastScanComplete; + millis_t m_lastScanComplete; }; String WifiScanMenu::text() const diff --git a/src/dpad.h b/src/dpad.h index a03b261..410421b 100644 --- a/src/dpad.h +++ b/src/dpad.h @@ -5,21 +5,24 @@ #include #include "globals.h" +#include "types.h" namespace { -using DPadState = std::tuple; +namespace dpad +{ +using State = std::tuple; template -class DPadHelper +class Helper { public: void begin(); - DPadState read(); + State read(); }; template -void DPadHelper::begin() +void Helper::begin() { pinMode(IN1, INPUT_PULLUP); pinMode(IN2, INPUT_PULLUP); @@ -28,7 +31,7 @@ void DPadHelper::begin() } template -DPadState DPadHelper::read() +State Helper::read() { const bool result0 = digitalRead(IN1); const bool result1 = digitalRead(IN2); @@ -39,30 +42,56 @@ DPadState DPadHelper::read() } #ifdef FEATURE_DPAD -DPadHelper dpad; +Helper helper; +State lastState; +millis_t debounceUp, debounceDown, debounceConfirm, debounceBack; -DPadState lastState; -void updateDpad() +void init() { - const auto state = dpad.read(); + helper.begin(); +} + +void update() +{ + const auto state = helper.read(); + const auto now = millis(); enum { - ButtonDown = 0, - ButtonConfirm = 1, - ButtonBack = 2, - ButtonUp = 3 + ButtonUp = 0, + ButtonDown = 1, + ButtonConfirm = 2, + ButtonBack = 3 }; - if (!std::get(lastState) && std::get(state)) - InputDispatcher::rotate(-1); - if (!std::get(lastState) && std::get(state)) - InputDispatcher::rotate(1); - if (std::get(lastState) != std::get(state)) - InputDispatcher::confirmButton(std::get(state)); - if (std::get(lastState) != std::get(state)) - InputDispatcher::backButton(std::get(state)); + constexpr auto debounceTime = 25; - lastState = state; + if (std::get(lastState) != std::get(state) && now-debounceUp > debounceTime) + { + if (std::get(state)) + InputDispatcher::rotate(-1); + std::get(lastState) = std::get(state); + debounceUp = now; + } + if (std::get(lastState) != std::get(state) && now-debounceDown > debounceTime) + { + if (std::get(state)) + InputDispatcher::rotate(1); + std::get(lastState) = std::get(state); + debounceDown = now; + } + if (std::get(lastState) != std::get(state) && now-debounceConfirm > debounceTime) + { + InputDispatcher::confirmButton(std::get(state)); + std::get(lastState) = std::get(state); + debounceConfirm = now; + } + if (std::get(lastState) != std::get(state) && now-debounceBack > debounceTime) + { + InputDispatcher::backButton(std::get(state)); + std::get(lastState) = std::get(state); + debounceBack = now; + } } #endif } +} diff --git a/src/dpad3wire.h b/src/dpad3wire.h new file mode 100644 index 0000000..2ab66cb --- /dev/null +++ b/src/dpad3wire.h @@ -0,0 +1,112 @@ +#pragma once + +#include + +#include "globals.h" + +#include "dpad.h" +#include "types.h" + +namespace { +namespace dpad3wire +{ +template +class Helper +{ +public: + static constexpr auto OutPin = OUT; + static constexpr auto In1Pin = IN1; + static constexpr auto In2Pin = IN2; + + void begin(); + + dpad::State read(); +}; + +template +void Helper::begin() +{ + pinMode(OUT, OUTPUT); +} + +template +dpad::State Helper::read() +{ + digitalWrite(OUT, LOW); + + pinMode(IN1, INPUT_PULLUP); + pinMode(IN2, INPUT_PULLUP); + + delay(1); + + const bool result0 = digitalRead(IN1)==LOW; + const bool result1 = digitalRead(IN2)==LOW; + + digitalWrite(OUT, HIGH); + + pinMode(IN1, INPUT_PULLDOWN); + pinMode(IN2, INPUT_PULLDOWN); + + delay(1); + + const bool result2 = digitalRead(IN1); + const bool result3 = digitalRead(IN2); + + return std::make_tuple(result0, result1, result2, result3); +} + +#ifdef FEATURE_DPAD_3WIRESW +Helper helper; +dpad::State lastState; +millis_t debounceUp, debounceDown, debounceConfirm, debounceBack; + +void init() +{ + helper.begin(); + debounceUp = debounceDown = debounceConfirm = debounceBack = millis(); +} + +void update() +{ + const auto state = helper.read(); + const auto now = millis(); + + enum { + ButtonUp = 3, + ButtonDown = 0, + ButtonConfirm = 1, + ButtonBack = 2 + }; + + constexpr auto debounceTime = 25; + + if (std::get(lastState) != std::get(state) && now-debounceUp > debounceTime) + { + if (std::get(state)) + InputDispatcher::rotate(-1); + std::get(lastState) = std::get(state); + debounceUp = now; + } + if (std::get(lastState) != std::get(state) && now-debounceDown > debounceTime) + { + if (std::get(state)) + InputDispatcher::rotate(1); + std::get(lastState) = std::get(state); + debounceDown = now; + } + if (std::get(lastState) != std::get(state) && now-debounceConfirm > debounceTime) + { + InputDispatcher::confirmButton(std::get(state)); + std::get(lastState) = std::get(state); + debounceConfirm = now; + } + if (std::get(lastState) != std::get(state) && now-debounceBack > debounceTime) + { + InputDispatcher::backButton(std::get(state)); + std::get(lastState) = std::get(state); + debounceBack = now; + } +} +#endif +} +} diff --git a/src/dpadHack.h b/src/dpadHack.h deleted file mode 100644 index a8f9ddb..0000000 --- a/src/dpadHack.h +++ /dev/null @@ -1,85 +0,0 @@ -#pragma once - -#include - -#include - -#include "globals.h" - -#include "dpad.h" - -namespace { - -template -class DPadHackHelper -{ -public: - static constexpr auto OutPin = OUT; - static constexpr auto In1Pin = IN1; - static constexpr auto In2Pin = IN2; - - void begin(); - - DPadState read(); -}; - -template -void DPadHackHelper::begin() -{ - pinMode(OUT, OUTPUT); -} - -template -DPadState DPadHackHelper::read() -{ - digitalWrite(OUT, LOW); - - pinMode(IN1, INPUT_PULLUP); - pinMode(IN2, INPUT_PULLUP); - - delay(1); - - const bool result0 = digitalRead(IN1)==LOW; - const bool result1 = digitalRead(IN2)==LOW; - - digitalWrite(OUT, HIGH); - - pinMode(IN1, INPUT_PULLDOWN); - pinMode(IN2, INPUT_PULLDOWN); - - delay(1); - - const bool result2 = digitalRead(IN1); - const bool result3 = digitalRead(IN2); - - return std::make_tuple(result0, result1, result2, result3); -} - -#ifdef FEATURE_3WIRESW -DPadHackHelper dpadHack; - -DPadState lastState; -void updateDpadHack() -{ - const auto state = dpadHack.read(); - - enum { - ButtonUp = 3, - ButtonDown = 0, - ButtonConfirm = 1, - ButtonBack = 2 - }; - - if (!std::get(lastState) && std::get(state)) - InputDispatcher::rotate(-1); - if (!std::get(lastState) && std::get(state)) - InputDispatcher::rotate(1); - if (std::get(lastState) != std::get(state)) - InputDispatcher::confirmButton(std::get(state)); - if (std::get(lastState) != std::get(state)) - InputDispatcher::backButton(std::get(state)); - - lastState = state; -} -#endif -} diff --git a/src/feedbackparser.h b/src/feedbackparser.h index df82308..b9a979a 100644 --- a/src/feedbackparser.h +++ b/src/feedbackparser.h @@ -7,6 +7,8 @@ #include "bobbycar-protocol/protocol.h" +#include "types.h" + namespace { class FeedbackParser { @@ -78,7 +80,7 @@ private: uint8_t m_incomingByte{}; uint8_t m_incomingBytePrev{}; - unsigned long m_lastFeedback{millis()}; + millis_t m_lastFeedback{millis()}; const std::reference_wrapper &m_serial; bool &m_feedbackValid; Feedback &m_feedback, m_newFeedback; diff --git a/src/globals.h b/src/globals.h index 8195bc7..9b8bc6f 100644 --- a/src/globals.h +++ b/src/globals.h @@ -12,10 +12,9 @@ #include "modeinterface.h" #include "settings.h" #include "settingssaver.h" +#include "types.h" namespace { -using pin_t = int; - int16_t raw_gas, raw_brems; float gas, brems; #ifdef FEATURE_GAMETRAK @@ -33,7 +32,7 @@ Controller front{Serial1, settings.controllerHardware.enableFrontLeft, settings. Controller back{Serial2, settings.controllerHardware.enableBackLeft, settings.controllerHardware.enableBackRight, settings.controllerHardware.invertBackLeft, settings.controllerHardware.invertBackRight}; struct { - unsigned long lastTime = millis(); + millis_t lastTime = millis(); int current{0}; int last{0}; } performance; @@ -63,7 +62,7 @@ public: static void confirmButton(bool pressed) { - static unsigned long pressBegin = 0; + static millis_t pressBegin = 0; const auto now = millis(); @@ -86,7 +85,7 @@ public: static void backButton(bool pressed) { - static unsigned long pressBegin = 0; + static millis_t pressBegin = 0; const auto now = millis(); diff --git a/src/main.cpp b/src/main.cpp index 4e865e4..7140fe3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ #include "modes/tempomatmode.h" #include "screens.h" #include "dpad.h" -#include "dpadHack.h" +#include "dpad3wire.h" #include "rotary.h" #include "serialhandler.h" #include "ota.h" @@ -22,12 +22,13 @@ #include "actions/bluetoothbeginmasteraction.h" #include "actions/bluetoothconnectbmsaction.h" #include "bobby_webserver.h" +#include "types.h" namespace { ModeInterface *lastMode{}; -unsigned long lastModeUpdate{}; -unsigned long lastStatsUpdate{}; -unsigned long lastDisplayRedraw{}; +millis_t lastModeUpdate{}; +millis_t lastStatsUpdate{}; +millis_t lastDisplayRedraw{}; constexpr auto modeUpdateRate = 50; constexpr auto statsUpdateRate = 50; @@ -46,11 +47,11 @@ void setup() initScreen(); #ifdef FEATURE_DPAD - dpad.begin(); + dpad::init(); #endif -#ifdef FEATURE_3WIRESW - dpadHack.begin(); +#ifdef FEATURE_DPAD_3WIRESW + dpad3wire::init(); #endif #ifdef FEATURE_ROTARY @@ -139,11 +140,11 @@ void loop() const auto now = millis(); #ifdef FEATURE_DPAD - updateDpad(); + dpad::update(); #endif -#ifdef FEATURE_3WIRESW - updateDpadHack(); +#ifdef FEATURE_DPAD_3WIRESW + dpad3wire::update(); #endif if (!lastModeUpdate) diff --git a/src/modes/defaultmode.h b/src/modes/defaultmode.h index 4a2e57e..f1c1e53 100644 --- a/src/modes/defaultmode.h +++ b/src/modes/defaultmode.h @@ -7,6 +7,7 @@ #include "modeinterface.h" #include "globals.h" #include "utils.h" +#include "types.h" namespace { class DefaultMode : public ModeInterface @@ -20,7 +21,7 @@ public: bool waitForBremsLoslass{false}; private: - unsigned long lastTime{millis()}; + millis_t lastTime{millis()}; float lastPwm{0}; }; diff --git a/src/rotary.h b/src/rotary.h index bf68c6b..0e3f6fd 100644 --- a/src/rotary.h +++ b/src/rotary.h @@ -3,6 +3,7 @@ #include #include "globals.h" +#include "types.h" namespace { template diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..4b4c23d --- /dev/null +++ b/src/types.h @@ -0,0 +1,6 @@ +#pragma once + +namespace { +using pin_t = int; +using millis_t = unsigned long; +}