1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include "esp_err.h"
22 
23 #define IR_TOOLS_FLAGS_PROTO_EXT (1 << 0) /*!< Enable Extended IR protocol */
24 #define IR_TOOLS_FLAGS_INVERSE (1 << 1)   /*!< Inverse the IR signal, i.e. take high level as low, and vice versa */
25 
26 /**
27 * @brief IR device type
28 *
29 */
30 typedef void *ir_dev_t;
31 
32 /**
33 * @brief IR builder type
34 *
35 */
36 typedef struct ir_builder_s ir_builder_t;
37 
38 /**
39 * @brief IR parser type
40 *
41 */
42 typedef struct ir_parser_s ir_parser_t;
43 
44 /**
45 * @brief Type definition of IR builder
46 *
47 */
48 struct ir_builder_s {
49     /**
50     * @brief Period time of sending repeat code
51     *
52     */
53     uint32_t repeat_period_ms;
54 
55     /**
56     * @brief Build frame header
57     *
58     * @param[in] builder: Handle of IR builder
59     *
60     * @return
61     *      - ESP_OK: Build frame header successfully
62     *      - ESP_FAIL: Build frame header failed because some error occurred
63     */
64     esp_err_t (*make_head)(ir_builder_t *builder);
65 
66     /**
67     * @brief Build logic bit zero
68     *
69     * @param[in] builder: Handle of IR builder
70     *
71     * @return
72     *      - ESP_OK: Build logic bit zero successfully
73     *      - ESP_FAIL: Build logic bit zero failed because some error occurred
74     */
75     esp_err_t (*make_logic0)(ir_builder_t *builder);
76 
77     /**
78     * @brief Build logic bit one
79     *
80     * @param[in] builder: Handle of IR builder
81     *
82     * @return
83     *      ESP_OK: Build logic bit one successfully
84     *      ESP_FAIL: Build logic bit one failed because some error occurred
85     */
86     esp_err_t (*make_logic1)(ir_builder_t *builder);
87 
88     /**
89     * @brief Build frame tail
90     *
91     * @param[in] builder: Handle of IR builder
92     *
93     * @return
94     *      - ESP_OK: Build frame tail successfully
95     *      - ESP_FAIL: Build frame tail failed because some error occurred
96     */
97     esp_err_t (*make_end)(ir_builder_t *builder);
98 
99     /**
100     * @brief Build a complete frame
101     *
102     * @param[in] builder: Handle of IR builder
103     *
104     * @return
105     *      - ESP_OK: Build a complete frame successfully
106     *      - ESP_FAIL: Build a complete frame failed because some error occurred
107     */
108     esp_err_t (*build_frame)(ir_builder_t *builder, uint32_t address, uint32_t command);
109 
110     /**
111     * @brief Build a repeat frame
112     *
113     * @param[in] builder: Handle of IR builder
114     *
115     * @return
116     *      - ESP_OK: Build a repeat frame successfully
117     *      - ESP_FAIL: Build a repeat frame failed because some error occurred
118     */
119     esp_err_t (*build_repeat_frame)(ir_builder_t *builder);
120 
121     /**
122     * @brief Get the result frame after a series of building steps
123     *
124     * @param[in] builder: Handle of IR builder
125     * @param[out] result: Result of frame building, which contains all of the raw data that could be send directly
126     * @param[out] length: Length of result data
127     *
128     * @return
129     *      - ESP_OK: Get result data successfully
130     *      - ESP_ERR_INVALID_ARG: Get result data failed because of invalid arguments
131     *      - ESP_FAIL: Get result data failed because some other errors occurred
132     */
133     esp_err_t (*get_result)(ir_builder_t *builder, void *result, size_t *length);
134 
135     /**
136     * @brief Free resources used by IR builder
137     *
138     * @param[in] builder: Handle of IR builder
139     *
140     * @return
141     *      - ESP_OK: Free resources successfully
142     *      - ESP_FAIL: Free resources failed because some error occurred
143     */
144     esp_err_t (*del)(ir_builder_t *builder);
145 };
146 
147 /**
148 * @brief Type definition of IR parser
149 *
150 */
151 struct ir_parser_s {
152     /**
153     * @brief Input raw data to IR parser
154     *
155     * @param[in] parser: Handle of IR parser
156     * @param[in] raw_data: Raw data which need decoding by IR parser
157     * @param[in] length: Length of raw data
158     *
159     * @return
160     *      - ESP_OK: Input raw data successfully
161     *      - ESP_ERR_INVALID_ARG: Input raw data failed because of invalid argument
162     *      - ESP_FAIL: Input raw data failed because some other error occurred
163     */
164     esp_err_t (*input)(ir_parser_t *parser, void *raw_data, uint32_t length);
165 
166     /**
167     * @brief Get the scan code after decoding of raw data
168     *
169     * @param[in] parser: Handle of IR parser
170     * @param[out] address: Address of the scan code
171     * @param[out] command: Command of the scan code
172     * @param[out] repeat: Indicate if it's a repeat code
173     *
174     * @return
175     *      - ESP_OK: Get scan code successfully
176     *      - ESP_ERR_INVALID_ARG: Get scan code failed because of invalid arguments
177     *      - ESP_FAIL: Get scan code failed because some error occurred
178     */
179     esp_err_t (*get_scan_code)(ir_parser_t *parser, uint32_t *address, uint32_t *command, bool *repeat);
180 
181     /**
182     * @brief Free resources used by IR parser
183     *
184     * @param[in] parser: Handle of IR parser
185     *
186     * @return
187     *      - ESP_OK: Free resource successfully
188     *      - ESP_FAIL: Free resources fail failed because some error occurred
189     */
190     esp_err_t (*del)(ir_parser_t *parser);
191 };
192 
193 /**
194 * @brief Configuration type of IR builder
195 *
196 */
197 typedef struct {
198     uint32_t buffer_size; /*!< Size of the internal buffer used by IR builder */
199     ir_dev_t dev_hdl;     /*!< IR device handle */
200     uint32_t flags;       /*!< Flags for IR builder, different flags will enable different features */
201 } ir_builder_config_t;
202 
203 /**
204 * @brief Configuration type of IR parser
205 *
206 */
207 typedef struct {
208     ir_dev_t dev_hdl;   /*!< IR device handle */
209     uint32_t flags;     /*!< Flags for IR parser, different flags will enable different features */
210     uint32_t margin_us; /*!< Timing parameter, indicating the tolerance to environment noise */
211 } ir_parser_config_t;
212 
213 /**
214  * @brief Default configuration for IR builder
215  *
216  */
217 #define IR_BUILDER_DEFAULT_CONFIG(dev) \
218     {                                  \
219         .buffer_size = 64,             \
220         .dev_hdl = dev,                \
221         .flags = 0,                    \
222     }
223 
224 /**
225  * @brief Default configuration for IR parser
226  *
227  */
228 #define IR_PARSER_DEFAULT_CONFIG(dev) \
229     {                                 \
230         .dev_hdl = dev,               \
231         .flags = 0,                   \
232         .margin_us = 200,             \
233     }
234 
235 /**
236 * @brief Creat a NEC protocol builder
237 *
238 * @param config: configuration of NEC builder
239 * @return
240 *      Handle of NEC builder or NULL
241 */
242 ir_builder_t *ir_builder_rmt_new_nec(const ir_builder_config_t *config);
243 
244 /**
245 * @brief Creat a RC5 protocol builder
246 *
247 * @param config: configuration of RC5 builder
248 * @return
249 *      Handle of RC5 builder or NULL
250 */
251 ir_builder_t *ir_builder_rmt_new_rc5(const ir_builder_config_t *config);
252 
253 /**
254 * @brief Creat a NEC protocol parser
255 *
256 * @param config: configuration of NEC parser
257 * @return
258 *      Handle of NEC parser or NULL
259 */
260 ir_parser_t *ir_parser_rmt_new_nec(const ir_parser_config_t *config);
261 
262 /**
263 * @brief Creat a RC5 protocol parser
264 *
265 * @param config: configuration of RC5 parser
266 * @return
267 *      Handle of RC5 parser or NULL
268 */
269 ir_parser_t *ir_parser_rmt_new_rc5(const ir_parser_config_t *config);
270 #ifdef __cplusplus
271 }
272 #endif
273