infrared.c 18 KB

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