IR learn

[中文]

This is an IR learning component based on the RMT module, capable of receiving infrared signals with a carrier frequency of 38KHz. The received signals are stored and forwarded in raw format, without support for the specific parsing of IR protocols.

Application Examples

Create IR_learn

const ir_learn_cfg_t config = {
    .learn_count = 4,              /*!< IR learn count needed */
    .learn_gpio = GPIO_NUM_38,     /*!< IR learn io that consumed by the sensor */
    .clk_src = RMT_CLK_SRC_DEFAULT,/*!< RMT clock source */
    .resolution = 1000000,         /*!< RMT resolution, 1M, 1 tick = 1us*/

    .task_stack = 4096,   /*!< IR learn task stack size */
    .task_priority = 5,   /*!< IR learn task priority */
    .task_affinity = 1,   /*!< IR learn task pinned to core (-1 is no affinity) */
    .callback = cb,       /*!< IR learn result callback for user */
};

ESP_ERROR_CHECK(ir_learn_new(&config, &handle));

Callback function

 void ir_learn_auto_learn_cb(ir_learn_state_t state, uint8_t sub_step, struct ir_learn_sub_list_head *data)
 {
   switch (state) {
   /**< IR learn ready, after successful initialization */
   case IR_LEARN_STATE_READY:
      ESP_LOGI(TAG, "IR Learn ready");
      break;
   /**< IR learn exit */
   case IR_LEARN_STATE_EXIT:
      ESP_LOGI(TAG, "IR Learn exit");
      break;
   /**< IR learn successfully */
   case IR_LEARN_STATE_END:
      ESP_LOGI(TAG, "IR Learn end");
      ir_learn_save_result(&ir_test_result, data);
      ir_learn_print_raw(data);
      ir_learn_stop(&handle);
      break;
   /**< IR learn failure */
   case IR_LEARN_STATE_FAIL:
      ESP_LOGI(TAG, "IR Learn failed, retry");
      break;
   /**< IR learn step, start from 1 */
   case IR_LEARN_STATE_STEP:
   default:
      ESP_LOGI(TAG, "IR Learn step:[%d][%d]", state, sub_step);
      break;
   }
   return;
}

IR_learn supports single and multi-packet learning. Upon successful learning, it notifies the IR_LEARN_STATE_END event. Users can handle the learning result independently, and the format of the learned data packet is described in struct ir_learn_sub_list_t.

The learning process automatically verifies the data. If the verification fails, it notifies the IR_LEARN_STATE_FAIL event. The verification logic is as follows:
  • For each packet, if the quantity of symbols is inconsistent, it is considered a learning failure.

  • For each level’s time difference, if it exceeds the threshold, it is considered a learning failure. (Threshold can be adjusted using the RMT_DECODE_MARGIN_US in menuconfig.)

Send out

void ir_learn_test_tx_raw(struct ir_learn_sub_list_head *rmt_out)
{
   /**< Create RMT TX channel */
   rmt_tx_channel_config_t tx_channel_cfg = {
      .clk_src = RMT_CLK_SRC_DEFAULT,
      .resolution_hz = IR_RESOLUTION_HZ,
      .mem_block_symbols = 128,  // amount of RMT symbols that the channel can store at a time
      .trans_queue_depth = 4,    // number of transactions that allowed to pending in the background
      .gpio_num = IR_TX_GPIO_NUM,
   };
   rmt_channel_handle_t tx_channel = NULL;
   ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_channel_cfg, &tx_channel));

   /**< Modulate carrier to TX channel */
   rmt_carrier_config_t carrier_cfg = {
      .duty_cycle = 0.33,
      .frequency_hz = 38000, // 38KHz
   };
   ESP_ERROR_CHECK(rmt_apply_carrier(tx_channel, &carrier_cfg));

   rmt_transmit_config_t transmit_cfg = {
      .loop_count = 0, // no loop
   };

   /**< Install IR encoder, here's raw format */
   ir_encoder_config_t raw_encoder_cfg = {
      .resolution = IR_RESOLUTION_HZ,
   };
   rmt_encoder_handle_t raw_encoder = NULL;
   ESP_ERROR_CHECK(ir_encoder_new(&raw_encoder_cfg, &raw_encoder));

   ESP_ERROR_CHECK(rmt_enable(tx_channel));  // Enable RMT TX channels

   /**< Traverse and send commands */
   struct ir_learn_sub_list_t *sub_it;
   SLIST_FOREACH(sub_it, rmt_out, next) {
      vTaskDelay(pdMS_TO_TICKS(sub_it->timediff / 1000));

      rmt_symbol_word_t *rmt_symbols = sub_it->symbols.received_symbols;
      size_t symbol_num = sub_it->symbols.num_symbols;

      ESP_ERROR_CHECK(rmt_transmit(tx_channel, raw_encoder, rmt_symbols, symbol_num, &transmit_cfg));
      rmt_tx_wait_all_done(tx_channel, -1);  // wait all transactions finished
   }

   /**< Disable and delete RMT TX channels */
   rmt_disable(tx_channel);
   rmt_del_channel(tx_channel);
   raw_encoder->del(raw_encoder);
}

API Reference

Header File

Functions

esp_err_t ir_learn_new(const ir_learn_cfg_t *cfg, ir_learn_handle_t *handle_out)

Create new IR learn handle.

Parameters
  • cfg[in] Config for IR learn

  • handle_out[out] New IR learn handle

Returns

  • ESP_OK Device handle creation success.

  • ESP_ERR_INVALID_ARG Invalid device handle or argument.

  • ESP_ERR_NO_MEM Memory allocation failed.

esp_err_t ir_learn_restart(ir_learn_handle_t ir_learn_hdl)

Restart IR learn process.

Parameters

ir_learn_hdl[in] IR learn handle

Returns

  • ESP_OK Restart process success.

  • ESP_ERR_INVALID_ARG Invalid device handle or argument.

esp_err_t ir_learn_stop(ir_learn_handle_t *ir_learn_hdl)

Stop IR learn process.

Note

Delete all

Parameters

ir_learn_hdl[in] IR learn handle

Returns

  • ESP_OK Stop process success.

  • ESP_ERR_INVALID_ARG Invalid device handle or argument.

esp_err_t ir_learn_add_list_node(struct ir_learn_list_head *learn_head)

Add IR learn list node, every new learn list will create it.

Parameters

learn_head[in] IR learn list head

Returns

  • ESP_OK Create learn list success.

  • ESP_ERR_NO_MEM Memory allocation failed.

esp_err_t ir_learn_add_sub_list_node(struct ir_learn_sub_list_head *sub_head, uint32_t timediff, const rmt_rx_done_event_data_t *symbol)

Add IR learn sub step list node, every sub step should be added.

Parameters
  • sub_head[in] IR learn sub step list head

  • timediff[in] Time diff between each sub step

  • symbol[in] symbols of each sub step

Returns

  • ESP_OK Create learn list success.

  • ESP_ERR_NO_MEM Memory allocation failed.

esp_err_t ir_learn_clean_data(struct ir_learn_list_head *learn_head)

Delete IR learn list node, will recursively delete sub steps.

Parameters

learn_head[in] IR learn list head

  • ESP_OK Stop process success.

  • ESP_ERR_INVALID_ARG Invalid device handle or argument.

esp_err_t ir_learn_clean_sub_data(struct ir_learn_sub_list_head *sub_head)

Delete sub steps.

Parameters

sub_head[in] IR learn sub list head

  • ESP_OK Stop process success.

  • ESP_ERR_INVALID_ARG Invalid device handle or argument.

esp_err_t ir_learn_check_valid(struct ir_learn_list_head *learn_head, struct ir_learn_sub_list_head *result_out)

Add IR learn list node, every new learn list will create it.

Parameters
  • learn_head[in] IR learn list head

  • result_out[out] IR learn result

Returns

  • ESP_OK Get learn result process.

  • ESP_ERR_INVALID_SIZE Size error.

esp_err_t ir_learn_print_raw(struct ir_learn_sub_list_head *cmd_list)

Print the RMT symbols.

Parameters

cmd_list[in] IR learn list head

  • ESP_OK Stop process success.

  • ESP_ERR_INVALID_ARG Invalid device handle or argument.

Structures

struct ir_learn_sub_list_t

An element in the list of infrared (IR) learn data packets.

Public Functions

SLIST_ENTRY (ir_learn_sub_list_t) next

Pointer to the next packet

Public Members

uint32_t timediff

The interval time from the previous packet (ms)

rmt_rx_done_event_data_t symbols

Received RMT symbols

struct ir_learn_list_t

The head of a list of infrared (IR) learn data packets.

Public Functions

SLIST_ENTRY (ir_learn_list_t) next

Pointer to the next packet

Public Members

struct ir_learn_sub_list_head cmd_sub_node

Package head of every cmd

struct ir_learn_cfg_t

IR learn configuration.

Public Members

rmt_clock_source_t clk_src

RMT clock source

uint32_t resolution

RMT resolution, in Hz

int learn_count

IR learn count needed

int learn_gpio

IR learn io that consumed by the sensor

ir_learn_result_cb callback

IR learn result callback for user

int task_priority

IR learn task priority

int task_stack

IR learn task stack size

int task_affinity

IR learn task pinned to core (-1 is no affinity)

Type Definitions

typedef void *ir_learn_handle_t

Type of IR learn handle.

typedef void (*ir_learn_result_cb)(ir_learn_state_t state, uint8_t sub_step, struct ir_learn_sub_list_head *data)

IR learn result user callback.

Param state

[out] IR learn step

Param sub_step

[out] Interval less than 500 ms, we think it’s the same command

Param data

[out] Command list of this step

Enumerations

enum ir_learn_state_t

Type of IR learn step.

Values:

enumerator IR_LEARN_STATE_STEP

IR learn step, start from 1

enumerator IR_LEARN_STATE_READY

IR learn ready, after successful initialization

enumerator IR_LEARN_STATE_END

IR learn successfully

enumerator IR_LEARN_STATE_FAIL

IR learn failure

enumerator IR_LEARN_STATE_EXIT

IR learn exit