add click and long_press event

This commit is contained in:
Sheikah
2023-01-27 22:37:42 +08:00
parent 423e3f3571
commit 0b8875b90a
4 changed files with 31 additions and 2 deletions

View File

@@ -2,6 +2,7 @@
#include "keypad.h"
#include "gpio.h"
#include "RotaryCoder.h"
#include "time.h"
static uint8_t key_status = 0;
static uint16_t encoder_value = 0;
@@ -32,6 +33,15 @@ static uint8_t keys[] = {
kp_KEY6,
kp_KEYENCODER
};
static int32_t pressed_at[] = {
-1,
-1,
-1,
-1,
-1,
-1,
-1
};
#define K_GET(x) (((0b1 << x) & key_status) >> x)
#define K_SET0(x) (key_status = (~(0b1 << x)) & key_status)
@@ -41,6 +51,7 @@ static uint8_t keys[] = {
uint8_t kp_query() {
// Scan keypad
int32_t now = ticks_ms();
uint16_t rota_value = EC11_COUNT();
uint16_t differ = rota_value - encoder_value;
uint8_t direction = kp_ROTATE_LEFT;
@@ -61,11 +72,23 @@ uint8_t kp_query() {
mstate = K_GET(key_n);
if (state && (!mstate)) {
K_SET1(key_n);
pressed_at[key_n] = now;
return K_EVENT(kp_KEY_DOWN, keys[key_n]);
} else if ((!state) && mstate) {
if (pressed_at[key_n] >= 0) {
pressed_at[key_n] = -1;
return K_EVENT(kp_SHORT_CLICK, keys[key_n]);
}
K_SET0(key_n);
return K_EVENT(kp_KEY_UP, keys[key_n]);
}
// ticks pressed time
if (pressed_at[key_n] >= 0 && state) {
if (ticks_diff(now, pressed_at[key_n]) > kp_LONG_PRESS_TIMEOUT_MS) {
pressed_at[key_n] = -1;
return K_EVENT(kp_LONG_PRESS, keys[key_n]);
}
}
}
return K_EVENT(kp_NOP, 0);
}