红外遥控例程:
选择 NEC 解码器,初始化 GPIO 引脚。然后在 while 循环中扫描按键、打印输出接收到的红外数据,当 KEY0 按下后将会把最近一次接收到的红外数据通过红外发射头发送出去。
#include <rtthread.h> #include <rtdevice.h> #include <board.h> #include "infrared.h" #define DBG_TAG "main" #define DBG_LVL DBG_LOG #include <rtdbg.h> rt_int16_t key_scan(void) { if (rt_pin_read(PIN_KEY0) == PIN_LOW) { rt_thread_mdelay(50); if (rt_pin_read(PIN_KEY0) == PIN_LOW) { return PIN_KEY0; } } return -RT_ERROR; } int main(void) { unsigned int count = 1; rt_int16_t key; struct infrared_decoder_data infrared_data; /* 选择 NEC 解码器 */ ir_select_decoder("nec"); /* 设置按键引脚为输入模式 */ rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT); /* 设置 RGB 引脚为输出模式*/ rt_pin_mode(PIN_LED_R, PIN_MODE_OUTPUT); rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT); rt_pin_mode(PIN_LED_G,PIN_MODE_OUTPUT); rt_pin_write(PIN_LED_R, PIN_HIGH); rt_pin_write(PIN_LED_B, PIN_HIGH); rt_pin_write(PIN_LED_G,PIN_HIGH); while (count > 0) { /*绿灯常亮 */ rt_pin_write(PIN_LED_G,PIN_LOW); /* 按键扫描 */ key = key_scan(); if(key == PIN_KEY0) { /* 有按键按下,蓝灯亮起 */ rt_pin_write(PIN_LED_B, PIN_LOW); infrared_data.data.nec.repeat = 0; /* 发送红外数据 */ infrared_write("nec",&infrared_data); rt_thread_mdelay(200); LOG_I("SEND OK: addr:0x%02X key:0x%02X repeat:%d", infrared_data.data.nec.addr,infrared_data.data.nec.key, infrared_data.data.nec.repeat); } else if(infrared_read("nec",&infrared_data)== RT_EOK) { /* 读取到红外数据,红灯亮起 */ rt_pin_write(PIN_LED_R, PIN_LOW); LOG_I("RECEIVE OK: addr:0x%02Xkey:0x%02X repeat:%d", infrared_data.data.nec.addr,infrared_data.data.nec.key, infrared_data.data.nec.repeat); } rt_thread_mdelay(10); /* 熄灭蓝灯 */ rt_pin_write(PIN_LED_B, PIN_HIGH); /* 熄灭红灯 */ rt_pin_write(PIN_LED_R, PIN_HIGH); count++; } return 0; } |
实物图片: