infrared.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * infrared receiver (using espressif's example)
  3. *
  4. * (c) Philippe G. 2020, philippe_44@outlook.com
  5. *
  6. * This software is released under the MIT License.
  7. * https://opensource.org/licenses/MIT
  8. *
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "esp_err.h"
  15. #include "esp_log.h"
  16. #include "driver/rmt.h"
  17. #include "infrared.h"
  18. static const char* TAG = "IR";
  19. #define IR_TOOLS_FLAGS_PROTO_EXT (1 << 0) /*!< Enable Extended IR protocol */
  20. #define IR_TOOLS_FLAGS_INVERSE (1 << 1) /*!< Inverse the IR signal, i.e. take high level as low, and vice versa */
  21. /**
  22. * @brief IR device type
  23. *
  24. */
  25. typedef void *ir_dev_t;
  26. /**
  27. * @brief IR parser type
  28. *
  29. */
  30. typedef struct ir_parser_s ir_parser_t;
  31. /**
  32. * @brief Type definition of IR parser
  33. *
  34. */
  35. struct ir_parser_s {
  36. /**
  37. * @brief Input raw data to IR parser
  38. *
  39. * @param[in] parser: Handle of IR parser
  40. * @param[in] raw_data: Raw data which need decoding by IR parser
  41. * @param[in] length: Length of raw data
  42. *
  43. * @return
  44. * - ESP_OK: Input raw data successfully
  45. * - ESP_ERR_INVALID_ARG: Input raw data failed because of invalid argument
  46. * - ESP_FAIL: Input raw data failed because some other error occurred
  47. */
  48. esp_err_t (*input)(ir_parser_t *parser, void *raw_data, uint32_t length);
  49. /**
  50. * @brief Get the scan code after decoding of raw data
  51. *
  52. * @param[in] parser: Handle of IR parser
  53. * @param[out] address: Address of the scan code
  54. * @param[out] command: Command of the scan code
  55. * @param[out] repeat: Indicate if it's a repeat code
  56. *
  57. * @return
  58. * - ESP_OK: Get scan code successfully
  59. * - ESP_ERR_INVALID_ARG: Get scan code failed because of invalid arguments
  60. * - ESP_FAIL: Get scan code failed because some error occurred
  61. */
  62. esp_err_t (*get_scan_code)(ir_parser_t *parser, uint32_t *address, uint32_t *command, bool *repeat);
  63. /**
  64. * @brief Free resources used by IR parser
  65. *
  66. * @param[in] parser: Handle of IR parser
  67. *
  68. * @return
  69. * - ESP_OK: Free resource successfully
  70. * - ESP_FAIL: Free resources fail failed because some error occurred
  71. */
  72. esp_err_t (*del)(ir_parser_t *parser);
  73. };
  74. typedef struct {
  75. ir_dev_t dev_hdl; /*!< IR device handle */
  76. uint32_t flags; /*!< Flags for IR parser, different flags will enable different features */
  77. uint32_t margin_us; /*!< Timing parameter, indicating the tolerance to environment noise */
  78. } ir_parser_config_t;
  79. #define IR_PARSER_DEFAULT_CONFIG(dev) \
  80. { \
  81. .dev_hdl = dev, \
  82. .flags = 0, \
  83. .margin_us = 200, \
  84. }
  85. ir_parser_t *ir_parser = NULL;
  86. #define RMT_RX_CHANNEL 0 /*!< RMT channel for receiver */
  87. #define RMT_CHECK(a, str, goto_tag, ret_value, ...) \
  88. do \
  89. { \
  90. if (!(a)) \
  91. { \
  92. ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  93. ret = ret_value; \
  94. goto goto_tag; \
  95. } \
  96. } while (0)
  97. /****************************************************************************************
  98. * NEC protocol
  99. ****************************************************************************************/
  100. #define NEC_DATA_FRAME_RMT_WORDS (34)
  101. #define NEC_REPEAT_FRAME_RMT_WORDS (2)
  102. #define NEC_LEADING_CODE_HIGH_US (9000)
  103. #define NEC_LEADING_CODE_LOW_US (4500)
  104. #define NEC_PAYLOAD_ONE_HIGH_US (560)
  105. #define NEC_PAYLOAD_ONE_LOW_US (1690)
  106. #define NEC_PAYLOAD_ZERO_HIGH_US (560)
  107. #define NEC_PAYLOAD_ZERO_LOW_US (560)
  108. #define NEC_REPEAT_CODE_HIGH_US (9000)
  109. #define NEC_REPEAT_CODE_LOW_US (2250)
  110. #define NEC_ENDING_CODE_HIGH_US (560)
  111. typedef struct {
  112. ir_parser_t parent;
  113. uint32_t flags;
  114. uint32_t leading_code_high_ticks;
  115. uint32_t leading_code_low_ticks;
  116. uint32_t repeat_code_high_ticks;
  117. uint32_t repeat_code_low_ticks;
  118. uint32_t payload_logic0_high_ticks;
  119. uint32_t payload_logic0_low_ticks;
  120. uint32_t payload_logic1_high_ticks;
  121. uint32_t payload_logic1_low_ticks;
  122. uint32_t margin_ticks;
  123. rmt_item32_t *buffer;
  124. uint32_t cursor;
  125. uint32_t last_address;
  126. uint32_t last_command;
  127. bool repeat;
  128. bool inverse;
  129. } nec_parser_t;
  130. /****************************************************************************************
  131. *
  132. */
  133. static inline bool nec_check_in_range(uint32_t raw_ticks, uint32_t target_ticks, uint32_t margin_ticks) {
  134. return (raw_ticks < (target_ticks + margin_ticks)) && (raw_ticks > (target_ticks - margin_ticks));
  135. }
  136. /****************************************************************************************
  137. *
  138. */
  139. static bool nec_parse_head(nec_parser_t *nec_parser) {
  140. nec_parser->cursor = 0;
  141. rmt_item32_t item = nec_parser->buffer[nec_parser->cursor];
  142. bool ret = (item.level0 == nec_parser->inverse) && (item.level1 != nec_parser->inverse) &&
  143. nec_check_in_range(item.duration0, nec_parser->leading_code_high_ticks, nec_parser->margin_ticks) &&
  144. nec_check_in_range(item.duration1, nec_parser->leading_code_low_ticks, nec_parser->margin_ticks);
  145. nec_parser->cursor += 1;
  146. return ret;
  147. }
  148. /****************************************************************************************
  149. *
  150. */
  151. static bool nec_parse_logic0(nec_parser_t *nec_parser) {
  152. rmt_item32_t item = nec_parser->buffer[nec_parser->cursor];
  153. bool ret = (item.level0 == nec_parser->inverse) && (item.level1 != nec_parser->inverse) &&
  154. nec_check_in_range(item.duration0, nec_parser->payload_logic0_high_ticks, nec_parser->margin_ticks) &&
  155. nec_check_in_range(item.duration1, nec_parser->payload_logic0_low_ticks, nec_parser->margin_ticks);
  156. return ret;
  157. }
  158. /****************************************************************************************
  159. *
  160. */
  161. static bool nec_parse_logic1(nec_parser_t *nec_parser) {
  162. rmt_item32_t item = nec_parser->buffer[nec_parser->cursor];
  163. bool ret = (item.level0 == nec_parser->inverse) && (item.level1 != nec_parser->inverse) &&
  164. nec_check_in_range(item.duration0, nec_parser->payload_logic1_high_ticks, nec_parser->margin_ticks) &&
  165. nec_check_in_range(item.duration1, nec_parser->payload_logic1_low_ticks, nec_parser->margin_ticks);
  166. return ret;
  167. }
  168. /****************************************************************************************
  169. *
  170. */
  171. static esp_err_t nec_parse_logic(ir_parser_t *parser, bool *logic) {
  172. esp_err_t ret = ESP_FAIL;
  173. bool logic_value = false;
  174. nec_parser_t *nec_parser = __containerof(parser, nec_parser_t, parent);
  175. if (nec_parse_logic0(nec_parser)) {
  176. logic_value = false;
  177. ret = ESP_OK;
  178. } else if (nec_parse_logic1(nec_parser)) {
  179. logic_value = true;
  180. ret = ESP_OK;
  181. }
  182. if (ret == ESP_OK) {
  183. *logic = logic_value;
  184. }
  185. nec_parser->cursor += 1;
  186. return ret;
  187. }
  188. /****************************************************************************************
  189. *
  190. */
  191. static bool nec_parse_repeat_frame(nec_parser_t *nec_parser) {
  192. nec_parser->cursor = 0;
  193. rmt_item32_t item = nec_parser->buffer[nec_parser->cursor];
  194. bool ret = (item.level0 == nec_parser->inverse) && (item.level1 != nec_parser->inverse) &&
  195. nec_check_in_range(item.duration0, nec_parser->repeat_code_high_ticks, nec_parser->margin_ticks) &&
  196. nec_check_in_range(item.duration1, nec_parser->repeat_code_low_ticks, nec_parser->margin_ticks);
  197. nec_parser->cursor += 1;
  198. return ret;
  199. }
  200. /****************************************************************************************
  201. *
  202. */
  203. static esp_err_t nec_parser_input(ir_parser_t *parser, void *raw_data, uint32_t length) {
  204. esp_err_t ret = ESP_OK;
  205. nec_parser_t *nec_parser = __containerof(parser, nec_parser_t, parent);
  206. RMT_CHECK(raw_data, "input data can't be null", err, ESP_ERR_INVALID_ARG);
  207. nec_parser->buffer = raw_data;
  208. // Data Frame costs 34 items and Repeat Frame costs 2 items
  209. if (length == NEC_DATA_FRAME_RMT_WORDS) {
  210. nec_parser->repeat = false;
  211. } else if (length == NEC_REPEAT_FRAME_RMT_WORDS) {
  212. nec_parser->repeat = true;
  213. } else {
  214. ret = ESP_FAIL;
  215. }
  216. return ret;
  217. err:
  218. return ret;
  219. }
  220. /****************************************************************************************
  221. *
  222. */
  223. static esp_err_t nec_parser_get_scan_code(ir_parser_t *parser, uint32_t *address, uint32_t *command, bool *repeat) {
  224. esp_err_t ret = ESP_FAIL;
  225. uint32_t addr = 0;
  226. uint32_t cmd = 0;
  227. bool logic_value = false;
  228. nec_parser_t *nec_parser = __containerof(parser, nec_parser_t, parent);
  229. RMT_CHECK(address && command && repeat, "address, command and repeat can't be null", out, ESP_ERR_INVALID_ARG);
  230. if (nec_parser->repeat) {
  231. if (nec_parse_repeat_frame(nec_parser)) {
  232. *address = nec_parser->last_address;
  233. *command = nec_parser->last_command;
  234. *repeat = true;
  235. ret = ESP_OK;
  236. }
  237. } else {
  238. if (nec_parse_head(nec_parser)) {
  239. // for the forgetful, need to do a bitreverse
  240. for (int i = 15; i >= 0; i--) {
  241. if (nec_parse_logic(parser, &logic_value) == ESP_OK) {
  242. addr |= (logic_value << i);
  243. }
  244. }
  245. for (int i = 15; i >= 0; i--) {
  246. if (nec_parse_logic(parser, &logic_value) == ESP_OK) {
  247. cmd |= (logic_value << i);
  248. }
  249. }
  250. *address = addr;
  251. *command = cmd;
  252. *repeat = false;
  253. // keep it as potential repeat code
  254. nec_parser->last_address = addr;
  255. nec_parser->last_command = cmd;
  256. ret = ESP_OK;
  257. }
  258. }
  259. out:
  260. return ret;
  261. }
  262. /****************************************************************************************
  263. *
  264. */
  265. static esp_err_t nec_parser_del(ir_parser_t *parser) {
  266. nec_parser_t *nec_parser = __containerof(parser, nec_parser_t, parent);
  267. free(nec_parser);
  268. return ESP_OK;
  269. }
  270. /****************************************************************************************
  271. *
  272. */
  273. ir_parser_t *ir_parser_rmt_new_nec(const ir_parser_config_t *config) {
  274. ir_parser_t *ret = NULL;
  275. nec_parser_t *nec_parser = calloc(1, sizeof(nec_parser_t));
  276. nec_parser->flags = config->flags;
  277. if (config->flags & IR_TOOLS_FLAGS_INVERSE) {
  278. nec_parser->inverse = true;
  279. }
  280. uint32_t counter_clk_hz = 0;
  281. RMT_CHECK(rmt_get_counter_clock((rmt_channel_t)config->dev_hdl, &counter_clk_hz) == ESP_OK,
  282. "get rmt counter clock failed", err, NULL);
  283. float ratio = (float)counter_clk_hz / 1e6;
  284. nec_parser->leading_code_high_ticks = (uint32_t)(ratio * NEC_LEADING_CODE_HIGH_US);
  285. nec_parser->leading_code_low_ticks = (uint32_t)(ratio * NEC_LEADING_CODE_LOW_US);
  286. nec_parser->repeat_code_high_ticks = (uint32_t)(ratio * NEC_REPEAT_CODE_HIGH_US);
  287. nec_parser->repeat_code_low_ticks = (uint32_t)(ratio * NEC_REPEAT_CODE_LOW_US);
  288. nec_parser->payload_logic0_high_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ZERO_HIGH_US);
  289. nec_parser->payload_logic0_low_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ZERO_LOW_US);
  290. nec_parser->payload_logic1_high_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ONE_HIGH_US);
  291. nec_parser->payload_logic1_low_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ONE_LOW_US);
  292. nec_parser->margin_ticks = (uint32_t)(ratio * config->margin_us);
  293. nec_parser->parent.input = nec_parser_input;
  294. nec_parser->parent.get_scan_code = nec_parser_get_scan_code;
  295. nec_parser->parent.del = nec_parser_del;
  296. return &nec_parser->parent;
  297. err:
  298. return ret;
  299. }
  300. /****************************************************************************************
  301. * RC5 protocol
  302. ****************************************************************************************/
  303. #define RC5_MAX_FRAME_RMT_WORDS (14) // S1+S2+T+ADDR(5)+CMD(6)
  304. #define RC5_PULSE_DURATION_US (889)
  305. typedef struct {
  306. ir_parser_t parent;
  307. uint32_t flags;
  308. uint32_t pulse_duration_ticks;
  309. uint32_t margin_ticks;
  310. rmt_item32_t *buffer;
  311. uint32_t buffer_len;
  312. uint32_t last_command;
  313. uint32_t last_address;
  314. bool last_t_bit;
  315. } rc5_parser_t;
  316. /****************************************************************************************
  317. *
  318. */
  319. static inline bool rc5_check_in_range(uint32_t raw_ticks, uint32_t target_ticks, uint32_t margin_ticks) {
  320. return (raw_ticks < (target_ticks + margin_ticks)) && (raw_ticks > (target_ticks - margin_ticks));
  321. }
  322. /****************************************************************************************
  323. *
  324. */
  325. static esp_err_t rc5_parser_input(ir_parser_t *parser, void *raw_data, uint32_t length) {
  326. esp_err_t ret = ESP_OK;
  327. rc5_parser_t *rc5_parser = __containerof(parser, rc5_parser_t, parent);
  328. rc5_parser->buffer = raw_data;
  329. rc5_parser->buffer_len = length;
  330. if (length > RC5_MAX_FRAME_RMT_WORDS) {
  331. ret = ESP_FAIL;
  332. }
  333. return ret;
  334. }
  335. /****************************************************************************************
  336. *
  337. */
  338. static inline bool rc5_duration_one_unit(rc5_parser_t *rc5_parser, uint32_t duration) {
  339. return (duration < (rc5_parser->pulse_duration_ticks + rc5_parser->margin_ticks)) &&
  340. (duration > (rc5_parser->pulse_duration_ticks - rc5_parser->margin_ticks));
  341. }
  342. /****************************************************************************************
  343. *
  344. */
  345. static inline bool rc5_duration_two_unit(rc5_parser_t *rc5_parser, uint32_t duration) {
  346. return (duration < (rc5_parser->pulse_duration_ticks * 2 + rc5_parser->margin_ticks)) &&
  347. (duration > (rc5_parser->pulse_duration_ticks * 2 - rc5_parser->margin_ticks));
  348. }
  349. /****************************************************************************************
  350. *
  351. */
  352. static esp_err_t rc5_parser_get_scan_code(ir_parser_t *parser, uint32_t *address, uint32_t *command, bool *repeat) {
  353. esp_err_t ret = ESP_FAIL;
  354. uint32_t parse_result = 0; // 32 bit is enough to hold the parse result of one RC5 frame
  355. uint32_t addr = 0;
  356. uint32_t cmd = 0;
  357. bool s1 = true;
  358. bool s2 = true;
  359. bool t = false;
  360. bool exchange = false;
  361. rc5_parser_t *rc5_parser = __containerof(parser, rc5_parser_t, parent);
  362. RMT_CHECK(address && command && repeat, "address, command and repeat can't be null", out, ESP_ERR_INVALID_ARG);
  363. for (int i = 0; i < rc5_parser->buffer_len; i++) {
  364. if (rc5_duration_one_unit(rc5_parser, rc5_parser->buffer[i].duration0)) {
  365. parse_result <<= 1;
  366. parse_result |= exchange;
  367. if (rc5_duration_two_unit(rc5_parser, rc5_parser->buffer[i].duration1)) {
  368. exchange = !exchange;
  369. }
  370. } else if (rc5_duration_two_unit(rc5_parser, rc5_parser->buffer[i].duration0)) {
  371. parse_result <<= 1;
  372. parse_result |= rc5_parser->buffer[i].level0;
  373. parse_result <<= 1;
  374. parse_result |= !rc5_parser->buffer[i].level0;
  375. if (rc5_duration_one_unit(rc5_parser, rc5_parser->buffer[i].duration1)) {
  376. exchange = !exchange;
  377. }
  378. } else {
  379. goto out;
  380. }
  381. }
  382. if (!(rc5_parser->flags & IR_TOOLS_FLAGS_INVERSE)) {
  383. parse_result = ~parse_result;
  384. }
  385. s1 = ((parse_result & 0x2000) >> 13) & 0x01;
  386. s2 = ((parse_result & 0x1000) >> 12) & 0x01;
  387. t = ((parse_result & 0x800) >> 11) & 0x01;
  388. // Check S1, must be 1
  389. if (s1) {
  390. if (!(rc5_parser->flags & IR_TOOLS_FLAGS_PROTO_EXT) && !s2) {
  391. // Not standard RC5 protocol, but S2 is 0
  392. goto out;
  393. }
  394. addr = (parse_result & 0x7C0) >> 6;
  395. cmd = (parse_result & 0x3F);
  396. if (!s2) {
  397. cmd |= 1 << 6;
  398. }
  399. *repeat = (t == rc5_parser->last_t_bit && addr == rc5_parser->last_address && cmd == rc5_parser->last_command);
  400. *address = addr;
  401. *command = cmd;
  402. rc5_parser->last_address = addr;
  403. rc5_parser->last_command = cmd;
  404. rc5_parser->last_t_bit = t;
  405. ret = ESP_OK;
  406. }
  407. out:
  408. return ret;
  409. }
  410. /****************************************************************************************
  411. *
  412. */
  413. static esp_err_t rc5_parser_del(ir_parser_t *parser) {
  414. rc5_parser_t *rc5_parser = __containerof(parser, rc5_parser_t, parent);
  415. free(rc5_parser);
  416. return ESP_OK;
  417. }
  418. /****************************************************************************************
  419. *
  420. */
  421. ir_parser_t *ir_parser_rmt_new_rc5(const ir_parser_config_t *config) {
  422. ir_parser_t *ret = NULL;
  423. rc5_parser_t *rc5_parser = calloc(1, sizeof(rc5_parser_t));
  424. rc5_parser->flags = config->flags;
  425. uint32_t counter_clk_hz = 0;
  426. RMT_CHECK(rmt_get_counter_clock((rmt_channel_t)config->dev_hdl, &counter_clk_hz) == ESP_OK,
  427. "get rmt counter clock failed", err, NULL);
  428. float ratio = (float)counter_clk_hz / 1e6;
  429. rc5_parser->pulse_duration_ticks = (uint32_t)(ratio * RC5_PULSE_DURATION_US);
  430. rc5_parser->margin_ticks = (uint32_t)(ratio * config->margin_us);
  431. rc5_parser->parent.input = rc5_parser_input;
  432. rc5_parser->parent.get_scan_code = rc5_parser_get_scan_code;
  433. rc5_parser->parent.del = rc5_parser_del;
  434. return &rc5_parser->parent;
  435. err:
  436. return ret;
  437. }
  438. /****************************************************************************************
  439. *
  440. */
  441. void infrared_receive(RingbufHandle_t rb, infrared_handler handler) {
  442. size_t rx_size = 0;
  443. rmt_item32_t* item = (rmt_item32_t*) xRingbufferReceive(rb, &rx_size, 10 / portTICK_RATE_MS);
  444. if (item) {
  445. uint32_t addr, cmd;
  446. bool repeat = false;
  447. rx_size /= 4; // one RMT = 4 Bytes
  448. if (ir_parser->input(ir_parser, item, rx_size) == ESP_OK) {
  449. if (ir_parser->get_scan_code(ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
  450. handler(addr, cmd);
  451. ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
  452. }
  453. }
  454. // after parsing the data, return spaces to ringbuffer.
  455. vRingbufferReturnItem(rb, (void*) item);
  456. }
  457. }
  458. /****************************************************************************************
  459. *
  460. */
  461. void infrared_init(RingbufHandle_t *rb, int gpio, infrared_mode_t mode) {
  462. ESP_LOGI(TAG, "Starting Infrared Receiver mode %s on gpio %d", mode == IR_NEC ? "nec" : "rc5", gpio);
  463. rmt_config_t rmt_rx_config = RMT_DEFAULT_CONFIG_RX(gpio, RMT_RX_CHANNEL);
  464. rmt_config(&rmt_rx_config);
  465. rmt_driver_install(rmt_rx_config.channel, 1000, 0);
  466. ir_parser_config_t ir_parser_config = IR_PARSER_DEFAULT_CONFIG((ir_dev_t) rmt_rx_config.channel);
  467. ir_parser_config.flags |= IR_TOOLS_FLAGS_PROTO_EXT; // Using extended IR protocols (both NEC and RC5 have extended version)
  468. ir_parser = (mode == IR_NEC) ? ir_parser_rmt_new_nec(&ir_parser_config) : ir_parser_rmt_new_rc5(&ir_parser_config);
  469. // get RMT RX ringbuffer
  470. rmt_get_ringbuf_handle(RMT_RX_CHANNEL, rb);
  471. rmt_rx_start(RMT_RX_CHANNEL, 1);
  472. }