added debounce time

This commit is contained in:
asus
2024-01-13 14:38:02 +01:00
parent dabb4acf7a
commit a2f5f90fce
9 changed files with 80 additions and 37 deletions

View File

@@ -34,6 +34,36 @@ void keypress_more(int keycode, t_fdf *fdf)
fdf->altitude--;
}
int should_ignore_keypress(const struct timeval *current_time, t_fdf *fdf)
{
int is_less;
unsigned long current_milliseconds;
unsigned long last_milliseconds;
current_milliseconds = (current_time->tv_sec % 1000) * 1000 + current_time->tv_usec / 1000;
last_milliseconds = (fdf->last_keypress_time.tv_sec % 1000) * 1000 + fdf->last_keypress_time.tv_usec / 1000;
is_less = (current_milliseconds - last_milliseconds) < DEBOUNCE_TIME;
/*
[1 705 151 587 ,905 277,49579540-1000000]
1 705 151 587 000
1 705 151 587 905
1 705 151 587 % 1000 = 587
587 * 1000 + 905 277 / 1000 = 587 000 + 905 = 587905
[836 763,836 066]
ft_putchar('[');
ft_putnbr(current_milliseconds);
ft_putchar(',');
ft_putnbr(last_milliseconds);
ft_putchar(']');
*/
if (!is_less)
fdf->last_keypress_time = *current_time;
return is_less;
}
/*
** Q -> move left
** D -> move right
@@ -44,6 +74,11 @@ void keypress_more(int keycode, t_fdf *fdf)
*/
int keypress(int keycode, t_fdf *fdf)
{
struct timeval current_time;
gettimeofday(&current_time, NULL);
if (should_ignore_keypress(&current_time, fdf))
return (0);
if (keycode == ESCAPE)
shut_down(fdf);
else if (keycode == LEFT)