Skip to content

Commit 6b55579

Browse files
committed
rg_input: Made press/release debounce configurable independently
Also now the constants use an actual level rather than a bitmask value, it's more user friendly. This change is related to ducalex#171 but isn't a replacement/solution for the discussed changes over there.
1 parent 51e2a1e commit 6b55579

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

components/retro-go/config.h

-4
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,3 @@
144144
#ifndef RG_SCREEN_PARTIAL_UPDATES
145145
#define RG_SCREEN_PARTIAL_UPDATES 1
146146
#endif
147-
148-
#ifndef RG_GAMEPAD_DEBOUNCE_LEVEL
149-
#define RG_GAMEPAD_DEBOUNCE_LEVEL 0x03
150-
#endif

components/retro-go/rg_input.c

+20-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#ifdef ESP_PLATFORM
99
#include <driver/gpio.h>
1010
#include <driver/adc.h>
11+
// This is a lazy way to silence deprecation notices on some esp-idf versions...
12+
// This hardcoded value is the first thing to check if something stops working!
13+
#define ADC_ATTEN_DB_11 3
1114
#else
1215
#include <SDL2/SDL.h>
1316
#endif
@@ -17,9 +20,15 @@
1720
static esp_adc_cal_characteristics_t adc_chars;
1821
#endif
1922

20-
// This is a lazy way to silence deprecation notices on some esp-idf versions...
21-
// This hardcoded value is the first thing to check if something stops working!
22-
#define ADC_ATTEN_DB_11 3
23+
// Number of cycles the hardware state must be maintained before the change is reflected in rg_input_read_gamepad.
24+
// The reaction time is calculated as such: N*10ms +/- 10ms. Different hardware types have different requirements.
25+
// Valid range is 1-9
26+
#ifndef RG_GAMEPAD_DEBOUNCE_PRESS
27+
#define RG_GAMEPAD_DEBOUNCE_PRESS (2)
28+
#endif
29+
#ifndef RG_GAMEPAD_DEBOUNCE_RELEASE
30+
#define RG_GAMEPAD_DEBOUNCE_RELEASE (2)
31+
#endif
2332

2433
#ifdef RG_GAMEPAD_ADC_MAP
2534
static rg_keymap_adc_t keymap_adc[] = RG_GAMEPAD_ADC_MAP;
@@ -196,13 +205,13 @@ bool rg_input_read_gamepad_raw(uint32_t *out)
196205

197206
static void input_task(void *arg)
198207
{
199-
const uint8_t debounce_level = RG_GAMEPAD_DEBOUNCE_LEVEL;
200208
uint8_t debounce[RG_KEY_COUNT];
201209
uint32_t local_gamepad_state = 0;
202210
uint32_t state;
203211
int64_t next_battery_update = 0;
204212

205-
memset(debounce, debounce_level, sizeof(debounce));
213+
// Start the task with debounce history full to allow a button held during boot to be detected
214+
memset(debounce, 0xFF, sizeof(debounce));
206215
input_task_running = true;
207216

208217
while (input_task_running)
@@ -211,16 +220,16 @@ static void input_task(void *arg)
211220
{
212221
for (int i = 0; i < RG_KEY_COUNT; ++i)
213222
{
214-
debounce[i] = ((debounce[i] << 1) | ((state >> i) & 1));
215-
debounce[i] &= debounce_level;
223+
uint32_t val = ((debounce[i] << 1) | ((state >> i) & 1));
224+
debounce[i] = val & 0xFF;
216225

217-
if (debounce[i] == debounce_level) // Pressed
226+
if ((val & ((1 << RG_GAMEPAD_DEBOUNCE_PRESS) - 1)) == ((1 << RG_GAMEPAD_DEBOUNCE_PRESS) - 1))
218227
{
219-
local_gamepad_state |= (1 << i);
228+
local_gamepad_state |= (1 << i); // Pressed
220229
}
221-
else if (debounce[i] == 0x00) // Released
230+
else if ((val & ((1 << RG_GAMEPAD_DEBOUNCE_RELEASE) - 1)) == 0)
222231
{
223-
local_gamepad_state &= ~(1 << i);
232+
local_gamepad_state &= ~(1 << i); // Released
224233
}
225234
}
226235
gamepad_state = local_gamepad_state;

0 commit comments

Comments
 (0)