irda.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. typedef struct IrdaDecoderHandler IrdaDecoderHandler;
  10. typedef struct IrdaEncoderHandler IrdaEncoderHandler;
  11. // Do not change protocol order, as it can be saved into memory and fw update can be performed!
  12. typedef enum {
  13. IrdaProtocolUnknown = -1,
  14. IrdaProtocolNEC = 0,
  15. IrdaProtocolNECext = 1,
  16. IrdaProtocolSamsung32 = 2,
  17. IrdaProtocolRC6 = 3,
  18. } IrdaProtocol;
  19. typedef struct {
  20. IrdaProtocol protocol;
  21. uint32_t address;
  22. uint32_t command;
  23. bool repeat;
  24. } IrdaMessage;
  25. typedef enum {
  26. IrdaStatusError,
  27. IrdaStatusOk,
  28. IrdaStatusDone,
  29. IrdaStatusReady,
  30. } IrdaStatus;
  31. /**
  32. * Initialize decoder.
  33. *
  34. * \return returns pointer to IRDA decoder handler if success, otherwise - error.
  35. */
  36. IrdaDecoderHandler* irda_alloc_decoder(void);
  37. /**
  38. * Provide to decoder next timing.
  39. *
  40. * \param[in] handler - handler to IRDA decoders. Should be aquired with \c irda_alloc_decoder().
  41. * \param[in] level - high(true) or low(false) level of input signal to analyze.
  42. * it should alternate every call, otherwise it is an error case,
  43. * and decoder resets its state and start decoding from the start.
  44. * \param[in] duration - duration of steady high/low input signal.
  45. * \return if message is ready, returns pointer to decoded message, returns NULL.
  46. */
  47. const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration);
  48. /**
  49. * Deinitialize decoder and free allocated memory.
  50. *
  51. * \param[in] handler - handler to IRDA decoders. Should be aquired with \c irda_alloc_decoder().
  52. */
  53. void irda_free_decoder(IrdaDecoderHandler* handler);
  54. /**
  55. * Reset IRDA decoder.
  56. *
  57. * \param[in] handler - handler to IRDA decoders. Should be aquired with \c irda_alloc_decoder().
  58. */
  59. void irda_reset_decoder(IrdaDecoderHandler* handler);
  60. /**
  61. * Get protocol name by protocol enum.
  62. *
  63. * \param[in] protocol - protocol identifier.
  64. * \return string to protocol name.
  65. */
  66. const char* irda_get_protocol_name(IrdaProtocol protocol);
  67. /**
  68. * Get protocol enum by protocol name.
  69. *
  70. * \param[in] protocol_name - string to protocol name.
  71. * \return protocol identifier.
  72. */
  73. IrdaProtocol irda_get_protocol_by_name(const char* protocol_name);
  74. /**
  75. * Get address length by protocol enum.
  76. *
  77. * \param[in] protocol - protocol identifier.
  78. * \return length of address in nibbles.
  79. */
  80. uint8_t irda_get_protocol_address_length(IrdaProtocol protocol);
  81. /**
  82. * Get command length by protocol enum.
  83. *
  84. * \param[in] protocol - protocol identifier.
  85. * \return length of command in nibbles.
  86. */
  87. uint8_t irda_get_protocol_command_length(IrdaProtocol protocol);
  88. /**
  89. * Checks whether protocol valid.
  90. *
  91. * \param[in] protocol - protocol identifier.
  92. * \return true if protocol is valid, false otherwise.
  93. */
  94. bool irda_is_protocol_valid(IrdaProtocol protocol);
  95. /**
  96. * Allocate IRDA encoder.
  97. *
  98. * \return encoder handler.
  99. */
  100. IrdaEncoderHandler* irda_alloc_encoder(void);
  101. /**
  102. * Free encoder handler previously allocated with \c irda_alloc_encoder().
  103. *
  104. * \param[in] handler - handler to IRDA encoder. Should be aquired with \c irda_alloc_encoder().
  105. */
  106. void irda_free_encoder(IrdaEncoderHandler* handler);
  107. /**
  108. * Encode previously set IRDA message.
  109. * Usage:
  110. * 1) alloc with \c irda_alloc_encoder()
  111. * 2) set message to encode with \c irda_reset_encoder()
  112. * 3) call for \c irda_encode() to continuously get one at a time timings.
  113. * 4) when \c irda_encode() returns IrdaStatusDone, it means new message is fully encoded.
  114. * 5) to encode additional timings, just continue calling \c irda_encode().
  115. *
  116. * \param[in] handler - handler to IRDA encoder. Should be aquired with \c irda_alloc_encoder().
  117. * \param[out] duration - encoded timing.
  118. * \param[out] level - encoded level.
  119. *
  120. * \return status of encode operation.
  121. */
  122. IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level);
  123. /**
  124. * Reset IRDA encoder and set new message to encode. If it's not called after receiveing
  125. * IrdaStatusDone in \c irda_encode(), encoder will encode repeat messages
  126. * till the end of time.
  127. *
  128. * \param[in] handler - handler to IRDA encoder. Should be aquired with \c irda_alloc_encoder().
  129. * \param[in] message - message to encode.
  130. */
  131. void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message);
  132. #ifdef __cplusplus
  133. }
  134. #endif