这一章很重要。前面我们已经实现了显示图片和显示时间两个应用了,往后还会更多。所以我们要搭一个框架把这些应用管理起来。比如菜单,应用实例的创建和注销等。
1、先实现键盘操作:
keys.h
/*
* keys.h
*
* Created on: Sep 17, 2023
* Author: YoungMay
*/
#ifndef SRC_DRIVERS_KEYS_H_
#define SRC_DRIVERS_KEYS_H_
#include "stm32f4xx_hal.h"
typedef struct {
uint8_t X_C;
uint8_t Y_C;
GPIO_TypeDef *G_GPIO_B1;
uint16_t G_GPIO_Pin_B1;
GPIO_TypeDef *G_GPIO_B2;
uint16_t G_GPIO_Pin_B2;
GPIO_TypeDef *G_GPIO_B3;
uint16_t G_GPIO_Pin_B3;
GPIO_TypeDef *G_GPIO_B4;
uint16_t G_GPIO_Pin_B4;
} Keys_t;
extern Keys_t KeyBoard1;
extern Keys_t KeyBoard2;
extern Keys_t KeyBoardMain;
#define KEY_RETURN_BUTTON 0b00000100
#define KEY_LEFT 0b10000000
#define KEY_RIGHT 0b01000000
#define KEY_UP 0b00100000
#define KEY_DOWN 0b00010000
#define KEY_BUTTON_A 0b00001000
#define KEY_BUTTON_B 0b00000100
#define KEY_BUTTON_C 0b00000010
#define KEY_BUTTON_D 0b00000001
//void ADC_DMA_start();
uint8_t keys_get(Keys_t *board);
void keys_update();
#endif /* SRC_DRIVERS_KEYS_H_ */
keys.c
/*
* keys.c
*
* Created on: Sep 17, 2023
* Author: YoungMay
*/
#include "keys.h"
#include "tools.h"
extern ADC_HandleTypeDef hadc1;
ADC_ChannelConfTypeDef sConfig = { 0 };
#define ADC_MAX_NUM 4
uint32_t ADC_Values[4] = { 0 };
uint32_t keys_get_adc(uint8_t idx) {
uint32_t res;
if (HAL_ADC_Start(&hadc1) != HAL_OK) {
Serial_print("ADC开启失败");
}
if (HAL_ADC_PollForConversion(&hadc1, 10) != HAL_OK) //等待转换完成,第二个参数表示超时时间,单位ms
{
Serial_print("ADC转换失败");
}
if (HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC)) {
res = HAL_ADC_GetValue(&hadc1);
}
return res;
}
uint8_t keys_get(Keys_t *board) {
uint8_t res = 0;
if (ADC_Values[board->X_C] > 3500)
res = res | 0b10000000;
if (ADC_Values[board->X_C] < 500)
res = res | 0b1000000;
if (ADC_Values[board->Y_C] > 3500)
res = res | 0b100000;
if (ADC_Values[board->Y_C] < 500)
res = res | 0b10000;
if (!HAL_GPIO_ReadPin(board->G_GPIO_B1, board->G_GPIO_Pin_B1))
res = res | 0b1000;
if (!HAL_GPIO_ReadPin(board->G_GPIO_B2, board->G_GPIO_Pin_B2))
res = res | 0b100;
if (!HAL_GPIO_ReadPin(board->G_GPIO_B3, board->G_GPIO_Pin_B3))
res = res | 0b10;
if (!HAL_GPIO_ReadPin(board->G_GPIO_B4, board->G_GPIO_Pin_B4))
res = res | 0b1;
return res;
}
void keys_update() {
for (uint8_t i = 0; i < ADC_MAX_NUM; i++) {
ADC_Values[i] = keys_get_adc(i);
}
HAL_ADC_Stop(&hadc1);
// Serial_print("res = %d, %d, %d, %d", ADC_Values[0], ADC_Values[1],
// ADC_Values[2], ADC_Values[3]);
}
Keys_t KeyBoard1 = { 0, 1, GPIOE, GPIO_PIN_3,
GPIOE, GPIO_PIN_4, GPIOE, GPIO_PIN_5, GPIOE, GPIO_PIN_2 };
Keys_t KeyBoard2 = { 2, 3, GPIOE, GPIO_PIN_8,
GPIOE, GPIO_PIN_9, GPIOE, GPIO_PIN_10, GPIOE, GPIO_P