aboutsummaryrefslogtreecommitdiff
path: root/src/app/google_rcu/ir_module/ir_send_handle.c
blob: dba98d5054a6ac9a05bf40cf2b6becc8490ba410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
*********************************************************************************************************
*               Copyright(c) 2020, Realtek Semiconductor Corporation. All rights reserved.
**********************************************************************************************************
* @file         ir_send_handle.c
* @brief        This file provides IR sending data handler by interrupt.
* @details      Application users can use the global functions to send IR data.
* @author       barry_bian
* @date         2020-02-26
* @version      v1.1
*********************************************************************************************************
*/

/*============================================================================*
 *                              Header Files
 *============================================================================*/
#include "board.h"
#include "trace.h"
#include "string.h"
#include "rtl876x_ir.h"
#include "app_msg.h"
#include "ir_protocol_nec.h"
#include "ir_send_driver.h"
#include "ir_send_handle.h"
#include "ir_service_handle.h"
#include "key_handle.h"

#if SUPPORT_IR_TX_FEATURE

/*============================================================================*
 *                          Local Variables
 *============================================================================*/
static T_IR_SEND_KEY_STATE ir_send_key_state = IR_SEND_KEY_RELEASE;
static T_IR_SEND_PARA ir_send_parameters;

/*============================================================================*
 *                          Local Functions
 *============================================================================*/
/******************************************************************
* @brief   encode ir send command data
* @param   T_IR_PROTOCOL protocol
* @param   uint8_t ir_key_command
* @param   T_IR_SEND_PARA *p_ir_send_parameters
* @return  result
* @retval  T_IRDA_RET
*/
static T_IRDA_RET ir_send_command_encode(T_IR_PROTOCOL ir_protocol, IR_KEY_CODE ir_key_code,
                                         T_IR_SEND_PARA *p_ir_send_parameters)
{
    T_IRDA_RET ret = IRDA_SUCCEED;

    switch (ir_protocol)
    {
    case NEC_PROTOCOL:
        ret = ir_protocol_nec_command_encode(ir_key_code, p_ir_send_parameters);
        if (ret == IRDA_SUCCEED)
        {
            ret = ir_protocol_nec_repeat_code_encode(p_ir_send_parameters);
        }
        break;
    default:
        break;
    }

    return ret;
}

/******************************************************************
* @brief   encode ir send repeat code data
* @param   T_IR_PROTOCOL protocol
* @param   uint8_t ir_key_command
* @param   T_IR_SEND_PARA *p_ir_send_parameters
* @return  result
* @retval  T_IRDA_RET
*/
T_IRDA_RET ir_send_repeat_code_encode(T_IR_PROTOCOL ir_protocol, IR_KEY_CODE ir_key_code,
                                      T_IR_SEND_PARA *p_ir_send_parameters)
{
    T_IRDA_RET ret = IRDA_SUCCEED;

    switch (ir_protocol)
    {
    case NEC_PROTOCOL:
        ret = ir_protocol_nec_repeat_code_encode(p_ir_send_parameters);
        break;
    default:
        break;
    }

    return ret;
}

/******************************************************************
* @brief   ir_send_raw_encode
* @param   key_index
* @return  result
* @retval  true or false
*/
bool ir_send_raw_encode(T_KEY_INDEX_DEF key_index)
{
    bool result = false;
    uint32_t code_size = 0;
    uint8_t code[MAX_CODE_LENGTH];
    uint32_t data_index = 0;
    uint8_t duty_cycle = 0;
    uint16_t carrier_freq = 0;
    uint16_t seq_len_1 = 0;
    uint16_t seq_len_2 = 0;
    uint16_t repeat_delay;
    uint16_t on_data_value;
    uint16_t off_data_value;
    uint32_t total_cycle_cnt;

    if (true == ir_service_handle_get_key_code_by_key_id((uint8_t)key_index, &code_size, code))
    {
        /* get code type */
        ir_send_parameters.ir_send_type = code[data_index++];

        if (code_size >= 8)
        {
            data_index = 1;
            duty_cycle = code[data_index++];
            if (duty_cycle == 0)
            {
                duty_cycle = 50;
            }
            ir_send_parameters.duty_cycle = 100 / (float)duty_cycle;

            carrier_freq = ((uint16_t)code[data_index] << 8) + code[data_index + 1];
            data_index += 2;
            if (carrier_freq == 0)
            {
                carrier_freq = 380;
            }
            ir_send_parameters.carrier_frequency_hz = carrier_freq * 100;

            seq_len_1 = ((uint16_t)code[data_index] << 8) + code[data_index + 1];
            data_index += 2;

            seq_len_2 = ((uint16_t)code[data_index] << 8) + code[data_index + 1];
            data_index += 2;
            repeat_delay = seq_len_2;
        }

        if ((ir_send_parameters.ir_send_type == IR_SEND_TYPE_ONE_TIME)
            && (code_size >= seq_len_1 * 4 + 6))
        {
            data_index = 6;
            total_cycle_cnt = 0;

            for (uint8_t loop_index = 0; loop_index < seq_len_1; loop_index++)
            {
                on_data_value = ((uint16_t)code[data_index] << 8) + code[data_index + 1];
                off_data_value = ((uint16_t)code[data_index + 2] << 8) + code[data_index + 3];
                data_index += 4;
                total_cycle_cnt += on_data_value + off_data_value;

                ir_send_parameters.ir_seq_buf_1[2 * loop_index] = on_data_value | PULSE_HIGH;
                ir_send_parameters.ir_seq_buf_1[2 * loop_index + 1] = off_data_value;
            }
            ir_send_parameters.ir_seq_len_1 = seq_len_1 * 2;
            if (carrier_freq != 0)
            {
                ir_send_parameters.ir_seq_timeout_ms_1 = (total_cycle_cnt * 10 + (carrier_freq >> 1)) /
                                                         carrier_freq;
            }
            result = true;
        }
        else if ((ir_send_parameters.ir_send_type == IR_SEND_TYPE_REPEATED_SEQ)
                 && (code_size >= seq_len_1 * 4 + 8))
        {
            data_index = 8;
            total_cycle_cnt = 0;

            for (uint8_t loop_index = 0; loop_index < seq_len_1; loop_index++)
            {
                on_data_value = ((uint16_t)code[data_index] << 8) + code[data_index + 1];
                off_data_value = ((uint16_t)code[data_index + 2] << 8) + code[data_index + 3];
                data_index += 4;
                total_cycle_cnt += on_data_value + off_data_value;

                ir_send_parameters.ir_seq_buf_1[2 * loop_index] = on_data_value | PULSE_HIGH;
                ir_send_parameters.ir_seq_buf_1[2 * loop_index + 1] = off_data_value;
            }
            ir_send_parameters.ir_seq_len_1 = seq_len_1 * 2;
            ir_send_parameters.ir_seq_timeout_ms_1 = (repeat_delay * 10 + (carrier_freq >> 1)) / carrier_freq;

            result = true;
        }
        else if ((ir_send_parameters.ir_send_type == IR_SEND_TYPE_ONE_TIME_PLUS_REP_SEQ)
                 || (ir_send_parameters.ir_send_type == IR_SEND_TYPE_TWO_REP_SEQ))
        {
            if (code_size >= seq_len_1 * 4 + seq_len_2 * 4 + 8)
            {
                data_index = 8;

                total_cycle_cnt = 0;
                for (uint8_t loop_index = 0; loop_index < seq_len_1; loop_index++)
                {
                    on_data_value = ((uint16_t)code[data_index] << 8) + code[data_index + 1];
                    off_data_value = ((uint16_t)code[data_index + 2] << 8) + code[data_index + 3];
                    data_index += 4;
                    total_cycle_cnt += on_data_value + off_data_value;

                    ir_send_parameters.ir_seq_buf_1[2 * loop_index] = on_data_value | PULSE_HIGH;
                    ir_send_parameters.ir_seq_buf_1[2 * loop_index + 1] = off_data_value;
                }
                ir_send_parameters.ir_seq_len_1 = seq_len_1 * 2;
                ir_send_parameters.ir_seq_timeout_ms_1 = (total_cycle_cnt * 10 + (carrier_freq >> 1)) /
                                                         carrier_freq;

                total_cycle_cnt = 0;
                for (uint8_t loop_index = 0; loop_index < seq_len_2; loop_index++)
                {
                    on_data_value = ((uint16_t)code[data_index] << 8) + code[data_index + 1];
                    off_data_value = ((uint16_t)code[data_index + 2] << 8) + code[data_index + 3];
                    data_index += 4;
                    total_cycle_cnt += on_data_value + off_data_value;

                    ir_send_parameters.ir_seq_buf_2[2 * loop_index] = on_data_value | PULSE_HIGH;
                    ir_send_parameters.ir_seq_buf_2[2 * loop_index + 1] = off_data_value;
                }
                ir_send_parameters.ir_seq_len_2 = seq_len_2 * 2;
                ir_send_parameters.ir_seq_timeout_ms_2 = (total_cycle_cnt * 10 + (carrier_freq >> 1)) /
                                                         carrier_freq;

                result = true;
            }
        }
    }
    else
    {
        APP_PRINT_WARN0("[ir_send_key_press_handle] Failed to get key code");
    }

    APP_PRINT_INFO4("[ir_send_raw_encode] carrier_freq = %d, duty_cycle = %d, seq_len_1 = %d, seq_len_2 = %d",
                    carrier_freq, duty_cycle, seq_len_1, seq_len_2);
    APP_PRINT_INFO4("[ir_send_raw_encode] key_index = %d, result = %d, ir_send_type = %d, code_size = %d",
                    key_index,
                    result, ir_send_parameters.ir_send_type, code_size);

    return result;
}

/*============================================================================*
 *                          Global Functions
 *============================================================================*/
/******************************************************************
 * @brief   IR app send message process.
 * @param   msg_sub_type - message subtype
 * @return  none
 * @retval  void
 */
void ir_send_msg_proc(uint16_t msg_sub_type)
{
    APP_PRINT_INFO2("[ir_send_msg_proc] msg_sub_type is %d, ir_send_get_current_state is %d",
                    msg_sub_type, ir_send_get_current_state());

    if (ir_send_key_state == IR_SEND_KEY_RELEASE)
    {
        ir_send_exit();
        return;
    }

    if (msg_sub_type == IO_MSG_TYPE_IR_START_SEND_REPEAT_CODE)
    {
        if (ir_send_get_current_state() == IR_SEND_CAMMAND_COMPLETE ||
            ir_send_get_current_state() == IR_SEND_REPEAT_CODE_COMPLETE)
        {
            if (false == ir_send_repeat_code_start())
            {
                ir_send_exit();
            }
        }
        else
        {
            ir_send_exit();
        }
    }
}

/******************************************************************
 * @brief   Application code for IR send key press handle.
 * @param   T_KEY_INDEX_DEF ir_send_key_index
 * @return  none
 * @retval  void
 */
void ir_send_key_press_handle(T_KEY_INDEX_DEF ir_key_index, T_IR_PROTOCOL ir_protocol)
{
    bool result = false;
    ir_send_key_state = IR_SEND_KEY_PRESS;

    memset(&ir_send_parameters, 0, sizeof(T_IR_SEND_PARA));

    if (ir_protocol == NEC_PROTOCOL)
    {
        IR_KEY_CODE ir_code = key_handle_get_ir_key_code_by_index(ir_key_index);
        ir_send_command_encode(NEC_PROTOCOL, ir_code, &ir_send_parameters);
        ir_send_parameters.ir_send_type = IR_SEND_TYPE_ONE_TIME_PLUS_REP_SEQ;
        result = true;
    }
    else if (ir_protocol == RAW_PROTOCOL)
    {
        result = ir_send_raw_encode(ir_key_index);
    }
    else
    {
        APP_PRINT_WARN1("[ir_send_key_press_handle] Unknown ir_protocol is %d", ir_protocol);
        result = false;
    }


    if ((result == true)
        && (true == ir_send_module_init(&ir_send_parameters)))
    {
        result = ir_send_command_start();
    }

    APP_PRINT_INFO2("[IR] ir send key press, ir_protocol is %d, result is %d", ir_protocol, result);
}

/******************************************************************
 * @brief   Application code for IR send data one time handle.
 * @param   T_KEY_INDEX_DEF ir_send_key_index
 * @return  none
 * @retval  void
 */
void ir_send_data_one_time_handle(T_KEY_INDEX_DEF ir_key_index, T_IR_PROTOCOL ir_protocol)
{
    bool result = false;
    ir_send_key_state = IR_SEND_KEY_PRESS;

    memset(&ir_send_parameters, 0, sizeof(T_IR_SEND_PARA));

    if (ir_protocol == NEC_PROTOCOL)
    {
        IR_KEY_CODE ir_code = key_handle_get_ir_key_code_by_index(ir_key_index);
        ir_send_command_encode(NEC_PROTOCOL, ir_code, &ir_send_parameters);
        ir_send_parameters.ir_send_type = IR_SEND_TYPE_ONE_TIME;
        result = true;
    }
    else if (ir_protocol == RAW_PROTOCOL)
    {
        result = ir_send_raw_encode(ir_key_index);
    }
    else
    {
        APP_PRINT_WARN1("[ir_send_data_one_time_handle] Unknown ir_protocol is %d", ir_protocol);
        result = false;
    }


    if ((result == true)
        && (true == ir_send_module_init(&ir_send_parameters)))
    {
        result = ir_send_command_start();
    }

    APP_PRINT_INFO2("[IR] ir_send_data_one_time, ir_protocol is %d, result is %d", ir_protocol, result);
}
/******************************************************************
 * @brief   Application code for IR send key release handle.
 * @param   none
 * @return  none
 * @retval  void
 */
void ir_send_key_release_handle(void)
{
    APP_PRINT_INFO0("[IR] ir send key release.");
    ir_send_key_state = IR_SEND_KEY_RELEASE;

    if (ir_send_is_working() == false)
    {
        return;
    }

    if (ir_send_get_current_state() != IR_SEND_CAMMAND &&
        ir_send_get_current_state() != IR_SEND_REPEAT_CODE)
    {
        ir_send_exit();
    }
}

#endif /*end Micro @SUPPORT_IR_TX_FEATURE*/


/******************* (C) COPYRIGHT 2020 Realtek Semiconductor Corporation *****END OF FILE****/