Skip to content

Commit

Permalink
rg_input: Made press/release debounce configurable independently
Browse files Browse the repository at this point in the history
Also now the constants use an actual level rather than a bitmask value, it's more user friendly.

This change is related to #171 but isn't a replacement/solution for the discussed changes over there.
  • Loading branch information
ducalex committed Dec 5, 2024
1 parent 51e2a1e commit 6b55579
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
4 changes: 0 additions & 4 deletions components/retro-go/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,3 @@
#ifndef RG_SCREEN_PARTIAL_UPDATES
#define RG_SCREEN_PARTIAL_UPDATES 1
#endif

#ifndef RG_GAMEPAD_DEBOUNCE_LEVEL
#define RG_GAMEPAD_DEBOUNCE_LEVEL 0x03
#endif
31 changes: 20 additions & 11 deletions components/retro-go/rg_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#ifdef ESP_PLATFORM
#include <driver/gpio.h>
#include <driver/adc.h>
// This is a lazy way to silence deprecation notices on some esp-idf versions...
// This hardcoded value is the first thing to check if something stops working!
#define ADC_ATTEN_DB_11 3
#else
#include <SDL2/SDL.h>
#endif
Expand All @@ -17,9 +20,15 @@
static esp_adc_cal_characteristics_t adc_chars;
#endif

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

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

static void input_task(void *arg)
{
const uint8_t debounce_level = RG_GAMEPAD_DEBOUNCE_LEVEL;
uint8_t debounce[RG_KEY_COUNT];
uint32_t local_gamepad_state = 0;
uint32_t state;
int64_t next_battery_update = 0;

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

while (input_task_running)
Expand All @@ -211,16 +220,16 @@ static void input_task(void *arg)
{
for (int i = 0; i < RG_KEY_COUNT; ++i)
{
debounce[i] = ((debounce[i] << 1) | ((state >> i) & 1));
debounce[i] &= debounce_level;
uint32_t val = ((debounce[i] << 1) | ((state >> i) & 1));
debounce[i] = val & 0xFF;

if (debounce[i] == debounce_level) // Pressed
if ((val & ((1 << RG_GAMEPAD_DEBOUNCE_PRESS) - 1)) == ((1 << RG_GAMEPAD_DEBOUNCE_PRESS) - 1))
{
local_gamepad_state |= (1 << i);
local_gamepad_state |= (1 << i); // Pressed
}
else if (debounce[i] == 0x00) // Released
else if ((val & ((1 << RG_GAMEPAD_DEBOUNCE_RELEASE) - 1)) == 0)
{
local_gamepad_state &= ~(1 << i);
local_gamepad_state &= ~(1 << i); // Released
}
}
gamepad_state = local_gamepad_state;
Expand Down

0 comments on commit 6b55579

Please sign in to comment.