Use pulses instead of frame based movement

This commit is contained in:
Jaby 2024-01-15 20:46:00 -05:00
parent 2b371dc473
commit 5f0e8a9212
1 changed files with 31 additions and 19 deletions

View File

@ -10,32 +10,43 @@ namespace ScreenCenter {
using namespace JabyEngine;
using GenericButton = Periphery::GenericController::Button;
struct ButtonWatcher {
struct ButtonPulser {
static constexpr auto StartTime = 2500_ms;
static constexpr auto PulseTime = 100_ms;
enum struct State {
WentDown,
DownFor2500ms,
Pulse,
Unkown
};
SimpleTimer<uint16_t> timer;
SimpleTimer<uint8_t> timer;
uint8_t pulse_time;
void setup() {
this->timer.reset();
this->pulse_time = StartTime;
}
void reset() {
ButtonPulser::setup();
}
State check(GenericButton button) {
const auto controller = Periphery::get_primary_controller_as<Periphery::GenericController>();
if(!controller.button.was_down(button)) {
this->timer.reset();
ButtonPulser::reset();
if(controller.button.went_down(button)) {
return State::WentDown;
}
}
if(this->timer.is_expired_for(2500_ms)) {
return State::DownFor2500ms;
if(this->timer.is_expired_for(this->pulse_time)) {
this->pulse_time = PulseTime;
this->timer.reset();
return State::Pulse;
}
return State::Unkown;
}
@ -49,8 +60,9 @@ namespace ScreenCenter {
static constexpr uint16_t ScanlinesV = 240;
#endif //JABYENGINE_PAL
static const auto frame = Frame::create();
static ButtonWatcher button_watcher[4];
static const auto frame = Frame::create();
static ButtonPulser button_pulse[4];
static void (*update)() = nullptr;
static int16_t offset_x = 0;
static int16_t offset_y = 0;
@ -90,10 +102,10 @@ namespace ScreenCenter {
}
static void update_interactive() {
static const auto handle_button = [](ButtonWatcher& button_state, GenericButton button, int16_t &dst, const int16_t mlp) {
switch(button_state.check(button)) {
case ButtonWatcher::State::WentDown:
case ButtonWatcher::State::DownFor2500ms:
static const auto handle_button = [](ButtonPulser& button_pulse, GenericButton button, int16_t &dst, const int16_t mlp) {
switch(button_pulse.check(button)) {
case ButtonPulser::State::WentDown:
case ButtonPulser::State::Pulse:
dst += (1*mlp);
break;
}
@ -108,10 +120,10 @@ namespace ScreenCenter {
const auto controller = Periphery::get_primary_controller_as<Periphery::GenericController>();
handle_button(button_watcher[0], GenericButton::Left, offset_x, -1);
handle_button(button_watcher[1], GenericButton::Right, offset_x, 1);
handle_button(button_watcher[2], GenericButton::Up, offset_y, -1);
handle_button(button_watcher[3], GenericButton::Down, offset_y, 1);
handle_button(button_pulse[0], GenericButton::Left, offset_x, -1);
handle_button(button_pulse[1], GenericButton::Right, offset_x, 1);
handle_button(button_pulse[2], GenericButton::Up, offset_y, -1);
handle_button(button_pulse[3], GenericButton::Down, offset_y, 1);
auto cursor = FontWriter::update(CenterPoint);
FontWriter::bios_font_writer.write(cursor, ModeStr, TVModeStr);
@ -124,8 +136,8 @@ namespace ScreenCenter {
static void setup() {
Shared::back_menu.reset();
for(auto& button : button_watcher) {
button.setup();
for(auto& pulse : button_pulse) {
pulse.setup();
}
update = update_enter_state;