forked from karawin/Ka-Radio32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirnec.c
277 lines (251 loc) · 9.85 KB
/
irnec.c
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
/******************************************************************************
*
* Copyright 2017 karawin (http://www.karawin.fr)
*
* Receive and decode nec IR. Send result in a queue
*
*******************************************************************************/
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>
#include "ClickEncoder.h"
#include "app_main.h"
#include "gpio.h"
#include "webclient.h"
#include "webserver.h"
#include "interface.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/rmt.h"
#include "driver/periph_ctrl.h"
#include "soc/rmt_reg.h"
//IR
#define RMT_RX_ACTIVE_LEVEL 0 /*!< If we connect with a IR receiver, the data is active low */
#define RMT_RX_CHANNEL 0 /*!< RMT channel for receiver */
#define RMT_RX_GPIO_NUM PIN_IR_SIGNAL /*!< GPIO number for receiver */
#define RMT_CLK_DIV 100 /*!< RMT counter clock divider */
#define RMT_TICK_10_US (80000000/RMT_CLK_DIV/100000) /*!< RMT counter value for 10 us.(Source clock is APB clock) */
#define NEC_HEADER_HIGH_US 9300 /*!< NEC protocol header: positive 9ms */
#define NEC_HEADER_LOW_US 4600 /*!< NEC protocol header: negative 4.5ms*/
#define NEC_REPEAT_LOW_US 2300 /*!< NEC protocol repeat: negative 2.25ms*/
#define NEC_BIT_ONE_HIGH_US 600 /*!< NEC protocol data bit 1: positive 0.56ms */
#define NEC_BIT_ONE_LOW_US (2250-NEC_BIT_ONE_HIGH_US) /*!< NEC protocol data bit 1: negative 1.69ms */
#define NEC_BIT_ZERO_HIGH_US 600 /*!< NEC protocol data bit 0: positive 0.56ms */
#define NEC_BIT_ZERO_LOW_US (1120-NEC_BIT_ZERO_HIGH_US) /*!< NEC protocol data bit 0: negative 0.56ms */
#define NEC_BIT_END 600/*560*/ /*!< NEC protocol end: positive 0.56ms */
#define NEC_BIT_MARGIN 400/*20*/ /*!< NEC parse margin time */
#define NEC_ITEM_DURATION(d) ((d & 0x7fff)*10/RMT_TICK_10_US) /*!< Parse duration time from memory register value */
#define NEC_DATA_ITEM_NUM 34 /*!< NEC code item number: header + 32bit data + end */
#define rmt_item32_tIMEOUT_US 9500 /*!< RMT receiver timeout value(us) */
#define RTN_REPEAT 0xFF /*!< return code for a repeat frame*/
static const char* NEC_TAG = "NEC";
/*
* @brief Check whether duration is around target_us
*/
inline bool nec_check_in_range(int duration_ticks, int target_us, int margin_us)
{
// ESP_LOGD(NEC_TAG,"NEC_ITEM_DURATION(%d)= %d",duration_ticks,NEC_ITEM_DURATION(duration_ticks));
if(( NEC_ITEM_DURATION(duration_ticks) < (target_us + margin_us))
&& ( NEC_ITEM_DURATION(duration_ticks) > (target_us - margin_us))) {
return true;
} else {
return false;
}
}
/*
* @brief Check whether this value represents an NEC header
*/
static bool nec_header_if(rmt_item32_t* item)
{
ESP_LOGD(NEC_TAG,"Header Duration0: %x Level0: %x, Duration1: %x, Level1: %x",item->duration0,item->level0,item->duration1,item->level1);
if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
&& nec_check_in_range(item->duration0, NEC_HEADER_HIGH_US, NEC_BIT_MARGIN)
&& nec_check_in_range(item->duration1, NEC_HEADER_LOW_US, NEC_BIT_MARGIN)) {
return true;
}
return false;
}
/*
* @brief Check whether this value represents an NEC repeat
*/
static bool nec_repeat_if(rmt_item32_t* item)
{
ESP_LOGD(NEC_TAG,"Repeat Duration0: %x Level0: %x, Duration1: %x, Level1: %x",item->duration0,item->level0,item->duration1,item->level1);
ESP_LOGV(NEC_TAG,"NEC_ITEM_DURATION0(%d)= %d",item->duration0,NEC_ITEM_DURATION(item->duration0));
ESP_LOGV(NEC_TAG,"NEC_ITEM_DURATION1(%d)= %d",item->duration1,NEC_ITEM_DURATION(item->duration1));
if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
&& nec_check_in_range(item->duration0, NEC_HEADER_HIGH_US, NEC_BIT_MARGIN)
&& nec_check_in_range(item->duration1, NEC_REPEAT_LOW_US, NEC_BIT_MARGIN)) {
ESP_LOGD(NEC_TAG,"nec_repeat_if true");
return true;
}
return false;
}
/*
* @brief Check whether this value represents an NEC data bit 1
*/
static bool nec_bit_one_if(rmt_item32_t* item)
{
ESP_LOGD(NEC_TAG,"nec_bit_one_if Duration0: %x Level0: %x, Duration1: %x, Level1: %x",item->duration0,item->level0,item->duration1,item->level1);
ESP_LOGV(NEC_TAG,"NEC_ITEM_DURATION0(%d)= %d",item->duration0,NEC_ITEM_DURATION(item->duration0));
ESP_LOGV(NEC_TAG,"NEC_ITEM_DURATION1(%d)= %d",item->duration1,NEC_ITEM_DURATION(item->duration1));
if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
&& nec_check_in_range(item->duration0, NEC_BIT_ONE_HIGH_US, NEC_BIT_MARGIN)
&& nec_check_in_range(item->duration1, NEC_BIT_ONE_LOW_US, NEC_BIT_MARGIN)) {
ESP_LOGD(NEC_TAG,"nec_bit_one_if true");
return true;
}
return false;
}
/*
* @brief Check whether this value represents an NEC data bit 0
*/
static bool nec_bit_zero_if(rmt_item32_t* item)
{
ESP_LOGD(NEC_TAG,"nec_bit_zero_if Duration0: %x Level0: %x, Duration1: %x, Level1: %x",item->duration0,item->level0,item->duration1,item->level1);
ESP_LOGV(NEC_TAG,"NEC_ITEM_DURATION0(%d)= %d",item->duration0,NEC_ITEM_DURATION(item->duration0));
ESP_LOGV(NEC_TAG,"NEC_ITEM_DURATION1(%d)= %d",item->duration1,NEC_ITEM_DURATION(item->duration1));
if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
&& nec_check_in_range(item->duration0, NEC_BIT_ZERO_HIGH_US, NEC_BIT_MARGIN)
&& nec_check_in_range(item->duration1, NEC_BIT_ZERO_LOW_US, NEC_BIT_MARGIN)) {
ESP_LOGD(NEC_TAG,"nec_bit_zero_if true");
return true;
}
return false;
}
/*
* @brief Parse NEC 32 bit waveform to address and command.
*/
static int nec_parse_items(rmt_item32_t* item, int item_num, uint16_t* addr, uint16_t* data)
{
ESP_LOGD(NEC_TAG,"RMT item len: %d",item_num);
// ESP_LOGD(NEC_TAG,"Duration0: %x, Level0: %x, Duration1: %x, Level1: %x",item->duration0,item->level0,item->duration1,item->level1);
// ESP_LOG_BUFFER_HEXDUMP(NEC_TAG, item, item_num, ESP_LOG_DEBUG);
int w_len = item_num;
/* if(w_len < NEC_DATA_ITEM_NUM) {
ESP_LOGD(NEC_TAG,"Duration0: %x, Level0: %x, Duration1: %x, Level1: %x",item->duration0,item->level0,item->duration1,item->level1);
return -1;
}
*/
int i = 0, j = 0;
if((w_len == 2)&&(nec_repeat_if(item)))
{
ESP_LOGD(NEC_TAG,"Repeat detected");
return RTN_REPEAT;
}
if(!nec_header_if(item++)) {
item--;
ESP_LOGD(NEC_TAG,"Duration0: %x, Level0: %x, Duration1: %x, Level1: %x",item->duration0,item->level0,item->duration1,item->level1);
return -2;
}
uint16_t addr_t = 0;
for(j = 0; j < 16; j++) {
if(nec_bit_one_if(item)) {
addr_t |= (1 << j);
} else if(nec_bit_zero_if(item)) {
addr_t |= (0 << j);
} else {
return -3;
}
item++;
i++;
}
uint16_t data_t = 0;
for(j = 0; j < 16; j++) {
if(nec_bit_one_if(item)) {
data_t |= (1 << j);
} else if(nec_bit_zero_if(item)) {
data_t |= (0 << j);
} else {
return -4;
}
item++;
i++;
}
*addr = addr_t ;
*data = data_t;
return i;
}
/*
* @brief RMT receiver initialization
*/
static void nec_rx_init()
{
rmt_config_t rmt_rx;
rmt_rx.channel = RMT_RX_CHANNEL;
rmt_rx.gpio_num = RMT_RX_GPIO_NUM;
rmt_rx.clk_div = RMT_CLK_DIV;
rmt_rx.mem_block_num = 1;
rmt_rx.rmt_mode = RMT_MODE_RX;
rmt_rx.rx_config.filter_en = true;
rmt_rx.rx_config.filter_ticks_thresh = 100;
rmt_rx.rx_config.idle_threshold = rmt_item32_tIMEOUT_US / 10 * (RMT_TICK_10_US);
rmt_config(&rmt_rx);
rmt_driver_install(rmt_rx.channel, 1000, 0);
}
/**
* @brief RMT receiver , this task will send each received NEC data to a queue.
*
*/
void rmt_nec_rx_task()
{
event_ir_t evt;
event_ir_t last_evt;
int channel = RMT_RX_CHANNEL;
nec_rx_init();
RingbufHandle_t rb = NULL;
bool flagFirstRepeat = false;
//get RMT RX ringbuffer
ESP_ERROR_CHECK(rmt_get_ringbuf_handle(channel, &rb));
ESP_ERROR_CHECK(rmt_rx_start(channel, 1));
ESP_LOGD(NEC_TAG,"RMT started");
while(rb) {
size_t rx_size = 0;
//try to receive data from ringbuffer.
//RMT driver will push all the data it receives to its ringbuffer.
//We just need to parse the value and return the spaces of ringbuffer.
rmt_item32_t* item = (rmt_item32_t*) xRingbufferReceive(rb, &rx_size, 1000);
if(item) {
uint16_t rmt_addr;
uint16_t rmt_cmd;
int offset = 0;
while(1) {
//parse data value from ringbuffer.
int res = nec_parse_items(item + offset, rx_size / 4 - offset, &rmt_addr, &rmt_cmd);
if (res == RTN_REPEAT)
{
if (flagFirstRepeat == true)
xQueueSend(event_ir,&last_evt, 0);
flagFirstRepeat = true; // not the first one
offset += 3;
}
else if(res > 0) {
offset += res + 1;
ESP_LOGD(NEC_TAG, "RMT RCV --- addr: 0x%04x cmd: 0x%04x", rmt_addr, rmt_cmd);
evt.channel = channel;
evt.addr = rmt_addr;
evt.cmd = rmt_cmd;
evt.repeat_flag = false;
last_evt.addr = evt.addr;
last_evt.cmd = evt.cmd;
last_evt.repeat_flag = true;
flagFirstRepeat = false;
xQueueSend(event_ir,&evt, 0);
} else {
ESP_LOGD(NEC_TAG, "RMT Res: %d",res);
break;
}
}
//after parsing the data, return spaces to ringbuffer.
vRingbufferReturnItem(rb, (void*) item);
} else {
vTaskDelay(10);
//break;
}
}
ESP_LOGD(NEC_TAG,"RMT finished");
vTaskDelete(NULL);
}