add button support
This commit is contained in:
+1
-1
@@ -35,7 +35,7 @@ pico_sdk_init()
|
||||
|
||||
# Add executable. Default name is the project name, version 0.1
|
||||
|
||||
add_executable(BattMITM_2.0 main.c smbus.c static_queue.c mitm.c status.c display.c font.c battery.c )
|
||||
add_executable(BattMITM_2.0 main.c smbus.c static_queue.c mitm.c status.c display.c font.c battery.c button.c )
|
||||
|
||||
pico_set_program_name(BattMITM_2.0 "BattMITM_2.0")
|
||||
pico_set_program_version(BattMITM_2.0 "0.1")
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Benjamin Wiegand
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
*/
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
|
||||
// stores general button state
|
||||
// called "button_bouncer" because I think it's funny
|
||||
struct button_bouncer {
|
||||
button_func_t function;
|
||||
uint64_t up_timestamp;
|
||||
uint64_t down_timestamp;
|
||||
button_event_t state;
|
||||
};
|
||||
|
||||
typedef struct button_bouncer button_bouncer_t;
|
||||
|
||||
|
||||
button_callback_t button_callback_internal = NULL;
|
||||
|
||||
button_bouncer_t button_bouncers[BUTTON_FUNCTIONS];
|
||||
|
||||
|
||||
button_func_t button_pin_to_func(uint pin) {
|
||||
switch (pin) {
|
||||
case BUTTON_NAV_UP_PIN: return BUTTON_NAV_UP;
|
||||
case BUTTON_SELECT_PIN: return BUTTON_SELECT;
|
||||
case BUTTON_NAV_DOWN_PIN: return BUTTON_NAV_DOWN;
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int64_t button_repeat_handler(alarm_id_t id, void* bouncer_ptr) {
|
||||
button_bouncer_t* bouncer = bouncer_ptr;
|
||||
uint64_t timestamp = time_us_64();
|
||||
|
||||
if (bouncer->state != BUTTON_DOWN) return 0;
|
||||
if (bouncer->down_timestamp + BUTTON_REPEAT_DELAY > timestamp) return 0;
|
||||
|
||||
button_callback_internal(bouncer->function, BUTTON_DOWN_REPEAT);
|
||||
return -BUTTON_REPEAT_INTERVAL;
|
||||
}
|
||||
|
||||
|
||||
int64_t button_state_handler(alarm_id_t id, void* bouncer_ptr) {
|
||||
button_bouncer_t* bouncer = bouncer_ptr;
|
||||
button_event_t event;
|
||||
uint64_t timestamp = time_us_64();
|
||||
uint64_t leading_timestamp;
|
||||
|
||||
if (bouncer->up_timestamp > bouncer->down_timestamp) { // up
|
||||
leading_timestamp = bouncer->up_timestamp;
|
||||
event = BUTTON_UP;
|
||||
} else if (bouncer->down_timestamp > bouncer->up_timestamp) { // down
|
||||
leading_timestamp = bouncer->down_timestamp;
|
||||
event = BUTTON_DOWN;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (bouncer->state == event) return 0;
|
||||
if (leading_timestamp + BUTTON_DEBOUNCE_THRESHOLD > timestamp) return 0;
|
||||
bouncer->state = event;
|
||||
|
||||
// start repeat
|
||||
if (event == BUTTON_DOWN)
|
||||
add_alarm_at(timestamp + BUTTON_REPEAT_DELAY, &button_repeat_handler, bouncer, true);
|
||||
|
||||
button_callback_internal(bouncer->function, event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void button_irq_handler(uint pin, uint32_t event_mask) {
|
||||
button_func_t function = button_pin_to_func(pin);
|
||||
button_bouncer_t* bouncer = &button_bouncers[function];
|
||||
uint64_t timestamp = time_us_64();
|
||||
|
||||
if (event_mask & GPIO_IRQ_EDGE_RISE) { // up
|
||||
bouncer->up_timestamp = timestamp;
|
||||
} else if (event_mask & GPIO_IRQ_EDGE_FALL) { // down
|
||||
bouncer->down_timestamp = timestamp;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
add_alarm_at(timestamp + BUTTON_DEBOUNCE_THRESHOLD, &button_state_handler, bouncer, true);
|
||||
}
|
||||
|
||||
button_event_t button_get_state(button_func_t function) {
|
||||
if (function >= BUTTON_FUNCTIONS) return BUTTON_UP;
|
||||
if (function < 0) return BUTTON_UP;
|
||||
return button_bouncers[function].state;
|
||||
}
|
||||
|
||||
void button_set_callback(button_callback_t callback) {
|
||||
button_callback_internal = callback;
|
||||
}
|
||||
|
||||
void button_setup_gpio(uint pin) {
|
||||
gpio_init(pin);
|
||||
gpio_set_dir(pin, GPIO_IN);
|
||||
gpio_pull_up(pin);
|
||||
gpio_set_irq_enabled_with_callback(pin, 0b1100, true, button_irq_handler);
|
||||
}
|
||||
|
||||
|
||||
void init_button() {
|
||||
for (int i = 0; i < BUTTON_FUNCTIONS; i++)
|
||||
button_bouncers[i] = (button_bouncer_t){i, 0, 0, BUTTON_UP};
|
||||
|
||||
button_setup_gpio(BUTTON_NAV_UP_PIN);
|
||||
button_setup_gpio(BUTTON_SELECT_PIN);
|
||||
button_setup_gpio(BUTTON_NAV_DOWN_PIN);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Benjamin Wiegand
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
*/
|
||||
#define BUTTON_DEBOUNCE_THRESHOLD 1000 // min delay in microseconds from button up/down transition to event firing
|
||||
#define BUTTON_REPEAT_INTERVAL 50000 // in microseconds
|
||||
#define BUTTON_REPEAT_DELAY 1000000 // in microseconds
|
||||
|
||||
|
||||
enum button_event {
|
||||
BUTTON_UP,
|
||||
BUTTON_DOWN,
|
||||
BUTTON_DOWN_REPEAT
|
||||
};
|
||||
|
||||
typedef enum button_event button_event_t;
|
||||
|
||||
enum button_func {
|
||||
BUTTON_NAV_UP,
|
||||
BUTTON_SELECT,
|
||||
BUTTON_NAV_DOWN,
|
||||
|
||||
BUTTON_FUNCTIONS
|
||||
};
|
||||
|
||||
typedef enum button_func button_func_t;
|
||||
|
||||
typedef void (*button_callback_t)(button_func_t function, button_event_t event);
|
||||
|
||||
|
||||
button_event_t button_get_state(button_func_t function);
|
||||
|
||||
void button_set_callback(button_callback_t callback);
|
||||
void init_button();
|
||||
|
||||
@@ -61,3 +61,10 @@
|
||||
#define DISPLAY_RESET_PIN 5
|
||||
|
||||
#define DISPLAY_FLIP_180 false
|
||||
|
||||
|
||||
// button inputs
|
||||
#define BUTTON_NAV_UP_PIN 2
|
||||
#define BUTTON_SELECT_PIN 1
|
||||
#define BUTTON_NAV_DOWN_PIN 0
|
||||
|
||||
|
||||
@@ -26,14 +26,19 @@
|
||||
#include "pico/stdlib.h"
|
||||
#include <stdio.h>
|
||||
#include "display.h"
|
||||
#include "button.h"
|
||||
|
||||
#include "pico/rand.h"
|
||||
#include "battery.h"
|
||||
#include "smbus.h"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
bool do_update = false;
|
||||
uint8_t burn_offset_x = 0;
|
||||
uint8_t burn_offset_y = 0;
|
||||
uint8_t button_down_ctrs[] = {0, 0, 0};
|
||||
uint8_t button_up_ctrs[] = {0, 0, 0};
|
||||
|
||||
bool trigger_display_update(struct repeating_timer* t) {
|
||||
do_update = true;
|
||||
@@ -88,6 +93,29 @@ void update_display() {
|
||||
|
||||
display_print("\n");
|
||||
|
||||
display_print(button_get_state(BUTTON_NAV_UP) == BUTTON_DOWN ? " D " : " U ");
|
||||
display_print(button_get_state(BUTTON_SELECT) == BUTTON_DOWN ? " D " : " U ");
|
||||
display_print(button_get_state(BUTTON_NAV_DOWN) == BUTTON_DOWN ? " D " : " U ");
|
||||
display_print("\n");
|
||||
|
||||
display_printf("%03d %03d %03d\n", button_down_ctrs[0], button_down_ctrs[1], button_down_ctrs[2]);
|
||||
display_printf("%03d %03d %03d\n", button_up_ctrs[0], button_up_ctrs[1], button_up_ctrs[2]);
|
||||
|
||||
}
|
||||
|
||||
void button_callback(button_func_t function, button_event_t event) {
|
||||
switch (event) {
|
||||
case BUTTON_UP:
|
||||
button_up_ctrs[function]++;
|
||||
break;
|
||||
case BUTTON_DOWN:
|
||||
case BUTTON_DOWN_REPEAT:
|
||||
button_down_ctrs[function]++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
do_update = true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -95,6 +123,9 @@ int main() {
|
||||
init_status();
|
||||
init_display();
|
||||
init_battery();
|
||||
init_button();
|
||||
|
||||
button_set_callback(&button_callback);
|
||||
|
||||
display_set_text_position(1, 40);
|
||||
display_set_text_color(0xFFFF);
|
||||
|
||||
Reference in New Issue
Block a user