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);
}

View File

@@ -5,6 +5,8 @@
#define kp_KEY_UP 0x2
#define kp_ROTATE_LEFT 0x3
#define kp_ROTATE_RIGHT 0x4
#define kp_SHORT_CLICK 0x5
#define kp_LONG_PRESS 0x6
#define kp_KEY1 0x0
#define kp_KEY2 0x1
@@ -17,5 +19,7 @@
#define kp_Type(x) (x & 0b1111)
#define kp_Value(x) (x >> 4)
#define kp_LONG_PRESS_TIMEOUT_MS 250
// query and return keypad event
uint8_t kp_query();