Button driver based on Hongmeng OS

Keys are commonly used input systems. How to obtain key values ​​accurately and efficiently is a frequently faced problem. Today we look at how to obtain the key values ​​of independent keys in the Hongmeng system.

Goals

This time we take the USER button S2 at the lower left corner of the Hi3861 core board as an example. When the button is pressed, the information will be output through the serial port corresponding to USB Type-c.

The corresponding relationship of button S2 in the real object is shown as the yellow line in the figure below:

Button driver based on Hongmeng OS

Button schematic

The schematic diagram of the button S2 in the lower left corner of the core board is as follows:

Button driver based on Hongmeng OS

When S2 is pressed, GPIO05 is connected to GND, and the GPIO05 input is low at this time.

Code

Implementation method 1: Read the state of input IO

#include <stdio.h>

#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_errno.h"

static void Key_Task(const char* arg)
{   
    (void)arg;

    printf("Enter the Key_Task ... \n");

    while (1) 
    {
        WifiIotGpioValue wigv;

        GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_5,&wigv);

        if (wigv == WIFI_IOT_GPIO_VALUE0)
        {
            usleep(10*1000);        //10ms

            while(1)
            {
                GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_5,&wigv);
                if (wigv == WIFI_IOT_GPIO_VALUE1){
                    printf("[DEMO] GPIO05 Low level.\n");
                    break;
                }        
            }                           
        }
    }

    return;
}

static void KeyExampleEntry(void)
{
    unsigned int ret = 0;
    GpioInit();
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_IO_FUNC_GPIO_5_GPIO);
    GpioSetDir(WIFI_IOT_GPIO_IDX_5, WIFI_IOT_GPIO_DIR_IN);

    if (ret != WIFI_IOT_SUCCESS)
    {
        printf("===== ERROR ======gpio -> GpioSetDir ret:%d\r\n", ret);
        return;
    }

    osThreadAttr_t attr = {0};

    attr.name = "Key_Task";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024;
    attr.priority = osPriorityNormal;

    if(osThreadNew((osThreadFunc_t)Key_Task,NULL,&attr) == NULL)
    {
        printf("Failed to create Key_Task !\n");
    }   
}

SYS_RUN(KeyExampleEntry);

Compile the code:

python build.py wifiiot

Button driver based on Hongmeng OS

After updating the firmware, restart the minimum system board, open the serial port assistant, click the USER button S2 on the core board, and the serial port assistant outputs information as follows:

Button driver based on Hongmeng OS

Note:
This example creates a new task to read the state of the button in a loop. KeyExampleEntry is used as the entry function of the application. You cannot use the time-consuming operation of while(1) at will. You must return quickly, otherwise it will hinder the rest of the Hongmeng OS The operation of the application program, therefore, create a dedicated task (thread) for button status monitoring in this entry function to determine the button status.

GPIO interrupt

From the above schematic diagram, we can see that when button S2 is not pressed, GPIO05 is in the default state of high level. When button S2 is pressed, GPIO05 is connected to GND, and GPIO05 is pulled low. When button S2 is released At that time, GPIO05 returns to high level.

In this process, when the button S2 is pressed, GPIO05 will receive a level change from high to low. We call this process a falling edge; when the button S2 is released, GPIO05 will receive a low To a high level change, we call this process a rising edge.

In summary, without considering the influence of jitter, GPIO05 will receive a falling edge every time the button is pressed; GPIO05 will receive a rising edge when the button is released.

We register an edge trigger function (either rising edge or falling edge trigger) on the GPIO05 pin, then this registered edge trigger callback function is called once, in theory, there is a key press.


#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_errno.h"

/* gpio callback func */
void gpio5_isr_func(char *arg)
{
    (void)arg;
    printf("----- gpio05 isr success -----\r\n");
}

static void KeyExampleEntry(void)
{
    unsigned int ret = 0;
    GpioInit();
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_IO_FUNC_GPIO_5_GPIO);
    GpioSetDir(WIFI_IOT_GPIO_IDX_5, WIFI_IOT_GPIO_DIR_IN);
    //IoSetPull(WIFI_IOT_GPIO_IDX_5,WIFI_IOT_IO_PULL_UP);

    if (ret != WIFI_IOT_SUCCESS) 
    {
        printf("===== ERROR ======gpio -> GpioSetDir ret:%d\r\n", ret);
        return;
    }
    ret = GpioRegisterIsrFunc(WIFI_IOT_GPIO_IDX_5,WIFI_IOT_INT_TYPE_EDGE,WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH, gpio5_isr_func, NULL);
    if (ret != WIFI_IOT_SUCCESS) 
    {
        printf("===== ERROR ======gpio -> hi_gpio_register_isr_function ret:%d\r\n", ret);
    }
}

SYS_RUN(KeyExampleEntry);

Code description:

  1. WIFI_IOT_IO_NAME_GPIO_5 is a GPIO connected to button S2. To realize button interrupt capture, you need to use the IoSetFunc() function to redefine the port function;

  2. Call the GpioSetDir() function, set GPIO05 as an input, and set the port as a pull-up input (Pull Up) through the IoSetPull() function;

  3. Call the GpioRegisterIsrFunc() function to complete the registration binding of GPIO05 and the callback function gpio5_isr_func(), and set the trigger mode to rising edge trigger: WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH. When the button S2 is lifted, a rising edge is generated to trigger the callback function gpio5_isr_func() to work.

Button driver based on Hongmeng OS

Through the above two methods, we have learned how to obtain independent button status, how to create tasks in the Hongmeng system and the use of external interrupts. With this code, we can also be used to identify the response signal of the pyro-infrared sensor.

Information acquisition

No public comments section Top Comments obtain the code for this article.

ps: The article was first published by electronics enthusiasts.

Xiaoha has something to say

Recently, I saw in the Hongmeng exchange group that a large number of development boards have to be transplanted with the Hongmeng operating system. When these manufacturers have transplanted the boards, then Hongmeng will really become a climate. As an embedded developer, learn a real-time operating system. It is indispensable. Learning everything is learning, why not learn a promising one?

Button driver based on Hongmeng OS

Welcome to follow

Programmer Xiaoha takes you to play embedded, WeChat search: embedded from 0 to 1 , more dry goods are waiting for you.

Guess you like

Origin blog.51cto.com/14950741/2547478