Skip to content

Commit

Permalink
Merge pull request #521 from pimoroni/patch-input-bounce
Browse files Browse the repository at this point in the history
Fix input debounce/update rate
  • Loading branch information
Gadgetoid authored Jan 2, 2021
2 parents 4d6832a + f0dd063 commit 677f4e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 8 additions & 2 deletions 32blit/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace blit {
uint32_t pending_render_time = 0;

uint32_t last_tick_time = 0;
uint32_t last_state = 0;

bool tick(uint32_t time) {
if (last_tick_time == 0) {
Expand All @@ -77,10 +78,15 @@ namespace blit {
// catch up on updates if any pending
pending_update_time += (time - last_tick_time);
while (pending_update_time >= update_rate_ms) {
// button state changes
uint32_t changed = api.buttons.state ^ last_state;

api.buttons.pressed = changed & api.buttons.state;
api.buttons.released = changed & last_state;
last_state = api.buttons.state;

update(time - pending_update_time); // create fake timestamp that would have been accurate for the update event
pending_update_time -= update_rate_ms;

api.buttons.pressed = api.buttons.released = 0;
}

last_tick_time = time;
Expand Down
5 changes: 0 additions & 5 deletions 32blit/engine/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ namespace blit {

struct ButtonState {
ButtonState &operator=(uint32_t v) {
uint32_t changed = state ^ v;

pressed |= changed & v;
released |= changed & state;

state = v;

return *this;
Expand Down

0 comments on commit 677f4e7

Please sign in to comment.