This repository has been archived on 2024-12-31. You can view files and clone it, but cannot push or open issues or pull requests.
mfps/App/Src/key.c

102 lines
2.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "key.h"
#include "main.h"
#include "tmc5160.h"
#include "oled.h"
#include "delay.h"
unsigned char key_i = 0;
unsigned int key_cnt[3] = {0}; //延时用计数
unsigned char key_msg[3] = {0}; //按键事件
unsigned char key_val[3] = {0}; //按键值
unsigned char oled_flag = 1; //oled初始化时为点亮状态1表示点亮0表示熄灭。
#define KEY_CNT 5
#define PAGEMAX 2
void Key_Scan(void) // 扫描K1-K3的状态
{
key_val[0] = HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
key_val[1] = HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
key_val[2] = HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin);
//检测按键
if(key_i >= 3) key_i = 0;
if (key_val[key_i] == 0 && key_msg[key_i] == 0) //按键按下
{
key_cnt[key_i]++;
if(key_cnt[key_i] > KEY_CNT)
{
key_cnt[key_i] = 0;
key_msg[key_i] = 1; //按键按下触发事件1
}
}
if (key_val[key_i] == 1 && key_msg[key_i] == 2) //按键抬起
{
key_cnt[key_i]++;
if(key_cnt[key_i] > KEY_CNT)
{
key_cnt[key_i] = 0;
key_msg[key_i] = 0; //完成抬起动作后,再清空事件
}
}
//按键按顺序增加
key_i++;
if(key_i >= 3) key_i = 0;
}
void msg_clr()
{
key_msg[0] = 0;
key_msg[1] = 0;
key_msg[2] = 0;
}
//按键功能
void key_act(void)
{
if(key_msg[0] == 1) //K1完成按下后切换OLED与绿灯状态两者同亮同灭
{
if(oled_flag == 0)
{
HAL_GPIO_WritePin(LED_NOR_GPIO_Port,LED_NOR_Pin,GPIO_PIN_RESET);
OLED_DisPlay_On();
oled_flag = 1;
key_msg[0] = 2; //完成OLED和绿灯操作后触发事件2
}else
{
HAL_GPIO_WritePin(LED_ERR_GPIO_Port,LED_NOR_Pin,GPIO_PIN_SET);
OLED_DisPlay_Off();
oled_flag = 0;
key_msg[0] = 2; //完成OLED和绿灯操作后触发事件2
}
}
if(key_msg[1] == 1)//K2完成按下后进行翻页page one->last page->page one
{
if(oled_page >= PAGEMAX)
{
oled_page = 1;
}else oled_page++;
OLED_NewFrame();
key_msg[1] = 2;
}
if(key_msg[2] == 1)//K3完成按下后
{
tmc5160_sw =(tmc5160_sw == 0);
key_msg[2] = 2;
}
}