ESP32学习笔记(3)-添加自定义组件及按键中断

目录

重新调整工程目录结构 主要参考官方的例程进行简单更改

一、新工程创建

  1. 进入到ESP32 idf 安装目录,复制一个例程到自己的文件目录并改一个名字
    在这里插入图片描述在这里插入图片描述
  2. vs code 打开,新建一下文件夹
components
         ---bsp
              ---boards
                      ---led.c 
                      ---led.h
                      ---key.c
                      ---key.h
              ---include
                      ----bsp_board.h
              ---src
                      ----bsp_board.h
         ---CMakeList.txt
       

在这里插入图片描述
3.在components下的CMakeList.txt 添加以下代码

file(GLOB_RECURSE BSP_SRCS
    "bsp/boards/led.c"      
    "bsp/boards/key.c"
     )

list(APPEND BSP_BOARD_SRC "./bsp/src/bsp_board.c")


idf_component_register(
    SRCS
        ${BSP_SRCS}
        ${BSP_BOARD_SRC}
    INCLUDE_DIRS
        "bsp/include"
        "bsp/boards"
    REQUIRES
     spi_flash
     freertos
  )

  1. 更改工程目录 文件下的 CMakeList.txt 文件代码,将project(blink) 改为 project(demo)
    在这里插入图片描述

二、添加代码

led.c

/**
 * @file led.c
 * @brief 
 * @version 0.1
 * @date 2021-12-18
 * 
 * @copyright 
 *
 */
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_rom_sys.h"
#include "bsp_board.h"
#include "led.h"

/**
 * @brief init led pin
 * 
 * @return
 *    - ESP_OK: Success
 *    - Others: Fail
 */
esp_err_t led_init(void)
{
    gpio_reset_pin(led_pin);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(led_pin, GPIO_MODE_OUTPUT);

    return ESP_OK;
}

/**
 * @brief open the led
 * @return
 *    - ESP_OK: Success
 *    - Others: Fail
 */
esp_err_t led_open(void)
{  
   printf("Turning on the LED\n");
   gpio_set_level(led_pin, 0);

   return ESP_OK ;
}

/**
 * @brief close the led
 * @return
 *    - ESP_OK: Success
 *    - Others: Fail
 */
esp_err_t led_close(void)
{  
   printf("Turning off the LED\n");
   gpio_set_level(led_pin, 1);
   
   return ESP_OK ;
}

led.h

#ifndef _LED_H_
#define _LED_H_

#ifdef __cplusplus
extern "C" {
#endif 

#include <stdbool.h>
#include "esp_err.h"

#define led_pin  GPIO_NUM_23

esp_err_t led_init(void);

esp_err_t led_open(void);

esp_err_t led_close(void);

#ifdef __cplusplus
}
#endif

#endif

key.c

/**
 * @file key.c
 * @brief 
 * @version 0.1
 * @date 2021-12-18
 * 
 * @copyright 
 *
 */
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_rom_sys.h"
#include "bsp_board.h"
#include "key.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"

/*创建消息队列*/
 static xQueueHandle gpio_evt_queue = NULL;


static void IRAM_ATTR gpio_isr_handler(void* arg)
{
    uint32_t gpio_num = (uint32_t) arg;
    xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
}


static void key_gpio_task(void* arg)
{
    uint32_t io_num;
    for(;;) {
        if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {

            printf("收到消息\r\n");
            printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));

        }
    }
}


/**
 * @brief init key pin
 * 
 * @return
 *    - ESP_OK: Success
 *    - Others: Fail
 */
esp_err_t key_init(void)
{
  
    gpio_config_t io_config=
    {
      io_config.pin_bit_mask =(1ull<<KEY_PIN),
      io_config.mode =GPIO_MODE_INPUT,
      io_config.pull_up_en =0x01,
      io_config.pull_down_en =0x00,
      io_config.intr_type =GPIO_INTR_POSEDGE
    };

    gpio_config(&io_config);

    //create a queue to handle gpio event from isr
    gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));

     //start gpio task
    xTaskCreate(key_gpio_task, "key_gpio_task", 2048, NULL, 10, NULL);

    //install gpio isr service
    gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
    //hook isr handler for specific gpio pin
    gpio_isr_handler_add(KEY_PIN, gpio_isr_handler, (void*) KEY_PIN);

    return ESP_OK;
}

key.h

#ifndef _KEY_H_
#define _KEY_H_

#ifdef __cplusplus
extern "C" {
#endif 

#include <stdbool.h>
#include "esp_err.h"

#define KEY_PIN  GPIO_NUM_5
#define ESP_INTR_FLAG_DEFAULT 0

esp_err_t key_init(void);


#ifdef __cplusplus
}
#endif

#endif

main.c

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "led.h"
#include  "bsp_board.h"

void app_main(void)
{
    bsp_board_init();
 
    while(1) {
        led_open();
        vTaskDelay(1000 / portTICK_PERIOD_MS);

        led_close();  
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

三、编译下载到开发板并查看结果

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在ESP-IDF项目中添加自定义组件,有以下几个步骤: 1. 创建自定义组件文件夹:在ESP-IDF项目的`components`目录下创建一个新的文件夹,用于存放自定义组件的代码和头文件。你可以为该文件夹取一个有意义的名称,以便在项目中使用。 2. 添加组件源代码和头文件:将自定义组件的源代码和头文件放入新创建的文件夹中。确保这些文件是与ESP32芯片兼容的,并且符合ESP-IDF编程规范。 3. 创建组件CMakeLists.txt文件:在自定义组件文件夹中创建一个CMakeLists.txt文件,用于描述组件的配置信息。其中包括组件的源文件、头文件和依赖项等。示例内容如下: ``` idf_component_register(SRCS "component_source_file1.c" "component_source_file2.c" INCLUDE_DIRS "include" REQUIRES dependency1 dependency2 ) ``` 确保将"component_source_file1.c"和"component_source_file2.c"替换为你组件的实际源文件名称,将"include"替换为你组件的头文件所在的文件夹名称,将"dependency1"和"dependency2"替换为你组件所依赖的其他组件的名称。 4. 在主项目中引用自定义组件:在主项目的CMakeLists.txt文件中,添加自定义组件的引用。示例代码如下: ``` set(EXTRA_COMPONENT_DIRS "path_to_custom_component_folder" "path_to_another_custom_component_folder" ) ``` 将"path_to_custom_component_folder"和"path_to_another_custom_component_folder"替换为你自定义组件所在的文件夹路径。 5. 构建和编译项目:在完成以上步骤后,重新构建和编译ESP-IDF项目。确保自定义组件被成功添加并正确引用。 如此,你就成功地将自定义组件添加到ESP-IDF项目中啦!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值