diff --git a/crossbow/crossbow.ino b/crossbow/crossbow.ino index 8268dd4..1de81b2 100644 --- a/crossbow/crossbow.ino +++ b/crossbow/crossbow.ino @@ -66,10 +66,15 @@ uint32_t lastOledTaskTime = 0; #endif - uint8_t buttonStates[2] = {HIGH, HIGH}; - uint8_t previousButtonStates[2] = {HIGH, HIGH}; - uint32_t buttonPressMillis[2] = {0, 0}; - uint8_t buttonAction[2] = {BUTTON_ACTION_NONE, BUTTON_ACTION_NONE}; +#include "tactile.h" + +Tactile button0(BUTTON_0_PIN); +Tactile button1(BUTTON_1_PIN); + + // uint8_t buttonStates[2] = {HIGH, HIGH}; + // uint8_t previousButtonStates[2] = {HIGH, HIGH}; + // uint32_t buttonPressMillis[2] = {0, 0}; + // uint8_t buttonAction[2] = {BUTTON_ACTION_NONE, BUTTON_ACTION_NONE}; #endif @@ -278,8 +283,8 @@ void setup(void) /* * Buttons on TX module */ - pinMode(BUTTON_0_PIN, INPUT_PULLUP); - pinMode(BUTTON_1_PIN, INPUT_PULLUP); + button0.start(); + button1.start(); #endif @@ -359,38 +364,10 @@ void loop(void) * If we are not receiving SBUS frames from radio, try to restart serial */ #ifdef DEVICE_MODE_TX - /* - * Button state processing - */ - buttonStates[0] = digitalRead(BUTTON_0_PIN); - buttonStates[1] = digitalRead(BUTTON_1_PIN); - //Press - if (buttonStates[0] == LOW and previousButtonStates[0] == HIGH) { - buttonPressMillis[0] = currentMillis; - } - - if (buttonStates[1] == LOW and previousButtonStates[1] == HIGH) { - buttonPressMillis[1] = currentMillis; - } - - //Release - if (buttonStates[0] == HIGH && previousButtonStates[0] == LOW) { - const uint32_t buttonTime = abs(currentMillis - buttonPressMillis[0]); - if (buttonTime > BUTTON_LONG_PRESS_TIME) { - buttonAction[0] = BUTTON_ACTION_LONG_PRESS; - } else if (buttonTime > BUTTON_MIN_PRESS_TIME) { - buttonAction[0] = BUTTON_ACTION_SHORT_PRESS; - } else { - buttonAction[0] = BUTTON_ACTION_NONE; - } - - } else { - buttonAction[0] = BUTTON_ACTION_NONE; - } - - previousButtonStates[0] = buttonStates[0]; - previousButtonStates[1] = buttonStates[1]; + //Process buttons + button0.loop(); + button1.loop(); txInput.recoverStuckFrames(); diff --git a/crossbow/tactile.cpp b/crossbow/tactile.cpp new file mode 100644 index 0000000..922da08 --- /dev/null +++ b/crossbow/tactile.cpp @@ -0,0 +1,42 @@ +#include "Arduino.h" +#include "tactile.h" + +Tactile::Tactile(uint8_t pin) { + _pin = pin; +} + +void Tactile::loop(void) { + + uint8_t pinState = digitalRead(_pin); + + //Press moment + if (pinState == LOW && _previousPinState == HIGH) { + _pressMillis = millis(); + } + + //Release moment + if (pinState == HIGH && _previousPinState == LOW) { + const uint32_t buttonTime = abs(millis() - _pressMillis); + + if (buttonTime > TACTILE_LONG_PRESS_TIME) { + _state = TACTILE_STATE_LONG_PRESS; + } else if (buttonTime > TACTILE_MIN_PRESS_TIME) { + _state = TACTILE_STATE_SHORT_PRESS; + } else { + _state = TACTILE_STATE_NONE; + } + + } else { + _state = TACTILE_STATE_NONE; + } + + _previousPinState = pinState; +} + +void Tactile::start(void) { + pinMode(_pin, INPUT_PULLUP); +} + +uint8_t Tactile::getState(void) { + return _state; +} \ No newline at end of file diff --git a/crossbow/tactile.h b/crossbow/tactile.h new file mode 100644 index 0000000..d95ac91 --- /dev/null +++ b/crossbow/tactile.h @@ -0,0 +1,30 @@ +#pragma once + +#ifndef TACTILE_H +#define TACTILE_H + +#include "Arduino.h" + +enum tactileStateFlags { + TACTILE_STATE_NONE, + TACTILE_STATE_SHORT_PRESS, + TACTILE_STATE_LONG_PRESS +}; + +#define TACTILE_MIN_PRESS_TIME 50 +#define TACTILE_LONG_PRESS_TIME 1000 + +class Tactile { + public: + Tactile(uint8_t pin); + void loop(void); + void start(void); + uint8_t getState(void); + private: + uint8_t _pin; + uint8_t _previousPinState = HIGH; + uint32_t _pressMillis = 0; + uint8_t _state = TACTILE_STATE_NONE; +}; + +#endif \ No newline at end of file diff --git a/crossbow/variables.h b/crossbow/variables.h index 4e83209..fd75559 100644 --- a/crossbow/variables.h +++ b/crossbow/variables.h @@ -93,15 +93,6 @@ enum debugConfigFlags { #define RADIO_CHANNEL_COUNT 9 // 9 channels in 2MHz range (RADIO_FREQUENCY_RANGE/RADIO_CHANNEL_WIDTH) + 1 #define RADIO_HOP_OFFSET 5 -#define BUTTON_MIN_PRESS_TIME 50 -#define BUTTON_LONG_PRESS_TIME 1000 - -enum buttonActionFlags { - BUTTON_ACTION_NONE, - BUTTON_ACTION_SHORT_PRESS, - BUTTON_ACTION_LONG_PRESS -}; - struct RadioState_t { uint32_t loraBandwidth = 250000; uint8_t loraSpreadingFactor = 7;