infrared.c 19 KB

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