Merge pull request #57 from bobbycar-graz/39-debounce-buttons

Debounce dpad buttons
This commit is contained in:
2020-06-06 23:33:57 +02:00
committed by GitHub
16 changed files with 209 additions and 211 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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;

View File

@ -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;
};

View File

@ -9,6 +9,7 @@
#include "checkboxicon.h"
#include "icons/back.h"
#include "texts.h"
#include "types.h"
namespace {
class MainMenu;

View File

@ -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<makeComponent<MenuItem, ChangeableText, DummyAction>> vec;
unsigned long m_lastScanComplete;
millis_t m_lastScanComplete;
};
String WifiScanMenu::text() const

View File

@ -5,21 +5,24 @@
#include <Arduino.h>
#include "globals.h"
#include "types.h"
namespace {
using DPadState = std::tuple<bool, bool, bool, bool>;
namespace dpad
{
using State = std::tuple<bool, bool, bool, bool>;
template<pin_t IN1, pin_t IN2, pin_t IN3, pin_t IN4>
class DPadHelper
class Helper
{
public:
void begin();
DPadState read();
State read();
};
template<pin_t IN1, pin_t IN2, pin_t IN3, pin_t IN4>
void DPadHelper<IN1, IN2, IN3, IN4>::begin()
void Helper<IN1, IN2, IN3, IN4>::begin()
{
pinMode(IN1, INPUT_PULLUP);
pinMode(IN2, INPUT_PULLUP);
@ -28,7 +31,7 @@ void DPadHelper<IN1, IN2, IN3, IN4>::begin()
}
template<pin_t IN1, pin_t IN2, pin_t IN3, pin_t IN4>
DPadState DPadHelper<IN1, IN2, IN3, IN4>::read()
State Helper<IN1, IN2, IN3, IN4>::read()
{
const bool result0 = digitalRead(IN1);
const bool result1 = digitalRead(IN2);
@ -39,30 +42,56 @@ DPadState DPadHelper<IN1, IN2, IN3, IN4>::read()
}
#ifdef FEATURE_DPAD
DPadHelper<PINS_DPAD_UP, PINS_DPAD_DOWN, PINS_DPAD_BACK, PINS_DPAD_CONFIRM> dpad;
Helper<PINS_DPAD_UP, PINS_DPAD_DOWN, PINS_DPAD_CONFIRM, PINS_DPAD_BACK> 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<ButtonUp>(lastState) && std::get<ButtonUp>(state))
InputDispatcher::rotate(-1);
if (!std::get<ButtonDown>(lastState) && std::get<ButtonDown>(state))
InputDispatcher::rotate(1);
if (std::get<ButtonConfirm>(lastState) != std::get<ButtonConfirm>(state))
InputDispatcher::confirmButton(std::get<ButtonConfirm>(state));
if (std::get<ButtonBack>(lastState) != std::get<ButtonBack>(state))
InputDispatcher::backButton(std::get<ButtonBack>(state));
constexpr auto debounceTime = 25;
lastState = state;
if (std::get<ButtonUp>(lastState) != std::get<ButtonUp>(state) && now-debounceUp > debounceTime)
{
if (std::get<ButtonUp>(state))
InputDispatcher::rotate(-1);
std::get<ButtonUp>(lastState) = std::get<ButtonUp>(state);
debounceUp = now;
}
if (std::get<ButtonDown>(lastState) != std::get<ButtonDown>(state) && now-debounceDown > debounceTime)
{
if (std::get<ButtonDown>(state))
InputDispatcher::rotate(1);
std::get<ButtonDown>(lastState) = std::get<ButtonDown>(state);
debounceDown = now;
}
if (std::get<ButtonConfirm>(lastState) != std::get<ButtonConfirm>(state) && now-debounceConfirm > debounceTime)
{
InputDispatcher::confirmButton(std::get<ButtonConfirm>(state));
std::get<ButtonConfirm>(lastState) = std::get<ButtonConfirm>(state);
debounceConfirm = now;
}
if (std::get<ButtonBack>(lastState) != std::get<ButtonBack>(state) && now-debounceBack > debounceTime)
{
InputDispatcher::backButton(std::get<ButtonBack>(state));
std::get<ButtonBack>(lastState) = std::get<ButtonBack>(state);
debounceBack = now;
}
}
#endif
}
}

112
src/dpad3wire.h Normal file
View File

@ -0,0 +1,112 @@
#pragma once
#include <Arduino.h>
#include "globals.h"
#include "dpad.h"
#include "types.h"
namespace {
namespace dpad3wire
{
template<pin_t OUT, pin_t IN1, pin_t IN2>
class Helper
{
public:
static constexpr auto OutPin = OUT;
static constexpr auto In1Pin = IN1;
static constexpr auto In2Pin = IN2;
void begin();
dpad::State read();
};
template<pin_t OUT, pin_t IN1, pin_t IN2>
void Helper<OUT, IN1, IN2>::begin()
{
pinMode(OUT, OUTPUT);
}
template<pin_t OUT, pin_t IN1, pin_t IN2>
dpad::State Helper<OUT, IN1, IN2>::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<PINS_DPAD_3WIRESW_OUT, PINS_DPAD_3WIRESW_IN1, PINS_DPAD_3WIRESW_IN2> 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<ButtonUp>(lastState) != std::get<ButtonUp>(state) && now-debounceUp > debounceTime)
{
if (std::get<ButtonUp>(state))
InputDispatcher::rotate(-1);
std::get<ButtonUp>(lastState) = std::get<ButtonUp>(state);
debounceUp = now;
}
if (std::get<ButtonDown>(lastState) != std::get<ButtonDown>(state) && now-debounceDown > debounceTime)
{
if (std::get<ButtonDown>(state))
InputDispatcher::rotate(1);
std::get<ButtonDown>(lastState) = std::get<ButtonDown>(state);
debounceDown = now;
}
if (std::get<ButtonConfirm>(lastState) != std::get<ButtonConfirm>(state) && now-debounceConfirm > debounceTime)
{
InputDispatcher::confirmButton(std::get<ButtonConfirm>(state));
std::get<ButtonConfirm>(lastState) = std::get<ButtonConfirm>(state);
debounceConfirm = now;
}
if (std::get<ButtonBack>(lastState) != std::get<ButtonBack>(state) && now-debounceBack > debounceTime)
{
InputDispatcher::backButton(std::get<ButtonBack>(state));
std::get<ButtonBack>(lastState) = std::get<ButtonBack>(state);
debounceBack = now;
}
}
#endif
}
}

View File

@ -1,85 +0,0 @@
#pragma once
#include <tuple>
#include <Arduino.h>
#include "globals.h"
#include "dpad.h"
namespace {
template<pin_t OUT, pin_t IN1, pin_t IN2>
class DPadHackHelper
{
public:
static constexpr auto OutPin = OUT;
static constexpr auto In1Pin = IN1;
static constexpr auto In2Pin = IN2;
void begin();
DPadState read();
};
template<pin_t OUT, pin_t IN1, pin_t IN2>
void DPadHackHelper<OUT, IN1, IN2>::begin()
{
pinMode(OUT, OUTPUT);
}
template<pin_t OUT, pin_t IN1, pin_t IN2>
DPadState DPadHackHelper<OUT, IN1, IN2>::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<PINS_3WIRESW_OUT, PINS_3WIRESW_IN1, PINS_3WIRESW_IN2> dpadHack;
DPadState lastState;
void updateDpadHack()
{
const auto state = dpadHack.read();
enum {
ButtonUp = 3,
ButtonDown = 0,
ButtonConfirm = 1,
ButtonBack = 2
};
if (!std::get<ButtonUp>(lastState) && std::get<ButtonUp>(state))
InputDispatcher::rotate(-1);
if (!std::get<ButtonDown>(lastState) && std::get<ButtonDown>(state))
InputDispatcher::rotate(1);
if (std::get<ButtonConfirm>(lastState) != std::get<ButtonConfirm>(state))
InputDispatcher::confirmButton(std::get<ButtonConfirm>(state));
if (std::get<ButtonBack>(lastState) != std::get<ButtonBack>(state))
InputDispatcher::backButton(std::get<ButtonBack>(state));
lastState = state;
}
#endif
}

View File

@ -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<HardwareSerial> &m_serial;
bool &m_feedbackValid;
Feedback &m_feedback, m_newFeedback;

View File

@ -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();

View File

@ -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)

View File

@ -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};
};

View File

@ -3,6 +3,7 @@
#include <Arduino.h>
#include "globals.h"
#include "types.h"
namespace {
template<typename HANDLER, pin_t CLK, pin_t DT, pin_t SW>

6
src/types.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
namespace {
using pin_t = int;
using millis_t = unsigned long;
}