2023-01-13 13:50:19 +00:00
|
|
|
#include "infrared_protocol_nec_i.h"
|
|
|
|
#include <core/check.h>
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredMessage* infrared_decoder_nec_check_ready(void* ctx) {
|
|
|
|
return infrared_common_decoder_check_ready(ctx);
|
2021-09-15 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
bool infrared_decoder_nec_interpret(InfraredCommonDecoder* decoder) {
|
2021-05-18 10:51:00 +00:00
|
|
|
furi_assert(decoder);
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(decoder->databit_cnt == 32) {
|
2021-09-15 17:22:58 +00:00
|
|
|
uint8_t address = decoder->data[0];
|
|
|
|
uint8_t address_inverse = decoder->data[1];
|
|
|
|
uint8_t command = decoder->data[2];
|
|
|
|
uint8_t command_inverse = decoder->data[3];
|
2022-05-06 13:37:10 +00:00
|
|
|
uint8_t inverse_command_inverse = (uint8_t)~command_inverse;
|
|
|
|
uint8_t inverse_address_inverse = (uint8_t)~address_inverse;
|
|
|
|
if((command == inverse_command_inverse) && (address == inverse_address_inverse)) {
|
2022-02-25 15:22:58 +00:00
|
|
|
decoder->message.protocol = InfraredProtocolNEC;
|
2021-08-20 20:51:15 +00:00
|
|
|
decoder->message.address = address;
|
2021-09-15 17:22:58 +00:00
|
|
|
decoder->message.command = command;
|
|
|
|
decoder->message.repeat = false;
|
|
|
|
result = true;
|
2021-08-20 20:51:15 +00:00
|
|
|
} else {
|
2022-02-25 15:22:58 +00:00
|
|
|
decoder->message.protocol = InfraredProtocolNECext;
|
2021-08-20 20:51:15 +00:00
|
|
|
decoder->message.address = decoder->data[0] | (decoder->data[1] << 8);
|
2021-09-15 17:22:58 +00:00
|
|
|
decoder->message.command = decoder->data[2] | (decoder->data[3] << 8);
|
|
|
|
decoder->message.repeat = false;
|
|
|
|
result = true;
|
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
} else if(decoder->databit_cnt == 42) {
|
|
|
|
uint32_t* data1 = (void*)decoder->data;
|
|
|
|
uint16_t* data2 = (void*)(data1 + 1);
|
2021-09-15 17:22:58 +00:00
|
|
|
uint16_t address = *data1 & 0x1FFF;
|
|
|
|
uint16_t address_inverse = (*data1 >> 13) & 0x1FFF;
|
|
|
|
uint16_t command = ((*data1 >> 26) & 0x3F) | ((*data2 & 0x3) << 6);
|
|
|
|
uint16_t command_inverse = (*data2 >> 2) & 0xFF;
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if((address == (~address_inverse & 0x1FFF)) && (command == (~command_inverse & 0xFF))) {
|
2022-02-25 15:22:58 +00:00
|
|
|
decoder->message.protocol = InfraredProtocolNEC42;
|
2021-09-15 17:22:58 +00:00
|
|
|
decoder->message.address = address;
|
|
|
|
decoder->message.command = command;
|
|
|
|
decoder->message.repeat = false;
|
|
|
|
result = true;
|
|
|
|
} else {
|
2022-02-25 15:22:58 +00:00
|
|
|
decoder->message.protocol = InfraredProtocolNEC42ext;
|
2021-09-15 17:22:58 +00:00
|
|
|
decoder->message.address = address | (address_inverse << 13);
|
|
|
|
decoder->message.command = command | (command_inverse << 8);
|
|
|
|
decoder->message.repeat = false;
|
|
|
|
result = true;
|
2021-08-20 20:51:15 +00:00
|
|
|
}
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:51:00 +00:00
|
|
|
// timings start from Space (delay between message and repeat)
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredStatus infrared_decoder_nec_decode_repeat(InfraredCommonDecoder* decoder) {
|
2021-05-18 10:51:00 +00:00
|
|
|
furi_assert(decoder);
|
|
|
|
|
|
|
|
float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
|
|
|
|
uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredStatus status = InfraredStatusError;
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
if(decoder->timings_cnt < 4) return InfraredStatusOk;
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
if((decoder->timings[0] > INFRARED_NEC_REPEAT_PAUSE_MIN) &&
|
|
|
|
(decoder->timings[0] < INFRARED_NEC_REPEAT_PAUSE_MAX) &&
|
|
|
|
MATCH_TIMING(decoder->timings[1], INFRARED_NEC_REPEAT_MARK, preamble_tolerance) &&
|
|
|
|
MATCH_TIMING(decoder->timings[2], INFRARED_NEC_REPEAT_SPACE, preamble_tolerance) &&
|
2021-09-09 21:37:32 +00:00
|
|
|
MATCH_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)) {
|
2022-02-25 15:22:58 +00:00
|
|
|
status = InfraredStatusReady;
|
2021-05-18 10:51:00 +00:00
|
|
|
decoder->timings_cnt = 0;
|
|
|
|
} else {
|
2022-02-25 15:22:58 +00:00
|
|
|
status = InfraredStatusError;
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void* infrared_decoder_nec_alloc(void) {
|
2023-01-13 13:50:19 +00:00
|
|
|
return infrared_common_decoder_alloc(&infrared_protocol_nec);
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredMessage* infrared_decoder_nec_decode(void* decoder, bool level, uint32_t duration) {
|
|
|
|
return infrared_common_decode(decoder, level, duration);
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void infrared_decoder_nec_free(void* decoder) {
|
|
|
|
infrared_common_decoder_free(decoder);
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void infrared_decoder_nec_reset(void* decoder) {
|
|
|
|
infrared_common_decoder_reset(decoder);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|