Skip to content

Commit

Permalink
Suppress nkro when button stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
whowechina committed Apr 29, 2024
1 parent bbd2ff3 commit ed44780
Show file tree
Hide file tree
Showing 8 changed files with 1,046 additions and 1,026 deletions.
Binary file modified Production/firmware/mai_pico.uf2
Binary file not shown.
204 changes: 107 additions & 97 deletions firmware/src/button.c
Original file line number Diff line number Diff line change
@@ -1,97 +1,107 @@
/*
* Mai Controller Buttons
* WHowe <github.com/whowechina>
*
*/

#include "button.h"

#include <stdint.h>
#include <stdbool.h>

#include "hardware/gpio.h"
#include "hardware/timer.h"
#include "hardware/pwm.h"

#include "config.h"
#include "board_defs.h"

static const uint8_t gpio_def[] = BUTTON_DEF;
static uint8_t gpio_real[] = BUTTON_DEF;

#define BUTTON_NUM (sizeof(gpio_def))

static bool sw_val[BUTTON_NUM]; /* true if pressed */
static uint64_t sw_freeze_time[BUTTON_NUM];

void button_init()
{
for (int i = 0; i < BUTTON_NUM; i++)
{
sw_val[i] = false;
sw_freeze_time[i] = 0;
uint8_t gpio = mai_cfg->alt.buttons[i];
if (gpio > 29) {
gpio = gpio_def[i];
}
gpio_real[i] = gpio;
gpio_init(gpio);
gpio_set_function(gpio, GPIO_FUNC_SIO);
gpio_set_dir(gpio, GPIO_IN);
gpio_pull_up(gpio);
}
}

uint8_t button_num()
{
return BUTTON_NUM;
}

uint8_t button_real_gpio(int id)
{
if (id >= BUTTON_NUM) {
return 0xff;
}
return gpio_real[id];
}

uint8_t button_default_gpio(int id)
{
if (id >= BUTTON_NUM) {
return 0xff;
}
return gpio_def[id];
}

static uint16_t button_reading;

/* If a switch flips, it freezes for a while */
#define DEBOUNCE_FREEZE_TIME_US 3000
void button_update()
{
uint64_t now = time_us_64();
uint16_t buttons = 0;

for (int i = BUTTON_NUM - 1; i >= 0; i--) {
bool sw_pressed = !gpio_get(gpio_real[i]);

if (now >= sw_freeze_time[i]) {
if (sw_pressed != sw_val[i]) {
sw_val[i] = sw_pressed;
sw_freeze_time[i] = now + DEBOUNCE_FREEZE_TIME_US;
}
}

buttons <<= 1;
if (sw_val[i]) {
buttons |= 1;
}
}

button_reading = buttons;
}

uint16_t button_read()
{
return button_reading;
}
/*
* Mai Controller Buttons
* WHowe <github.com/whowechina>
*
*/

#include "button.h"

#include <stdint.h>
#include <stdbool.h>

#include "hardware/gpio.h"
#include "hardware/timer.h"
#include "hardware/pwm.h"

#include "config.h"
#include "board_defs.h"

static const uint8_t gpio_def[] = BUTTON_DEF;
static uint8_t gpio_real[] = BUTTON_DEF;

#define BUTTON_NUM (sizeof(gpio_def))

static bool sw_val[BUTTON_NUM]; /* true if pressed */
static uint64_t sw_freeze_time[BUTTON_NUM];

void button_init()
{
for (int i = 0; i < BUTTON_NUM; i++)
{
sw_val[i] = false;
sw_freeze_time[i] = 0;
uint8_t gpio = mai_cfg->alt.buttons[i];
if (gpio > 29) {
gpio = gpio_def[i];
}
gpio_real[i] = gpio;
gpio_init(gpio);
gpio_set_function(gpio, GPIO_FUNC_SIO);
gpio_set_dir(gpio, GPIO_IN);
gpio_pull_up(gpio);
}
}

bool button_is_stuck()
{
for (int i = 0; i < BUTTON_NUM; i++) {
if (!gpio_get(gpio_real[i])) {
return true;
}
}
return false;
}

uint8_t button_num()
{
return BUTTON_NUM;
}

uint8_t button_real_gpio(int id)
{
if (id >= BUTTON_NUM) {
return 0xff;
}
return gpio_real[id];
}

uint8_t button_default_gpio(int id)
{
if (id >= BUTTON_NUM) {
return 0xff;
}
return gpio_def[id];
}

static uint16_t button_reading;

/* If a switch flips, it freezes for a while */
#define DEBOUNCE_FREEZE_TIME_US 3000
void button_update()
{
uint64_t now = time_us_64();
uint16_t buttons = 0;

for (int i = BUTTON_NUM - 1; i >= 0; i--) {
bool sw_pressed = !gpio_get(gpio_real[i]);

if (now >= sw_freeze_time[i]) {
if (sw_pressed != sw_val[i]) {
sw_val[i] = sw_pressed;
sw_freeze_time[i] = now + DEBOUNCE_FREEZE_TIME_US;
}
}

buttons <<= 1;
if (sw_val[i]) {
buttons |= 1;
}
}

button_reading = buttons;
}

uint16_t button_read()
{
return button_reading;
}
44 changes: 24 additions & 20 deletions firmware/src/button.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
/*
* Mai Controller Buttons
* WHowe <github.com/whowechina>
*/

#ifndef BUTTONS_H
#define BUTTONS_H

#include <stdint.h>
#include <stdbool.h>
#include "hardware/flash.h"

void button_init();
uint8_t button_num();
void button_update();
uint16_t button_read();
uint8_t button_real_gpio(int id);
uint8_t button_default_gpio(int id);

#endif
/*
* Mai Controller Buttons
* WHowe <github.com/whowechina>
*/

#ifndef BUTTONS_H
#define BUTTONS_H

#include <stdint.h>
#include <stdbool.h>
#include "hardware/flash.h"

void button_init();

/* if anykey is pressed, no debounce */
bool button_is_stuck();

uint8_t button_num();
void button_update();
uint16_t button_read();
uint8_t button_real_gpio(int id);
uint8_t button_default_gpio(int id);

#endif
Loading

0 comments on commit ed44780

Please sign in to comment.