Better tactile processing

This commit is contained in:
Pawel Spychalski
2018-05-14 16:17:38 +02:00
parent 2fc84c78c5
commit b5cc91c814
4 changed files with 86 additions and 46 deletions

View File

@@ -66,10 +66,15 @@ uint32_t lastOledTaskTime = 0;
#endif #endif
uint8_t buttonStates[2] = {HIGH, HIGH}; #include "tactile.h"
uint8_t previousButtonStates[2] = {HIGH, HIGH};
uint32_t buttonPressMillis[2] = {0, 0}; Tactile button0(BUTTON_0_PIN);
uint8_t buttonAction[2] = {BUTTON_ACTION_NONE, BUTTON_ACTION_NONE}; 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 #endif
@@ -278,8 +283,8 @@ void setup(void)
/* /*
* Buttons on TX module * Buttons on TX module
*/ */
pinMode(BUTTON_0_PIN, INPUT_PULLUP); button0.start();
pinMode(BUTTON_1_PIN, INPUT_PULLUP); button1.start();
#endif #endif
@@ -359,38 +364,10 @@ void loop(void)
* If we are not receiving SBUS frames from radio, try to restart serial * If we are not receiving SBUS frames from radio, try to restart serial
*/ */
#ifdef DEVICE_MODE_TX #ifdef DEVICE_MODE_TX
/*
* Button state processing
*/
buttonStates[0] = digitalRead(BUTTON_0_PIN);
buttonStates[1] = digitalRead(BUTTON_1_PIN);
//Press //Process buttons
if (buttonStates[0] == LOW and previousButtonStates[0] == HIGH) { button0.loop();
buttonPressMillis[0] = currentMillis; button1.loop();
}
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];
txInput.recoverStuckFrames(); txInput.recoverStuckFrames();

42
crossbow/tactile.cpp Normal file
View File

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

30
crossbow/tactile.h Normal file
View File

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

View File

@@ -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_CHANNEL_COUNT 9 // 9 channels in 2MHz range (RADIO_FREQUENCY_RANGE/RADIO_CHANNEL_WIDTH) + 1
#define RADIO_HOP_OFFSET 5 #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 { struct RadioState_t {
uint32_t loraBandwidth = 250000; uint32_t loraBandwidth = 250000;
uint8_t loraSpreadingFactor = 7; uint8_t loraSpreadingFactor = 7;