美文网首页
ESP8266 API --- Key

ESP8266 API --- Key

作者: Alex_Lin | 来源:发表于2017-04-21 16:19 被阅读0次

    标签: esp8266 key

    KEY 相关API

    有关按键的程序在SDK根目录下的driver_lib文件夹中的key.c和key.h。

    相关结构体

    /** 按键处理函数 数据类型 */
    typedef void (* key_function)(void);
    
    struct single_key_param 
    {
        /*按键处理流程标号*/
        uint8 key_level;
        /*GPIO引脚号(0-15)*/
        uint8 gpio_id;
        /*GPIO 功能(详见ESP8266 管脚清单.xlsx)*/
        uint8 gpio_func;
        /*GPIO MUX寄存器(参见gpio.h)*/
        uint32 gpio_name;
        /*定时器相关数据结构(详见SDK开发文档)*/
        os_timer_t key_5s;
        /*定时器相关数据结构(详见SDK开发文档)*/
        os_timer_t key_50ms;
       /*按键短按处理程序*/
        key_function short_press;
       /*按键长按处理程序*/
        key_function long_press;
    };
    struct keys_param
    {
        /*按键的数量*/
        uint8 key_num;
        /*按键的相关信息*/
        struct single_key_param **single_key;
    };
    

    API

    struct single_key_param *key_init_single(uint8 gpio_id, uint32 gpio_name, uint8 gpio_func, key_function long_press, key_function short_press);
    

    说明:返回一个由程序分配的存储着按键数据的指针

    按键检查流程图:

    按键检查流程图

    源码剖析

    • 按键初始化函数
    void ICACHE_FLASH_ATTR key_init(struct keys_param *keys)
    {
        uint8 i;
    
        /* 配置GPIO中断的中断服务函数 */
        ETS_GPIO_INTR_ATTACH(key_intr_handler, keys);
        /* 关闭GPIO中断 */
        ETS_GPIO_INTR_DISABLE();
        for (i = 0; i < keys->key_num; i++) 
        {
            keys->single_key[i]->key_level = 1;
            /* 配置引脚模式为GPIO模式 */
            PIN_FUNC_SELECT(keys->single_key[i]->gpio_name, keys->single_key[i]->gpio_func);
            /*配置GPIO模式为输入模式 */
            gpio_output_set(0, 0, 0, GPIO_ID_PIN(keys->single_key[i]->gpio_id));
            /* 初始化GPIO中断配置 */
            gpio_register_set(GPIO_PIN_ADDR(keys->single_key[i]->gpio_id), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE)
                              | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE)
                              | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE));
            /* 清除GPIO中断标志 */
            GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(keys->single_key[i]->gpio_id));
            /* 配置GPIO中断触发类型为下降沿触发 */
            gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_NEGEDGE);
        }
        /* 开启GPIO中断 */
        ETS_GPIO_INTR_ENABLE();
    }
    
    • 按键长按检测函数
    static void ICACHE_FLASH_ATTR key_5s_cb(struct single_key_param *single_key)
    {
        /* 关闭按键长按检测定时器 */
        os_timer_disarm(&single_key->key_5s);
        /* 按键处于按下状态 */
        if (0 == GPIO_INPUT_GET(GPIO_ID_PIN(single_key->gpio_id))) 
        {
            /* 执行按键长按处理函数 */
            if (single_key->long_press) 
            {
                single_key->long_press();
            }
        }
    }
    
    • 按键短按检测函数
    static void ICACHE_FLASH_ATTR key_50ms_cb(struct single_key_param *single_key)
    {
        /* 关闭按键短按检测定时器 */
        os_timer_disarm(&single_key->key_50ms);
        /* 按键处于松开状态 */
        if (1 == GPIO_INPUT_GET(GPIO_ID_PIN(single_key->gpio_id)))
        {
            /* 停止按键长按状态检测 */
            os_timer_disarm(&single_key->key_5s);
            single_key->key_level = 1;
            /* 设置该按键中断触发方式为下降沿触发(等待按键再次按下) */
            gpio_pin_intr_state_set(GPIO_ID_PIN(single_key->gpio_id), GPIO_PIN_INTR_NEGEDGE);
            /* 运行按键短按处理程序 */
            if (single_key->short_press) 
            {
                single_key->short_press();
            }
        } 
        else 
        {
            /* 设置该按键中断触发方式为上升沿触发(等待按键松开) */
            gpio_pin_intr_state_set(GPIO_ID_PIN(single_key->gpio_id), GPIO_PIN_INTR_POSEDGE);
        }
    }
    
    • GPIO中断处理函数
    static void key_intr_handler(struct keys_param *keys)
    {
        uint8 i;
        //读取GPIO状态寄存器,获取中断信息
        uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
        for (i = 0; i < keys->key_num; i++) 
        {
            if (gpio_status & BIT(keys->single_key[i]->gpio_id)) 
            {
                /** 关闭该按键的中断 */
                gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_DISABLE);
    
                /** 清除该按键的中断标志 */
                GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(keys->single_key[i]->gpio_id));
    
                /** 按键处理流程一 */
                if (keys->single_key[i]->key_level == 1)
                {
                    /** 启动按键长按检测定时器 */
                    os_timer_disarm(&keys->single_key[i]->key_5s);
                    os_timer_setfn(&keys->single_key[i]->key_5s, (os_timer_func_t *)key_5s_cb, keys->single_key[i]);
                    os_timer_arm(&keys->single_key[i]->key_5s, 5000, 0);
                    keys->single_key[i]->key_level = 0;
                    /** 设置该按键中断触发方式为上升沿触发(等待按键松开) */
                    gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_POSEDGE);
                } 
                /** 按键处理流程二 */
                else
                {
                    /** 启动按键短按检测定时器 */
                    os_timer_disarm(&keys->single_key[i]->key_50ms);
                    os_timer_setfn(&keys->single_key[i]->key_50ms, (os_timer_func_t *)key_50ms_cb, keys->single_key[i]);
                    os_timer_arm(&keys->single_key[i]->key_50ms, 50, 0);
                }
            }
        }
    }
    

    KEY Demo

    #include "ets_sys.h"
    #include "osapi.h"
    #include "os_type.h"
    #include "user_interface.h"
    #include "eagle_soc.h"
    #include "gpio.h"
    #include "key.h"
    
    /*********************************************************************************
     * @brief      开关输入引脚配置
     ********************************************************************************/
    #define SWITCH_Pin_NUM         5
    #define SWITCH_Pin_FUNC        FUNC_GPIO5
    #define SWITCH_Pin_MUX         PERIPHS_IO_MUX_GPIO5_U
    
    #define SWITCH_Pin_Rd_Init()   GPIO_DIS_OUTPUT(SWITCH_Pin_NUM)
    #define SWITCH_Pin_Wr_Init()   GPIO_OUTPUT_SET(SWITCH_Pin_NUM,0)
    #define SWITCH_Pin_Set_High()  GPIO_OUTPUT_SET(SWITCH_Pin_NUM,1)
    #define SWITCH_Pin_Set_Low()   GPIO_OUTPUT_SET(SWITCH_Pin_NUM,0)
    #define SWITCH_Pin_State       ( GPIO_INPUT_GET(SWITCH_Pin_NUM) != 0 )
    
    /*********************************************************************************
     * @brief     按键相关变量
     ********************************************************************************/
    static struct keys_param switch_param;
    static struct single_key_param *switch_signle;
    static bool status = true;
    
    /*********************************************************************************
     * @brief       开关短按状态处理函数
     * @param       [in/out]  void
     * @return      void
     * @note        None
     ********************************************************************************/
    static void Switch_ShortPress_Handler( void )
    {
        if( status == true )
        {
            status = false;
        }
        else
        {
            status = true;
        }
    }
    
    /********************************************************************************
     * @brief       输入初始化函数
     * @param       [in/out]  void
     * @return      void
     * @note        None
    ********************************************************************************/
    void drv_Switch_Init( void )
    {
        switch_signle = key_init_single( SWITCH_Pin_NUM, SWITCH_Pin_MUX,
                                         SWITCH_Pin_FUNC,
                                         NULL,
                                         &Switch_ShortPress_Handler );
        switch_param.key_num = 1;
        switch_param.single_key = &switch_signle;
    
        key_init( &switch_param );
    }
    
    

    http://blog.csdn.net/qq_15647227/article/details/52403998
    http://www.cnblogs.com/mmmmar/p/6545202.html
    http://blog.csdn.net/qq_15647227/article/details/52218376

    相关文章

      网友评论

          本文标题:ESP8266 API --- Key

          本文链接:https://www.haomeiwen.com/subject/itbdzttx.html