pokemon/input.cpp
2023-07-25 00:25:12 +00:00

74 lines
1.8 KiB
C++

#include <cstdint>
#include "smpc.h"
#include "input.hpp"
#include "common/intback.hpp"
constexpr int input_arr = 10;
constexpr int input_das = 20;
constexpr int input_debounce = 2;
static inline void
input_count(count_flop_t& button, const uint32_t input, const uint32_t mask)
{
if ((input & mask) == 0) {
if (button.count < input_debounce)
button.count += 1;
else
button.das += 1;
} else {
if (button.count == 0) {
button.flop = 0;
button.das = 0;
button.repeat = 0;
}
else if (button.count > 0)
button.count -= 1;
}
}
static 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;
}
}
input_t input = { 0 };
void digital_callback(uint8_t fsm_state, uint8_t data)
{
switch (fsm_state) {
case intback::DATA1:
input_count(input.right, data, DIGITAL__1__RIGHT);
input_count(input.left, data, DIGITAL__1__LEFT);
input_count(input.down, data, DIGITAL__1__DOWN);
input_count(input.up, data, DIGITAL__1__UP);
input_count(input.start, data, DIGITAL__1__START);
input_count(input.a, data, DIGITAL__1__A);
input_count(input.c, data, DIGITAL__1__C);
input_count(input.b, data, DIGITAL__1__B);
break;
case intback::DATA2:
input_count(input.r, data, DIGITAL__2__R);
input_count(input.x, data, DIGITAL__2__X);
input_count(input.y, data, DIGITAL__2__Y);
input_count(input.z, data, DIGITAL__2__Z);
input_count(input.l, data, DIGITAL__2__L);
break;
default: break;
}
}