infrared_signal.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * @file infrared_signal.h
  3. * @brief Infrared signal library.
  4. *
  5. * Infrared signals may be of two types:
  6. * - known to the infrared signal decoder, or *parsed* signals
  7. * - the rest, or *raw* signals, which are treated merely as a set of timings.
  8. */
  9. #pragma once
  10. #include <flipper_format/flipper_format.h>
  11. #include <infrared/encoder_decoder/infrared.h>
  12. /**
  13. * @brief InfraredSignal opaque type declaration.
  14. */
  15. typedef struct InfraredSignal InfraredSignal;
  16. /**
  17. * @brief Raw signal type definition.
  18. *
  19. * Measurement units used:
  20. * - time: microseconds (uS)
  21. * - frequency: Hertz (Hz)
  22. * - duty_cycle: no units, fraction between 0 and 1.
  23. */
  24. typedef struct {
  25. size_t timings_size; /**< Number of elements in the timings array. */
  26. uint32_t* timings; /**< Pointer to an array of timings describing the signal. */
  27. uint32_t frequency; /**< Carrier frequency of the signal. */
  28. float duty_cycle; /**< Duty cycle of the signal. */
  29. } InfraredRawSignal;
  30. /**
  31. * @brief Create a new InfraredSignal instance.
  32. *
  33. * @returns pointer to the instance created.
  34. */
  35. InfraredSignal* infrared_signal_alloc();
  36. /**
  37. * @brief Delete an InfraredSignal instance.
  38. *
  39. * @param[in,out] signal pointer to the instance to be deleted.
  40. */
  41. void infrared_signal_free(InfraredSignal* signal);
  42. /**
  43. * @brief Test whether an InfraredSignal instance holds a raw signal.
  44. *
  45. * @param[in] signal pointer to the instance to be tested.
  46. * @returns true if the instance holds a raw signal, false otherwise.
  47. */
  48. bool infrared_signal_is_raw(const InfraredSignal* signal);
  49. /**
  50. * @brief Test whether an InfraredSignal instance holds any signal.
  51. *
  52. * @param[in] signal pointer to the instance to be tested.
  53. * @returns true if the instance holds raw signal, false otherwise.
  54. */
  55. bool infrared_signal_is_valid(const InfraredSignal* signal);
  56. /**
  57. * @brief Set an InfraredInstance to hold the signal from another one.
  58. *
  59. * Any instance's previous contents will be automatically deleted before
  60. * copying the source instance's contents.
  61. *
  62. * @param[in,out] signal pointer to the destination instance.
  63. * @param[in] other pointer to the source instance.
  64. */
  65. void infrared_signal_set_signal(InfraredSignal* signal, const InfraredSignal* other);
  66. /**
  67. * @brief Set an InfraredInstance to hold a raw signal.
  68. *
  69. * Any instance's previous contents will be automatically deleted before
  70. * copying the raw signal.
  71. *
  72. * After this call, infrared_signal_is_raw() will return true.
  73. *
  74. * @param[in,out] signal pointer to the destination instance.
  75. * @param[in] timings pointer to an array containing the raw signal timings.
  76. * @param[in] timings_size number of elements in the timings array.
  77. * @param[in] frequency signal carrier frequency, in Hertz.
  78. * @param[in] duty_cycle signal duty cycle, fraction between 0 and 1.
  79. */
  80. void infrared_signal_set_raw_signal(
  81. InfraredSignal* signal,
  82. const uint32_t* timings,
  83. size_t timings_size,
  84. uint32_t frequency,
  85. float duty_cycle);
  86. /**
  87. * @brief Get the raw signal held by an InfraredSignal instance.
  88. *
  89. * @warning the instance MUST hold a *raw* signal, otherwise undefined behaviour will occur.
  90. *
  91. * @param[in] signal pointer to the instance to be queried.
  92. * @returns pointer to the raw signal structure held by the instance.
  93. */
  94. const InfraredRawSignal* infrared_signal_get_raw_signal(const InfraredSignal* signal);
  95. /**
  96. * @brief Set an InfraredInstance to hold a parsed signal.
  97. *
  98. * Any instance's previous contents will be automatically deleted before
  99. * copying the raw signal.
  100. *
  101. * After this call, infrared_signal_is_raw() will return false.
  102. *
  103. * @param[in,out] signal pointer to the destination instance.
  104. * @param[in] message pointer to the message containing the parsed signal.
  105. */
  106. void infrared_signal_set_message(InfraredSignal* signal, const InfraredMessage* message);
  107. /**
  108. * @brief Get the parsed signal held by an InfraredSignal instance.
  109. *
  110. * @warning the instance MUST hold a *parsed* signal, otherwise undefined behaviour will occur.
  111. *
  112. * @param[in] signal pointer to the instance to be queried.
  113. * @returns pointer to the parsed signal structure held by the instance.
  114. */
  115. const InfraredMessage* infrared_signal_get_message(const InfraredSignal* signal);
  116. /**
  117. * @brief Read a signal and its name from a FlipperFormat file into an InfraredSignal instance.
  118. *
  119. * The file must be allocated and open prior to this call. The seek position determines
  120. * which signal will be read (if there is more than one in the file). Calling this function
  121. * repeatedly will result in all signals in the file to be read until no more are left.
  122. *
  123. * @param[in,out] signal pointer to the instance to be read into.
  124. * @param[in,out] ff pointer to the FlipperFormat file instance to read from.
  125. * @param[out] name pointer to the string to hold the signal name. Must be properly allocated.
  126. * @returns true if a signal was successfully read, false otherwise (e.g. no more signals to read).
  127. */
  128. bool infrared_signal_read(InfraredSignal* signal, FlipperFormat* ff, FuriString* name);
  129. /**
  130. * @brief Read a signal name from a FlipperFormat file.
  131. *
  132. * Same behaviour as infrared_signal_read(), but only the name is read.
  133. *
  134. * @param[in,out] ff pointer to the FlipperFormat file instance to read from.
  135. * @param[out] name pointer to the string to hold the signal name. Must be properly allocated.
  136. * @returns true if a signal name was successfully read, false otherwise (e.g. no more signals to read).
  137. */
  138. bool infrared_signal_read_name(FlipperFormat* ff, FuriString* name);
  139. /**
  140. * @brief Read a signal from a FlipperFormat file.
  141. *
  142. * Same behaviour as infrared_signal_read(), but only the body is read.
  143. *
  144. * @param[in,out] ff pointer to the FlipperFormat file instance to read from.
  145. * @param[out] body pointer to the InfraredSignal instance to hold the signal body. Must be properly allocated.
  146. * @returns true if a signal body was successfully read, false otherwise (e.g. syntax error).
  147. */
  148. bool infrared_signal_read_body(InfraredSignal* signal, FlipperFormat* ff);
  149. /**
  150. * @brief Read a signal with a particular name from a FlipperFormat file into an InfraredSignal instance.
  151. *
  152. * This function will look for a signal with the given name and if found, attempt to read it.
  153. * Same considerations apply as to infrared_signal_read().
  154. *
  155. * @param[in,out] signal pointer to the instance to be read into.
  156. * @param[in,out] ff pointer to the FlipperFormat file instance to read from.
  157. * @param[in] name pointer to a zero-terminated string containing the requested signal name.
  158. * @returns true if a signal was found and successfully read, false otherwise (e.g. the signal was not found).
  159. */
  160. bool infrared_signal_search_by_name_and_read(
  161. InfraredSignal* signal,
  162. FlipperFormat* ff,
  163. const char* name);
  164. /**
  165. * @brief Read a signal with a particular index from a FlipperFormat file into an InfraredSignal instance.
  166. *
  167. * This function will look for a signal with the given index and if found, attempt to read it.
  168. * Same considerations apply as to infrared_signal_read().
  169. *
  170. * @param[in,out] signal pointer to the instance to be read into.
  171. * @param[in,out] ff pointer to the FlipperFormat file instance to read from.
  172. * @param[in] index the requested signal index.
  173. * @returns true if a signal was found and successfully read, false otherwise (e.g. the signal was not found).
  174. */
  175. bool infrared_signal_search_by_index_and_read(
  176. InfraredSignal* signal,
  177. FlipperFormat* ff,
  178. size_t index);
  179. /**
  180. * @brief Save a signal contained in an InfraredSignal instance to a FlipperFormat file.
  181. *
  182. * The file must be allocated and open prior to this call. Additionally, an appropriate header
  183. * must be already written into the file.
  184. *
  185. * @param[in] signal pointer to the instance holding the signal to be saved.
  186. * @param[in,out] ff pointer to the FlipperFormat file instance to write to.
  187. * @param[in] name pointer to a zero-terminated string contating the name of the signal.
  188. */
  189. bool infrared_signal_save(const InfraredSignal* signal, FlipperFormat* ff, const char* name);
  190. /**
  191. * @brief Transmit a signal contained in an InfraredSignal instance.
  192. *
  193. * The transmission happens once per call using the built-in hardware (via HAL calls).
  194. *
  195. * @param[in] signal pointer to the instance holding the signal to be transmitted.
  196. */
  197. void infrared_signal_transmit(const InfraredSignal* signal);