-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32-rmt-rf-rx.ino
126 lines (108 loc) · 3.2 KB
/
esp32-rmt-rf-rx.ino
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
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/rmt_rx.h"
#define RF_PIN 2
#define P_HIGH 1040
#define P_LOW 340
#define P_MARGIN 70
#define P_SKIPMIN 250
#define RF_DEBUG 1
// https://github.com/junkfix/esp32-rmt-rf-rx
void recvRF(void* param){
const uint16_t tlow = (P_HIGH - P_LOW - (2 * P_MARGIN));
const uint16_t thigh = (P_HIGH - P_LOW + (2 * P_MARGIN));
rmt_channel_handle_t rx_channel = NULL;
rmt_symbol_word_t symbols[64];
rmt_rx_done_event_data_t rx_data;
rmt_receive_config_t rx_config = {
.signal_range_min_ns = 2000,
.signal_range_max_ns = 1250000,
};
rmt_rx_channel_config_t rx_ch_conf = {
.gpio_num = static_cast<gpio_num_t>(RF_PIN),
.clk_src = RMT_CLK_SRC_DEFAULT,
.resolution_hz = 1000000,
.mem_block_symbols = 64,
};
rmt_new_rx_channel(&rx_ch_conf, &rx_channel);
QueueHandle_t rx_queue = xQueueCreate(1, sizeof(rx_data));
assert(rx_queue);
rmt_rx_event_callbacks_t cbs = {
.on_recv_done = rfrx_done,
};
rmt_rx_register_event_callbacks(rx_channel, &cbs, rx_queue);
rmt_enable(rx_channel);
rmt_receive(rx_channel, symbols, sizeof(symbols), &rx_config);
for(;;){
//if(isOTA){break;}
if (xQueueReceive(rx_queue, &rx_data, pdMS_TO_TICKS(1000)) == pdPASS) {
char buf[128];
size_t len = rx_data.num_symbols;
uint32_t code = 0;
rmt_symbol_word_t *cur = rx_data.received_symbols;
int16_t diff = 0;
uint16_t low = 0, high = 0, dur0 = 0, dur1 = 0, err = 0;
if (len > 23){
for (uint8_t i=0; i < 24 ; i++ ) {
dur0 = (uint16_t)cur[i].duration0;
dur1 = (uint16_t)cur[i].duration1;
if(!(cur[i].level0 && !cur[i].level1 && dur0 >= P_SKIPMIN && dur1 >= P_SKIPMIN)){
code = 0;
break;
}
if((dur0 - dur1) > 0){
code = ( code | (1ULL << (23 - i)));
high += dur0;
low += dur1;
}else{
high += dur1;
low += dur0;
}
diff = abs(dur0 - dur1);
if((diff < tlow) || (diff > thigh)){
err++;
}
}
if(code){
high /= 24;
low /= 24;
snprintf(buf, 50, "RF%d 0x%x E=%d H%d L%d", len, code, err, high, low );
Serial.println((const char *)buf);
}
if(RF_DEBUG){
char tbuf[20];
snprintf(tbuf, 20, "Rf%d: ", len);
strcpy(buf, tbuf);
for (uint8_t i=0; i < len ; i++ ) {
if(strlen(buf)>100){
Serial.print((const char *)buf); buf[0]='\0';
}
int d0 = cur[i].duration0; if(!cur[i].level0){d0 *= -1;}
int d1 = cur[i].duration1; if(!cur[i].level1){d1 *= -1;}
snprintf(tbuf, 20, "%d,%d ", d0, d1);
strcat(buf, tbuf);
}
Serial.println((const char *)buf); Serial.println();
}
}
rmt_receive(rx_channel, symbols, sizeof(symbols), &rx_config);
}
}
rmt_disable(rx_channel);
rmt_del_channel(rx_channel);
vTaskDelete(NULL);
}
bool rfrx_done(rmt_channel_handle_t channel, const rmt_rx_done_event_data_t *edata, void *udata){
BaseType_t high_task_wakeup = pdFALSE;
QueueHandle_t drx_queue = (QueueHandle_t)udata;
xQueueSendFromISR(drx_queue, edata, &high_task_wakeup);
return high_task_wakeup == pdTRUE;
}
void setup(){
Serial.begin(115200);
xTaskCreatePinnedToCore(recvRF, "recvRF", 2048, NULL, 10, NULL, 1);
}
void loop() {
delay(2000);
}