update
This commit is contained in:
90
button.c
Normal file
90
button.c
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2017-2024 Oleg Borodin <onborodin@gmail.com>
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
#include <button.h>
|
||||
#include <tool.h>
|
||||
|
||||
typedef struct {
|
||||
bool was_pressed;
|
||||
bool strokes_ended;
|
||||
uint16_t time_counter;
|
||||
uint16_t released_time;
|
||||
uint16_t push_counter;
|
||||
} button_t;
|
||||
|
||||
button_t button;
|
||||
|
||||
void button_init(void) {
|
||||
/* D3 PD3: Set key input */
|
||||
REG_SETDOWN_BIT(DDRD, PD3);
|
||||
REG_SETUP_BIT(PORTD, PD3);
|
||||
}
|
||||
|
||||
bool button_is_pressed(void) {
|
||||
if (REG_BIT_VALUE(PIND, PD3)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t button_get(void) {
|
||||
if (button.strokes_ended) {
|
||||
uint8_t counter = button.push_counter;
|
||||
button.was_pressed = false;
|
||||
button.time_counter = 0;
|
||||
button.push_counter = 0;
|
||||
button.strokes_ended = false;
|
||||
return counter;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void button_reset(void) {
|
||||
button.was_pressed = false;
|
||||
button.time_counter = 0;
|
||||
button.push_counter = 0;
|
||||
button.strokes_ended = false;
|
||||
}
|
||||
|
||||
#define BUTTON_TIME_PRESSED 100
|
||||
#define BUTTON_TIME_RELEASED 100
|
||||
#define BUTTON_RELEASED_TIME 1500
|
||||
|
||||
void button_handle(void) {
|
||||
if (!button.strokes_ended) {
|
||||
if (!button.was_pressed) {
|
||||
if (button_is_pressed()) {
|
||||
button.time_counter++;
|
||||
button.released_time++;
|
||||
}
|
||||
if (button.time_counter > BUTTON_TIME_PRESSED) {
|
||||
button.time_counter = 0;
|
||||
button.was_pressed = true;
|
||||
}
|
||||
} else {
|
||||
if (!button_is_pressed()) {
|
||||
button.time_counter++;
|
||||
}
|
||||
if (button.time_counter > BUTTON_TIME_RELEASED) {
|
||||
button.time_counter = 0;
|
||||
button.was_pressed = false;
|
||||
button.push_counter++;
|
||||
button.released_time = 0;
|
||||
}
|
||||
}
|
||||
if (button.push_counter > 0) {
|
||||
button.released_time++;
|
||||
if (button.released_time > BUTTON_RELEASED_TIME) {
|
||||
button.strokes_ended = true;
|
||||
}
|
||||
} else {
|
||||
button.released_time = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user