Merge pull request #33 from speedypotato/feature/debounce_refactor

Refactored switch debouncing, added debouncing mode
This commit is contained in:
Nick G
2022-07-05 15:50:10 -07:00
committed by GitHub
8 changed files with 56 additions and 17 deletions
Binary file not shown.
+1 -1
View File
@@ -6,7 +6,7 @@
#define ENC_GPIO_SIZE 2 // Number of encoders
#define ENC_PPR 600 // Encoder PPR
#define MOUSE_SENS 1 // Mouse sensitivity multiplier
#define ENC_DEBOUNCE false // Encoder Debouncing
#define ENC_DEBOUNCE false // Encoder Debouncing
#define SW_DEBOUNCE_TIME_US 4000 // Switch debounce delay in us
#define ENC_PULSE (ENC_PPR * 4) // 4 pulses per PPR
#define REACTIVE_TIMEOUT_MAX 1000000 // HID to reactive timeout in us
+17
View File
@@ -0,0 +1,17 @@
/**
* Debouncing algorithm which waits for switch to output a constant n amount of time before outputting
* @author SpeedyPotato
**/
uint16_t constant_switch() {
uint16_t translate_buttons = 0;
for (int i = SW_GPIO_SIZE - 1; i >= 0; i--) {
if (!gpio_get(SW_GPIO[i]) &&
time_us_64() - sw_timestamp[i] >= SW_DEBOUNCE_TIME_US) {
translate_buttons = (translate_buttons << 1) | 1;
} else {
translate_buttons <<= 1;
}
}
return translate_buttons;
}
+11
View File
@@ -0,0 +1,11 @@
/**
* Simple header file to include all files in the folder
* @author SpeedyPotato
*
* To add a debounce mode, return a uint16_t representing the button states. These are saved in report.buttons as truth.
* Create debounce mode as desired and then add the #include here.
**/
extern uint64_t sw_timestamp[SW_GPIO_SIZE];
#include "constant_switch.c"
#include "minimum_hold.c"
+16
View File
@@ -0,0 +1,16 @@
/**
* Debouncing algorithm which sends a report immediately when a switch is triggered and holds it for n amount of time.
* @author SpeedyPotato
**/
uint16_t minimum_hold() {
uint16_t translate_buttons = 0;
for (int i = SW_GPIO_SIZE - 1; i >= 0; i--) {
if (time_us_64() - sw_timestamp[i] <= SW_DEBOUNCE_TIME_US || !gpio_get(SW_GPIO[i])) {
translate_buttons = (translate_buttons << 1) | 1;
} else {
translate_buttons <<= 1;
}
}
return translate_buttons;
}
+9 -15
View File
@@ -17,10 +17,12 @@
#include "hardware/pio.h"
#include "pico/multicore.h"
#include "pico/stdlib.h"
#include "rgb/rgb_include.h"
#include "tusb.h"
#include "usb_descriptors.h"
#include "rgb/rgb_include.h"
#include "debounce/debounce_include.h"
PIO pio, pio_1;
uint32_t enc_val[ENC_GPIO_SIZE];
uint32_t prev_enc_val[ENC_GPIO_SIZE];
@@ -35,6 +37,7 @@ uint64_t reactive_timeout_timestamp;
void (*ws2812b_mode)();
void (*loop_mode)();
uint16_t (*debounce_mode)();
bool joy_mode_check = true;
union {
@@ -95,18 +98,6 @@ struct report {
**/
void joy_mode() {
if (tud_hid_ready()) {
uint16_t translate_buttons = 0;
for (int i = SW_GPIO_SIZE - 1; i >= 0; i--) {
if (!gpio_get(SW_GPIO[i]) &&
time_us_64() - sw_timestamp[i] >= SW_DEBOUNCE_TIME_US) {
translate_buttons =
(translate_buttons << 1) | (!gpio_get(SW_GPIO[i]) ? 1 : 0);
} else {
translate_buttons <<= 1;
}
}
report.buttons = translate_buttons;
// find the delta between previous and current enc_val
for (int i = 0; i < ENC_GPIO_SIZE; i++) {
cur_enc_val[i] +=
@@ -133,8 +124,7 @@ void key_mode() {
/*------------- Keyboard -------------*/
uint8_t nkro_report[32] = {0};
for (int i = 0; i < SW_GPIO_SIZE; i++) {
if (!gpio_get(SW_GPIO[i]) &&
time_us_64() - sw_timestamp[i] >= SW_DEBOUNCE_TIME_US) {
if ((report.buttons >> i) % 2 == 1) {
uint8_t bit = SW_KEYCODE[i] % 8;
uint8_t byte = (SW_KEYCODE[i] / 8) + 1;
if (SW_KEYCODE[i] >= 240 && SW_KEYCODE[i] <= 247) {
@@ -284,6 +274,9 @@ void init() {
ws2812b_mode = &ws2812b_color_cycle;
}
// Debouncing Mode
debounce_mode = &minimum_hold;
// Disable RGB
if (gpio_get(SW_GPIO[8])) {
multicore_launch_core1(core1_entry);
@@ -301,6 +294,7 @@ int main(void) {
while (1) {
tud_task(); // tinyusb device task
update_inputs();
report.buttons = debounce_mode();
loop_mode();
update_lights();
}
+2
View File
@@ -5,6 +5,8 @@
* To add a lighting mode, create a function which accepts a uint32_t as a parameter.
* Create lighting mode as desired and then add the #include here.
**/
extern uint32_t enc_val[ENC_GPIO_SIZE];
#include "ws2812b_util.c"
#include "color_cycle.c"
#include "turbocharger.c"
-1
View File
@@ -30,7 +30,6 @@
* By strategically positioning led 0 at the top, this avoids lighting areas
* from suddenly appeaering.
**/
extern uint32_t enc_val[ENC_GPIO_SIZE];
#define TURBO_LIGHTS_CLAMP 0.1f
#define TURBO_LIGHTS_THRESHOLD 0.05f