8
8
#ifdef ESP_PLATFORM
9
9
#include <driver/gpio.h>
10
10
#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
11
14
#else
12
15
#include <SDL2/SDL.h>
13
16
#endif
17
20
static esp_adc_cal_characteristics_t adc_chars ;
18
21
#endif
19
22
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
23
32
24
33
#ifdef RG_GAMEPAD_ADC_MAP
25
34
static rg_keymap_adc_t keymap_adc [] = RG_GAMEPAD_ADC_MAP ;
@@ -196,13 +205,13 @@ bool rg_input_read_gamepad_raw(uint32_t *out)
196
205
197
206
static void input_task (void * arg )
198
207
{
199
- const uint8_t debounce_level = RG_GAMEPAD_DEBOUNCE_LEVEL ;
200
208
uint8_t debounce [RG_KEY_COUNT ];
201
209
uint32_t local_gamepad_state = 0 ;
202
210
uint32_t state ;
203
211
int64_t next_battery_update = 0 ;
204
212
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 ));
206
215
input_task_running = true;
207
216
208
217
while (input_task_running )
@@ -211,16 +220,16 @@ static void input_task(void *arg)
211
220
{
212
221
for (int i = 0 ; i < RG_KEY_COUNT ; ++ i )
213
222
{
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 ;
216
225
217
- if (debounce [ i ] == debounce_level ) // Pressed
226
+ if (( val & (( 1 << RG_GAMEPAD_DEBOUNCE_PRESS ) - 1 )) == (( 1 << RG_GAMEPAD_DEBOUNCE_PRESS ) - 1 ))
218
227
{
219
- local_gamepad_state |= (1 << i );
228
+ local_gamepad_state |= (1 << i ); // Pressed
220
229
}
221
- else if (debounce [ i ] == 0x00 ) // Released
230
+ else if (( val & (( 1 << RG_GAMEPAD_DEBOUNCE_RELEASE ) - 1 )) == 0 )
222
231
{
223
- local_gamepad_state &= ~(1 << i );
232
+ local_gamepad_state &= ~(1 << i ); // Released
224
233
}
225
234
}
226
235
gamepad_state = local_gamepad_state ;
0 commit comments