ir_protocols_main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* IR protocols example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "sdkconfig.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_log.h"
  13. #include "driver/rmt.h"
  14. #include "ir_tools.h"
  15. static const char *TAG = "example";
  16. static rmt_channel_t example_tx_channel = RMT_CHANNEL_0;
  17. static rmt_channel_t example_rx_channel = RMT_CHANNEL_1;
  18. /**
  19. * @brief RMT Receive Task
  20. *
  21. */
  22. static void example_ir_rx_task(void *arg)
  23. {
  24. uint32_t addr = 0;
  25. uint32_t cmd = 0;
  26. uint32_t length = 0;
  27. bool repeat = false;
  28. RingbufHandle_t rb = NULL;
  29. rmt_item32_t *items = NULL;
  30. rmt_config_t rmt_rx_config = RMT_DEFAULT_CONFIG_RX(CONFIG_EXAMPLE_RMT_RX_GPIO, example_rx_channel);
  31. rmt_config(&rmt_rx_config);
  32. rmt_driver_install(example_rx_channel, 1000, 0);
  33. ir_parser_config_t ir_parser_config = IR_PARSER_DEFAULT_CONFIG((ir_dev_t)example_rx_channel);
  34. ir_parser_config.flags |= IR_TOOLS_FLAGS_PROTO_EXT; // Using extended IR protocols (both NEC and RC5 have extended version)
  35. ir_parser_t *ir_parser = NULL;
  36. #if CONFIG_EXAMPLE_IR_PROTOCOL_NEC
  37. ir_parser = ir_parser_rmt_new_nec(&ir_parser_config);
  38. #elif CONFIG_EXAMPLE_IR_PROTOCOL_RC5
  39. ir_parser = ir_parser_rmt_new_rc5(&ir_parser_config);
  40. #endif
  41. //get RMT RX ringbuffer
  42. rmt_get_ringbuf_handle(example_rx_channel, &rb);
  43. // Start receive
  44. rmt_rx_start(example_rx_channel, true);
  45. while (rb) {
  46. items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
  47. if (items) {
  48. length /= 4; // one RMT = 4 Bytes
  49. if (ir_parser->input(ir_parser, items, length) == ESP_OK) {
  50. if (ir_parser->get_scan_code(ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
  51. ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
  52. }
  53. }
  54. //after parsing the data, return spaces to ringbuffer.
  55. vRingbufferReturnItem(rb, (void *) items);
  56. } else {
  57. break;
  58. }
  59. }
  60. ir_parser->del(ir_parser);
  61. rmt_driver_uninstall(example_rx_channel);
  62. vTaskDelete(NULL);
  63. }
  64. /**
  65. * @brief RMT Transmit Task
  66. *
  67. */
  68. static void example_ir_tx_task(void *arg)
  69. {
  70. uint32_t addr = 0x10;
  71. uint32_t cmd = 0x20;
  72. rmt_item32_t *items = NULL;
  73. uint32_t length = 0;
  74. ir_builder_t *ir_builder = NULL;
  75. rmt_config_t rmt_tx_config = RMT_DEFAULT_CONFIG_TX(CONFIG_EXAMPLE_RMT_TX_GPIO, example_tx_channel);
  76. rmt_tx_config.tx_config.carrier_en = true;
  77. rmt_config(&rmt_tx_config);
  78. rmt_driver_install(example_tx_channel, 0, 0);
  79. ir_builder_config_t ir_builder_config = IR_BUILDER_DEFAULT_CONFIG((ir_dev_t)example_tx_channel);
  80. ir_builder_config.flags |= IR_TOOLS_FLAGS_PROTO_EXT; // Using extended IR protocols (both NEC and RC5 have extended version)
  81. #if CONFIG_EXAMPLE_IR_PROTOCOL_NEC
  82. ir_builder = ir_builder_rmt_new_nec(&ir_builder_config);
  83. #elif CONFIG_EXAMPLE_IR_PROTOCOL_RC5
  84. ir_builder = ir_builder_rmt_new_rc5(&ir_builder_config);
  85. #endif
  86. while (1) {
  87. vTaskDelay(pdMS_TO_TICKS(2000));
  88. ESP_LOGI(TAG, "Send command 0x%x to address 0x%x", cmd, addr);
  89. // Send new key code
  90. ESP_ERROR_CHECK(ir_builder->build_frame(ir_builder, addr, cmd));
  91. ESP_ERROR_CHECK(ir_builder->get_result(ir_builder, &items, &length));
  92. //To send data according to the waveform items.
  93. rmt_write_items(example_tx_channel, items, length, false);
  94. // Send repeat code
  95. vTaskDelay(pdMS_TO_TICKS(ir_builder->repeat_period_ms));
  96. ESP_ERROR_CHECK(ir_builder->build_repeat_frame(ir_builder));
  97. ESP_ERROR_CHECK(ir_builder->get_result(ir_builder, &items, &length));
  98. rmt_write_items(example_tx_channel, items, length, false);
  99. cmd++;
  100. }
  101. ir_builder->del(ir_builder);
  102. rmt_driver_uninstall(example_tx_channel);
  103. vTaskDelete(NULL);
  104. }
  105. void app_main(void)
  106. {
  107. xTaskCreate(example_ir_rx_task, "ir_rx_task", 2048, NULL, 10, NULL);
  108. xTaskCreate(example_ir_tx_task, "ir_tx_task", 2048, NULL, 10, NULL);
  109. }