fork/GramFirmware

git clone https://git.julia.blue/fork/GramFirmware

CarVacrebase to configurator818d962

gram
769 B31 linesraw
1#ifndef _DEBOUNCE_HPP
2#define _DEBOUNCE_HPP
3
4#include "stdlib.hpp"
5
6typedef struct _DebounceState {
7    absolute_time_t locked_until = 0;
8    bool pressed = false;
9} DebounceState;
10
11inline bool update_debounce_state(
12    DebounceState &debounce_state,
13    bool current_reading,
14    uint32_t debounce_period_ms
15) {
16    // If currently locked out, do nothing.
17    if (!time_reached(debounce_state.locked_until)) {
18        return false;
19    }
20
21    // If latest reading differs from the last known state, update the value and timeout.
22    if (current_reading != debounce_state.pressed) {
23        debounce_state.pressed = current_reading;
24        debounce_state.locked_until = make_timeout_time_ms(debounce_period_ms);
25        return true;
26    }
27
28    return false;
29}
30
31#endif