pokemon/input.hpp

62 lines
1.5 KiB
C++

#pragma once
#include <stdint.h>
struct count_flop_t {
int8_t count;
uint8_t flop;
uint8_t das;
uint8_t repeat;
};
struct input_t {
count_flop_t right;
count_flop_t left;
count_flop_t down;
count_flop_t up;
count_flop_t start;
count_flop_t a;
count_flop_t b;
count_flop_t c;
count_flop_t r;
count_flop_t x;
count_flop_t y;
count_flop_t z;
count_flop_t l;
};
void digital_callback(uint8_t fsm_state, uint8_t data);
extern input_t input;
constexpr int input_arr = 10;
constexpr int input_das = 20;
constexpr int input_debounce = 2;
static constexpr inline int32_t
input_flopped(count_flop_t& button)
{
if (button.count == input_debounce && button.flop == 0) {
button.flop = 1;
return 1;
} else if (button.flop == 1 && button.das == input_das && button.repeat == 0) {
button.repeat = 1;
button.das = 0;
return 2;
} else if (button.repeat == 1 && (button.das == input_arr)) {
button.das = 0;
return 2;
} else {
return 0;
}
}
struct event {
static inline bool cursor_left() { return input_flopped(input.left ) >= 1; }
static inline bool cursor_right() { return input_flopped(input.right) >= 1; }
static inline bool cursor_up() { return input_flopped(input.up ) >= 1; }
static inline bool cursor_down() { return input_flopped(input.down ) >= 1; }
static inline bool button_a() { return input_flopped(input.a ) == 1; }
static inline bool button_b() { return input_flopped(input.b ) == 1; }
};