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
1720static 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
2534static rg_keymap_adc_t keymap_adc [] = RG_GAMEPAD_ADC_MAP ;
@@ -196,13 +205,13 @@ bool rg_input_read_gamepad_raw(uint32_t *out)
196205
197206static 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