Very ugly button handling

This commit is contained in:
Pawel Spychalski
2018-05-14 15:41:49 +02:00
parent 8bef083c0a
commit 2fc84c78c5
3 changed files with 67 additions and 8 deletions

View File

@@ -42,5 +42,5 @@
]
}
],
"version": 3
"version": 4
}

View File

@@ -15,15 +15,21 @@ Copyright (c) 20xx, MPL Contributor1 contrib1@example.net
#include "sbus.h"
#ifdef ARDUINO_AVR_FEATHER32U4
#define LORA_SS_PIN 8
#define LORA_RST_PIN 4
#define LORA_DI0_PIN 7
#define LORA_SS_PIN 8
#define LORA_RST_PIN 4
#define LORA_DI0_PIN 7
#define BUTTON_0_PIN 11
#define BUTTON_1_PIN 12
#elif defined(ARDUINO_SAMD_FEATHER_M0)
#define LORA_SS_PIN 8
#define LORA_RST_PIN 4
#define LORA_DI0_PIN 3
#define LORA_SS_PIN 8
#define LORA_RST_PIN 4
#define LORA_DI0_PIN 3
#define BUTTON_0_PIN 11 //Please verify
#define BUTTON_1_PIN 12 //Please verify
#else
#error please select hardware
#error please select hardware
#endif
/*
@@ -60,6 +66,11 @@ 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};
#endif
/*
@@ -263,6 +274,13 @@ void setup(void)
* Prepare Serial1 for S.Bus processing
*/
txInput.start();
/*
* Buttons on TX module
*/
pinMode(BUTTON_0_PIN, INPUT_PULLUP);
pinMode(BUTTON_1_PIN, INPUT_PULLUP);
#endif
pinMode(LED_BUILTIN, OUTPUT);
@@ -341,6 +359,38 @@ 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];
txInput.recoverStuckFrames();

View File

@@ -93,6 +93,15 @@ 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;