Skip to content

Commit 81e39a8

Browse files
authored
Merge pull request #58 from limpkin/dev
do not allow ir code 0
2 parents b54bce8 + a857c66 commit 81e39a8

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/MoonLight/Nodes/Drivers/D_Infrared.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
*/
2121
#define NEC_LEADING_CODE_DURATION_0 9000
2222
#define NEC_LEADING_CODE_DURATION_1 4500
23-
#define NEC_PAYLOAD_ZERO_DURATION_0 560
24-
#define NEC_PAYLOAD_ZERO_DURATION_1 560
25-
#define NEC_PAYLOAD_ONE_DURATION_0 560
26-
#define NEC_PAYLOAD_ONE_DURATION_1 1690
23+
#define NEC_PAYLOAD_ZERO_DURATION_0 562
24+
#define NEC_PAYLOAD_ZERO_DURATION_1 562
25+
#define NEC_PAYLOAD_ONE_DURATION_0 562
26+
#define NEC_PAYLOAD_ONE_DURATION_1 1687
2727
#define NEC_REPEAT_CODE_DURATION_0 9000
2828
#define NEC_REPEAT_CODE_DURATION_1 2250
29-
#define EXAMPLE_IR_NEC_DECODE_MARGIN 200 // Tolerance for parsing RMT symbols into bit stream
29+
#define EXAMPLE_IR_NEC_DECODE_MARGIN 300 // Tolerance for parsing RMT symbols into bit stream
3030

3131
class IRDriver : public Node {
3232
public:
@@ -151,7 +151,7 @@ class IRDriver : public Node {
151151

152152
// the following timing requirement is based on NEC protocol
153153
rmt_receive_config_t receive_config = {
154-
.signal_range_min_ns = 1250, // the shortest duration for NEC signal is 560us, 1250ns < 560us, valid signal won't be treated as noise
154+
.signal_range_min_ns = 3186, // the shortest duration for NEC signal is 560us, (set to max value)
155155
.signal_range_max_ns = 12000000, // the longest duration for NEC signal is 9000us, 12000000ns > 9000us, the receive won't stop early
156156
};
157157

@@ -184,6 +184,7 @@ class IRDriver : public Node {
184184
} else if (nec_parse_logic0(cur)) {
185185
address &= ~(1 << i);
186186
} else {
187+
EXT_LOGI(IR_DRIVER_TAG, "addr bit decode error");
187188
return false;
188189
}
189190
cur++;
@@ -194,6 +195,7 @@ class IRDriver : public Node {
194195
} else if (nec_parse_logic0(cur)) {
195196
command &= ~(1 << i);
196197
} else {
198+
EXT_LOGI(IR_DRIVER_TAG, "command bit decode error");
197199
return false;
198200
}
199201
cur++;
@@ -216,22 +218,27 @@ class IRDriver : public Node {
216218
}
217219
// decode RMT symbols
218220

221+
bool valid_parsing = false;
219222
bool nec_repeat = false;
220223
if (symbol_num == 34)
221-
nec_parse_frame(rmt_nec_symbols);
224+
valid_parsing = nec_parse_frame(rmt_nec_symbols);
222225
else if (symbol_num == 2) {
223-
nec_parse_frame_repeat(rmt_nec_symbols);
226+
valid_parsing = nec_parse_frame_repeat(rmt_nec_symbols);
224227
nec_repeat = true;
225228
} else {
226229
//EXT_LOGI(IR_DRIVER_TAG, "Unknown NEC frame");
227230
return;
228231
}
229232

230-
EXT_LOGI(IR_DRIVER_TAG, "Address=%04X, Command=%04X %s", s_nec_code_address, s_nec_code_command, symbol_num == 2 ? "Longpress" : "");
231-
233+
// create combined code
232234
uint32_t combined_code = (((uint32_t)s_nec_code_address) << 16) | s_nec_code_command;
233235

234-
// EXT_LOGI(IR_DRIVER_TAG, "%08X %08X %d", combined_code, codeBlueDec, combined_code == codeBlueDec);
236+
// do not allow 0s, which also match non initialize IR codes
237+
if ((combined_code == 0) || (valid_parsing == false)){
238+
return;
239+
}
240+
241+
EXT_LOGI(IR_DRIVER_TAG, "Address=%04X, Command=%04X %s", s_nec_code_address, s_nec_code_command, symbol_num == 2 ? "Longpress" : "");
235242

236243
JsonDocument doc;
237244
JsonObject newState = doc.to<JsonObject>();
@@ -334,6 +341,7 @@ class IRDriver : public Node {
334341
void loop() override {
335342
if (receive_queue) {
336343
if (xQueueReceive(receive_queue, &rx_data, 0) == pdPASS) {
344+
EXT_LOGD(IR_DRIVER_TAG, "Received symbols: #%d", rx_data.num_symbols);
337345
// parse the receive symbols and print the result
338346
parse_nec_frame(rx_data.received_symbols, rx_data.num_symbols);
339347
// start receive again

0 commit comments

Comments
 (0)