infrared.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #define INFRARED_COMMON_CARRIER_FREQUENCY ((uint32_t)38000)
  9. #define INFRARED_COMMON_DUTY_CYCLE ((float)0.33)
  10. /* if we want to see split raw signals during bruteforce,
  11. * we have to have RX raw timing delay less than TX */
  12. #define INFRARED_RAW_RX_TIMING_DELAY_US 150000
  13. #define INFRARED_RAW_TX_TIMING_DELAY_US 180000
  14. typedef struct InfraredDecoderHandler InfraredDecoderHandler;
  15. typedef struct InfraredEncoderHandler InfraredEncoderHandler;
  16. typedef enum {
  17. InfraredProtocolUnknown = -1,
  18. InfraredProtocolNEC = 0,
  19. InfraredProtocolNECext,
  20. InfraredProtocolNEC42,
  21. InfraredProtocolNEC42ext,
  22. InfraredProtocolSamsung32,
  23. InfraredProtocolRC6,
  24. InfraredProtocolRC5,
  25. InfraredProtocolRC5X,
  26. InfraredProtocolSIRC,
  27. InfraredProtocolSIRC15,
  28. InfraredProtocolSIRC20,
  29. InfraredProtocolKaseikyo,
  30. InfraredProtocolMAX,
  31. } InfraredProtocol;
  32. typedef struct {
  33. InfraredProtocol protocol;
  34. uint32_t address;
  35. uint32_t command;
  36. bool repeat;
  37. } InfraredMessage;
  38. typedef enum {
  39. InfraredStatusError,
  40. InfraredStatusOk,
  41. InfraredStatusDone,
  42. InfraredStatusReady,
  43. } InfraredStatus;
  44. /**
  45. * Initialize decoder.
  46. *
  47. * \return returns pointer to INFRARED decoder handler if success, otherwise - error.
  48. */
  49. InfraredDecoderHandler* infrared_alloc_decoder(void);
  50. /**
  51. * Provide to decoder next timing.
  52. *
  53. * \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_alloc_decoder().
  54. * \param[in] level - high(true) or low(false) level of input signal to analyze.
  55. * it should alternate every call, otherwise it is an error case,
  56. * and decoder resets its state and start decoding from the start.
  57. * \param[in] duration - duration of steady high/low input signal.
  58. * \return if message is ready, returns pointer to decoded message, returns NULL.
  59. * Note: ownership of returned ptr belongs to handler. So pointer is valid
  60. * up to next infrared_free_decoder(), infrared_reset_decoder(),
  61. * infrared_decode(), infrared_check_decoder_ready() calls.
  62. */
  63. const InfraredMessage*
  64. infrared_decode(InfraredDecoderHandler* handler, bool level, uint32_t duration);
  65. /**
  66. * Check whether decoder is ready.
  67. * Functionality is quite similar to infrared_decode(), but with no timing providing.
  68. * Some protocols (e.g. Sony SIRC) has variable payload length, which means we
  69. * can't recognize end of message right after receiving last bit. That's why
  70. * application should call to infrared_check_decoder_ready() after some timeout to
  71. * retrieve decoded message, if so.
  72. *
  73. * \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_alloc_decoder().
  74. * \return if message is ready, returns pointer to decoded message, returns NULL.
  75. * Note: ownership of returned ptr belongs to handler. So pointer is valid
  76. * up to next infrared_free_decoder(), infrared_reset_decoder(),
  77. * infrared_decode(), infrared_check_decoder_ready() calls.
  78. */
  79. const InfraredMessage* infrared_check_decoder_ready(InfraredDecoderHandler* handler);
  80. /**
  81. * Deinitialize decoder and free allocated memory.
  82. *
  83. * \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_alloc_decoder().
  84. */
  85. void infrared_free_decoder(InfraredDecoderHandler* handler);
  86. /**
  87. * Reset INFRARED decoder.
  88. *
  89. * \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_alloc_decoder().
  90. */
  91. void infrared_reset_decoder(InfraredDecoderHandler* handler);
  92. /**
  93. * Get protocol name by protocol enum.
  94. *
  95. * \param[in] protocol - protocol identifier.
  96. * \return string to protocol name.
  97. */
  98. const char* infrared_get_protocol_name(InfraredProtocol protocol);
  99. /**
  100. * Get protocol enum by protocol name.
  101. *
  102. * \param[in] protocol_name - string to protocol name.
  103. * \return protocol identifier.
  104. */
  105. InfraredProtocol infrared_get_protocol_by_name(const char* protocol_name);
  106. /**
  107. * Get address length by protocol enum.
  108. *
  109. * \param[in] protocol - protocol identifier.
  110. * \return length of address in bits.
  111. */
  112. uint8_t infrared_get_protocol_address_length(InfraredProtocol protocol);
  113. /**
  114. * Get command length by protocol enum.
  115. *
  116. * \param[in] protocol - protocol identifier.
  117. * \return length of command in bits.
  118. */
  119. uint8_t infrared_get_protocol_command_length(InfraredProtocol protocol);
  120. /**
  121. * Checks whether protocol valid.
  122. *
  123. * \param[in] protocol - protocol identifier.
  124. * \return true if protocol is valid, false otherwise.
  125. */
  126. bool infrared_is_protocol_valid(InfraredProtocol protocol);
  127. /**
  128. * Allocate INFRARED encoder.
  129. *
  130. * \return encoder handler.
  131. */
  132. InfraredEncoderHandler* infrared_alloc_encoder(void);
  133. /**
  134. * Free encoder handler previously allocated with \c infrared_alloc_encoder().
  135. *
  136. * \param[in] handler - handler to INFRARED encoder. Should be acquired with \c infrared_alloc_encoder().
  137. */
  138. void infrared_free_encoder(InfraredEncoderHandler* handler);
  139. /**
  140. * Encode previously set INFRARED message.
  141. * Usage:
  142. * 1) alloc with \c infrared_alloc_encoder()
  143. * 2) set message to encode with \c infrared_reset_encoder()
  144. * 3) call for \c infrared_encode() to continuously get one at a time timings.
  145. * 4) when \c infrared_encode() returns InfraredStatusDone, it means new message is fully encoded.
  146. * 5) to encode additional timings, just continue calling \c infrared_encode().
  147. *
  148. * \param[in] handler - handler to INFRARED encoder. Should be acquired with \c infrared_alloc_encoder().
  149. * \param[out] duration - encoded timing.
  150. * \param[out] level - encoded level.
  151. *
  152. * \return status of encode operation.
  153. */
  154. InfraredStatus infrared_encode(InfraredEncoderHandler* handler, uint32_t* duration, bool* level);
  155. /**
  156. * Reset INFRARED encoder and set new message to encode. If it's not called after receiveing
  157. * InfraredStatusDone in \c infrared_encode(), encoder will encode repeat messages
  158. * till the end of time.
  159. *
  160. * \param[in] handler - handler to INFRARED encoder. Should be acquired with \c infrared_alloc_encoder().
  161. * \param[in] message - message to encode.
  162. */
  163. void infrared_reset_encoder(InfraredEncoderHandler* handler, const InfraredMessage* message);
  164. /**
  165. * Get PWM frequency value for selected protocol
  166. *
  167. * \param[in] protocol - protocol to get from PWM frequency
  168. *
  169. * \return frequency
  170. */
  171. uint32_t infrared_get_protocol_frequency(InfraredProtocol protocol);
  172. /**
  173. * Get PWM duty cycle value for selected protocol
  174. *
  175. * \param[in] protocol - protocol to get from PWM duty cycle
  176. *
  177. * \return duty cycle
  178. */
  179. float infrared_get_protocol_duty_cycle(InfraredProtocol protocol);
  180. /**
  181. * Get the minimum count of signal repeats for the selected protocol
  182. *
  183. * \param[in] protocol - protocol to get the repeat count from
  184. *
  185. * \return repeat count
  186. */
  187. size_t infrared_get_protocol_min_repeat_count(InfraredProtocol protocol);
  188. #ifdef __cplusplus
  189. }
  190. #endif