Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes to how timer differences are calculated #8585

Merged
merged 5 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion quantum/debounce/eager_pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ static bool matrix_need_update;
#define DEBOUNCE_ELAPSED 251
#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)

static uint16_t custom_wrap_timer_reference = 0;
static uint8_t custom_wrap_timer_last_value = 0;

static uint8_t custom_wrap_timer_read(void) {
uint16_t custom_wrap_timer_new_reference = timer_read();
uint16_t diff = custom_wrap_timer_new_reference - custom_wrap_timer_reference;
custom_wrap_timer_reference = custom_wrap_timer_new_reference;
custom_wrap_timer_last_value = (custom_wrap_timer_last_value + diff) % (MAX_DEBOUNCE + 1);
return custom_wrap_timer_last_value;
}
purdeaandrei marked this conversation as resolved.
Show resolved Hide resolved

void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);

Expand All @@ -59,7 +70,7 @@ void debounce_init(uint8_t num_rows) {
}

void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
uint8_t current_time = timer_read() % MAX_DEBOUNCE;
uint8_t current_time = custom_wrap_timer_read();
if (counters_need_update) {
update_debounce_counters(num_rows, current_time);
}
Expand Down
13 changes: 12 additions & 1 deletion quantum/debounce/eager_pr.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ static bool counters_need_update;
#define DEBOUNCE_ELAPSED 251
#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)

static uint16_t custom_wrap_timer_reference = 0;
static uint8_t custom_wrap_timer_last_value = 0;

static uint8_t custom_wrap_timer_read(void) {
uint16_t custom_wrap_timer_new_reference = timer_read();
uint16_t diff = custom_wrap_timer_new_reference - custom_wrap_timer_reference;
custom_wrap_timer_reference = custom_wrap_timer_new_reference;
custom_wrap_timer_last_value = (custom_wrap_timer_last_value + diff) % (MAX_DEBOUNCE + 1);
return custom_wrap_timer_last_value;
}

void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);

Expand All @@ -48,7 +59,7 @@ void debounce_init(uint8_t num_rows) {
}

void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
uint8_t current_time = timer_read() % MAX_DEBOUNCE;
uint8_t current_time = custom_wrap_timer_read();
bool needed_update = counters_need_update;
if (counters_need_update) {
update_debounce_counters(num_rows, current_time);
Expand Down
5 changes: 4 additions & 1 deletion tmk_core/common/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# include "avr/timer_avr.h"
#endif

#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a))
#define TIMER_DIFF(a, b, max) ((max == UINT8_MAX) ? ((uint8_t)((a)-(b))) : ( \
(max == UINT16_MAX) ? ((uint16_t)((a)-(b))) : ( \
(max == UINT32_MAX) ? ((uint32_t)((a)-(b))) : ( \
(a) >= (b) ? (a) - (b) : (max) + 1 - (b) + (a) ))))
#define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX)
#define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX)
#define TIMER_DIFF_32(a, b) TIMER_DIFF(a, b, UINT32_MAX)
Expand Down