-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbd2ff3
commit ed44780
Showing
8 changed files
with
1,046 additions
and
1,026 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.