组合按键移植

发布时间 2023-11-30 19:19:51作者: qsy_edt

参考gitee移植,key_board: 用于单片机中的小巧多功能按键支持;最强功能:支持不限数量、任意按键、任意按键的任意状态之间的随意组合!!! (gitee.com)

支持:

  1. 矩阵键盘
  2. 单io按键

 注:在此没有做矩阵键盘,注意按键的电气属性设置,引脚初始化默认是按键上拉,按下为低电平,要根据实际修改。

F103ZET6

PE3 PE4 PA0

一、单个按键功能

移植包含key_board.ckey_board.hkey_board_config.h  key_board_sample.h key_board_sample.c (在此基础上修改即可)

一、key_board_sample.h 文件引脚信息

1.修改引脚宏

2.添加按键数

3.修改引脚初始化时钟

二、key_board_sample.c文件引脚信息

 1.添加按键引脚信息

2.定义按键id及功能结构体

二、组合按键

main.c中

结构体功能初始化

static unsigned int test_id1, test_id2;
//注册组合状态
const struct key_combine_t test_combine1[] = {
        { .id = KEY_UP,     .state = KEY_PRESS },
        { .id = KEY_DOWN,   .state = KEY_PRESS_LONG },
        { .id = KEY_UP,     .state = KEY_PRESS },
};
const struct key_combine_t test_combine2[] = {
        { .id = KEY_UP,     .state = KEY_PRESS },
        { .id = KEY_DOWN,   .state = KEY_PRESS },
        { .id = KEY_UP,     .state = KEY_PRESS },
        { .id = KEY_DOWN,   .state = KEY_PRESS },
};

循环执行

        //组合按键
        if(key_check_combine_state(test_id1))
        {
            printf("combine test_id1\r\n");
        }

        if(key_check_combine_state(test_id2))
        {
            printf("combine test_id2\r\n");
        }

main.c的源码

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Printf_Use.h"
#include "key_board.h"
#include "key_board_sample.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

#define DEBUG_ON 1

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

static unsigned int test_id1, test_id2;
//注册组合状态
const struct key_combine_t test_combine1[] = {
        { .id = KEY_UP,     .state = KEY_PRESS },
        { .id = KEY_DOWN,   .state = KEY_PRESS_LONG },
        { .id = KEY_UP,     .state = KEY_PRESS },
};
const struct key_combine_t test_combine2[] = {
        { .id = KEY_UP,     .state = KEY_PRESS },
        { .id = KEY_DOWN,   .state = KEY_PRESS },
        { .id = KEY_UP,     .state = KEY_PRESS },
        { .id = KEY_DOWN,   .state = KEY_PRESS },
};

void key_print_debug_callback(const char *str)
{
    printf("%s\n", str);
}


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
    /* USER CODE BEGIN 1 */

    /* USER CODE END 1 */

    /* MCU Configuration--------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_TIM7_Init();
    MX_USART1_UART_Init();
    /* USER CODE BEGIN 2 */
    //串口重定向
    RetargetInit(&huart1);

    //按键初始化
    //注册调试接口
    key_board_debug_register(key_print_debug_callback);

    //初始化硬件及按键
    GPIO_Key_Board_Init();
    MX_GPIO_Init();
    //初始化定时器
    MX_TIM7_Init();

    printf("start running\n");

    test_id1 = key_combine_register(test_combine1, GET_ARRAY_SIZE(test_combine1));
    test_id2 = key_combine_register(test_combine2, GET_ARRAY_SIZE(test_combine2));

    unsigned int res;
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
        //up
        if(key_check_state(KEY_UP, KEY_RELEASE))
        {
            printf("KEY_UP KEY_RELEASE\r\n");
        }
        if(key_check_state(KEY_UP, KEY_PRESS))
        {
            printf("KEY_UP KEY_PRESS\r\n");
            HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_5);
        }
        if(key_check_state(KEY_UP, KEY_PRESS_LONG))
        {
            printf("KEY_UP KEY_PRESS_LONG\r\n");
        }
        if(key_check_state(KEY_UP, KEY_RELEASE_LONG))
        {
            printf("KEY_UP KEY_RELEASE_LONG\r\n");
        }
        //----------------------------------------------------------
        res = key_check_state(KEY_UP, KEY_PRESS_MULTI);
        if(res)
        {
            printf("KEY_UP KEY_PRESS_MULTI:%d\r\n", res);
        }
        res = key_check_state(KEY_UP, KEY_RELEASE_MULTI);
        if(res)
        {
            printf("KEY_UP KEY_RELEASE_MULTI:%d\r\n", res);
        }
        if(key_check_state(KEY_UP, KEY_PRESS_CONTINUOUS))
        {
            printf("KEY_UP KEY_PRESS_CONTINUOUS\r\n");
        }

        //down
        if(key_check_state(KEY_DOWN, KEY_RELEASE))
        {
            printf("KEY_DOWN KEY_RELEASE\r\n");
            HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_5);
        }
        if(key_check_state(KEY_DOWN, KEY_PRESS))
        {
            printf("KEY_DOWN KEY_PRESS\r\n");
        }
        if(key_check_state(KEY_DOWN, KEY_PRESS_LONG))
        {
            printf("KEY_DOWN KEY_PRESS_LONG\r\n");
        }
        if(key_check_state(KEY_DOWN, KEY_RELEASE_LONG))
        {
            printf("KEY_DOWN KEY_RELEASE_LONG\r\n");
        }
        res = key_check_state(KEY_DOWN, KEY_PRESS_MULTI);
        if(res)
        {
            printf("KEY_DOWN KEY_PRESS_MULTI:%d\r\n", res);
        }
        res = key_check_state(KEY_DOWN, KEY_RELEASE_MULTI);
        if(res)
        {
            printf("KEY_DOWN KEY_RELEASE_MULTI:%d\r\n", res);
        }
        if(key_check_state(KEY_DOWN, KEY_PRESS_CONTINUOUS))
        {
            printf("KEY_DOWN KEY_PRESS_CONTINUOUS\r\n");
        }

        //WK_UP
        if(key_check_state(KEY_WK_UP, KEY_RELEASE))
        {
            printf("KEY_WK_UP KEY_RELEASE\r\n");
        }
        if(key_check_state(KEY_WK_UP, KEY_PRESS))
        {
            printf("KEY_WK_UP KEY_PRESS\r\n");
            HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_5);
        }
        if(key_check_state(KEY_WK_UP, KEY_PRESS_LONG))
        {
            printf("KEY_WK_UP KEY_PRESS_LONG\r\n");
        }
        if(key_check_state(KEY_WK_UP, KEY_RELEASE_LONG))
        {
            printf("KEY_WK_UP KEY_RELEASE_LONG\r\n");
        }
        //----------------------------------------------------------
        res = key_check_state(KEY_WK_UP, KEY_PRESS_MULTI);
        if(res)
        {
            printf("KEY_WK_UP KEY_PRESS_MULTI:%d\r\n", res);
        }
        res = key_check_state(KEY_WK_UP, KEY_RELEASE_MULTI);
        if(res)
        {
            printf("KEY_WK_UP KEY_RELEASE_MULTI:%d\r\n", res);
        }
        if(key_check_state(KEY_WK_UP, KEY_PRESS_CONTINUOUS))
        {
            printf("KEY_WK_UP KEY_PRESS_CONTINUOUS\r\n");
        }

        //组合按键
        if(key_check_combine_state(test_id1))
        {
            printf("combine test_id1\r\n");
        }

        if(key_check_combine_state(test_id2))
        {
            printf("combine test_id2\r\n");
        }
    }
    /* USER CODE END 3 */
}