Skip to content

Commit

Permalink
Disable click handling if we fast tap mode btn and turn encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1ol committed Jul 11, 2024
1 parent 232d01b commit 416b9c0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/SettingsSetter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ void SettingsSetter::process() {
if (gModeSwitchBtn.click())
shift = 1;
else if (gModeSwitchBtn.pressing()) {
shift = getEncoderDir();
if ((shift = getEncoderDir()))
gModeSwitchBtn.ignoreTillPressing();
}

if (shift) {
Expand Down
37 changes: 37 additions & 0 deletions src/SmartButton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include "SafeEncButton.h"

// Needed to resolve this
// https://github.com/GyverLibs/EncButton/issues/58
template<uint8_t Pin>
class SmartButton : public ButtonT<Pin> {
public:
void tick() {
ButtonT<Pin>::tick();
if (m_ignoreTillPressing && ButtonT<Pin>::press()) {
m_ignoreTillPressing = false;
m_timeout = millis();
ButtonT<Pin>::clear();
}

// we need some some before wrapped button return click() == true, after press() == false
if (!m_ignoreTillPressing && m_ignore && static_cast<uint16_t>(millis()) - m_timeout > EB_DEB_TIME) {
m_ignore = false;
ButtonT<Pin>::clear();
}
}

void ignoreTillPressing() {
m_ignoreTillPressing = true;
m_ignore = true;
}

bool click() { return !m_ignore && ButtonT<Pin>::click(); }
bool hold() { return !m_ignore && ButtonT<Pin>::hold(); }

private:
bool m_ignoreTillPressing = false;
bool m_ignore = false;
uint16_t m_timeout;
};
8 changes: 4 additions & 4 deletions src/Tools.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "Tools.h"

EncButton gEncoder(ENCODER_DT, ENCODER_CLK);
ButtonT<MODE_SWITCH_BTN> gModeSwitchBtn;
ButtonT<START_BTN> gStartBtn;
ButtonT<EXTRA_BTN> gExtraBtn;
ButtonT<VIEW_BTN> gViewBtn;
SmartButton<MODE_SWITCH_BTN> gModeSwitchBtn;
SmartButton<START_BTN> gStartBtn;
SmartButton<EXTRA_BTN> gExtraBtn;
SmartButton<VIEW_BTN> gViewBtn;
Timer gTimer;
Settings gSettings;
Display gDisplay(LiquidCrystal(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7));
Expand Down
11 changes: 6 additions & 5 deletions src/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include "SafeEncButton.h"

#include "Beeper.h"
#include "Config.h"
#include "Display.h"
#include "Settings.h"
#include "SmartButton.h"
#include "Time.h"
#include "Timer.h"
#include "Config.h"

#define ADD_TO_ENUM(enumName, current, num) \
(enumName)(((uint8_t)(current) + (uint8_t)enumName::last_ + num) % (uint8_t)enumName::last_)
Expand All @@ -19,10 +20,10 @@ bool getTime(Time& time, bool smooth = false);
void setupEncoder();

extern EncButton gEncoder;
extern ButtonT<MODE_SWITCH_BTN> gModeSwitchBtn;
extern ButtonT<START_BTN> gStartBtn;
extern ButtonT<EXTRA_BTN> gExtraBtn;
extern ButtonT<VIEW_BTN> gViewBtn;
extern SmartButton<MODE_SWITCH_BTN> gModeSwitchBtn;
extern SmartButton<START_BTN> gStartBtn;
extern SmartButton<EXTRA_BTN> gExtraBtn;
extern SmartButton<VIEW_BTN> gViewBtn;
extern Timer gTimer;
extern Settings gSettings;
extern Display gDisplay;
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ void processModeSwitch() {

int8_t dir = getEncoderDir();
if (dir) {
gModeSwitchBtn.ignoreTillPressing();

gBlocked = gBlockedByPreview = true;

setMode(ADD_TO_ENUM(ModeId, gModeId, dir));
Expand Down

0 comments on commit 416b9c0

Please sign in to comment.