mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-07-15 08:32:42 +08:00
feature(ir): add ir tx rx driver
This commit is contained in:
6
examples/peripherals/ir_rx/CMakeLists.txt
Normal file
6
examples/peripherals/ir_rx/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
# The following four lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(ir_rx)
|
9
examples/peripherals/ir_rx/Makefile
Normal file
9
examples/peripherals/ir_rx/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := ir_rx
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
83
examples/peripherals/ir_rx/README.md
Normal file
83
examples/peripherals/ir_rx/README.md
Normal file
@ -0,0 +1,83 @@
|
||||
# IR RX Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
In this example, we use IO5 to recv the nec infrared signal
|
||||
|
||||
## How to Use Example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with ESP8266 SoC (e.g., ESP8266-DevKitC, etc.)
|
||||
* A USB cable for power supply and programming
|
||||
|
||||
### Configure the Project
|
||||
|
||||
```
|
||||
make menuconfig
|
||||
```
|
||||
|
||||
* Set serial port under Serial Flasher Options.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
make -j4 flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
```
|
||||
I (471) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:2
|
||||
I (19161) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x0, cmd2: 0xff
|
||||
I (19171) main: ir rx nec data: 0x0
|
||||
I (19211) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x0, cmd2: 0xff
|
||||
I (19221) main: ir rx nec data: 0x0
|
||||
I (19321) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x0, cmd2: 0xff
|
||||
I (19331) main: ir rx nec data: 0x0
|
||||
I (19431) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x0, cmd2: 0xff
|
||||
I (19431) main: ir rx nec data: 0x0
|
||||
I (19541) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x0, cmd2: 0xff
|
||||
I (19541) main: ir rx nec data: 0x0
|
||||
I (20601) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x1, cmd2: 0xfe
|
||||
I (20601) main: ir rx nec data: 0x1
|
||||
I (20651) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x1, cmd2: 0xfe
|
||||
I (20661) main: ir rx nec data: 0x1
|
||||
I (20761) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x1, cmd2: 0xfe
|
||||
I (20761) main: ir rx nec data: 0x1
|
||||
I (20871) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x1, cmd2: 0xfe
|
||||
I (20871) main: ir rx nec data: 0x1
|
||||
I (20981) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x1, cmd2: 0xfe
|
||||
I (20981) main: ir rx nec data: 0x1
|
||||
I (22041) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x2, cmd2: 0xfd
|
||||
I (22041) main: ir rx nec data: 0x2
|
||||
I (22091) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x2, cmd2: 0xfd
|
||||
I (22091) main: ir rx nec data: 0x2
|
||||
I (22201) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x2, cmd2: 0xfd
|
||||
I (22201) main: ir rx nec data: 0x2
|
||||
I (22311) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x2, cmd2: 0xfd
|
||||
I (22311) main: ir rx nec data: 0x2
|
||||
I (22411) main: addr1: 0x55, addr2: 0xaa, cmd1: 0x2, cmd2: 0xfd
|
||||
I (22421) main: ir rx nec data: 0x2
|
||||
```
|
||||
|
||||
If you have a logic analyzer, you can use a logic analyzer to grab online data. The following table describes the pins we use by default (Note that you can also use other pins for the same purpose).
|
||||
|
||||
| pin name| function | gpio_num |
|
||||
|:---:|:---:|:---:|
|
||||
| IR RX|Infrared reception | GPIO_NUM_5 |
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
* Program upload failure
|
||||
|
||||
* Hardware connection is not correct: run `make monitor`, and reboot your board to see if there are any output logs.
|
||||
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
|
||||
|
||||
For any technical queries, please open an [issue](https://github.com/espressif/ESP8266_RTOS_SDK/issues) on GitHub. We will get back to you soon.
|
3
examples/peripherals/ir_rx/main/CMakeLists.txt
Normal file
3
examples/peripherals/ir_rx/main/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
||||
set(COMPONENT_SRCS "ir_rx_example_main.c")
|
||||
|
||||
register_component()
|
3
examples/peripherals/ir_rx/main/component.mk
Normal file
3
examples/peripherals/ir_rx/main/component.mk
Normal file
@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
70
examples/peripherals/ir_rx/main/ir_rx_example_main.c
Normal file
70
examples/peripherals/ir_rx/main/ir_rx_example_main.c
Normal file
@ -0,0 +1,70 @@
|
||||
/* IR RX Example
|
||||
This is an ir rx demo of ir function( NEC CODE ,32 BIT LENGTH)
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/ir_rx.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
#define IR_RX_IO_NUM 5
|
||||
#define IR_RX_BUF_LEN 128 // The actual allocated memory size is sizeof(uint32_t)*BUF_LEN. If the allocation is too small, the old infrared data may be overwritten.
|
||||
|
||||
/**
|
||||
* @brief check whether the ir cmd and addr obey the protocol
|
||||
*
|
||||
* @param nec_code nec ir code that received
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK success
|
||||
* - ESP_ERR_INVALID_ARG Parameter error
|
||||
*/
|
||||
static esp_err_t ir_rx_nec_code_check(ir_rx_nec_data_t nec_code)
|
||||
{
|
||||
|
||||
if ((nec_code.addr1 != ((~nec_code.addr2) & 0xff))) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if ((nec_code.cmd1 != ((~nec_code.cmd2) & 0xff))) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void ir_rx_task(void *arg)
|
||||
{
|
||||
ir_rx_nec_data_t ir_data;
|
||||
ir_rx_config_t ir_rx_config = {
|
||||
.io_num = IR_RX_IO_NUM,
|
||||
.buf_len = IR_RX_BUF_LEN
|
||||
};
|
||||
ir_rx_init(&ir_rx_config);
|
||||
|
||||
while (1) {
|
||||
ir_data.val = 0;
|
||||
ir_rx_recv_data(&ir_data, 1, portMAX_DELAY);
|
||||
ESP_LOGI(TAG, "addr1: 0x%x, addr2: 0x%x, cmd1: 0x%x, cmd2: 0x%x", ir_data.addr1, ir_data.addr2, ir_data.cmd1, ir_data.cmd2);
|
||||
|
||||
if (ESP_OK == ir_rx_nec_code_check(ir_data)) {
|
||||
ESP_LOGI(TAG, "ir rx nec data: 0x%x", ir_data.cmd1);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Non-standard nec infrared protocol");
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
xTaskCreate(ir_rx_task, "ir_rx_task", 2048, NULL, 5, NULL);
|
||||
}
|
6
examples/peripherals/ir_tx/CMakeLists.txt
Normal file
6
examples/peripherals/ir_tx/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
# The following four lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(ir_tx)
|
9
examples/peripherals/ir_tx/Makefile
Normal file
9
examples/peripherals/ir_tx/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := ir
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
59
examples/peripherals/ir_tx/README.md
Normal file
59
examples/peripherals/ir_tx/README.md
Normal file
@ -0,0 +1,59 @@
|
||||
# IR TX Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
In this example, we use IO14 to transmit the nec infrared signal and i2s to generate the 38Khz carrier.
|
||||
|
||||
## How to Use Example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with ESP8266 SoC (e.g., ESP8266-DevKitC, etc.)
|
||||
* A USB cable for power supply and programming
|
||||
|
||||
### Configure the Project
|
||||
|
||||
```
|
||||
make menuconfig
|
||||
```
|
||||
|
||||
* Set serial port under Serial Flasher Options.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
make -j4 flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
```
|
||||
I (467) gpio: GPIO[14]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
|
||||
I (937) main: ir tx nec: addr:55h;cmd:00h;repeat:4
|
||||
I (2377) main: ir tx nec: addr:55h;cmd:01h;repeat:4
|
||||
I (3817) main: ir tx nec: addr:55h;cmd:02h;repeat:4
|
||||
I (5257) main: ir tx nec: addr:55h;cmd:03h;repeat:4
|
||||
I (6697) main: ir tx nec: addr:55h;cmd:04h;repeat:4
|
||||
I (8137) main: ir tx nec: addr:55h;cmd:05h;repeat:4
|
||||
```
|
||||
|
||||
If you have a logic analyzer, you can use a logic analyzer to grab online data. The following table describes the pins we use by default (Note that you can also use other pins for the same purpose).
|
||||
|
||||
| pin name| function | gpio_num |
|
||||
|:---:|:---:|:---:|
|
||||
| IR TX|Infrared emission | GPIO_NUM_14 |
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
* Program upload failure
|
||||
|
||||
* Hardware connection is not correct: run `make monitor`, and reboot your board to see if there are any output logs.
|
||||
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
|
||||
|
||||
For any technical queries, please open an [issue](https://github.com/espressif/ESP8266_RTOS_SDK/issues) on GitHub. We will get back to you soon.
|
3
examples/peripherals/ir_tx/main/CMakeLists.txt
Normal file
3
examples/peripherals/ir_tx/main/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
||||
set(COMPONENT_SRCS "ir_tx_example_main.c")
|
||||
|
||||
register_component()
|
3
examples/peripherals/ir_tx/main/component.mk
Normal file
3
examples/peripherals/ir_tx/main/component.mk
Normal file
@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
58
examples/peripherals/ir_tx/main/ir_tx_example_main.c
Normal file
58
examples/peripherals/ir_tx/main/ir_tx_example_main.c
Normal file
@ -0,0 +1,58 @@
|
||||
/* IR TX Example
|
||||
This is an ir tx demo of ir function( NEC CODE ,32 BIT LENGTH)
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
#include "driver/ir_tx.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
#define IR_TX_IO_NUM 14
|
||||
|
||||
void ir_tx_task(void *arg)
|
||||
{
|
||||
ir_tx_config_t ir_tx_config = {
|
||||
.io_num = IR_TX_IO_NUM,
|
||||
.freq = 38000
|
||||
};
|
||||
|
||||
ir_tx_init(&ir_tx_config);
|
||||
|
||||
ir_tx_nec_data_t ir_data[5];
|
||||
/*
|
||||
The standard NEC ir code is:
|
||||
addr + ~addr + cmd + ~cmd
|
||||
*/
|
||||
ir_data[0].addr1 = 0x55;
|
||||
ir_data[0].addr2 = ~0x55;
|
||||
ir_data[0].cmd1 = 0x00;
|
||||
ir_data[0].cmd2 = ~0x00;
|
||||
|
||||
while (1) {
|
||||
for (int x = 1; x < 5; x++) { // repeat 4 times
|
||||
ir_data[x] = ir_data[0];
|
||||
}
|
||||
|
||||
ir_tx_send_data(ir_data, 5, portMAX_DELAY);
|
||||
ESP_LOGI(TAG, "ir tx nec: addr:%02xh;cmd:%02xh;repeat:%d", ir_data[0].addr1, ir_data[0].cmd1, 4);
|
||||
ir_data[0].cmd1++;
|
||||
ir_data[0].cmd2 = ~ir_data[0].cmd1;
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
xTaskCreate(ir_tx_task, "ir_tx_task", 2048, NULL, 5, NULL);
|
||||
}
|
Reference in New Issue
Block a user