add keypad module, fix keys gpio define
This commit is contained in:
parent
df4db6d8c4
commit
22f90eb896
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -2,6 +2,9 @@
|
||||
"C_Cpp.errorSquiggles": "disabled",
|
||||
"files.associations": {
|
||||
"KT0915.C": "cpp",
|
||||
"tim.h": "c"
|
||||
"tim.h": "c",
|
||||
"compare": "c",
|
||||
"type_traits": "c",
|
||||
"gpio.h": "c"
|
||||
}
|
||||
}
|
71
Core/App/Input/keypad.c
Normal file
71
Core/App/Input/keypad.c
Normal file
@ -0,0 +1,71 @@
|
||||
#include <stdint.h>
|
||||
#include "keypad.h"
|
||||
#include "gpio.h"
|
||||
#include "RotaryCoder.h"
|
||||
|
||||
static uint8_t key_status = 0;
|
||||
static uint16_t encoder_value = 0;
|
||||
static GPIO_TypeDef *ports[] = {
|
||||
KEY_1_GPIO_Port,
|
||||
KEY_2_GPIO_Port,
|
||||
KEY_3_GPIO_Port,
|
||||
KEY_4_GPIO_Port,
|
||||
KEY_5_GPIO_Port,
|
||||
KEY_6_GPIO_Port,
|
||||
ENCODER_KEY_GPIO_Port
|
||||
};
|
||||
static uint16_t pins[] = {
|
||||
KEY_1_Pin,
|
||||
KEY_2_Pin,
|
||||
KEY_3_Pin,
|
||||
KEY_4_Pin,
|
||||
KEY_5_Pin,
|
||||
KEY_6_Pin,
|
||||
ENCODER_KEY_Pin
|
||||
};
|
||||
static uint8_t keys[] = {
|
||||
kp_KEY1,
|
||||
kp_KEY2,
|
||||
kp_KEY3,
|
||||
kp_KEY4,
|
||||
kp_KEY5,
|
||||
kp_KEY6,
|
||||
kp_KEYENCODER
|
||||
};
|
||||
|
||||
#define K_GET(x) (((0b1 << x) & key_status) >> x)
|
||||
#define K_SET0(x) (key_status = (~(0b1 << x)) & key_status)
|
||||
#define K_SET1(x) (key_status = (0b1 << x) | key_status)
|
||||
#define K_HGET(x) (HAL_GPIO_ReadPin(ports[x], pins[x]) == GPIO_PIN_RESET)
|
||||
#define K_EVENT(event, value) ((value << 4) | event)
|
||||
|
||||
uint8_t kp_query() {
|
||||
// Scan keypad
|
||||
uint16_t rota_value = EC11_COUNT();
|
||||
uint16_t differ = rota_value - encoder_value;
|
||||
uint8_t direction = kp_ROTATE_LEFT;
|
||||
if (differ > 0) {
|
||||
if (differ > 32768) {
|
||||
differ = 0xFFFF - differ + 1;
|
||||
direction = kp_ROTATE_RIGHT;
|
||||
}
|
||||
differ = differ / 2; // filter 2x
|
||||
}
|
||||
if (differ > 0) {
|
||||
encoder_value = rota_value;
|
||||
return K_EVENT(direction, differ);
|
||||
}
|
||||
uint8_t state, mstate, key_n;
|
||||
for (key_n = 0; key_n < sizeof(keys); key_n ++) {
|
||||
state = K_HGET(key_n);
|
||||
mstate = K_GET(key_n);
|
||||
if (state && (!mstate)) {
|
||||
K_SET1(key_n);
|
||||
return K_EVENT(kp_KEY_DOWN, keys[key_n]);
|
||||
} else if ((!state) && mstate) {
|
||||
K_SET0(key_n);
|
||||
return K_EVENT(kp_KEY_UP, keys[key_n]);
|
||||
}
|
||||
}
|
||||
return K_EVENT(kp_NOP, 0);
|
||||
}
|
21
Core/App/Input/keypad.h
Normal file
21
Core/App/Input/keypad.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#define kp_NOP 0x0
|
||||
#define kp_KEY_DOWN 0x1
|
||||
#define kp_KEY_UP 0x2
|
||||
#define kp_ROTATE_LEFT 0x3
|
||||
#define kp_ROTATE_RIGHT 0x4
|
||||
|
||||
#define kp_KEY1 0x0
|
||||
#define kp_KEY2 0x1
|
||||
#define kp_KEY3 0x2
|
||||
#define kp_KEY4 0x3
|
||||
#define kp_KEY5 0x4
|
||||
#define kp_KEY6 0x5
|
||||
#define kp_KEYENCODER 0x6
|
||||
|
||||
#define kp_Type(x) (x & 0b1111)
|
||||
#define kp_Value(x) (x >> 4)
|
||||
|
||||
// query and return keypad event
|
||||
uint8_t kp_query();
|
@ -1,11 +1,27 @@
|
||||
#include "gpio.h"
|
||||
#include "ui.h"
|
||||
#include "keypad.h"
|
||||
|
||||
void app_init() {
|
||||
// 程序开始时执行一次
|
||||
ui_screen_main();
|
||||
kp_query();
|
||||
printf("\n====start====\n");
|
||||
}
|
||||
|
||||
#include "gpio.h" //test
|
||||
void app_main_loop() {
|
||||
HAL_GPIO_TogglePin(LED_STATUS_GPIO_Port,LED_STATUS_Pin);
|
||||
HAL_Delay(300);
|
||||
// 反复被调用执行
|
||||
// HAL_GPIO_TogglePin(LED_STATUS_GPIO_Port,LED_STATUS_Pin);
|
||||
// HAL_Delay(1000);
|
||||
uint8_t event = kp_query();
|
||||
uint8_t event_type = kp_Type(event);
|
||||
uint8_t event_value = kp_Value(event);
|
||||
// printf("Raw Event Value: %d\n", event);
|
||||
if (event_type != kp_NOP) {
|
||||
printf("event: %d, key: %d\n", event_type, event_value);
|
||||
}
|
||||
|
||||
GPIO_PinState state = HAL_GPIO_ReadPin(ENCODER_KEY_GPIO_Port, ENCODER_KEY_Pin);
|
||||
HAL_GPIO_WritePin(LED_STATUS_GPIO_Port, LED_STATUS_Pin, state);
|
||||
}
|
||||
|
@ -2,38 +2,42 @@
|
||||
|
||||
#define ENCODER_KEY_Pin GPIO_PIN_13
|
||||
#define ENCODER_KEY_GPIO_Port GPIOC
|
||||
#define KEY_1_Pin GPIO_PIN_0
|
||||
#define KEY_1_GPIO_Port GPIOA
|
||||
#define KEY_2_Pin GPIO_PIN_1
|
||||
#define KEY_2_GPIO_Port GPIOA
|
||||
#define KEY_3_Pin GPIO_PIN_2
|
||||
#define KEY_3_GPIO_Port GPIOA
|
||||
#define KEY_4_Pin GPIO_PIN_3
|
||||
#define KEY_4_GPIO_Port GPIOA
|
||||
#define KEY_5_Pin GPIO_PIN_4
|
||||
#define KEY_5_GPIO_Port GPIOA
|
||||
#define LCD_BK_Pin GPIO_PIN_6
|
||||
#define LCD_BK_GPIO_Port GPIOA
|
||||
#define LED_2_Pin GPIO_PIN_8
|
||||
#define LED_2_GPIO_Port GPIOA
|
||||
#define ENCODER_A_Pin GPIO_PIN_6
|
||||
#define ENCODER_A_GPIO_Port GPIOC
|
||||
#define ENCODER_B_Pin GPIO_PIN_7
|
||||
#define ENCODER_B_GPIO_Port GPIOC
|
||||
|
||||
#define KEY_1_Pin GPIO_PIN_1
|
||||
#define KEY_1_GPIO_Port GPIOA
|
||||
#define KEY_2_Pin GPIO_PIN_3
|
||||
#define KEY_2_GPIO_Port GPIOA
|
||||
#define KEY_3_Pin GPIO_PIN_1
|
||||
#define KEY_3_GPIO_Port GPIOD
|
||||
#define KEY_4_Pin GPIO_PIN_0
|
||||
#define KEY_4_GPIO_Port GPIOA
|
||||
#define KEY_5_Pin GPIO_PIN_2
|
||||
#define KEY_5_GPIO_Port GPIOA
|
||||
#define KEY_6_Pin GPIO_PIN_4
|
||||
#define KEY_6_GPIO_Port GPIOA
|
||||
|
||||
#define LCD_BK_Pin GPIO_PIN_6
|
||||
#define LCD_BK_GPIO_Port GPIOA
|
||||
#define LCD_RST_Pin GPIO_PIN_0
|
||||
#define LCD_RST_GPIO_Port GPIOD
|
||||
#define LCD_DC_Pin GPIO_PIN_2
|
||||
#define LCD_DC_GPIO_Port GPIOD
|
||||
|
||||
#define LED_1_Pin GPIO_PIN_3
|
||||
#define LED_1_GPIO_Port GPIOD
|
||||
#define LED_2_Pin GPIO_PIN_8
|
||||
#define LED_2_GPIO_Port GPIOA
|
||||
#define LED_3_Pin GPIO_PIN_11
|
||||
#define LED_3_GPIO_Port GPIOA
|
||||
#define LED_4_Pin GPIO_PIN_12
|
||||
#define LED_4_GPIO_Port GPIOA
|
||||
#define LED_STATUS_Pin GPIO_PIN_15
|
||||
#define LED_STATUS_GPIO_Port GPIOA
|
||||
#define LCD_RST_Pin GPIO_PIN_0
|
||||
#define LCD_RST_GPIO_Port GPIOD
|
||||
#define KEY_6_Pin GPIO_PIN_1
|
||||
#define KEY_6_GPIO_Port GPIOD
|
||||
#define LCD_DC_Pin GPIO_PIN_2
|
||||
#define LCD_DC_GPIO_Port GPIOD
|
||||
#define LED_1_Pin GPIO_PIN_3
|
||||
#define LED_1_GPIO_Port GPIOD
|
||||
|
||||
#define SWI2C_SCL_Pin GPIO_PIN_3
|
||||
#define SWI2C_SCL_GPIO_Port GPIOB
|
||||
#define SWI2C_SDA_Pin GPIO_PIN_4
|
||||
|
@ -69,8 +69,7 @@ void MX_GPIO_Init(void)
|
||||
|
||||
/*Configure GPIO pins : PAPin PAPin PAPin PAPin
|
||||
PAPin */
|
||||
GPIO_InitStruct.Pin = KEY_1_Pin|KEY_2_Pin|KEY_3_Pin|KEY_4_Pin
|
||||
|KEY_5_Pin;
|
||||
GPIO_InitStruct.Pin = KEY_4_Pin|KEY_1_Pin|KEY_5_Pin|KEY_2_Pin|KEY_6_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
@ -92,10 +91,10 @@ void MX_GPIO_Init(void)
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : PtPin */
|
||||
GPIO_InitStruct.Pin = KEY_6_Pin;
|
||||
GPIO_InitStruct.Pin = KEY_3_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(KEY_6_GPIO_Port, &GPIO_InitStruct);
|
||||
HAL_GPIO_Init(KEY_3_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : PBPin PBPin */
|
||||
GPIO_InitStruct.Pin = SWI2C_SCL_Pin|SWI2C_SDA_Pin;
|
||||
|
@ -27,6 +27,7 @@
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include <stdio.h>
|
||||
#include "st7735.h"
|
||||
#include "RotaryCoder.h"
|
||||
#include "app.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
@ -69,6 +70,10 @@ void LCD_init()
|
||||
ST7735_Init(); // 屏幕初始化
|
||||
HAL_GPIO_WritePin(LCD_BK_GPIO_Port, LCD_BK_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
void Rota_init()
|
||||
{
|
||||
EC11_init();
|
||||
}
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@ -104,6 +109,7 @@ int main(void)
|
||||
MX_TIM3_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
LCD_init();
|
||||
Rota_init();
|
||||
app_init();
|
||||
/* USER CODE END 2 */
|
||||
|
||||
|
@ -43,15 +43,15 @@ void MX_TIM3_Init(void)
|
||||
htim3.Instance = TIM3;
|
||||
htim3.Init.Prescaler = 0;
|
||||
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim3.Init.Period = 255;
|
||||
htim3.Init.Period = 0xFFFF;
|
||||
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
|
||||
sConfig.IC1Polarity = TIM_ICPOLARITY_FALLING;
|
||||
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
|
||||
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
|
||||
sConfig.IC1Filter = 0;
|
||||
sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING;
|
||||
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
|
||||
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
|
||||
sConfig.IC2Filter = 0;
|
||||
|
2
Makefile
2
Makefile
@ -72,6 +72,7 @@ Core/App/Graphic/monoimg.c \
|
||||
Core/App/Graphic/ui.c \
|
||||
Core/App/Graphic/bmfont.c \
|
||||
Core/App/Graphic/asciifont.c \
|
||||
Core/App/Input/keypad.c \
|
||||
#Core/Hardware/kt0915/KT0915.C \
|
||||
#Core/Hardware/SWI2C/i2c_sw.c \
|
||||
|
||||
@ -141,6 +142,7 @@ C_INCLUDES = \
|
||||
-ICore/Hardware/RotaryCoder \
|
||||
-ICore/Hardware/ST7735 \
|
||||
-ICore/App/Graphic \
|
||||
-ICore/App/Input \
|
||||
#-ICore/Hardware/kt0915 \
|
||||
#-ICore/Hardware/SWI2C \
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user