esp32 event loop handle 代码

#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>

#include "esp_event.h"
#include "esp_timer.h"

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

ESP_EVENT_DECLARE_BASE(TIMER_EVENTS);  // declaration of the timer events family

enum {        // declaration of the specific events under the timer event family
	TIMER_EVENT_STARTED,               // raised when the timer is first started
	TIMER_EVENT_EXPIRY,         // raised when a period of the timer has elapsed
	TIMER_EVENT_STOPPED                // raised when the timer has been stopped
};

/* Event source periodic timer related definitions */
ESP_EVENT_DEFINE_BASE(TIMER_EVENTS);

void timer_started_handler(void *event_handler_arg, esp_event_base_t event_base,
		int32_t event_id, void *event_data) {
	   static int count = 0;
	    count++;
	printf("event_id: %ld , count : %d\n", event_id,count);
}

void app_main(void) {
	ESP_ERROR_CHECK(esp_event_loop_create_default());

	ESP_ERROR_CHECK(
			esp_event_handler_instance_register(TIMER_EVENTS, TIMER_EVENT_STARTED, timer_started_handler, NULL, NULL));
	ESP_ERROR_CHECK(
			esp_event_handler_instance_register(TIMER_EVENTS, TIMER_EVENT_STOPPED, timer_started_handler, NULL, NULL));

	while (1) {
		ESP_ERROR_CHECK(
				esp_event_post(TIMER_EVENTS, TIMER_EVENT_STARTED, NULL, 0, portMAX_DELAY));
		sleep(1);
		ESP_ERROR_CHECK(
				esp_event_post(TIMER_EVENTS, TIMER_EVENT_STOPPED, NULL, 0, portMAX_DELAY));
		sleep(1);

	}
//    while (true) {
//        printf("Hello from app_main!\n");
//        sleep(1);
//    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这是一段使用 micropython 在 ESP32 上实现红外接收的示例代码: ``` from machine import Pin import ir # 初始化红外接收头 ir_rx = ir.IR_RX(Pin(14)) while True: # 等待红外信号 while not ir_rx.signal(): pass # 读取红外信号 signal = ir_rx.read() # 输出红外信号的数据 print(signal.data) ``` 在这段代码中,我们首先使用 `machine` 库的 `Pin` 函数来初始化 ESP32 的红外接收头,然后在 `while` 循环中不断检测是否有红外信号,如果有就使用 `ir_rx.read()` 函数来读取红外信号,最后使用 `print` 函数将红外信号的数据输出到控制台。 希望这段代码能帮助到你。 ### 回答2: 使用MicroPython编写ESP32红外接收代码的步骤如下: 1. 首先,必须在ESP32上安装MicroPython固件。固件可以从MicroPython官方网站上下载。请根据ESP32的型号和配置选择合适的固件版本,并将固件烧录到ESP32上。 2. 创建一个名为"main.py"的新文件,这将是我们的主要代码文件。 3. 导入必要的库,即irremote和time库。此外,还可以导入其他所需的库,比如wifi库。 ```python import irremote import time ``` 4. 初始化红外接收器。使用`irremote.Pin`函数设置接收器的引脚。 ```python receiver_pin = irremote.Pin(4) ``` 5. 创建一个循环来监听红外信号。使用`irremote.get_events`函数获取接收到的红外信号。该函数接收两个参数:红外接收引脚和红外数据缓冲区大小。可以随时调整缓冲区的大小以适应接收到的红外数据量。 ```python while True: # 获取接收到的红外信号 events = irremote.get_events(receiver_pin) for event in events: # 处理接收到的红外信号,比如打印红外编码 print(event) ``` 6. 运行代码。将MicroPython固件烧录到ESP32上后,在终端或REPL(交互式解释器)中运行`main.py`文件。可以通过连接到ESP32的串口或使用WebREPL工具来执行代码。 这是一个基本的ESP32红外接收代码示例。根据具体的需求,可以根据事件类型和值执行其他操作,比如控制设备或记录红外编码。 ### 回答3: 使用MicroPython编写ESP32的红外接收代码很简单。首先,我们需要导入红外接收模块,并将其引脚连接到ESP32。 ```python from machine import Pin import ir_remote # 将红外接收器连接到GPIO 4引脚 ir_receiver = Pin(4, Pin.IN) # 创建红外接收对象 receiver = ir_remote.IRRemote(ir_receiver) # 定义处理红外接收的函数 def handle_ir(code): print("接收到红外码:", code) # 设置红外接收的回调函数 receiver.on_receive(handle_ir) # 进入主循环 while True: receiver.loop() ``` 上述代码首先导入了`Pin`和`ir_remote`模块。然后,我们创建了一个`Pin`对象,将红外接收器的引脚连接到GPIO 4。接下来,我们使用`IRRemote`类创建一个红外接收对象,并传递已经连接的引脚作为参数。 然后,我们定义了一个处理红外接收的函数`handle_ir`,它将接收到的红外码作为参数打印出来。 接下来,我们使用`on_receive`函数将处理函数注册为红外接收的回调函数。 最后,我们使用一个无限循环来保持程序运行,然后调用`loop`方法来监听红外接收的信号。 这就是使用MicroPython编写ESP32的红外接收代码的基本步骤。当ESP32接收到红外信号时,它将调用注册的处理函数进行处理。你可以根据需要进一步扩展该代码来执行其他操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值