renamed 3wire dpad to dpadHack.h; implemented proper dpad controls

This commit is contained in:
Patrick Pichler
2020-05-22 15:15:53 +02:00
parent a6f3bd658f
commit 10814713e9
4 changed files with 119 additions and 38 deletions

View File

@@ -62,10 +62,15 @@ build_flags =
-DPINS_TX2=26
-DPINS_GAS=35
-DPINS_BREMS=33
-DFEATURE_3WIRESW
-DPINS_3WIRESW_OUT=0
-DPINS_3WIRESW_IN1=16
-DPINS_3WIRESW_IN2=27
; -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_ROTARY
; -DPINS_ROTARY_CLK=16
; -DPINS_ROTARY_DT=27

View File

@@ -9,53 +9,37 @@
namespace {
using DPadState = std::tuple<bool, bool, bool, bool>;
template<pin_t OUT, pin_t IN1, pin_t IN2>
template<pin_t IN1, pin_t IN2, pin_t IN3, pin_t IN4>
class DPadHelper
{
public:
static constexpr auto OutPin = OUT;
static constexpr auto In1Pin = IN1;
static constexpr auto In2Pin = IN2;
public:
void begin();
DPadState read();
};
template<pin_t OUT, pin_t IN1, pin_t IN2>
void DPadHelper<OUT, IN1, IN2>::begin()
template<pin_t IN1, pin_t IN2, pin_t IN3, pin_t IN4>
void DPadHelper<IN1, IN2, IN3, IN4>::begin()
{
pinMode(OUT, OUTPUT);
pinMode(IN1, INPUT_PULLUP);
pinMode(IN2, INPUT_PULLUP);
pinMode(IN3, INPUT_PULLUP);
pinMode(IN4, INPUT_PULLUP);
}
template<pin_t OUT, pin_t IN1, pin_t IN2>
DPadState DPadHelper<OUT, IN1, IN2>::read()
template<pin_t IN1, pin_t IN2, pin_t IN3, pin_t IN4>
DPadState DPadHelper<IN1, IN2, IN3, IN4>::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);
const bool result0 = digitalRead(IN1);
const bool result1 = digitalRead(IN2);
const bool result2 = digitalRead(IN3);
const bool result3 = digitalRead(IN4);
return std::make_tuple(result0, result1, result2, result3);
}
#ifdef FEATURE_3WIRESW
DPadHelper<PINS_3WIRESW_OUT, PINS_3WIRESW_IN1, PINS_3WIRESW_IN2> dpad;
#ifdef FEATURE_DPAD
DPadHelper<PINS_DPAD_IN1, PINS_DPAD_IN2, PINS_DPAD_IN3, PINS_DPAD_IN4> dpad;
DPadState lastState;
void updateDpad()

83
src/dpadHack.h Normal file
View File

@@ -0,0 +1,83 @@
#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::button(std::get<ButtonConfirm>(state));
lastState = state;
}
#endif
}

View File

@@ -10,6 +10,7 @@
#include "modes/tempomatmode.h"
#include "screens.h"
#include "dpad.h"
#include "dpadHack.h"
#include "rotary.h"
#include "serialhandler.h"
#include "ota.h"
@@ -40,8 +41,12 @@ void setup()
initScreen();
#ifdef FEATURE_DPAD
dpad.begin();
#endif
#ifdef FEATURE_3WIRESW
dpad.begin();
dpadHack.begin();
#endif
#ifdef FEATURE_ROTARY
@@ -91,8 +96,12 @@ void loop()
{
const auto now = millis();
#ifdef FEATURE_DPAD
updateDpad();
#endif
#ifdef FEATURE_3WIRESW
updateDpad();
updateDpadHack();
#endif
if (!lastModeUpdate)