irda.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #define IRDA_COMMON_CARRIER_FREQUENCY 38000
  8. #define IRDA_COMMON_DUTY_CYCLE 0.33
  9. /* if we want to see splitted raw signals during brutforce,
  10. * we have to have RX raw timing delay less than TX */
  11. #define IRDA_RAW_RX_TIMING_DELAY_US 150000
  12. #define IRDA_RAW_TX_TIMING_DELAY_US 180000
  13. typedef struct IrdaDecoderHandler IrdaDecoderHandler;
  14. typedef struct IrdaEncoderHandler IrdaEncoderHandler;
  15. // Do not change protocol order, as it can be saved into memory and fw update can be performed!
  16. typedef enum {
  17. IrdaProtocolUnknown = -1,
  18. IrdaProtocolNEC = 0,
  19. IrdaProtocolNECext = 1,
  20. IrdaProtocolSamsung32 = 2,
  21. IrdaProtocolRC6 = 3,
  22. IrdaProtocolRC5 = 4,
  23. IrdaProtocolRC5X = 5,
  24. IrdaProtocolMAX,
  25. } IrdaProtocol;
  26. typedef struct {
  27. IrdaProtocol protocol;
  28. uint32_t address;
  29. uint32_t command;
  30. bool repeat;
  31. } IrdaMessage;
  32. typedef enum {
  33. IrdaStatusError,
  34. IrdaStatusOk,
  35. IrdaStatusDone,
  36. IrdaStatusReady,
  37. } IrdaStatus;
  38. /**
  39. * Initialize decoder.
  40. *
  41. * \return returns pointer to IRDA decoder handler if success, otherwise - error.
  42. */
  43. IrdaDecoderHandler* irda_alloc_decoder(void);
  44. /**
  45. * Provide to decoder next timing.
  46. *
  47. * \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
  48. * \param[in] level - high(true) or low(false) level of input signal to analyze.
  49. * it should alternate every call, otherwise it is an error case,
  50. * and decoder resets its state and start decoding from the start.
  51. * \param[in] duration - duration of steady high/low input signal.
  52. * \return if message is ready, returns pointer to decoded message, returns NULL.
  53. * Note: ownership of returned ptr belongs to handler. So pointer is valid
  54. * up to next irda_free_decoder(), irda_reset_decoder(), irda_decode() calls.
  55. */
  56. const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration);
  57. /**
  58. * Deinitialize decoder and free allocated memory.
  59. *
  60. * \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
  61. */
  62. void irda_free_decoder(IrdaDecoderHandler* handler);
  63. /**
  64. * Reset IRDA decoder.
  65. *
  66. * \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
  67. */
  68. void irda_reset_decoder(IrdaDecoderHandler* handler);
  69. /**
  70. * Get protocol name by protocol enum.
  71. *
  72. * \param[in] protocol - protocol identifier.
  73. * \return string to protocol name.
  74. */
  75. const char* irda_get_protocol_name(IrdaProtocol protocol);
  76. /**
  77. * Get protocol enum by protocol name.
  78. *
  79. * \param[in] protocol_name - string to protocol name.
  80. * \return protocol identifier.
  81. */
  82. IrdaProtocol irda_get_protocol_by_name(const char* protocol_name);
  83. /**
  84. * Get address length by protocol enum.
  85. *
  86. * \param[in] protocol - protocol identifier.
  87. * \return length of address in nibbles.
  88. */
  89. uint8_t irda_get_protocol_address_length(IrdaProtocol protocol);
  90. /**
  91. * Get command length by protocol enum.
  92. *
  93. * \param[in] protocol - protocol identifier.
  94. * \return length of command in nibbles.
  95. */
  96. uint8_t irda_get_protocol_command_length(IrdaProtocol protocol);
  97. /**
  98. * Checks whether protocol valid.
  99. *
  100. * \param[in] protocol - protocol identifier.
  101. * \return true if protocol is valid, false otherwise.
  102. */
  103. bool irda_is_protocol_valid(IrdaProtocol protocol);
  104. /**
  105. * Allocate IRDA encoder.
  106. *
  107. * \return encoder handler.
  108. */
  109. IrdaEncoderHandler* irda_alloc_encoder(void);
  110. /**
  111. * Free encoder handler previously allocated with \c irda_alloc_encoder().
  112. *
  113. * \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
  114. */
  115. void irda_free_encoder(IrdaEncoderHandler* handler);
  116. /**
  117. * Encode previously set IRDA message.
  118. * Usage:
  119. * 1) alloc with \c irda_alloc_encoder()
  120. * 2) set message to encode with \c irda_reset_encoder()
  121. * 3) call for \c irda_encode() to continuously get one at a time timings.
  122. * 4) when \c irda_encode() returns IrdaStatusDone, it means new message is fully encoded.
  123. * 5) to encode additional timings, just continue calling \c irda_encode().
  124. *
  125. * \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
  126. * \param[out] duration - encoded timing.
  127. * \param[out] level - encoded level.
  128. *
  129. * \return status of encode operation.
  130. */
  131. IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level);
  132. /**
  133. * Reset IRDA encoder and set new message to encode. If it's not called after receiveing
  134. * IrdaStatusDone in \c irda_encode(), encoder will encode repeat messages
  135. * till the end of time.
  136. *
  137. * \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
  138. * \param[in] message - message to encode.
  139. */
  140. void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message);
  141. /**
  142. * Get PWM frequency value for selected protocol
  143. *
  144. * \param[in] protocol - protocol to get from PWM frequency
  145. *
  146. * \return frequency
  147. */
  148. uint32_t irda_get_protocol_frequency(IrdaProtocol protocol);
  149. /**
  150. * Get PWM duty cycle value for selected protocol
  151. *
  152. * \param[in] protocol - protocol to get from PWM duty cycle
  153. *
  154. * \return duty cycle
  155. */
  156. float irda_get_protocol_duty_cycle(IrdaProtocol protocol);
  157. #ifdef __cplusplus
  158. }
  159. #endif