Very ugly button handling
This commit is contained in:
2
.vscode/c_cpp_properties.json
vendored
2
.vscode/c_cpp_properties.json
vendored
@@ -42,5 +42,5 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"version": 3
|
"version": 4
|
||||||
}
|
}
|
||||||
@@ -18,10 +18,16 @@ Copyright (c) 20xx, MPL Contributor1 contrib1@example.net
|
|||||||
#define LORA_SS_PIN 8
|
#define LORA_SS_PIN 8
|
||||||
#define LORA_RST_PIN 4
|
#define LORA_RST_PIN 4
|
||||||
#define LORA_DI0_PIN 7
|
#define LORA_DI0_PIN 7
|
||||||
|
|
||||||
|
#define BUTTON_0_PIN 11
|
||||||
|
#define BUTTON_1_PIN 12
|
||||||
#elif defined(ARDUINO_SAMD_FEATHER_M0)
|
#elif defined(ARDUINO_SAMD_FEATHER_M0)
|
||||||
#define LORA_SS_PIN 8
|
#define LORA_SS_PIN 8
|
||||||
#define LORA_RST_PIN 4
|
#define LORA_RST_PIN 4
|
||||||
#define LORA_DI0_PIN 3
|
#define LORA_DI0_PIN 3
|
||||||
|
|
||||||
|
#define BUTTON_0_PIN 11 //Please verify
|
||||||
|
#define BUTTON_1_PIN 12 //Please verify
|
||||||
#else
|
#else
|
||||||
#error please select hardware
|
#error please select hardware
|
||||||
#endif
|
#endif
|
||||||
@@ -60,6 +66,11 @@ uint32_t lastOledTaskTime = 0;
|
|||||||
|
|
||||||
#endif
|
#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
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -263,6 +274,13 @@ void setup(void)
|
|||||||
* Prepare Serial1 for S.Bus processing
|
* Prepare Serial1 for S.Bus processing
|
||||||
*/
|
*/
|
||||||
txInput.start();
|
txInput.start();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Buttons on TX module
|
||||||
|
*/
|
||||||
|
pinMode(BUTTON_0_PIN, INPUT_PULLUP);
|
||||||
|
pinMode(BUTTON_1_PIN, INPUT_PULLUP);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
pinMode(LED_BUILTIN, OUTPUT);
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
@@ -341,6 +359,38 @@ 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
|
||||||
|
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();
|
txInput.recoverStuckFrames();
|
||||||
|
|
||||||
|
|||||||
@@ -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_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;
|
||||||
|
|||||||
Reference in New Issue
Block a user