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

@ -4,6 +4,7 @@
void ui_text_number18x32(uint32_t num, uint16_t x, uint16_t y, uint16_t color); void ui_text_number18x32(uint32_t num, uint16_t x, uint16_t y, uint16_t color);
/* 绘制界面元素 */ /* 绘制界面元素 */
void ui_com_title_bar(uint8_t clear, uint8_t *text, uint32_t bytes_len);
void ui_com_freq_digital(uint8_t clear); void ui_com_freq_digital(uint8_t clear);
void ui_com_fm_am(uint8_t clear); void ui_com_fm_am(uint8_t clear);
void ui_com_vol_bar(uint8_t clear); void ui_com_vol_bar(uint8_t clear);

View File

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

View File

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

View File

@ -3,7 +3,6 @@
#include "ui.h" #include "ui.h"
#include "keypad.h" #include "keypad.h"
#include "global.h" #include "global.h"
#include "time.h"
void app_init() { void app_init() {
// 程序开始时执行一次 // 程序开始时执行一次
@ -33,7 +32,7 @@ void app_main_loop() {
} }
printf("Freq: %d\n", global_data.freq); printf("Freq: %d\n", global_data.freq);
ui_com_freq_digital(1); ui_com_freq_digital(1);
} else if (event_type == kp_KEY_DOWN) { } else if (event_type == kp_SHORT_CLICK) {
if (event_value == kp_KEY1) { if (event_value == kp_KEY1) {
// 切换FM和AM // 切换FM和AM
if (global_data.rf_mode == G_RF_MODE_AM) { if (global_data.rf_mode == G_RF_MODE_AM) {
@ -46,6 +45,8 @@ void app_main_loop() {
ui_com_fm_am(1); ui_com_fm_am(1);
ui_com_freq_digital(1); ui_com_freq_digital(1);
} }
} else if (event_type == kp_LONG_PRESS) {
//
} else if (event_type != kp_NOP) { } else if (event_type != kp_NOP) {
printf("event: %d, key: %d\n", event_type, event_value); printf("event: %d, key: %d\n", event_type, event_value);
} }