2022-07-20 10:56:33 +00:00
|
|
|
#include <core/check.h>
|
|
|
|
#include <core/common_defines.h>
|
2022-02-25 15:22:58 +00:00
|
|
|
#include "infrared.h"
|
|
|
|
#include "infrared_common_i.h"
|
2021-05-18 10:51:00 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <furi.h>
|
2022-02-25 15:22:58 +00:00
|
|
|
#include "infrared_i.h"
|
2021-09-09 21:37:32 +00:00
|
|
|
#include <stdint.h>
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void infrared_common_decoder_reset_state(InfraredCommonDecoder* decoder);
|
2021-09-09 21:37:32 +00:00
|
|
|
|
|
|
|
static inline size_t consume_samples(uint32_t* array, size_t len, size_t shift) {
|
|
|
|
furi_assert(len >= shift);
|
|
|
|
len -= shift;
|
2022-05-06 13:37:10 +00:00
|
|
|
for(size_t i = 0; i < len; ++i) {
|
|
|
|
array[i] = array[i + shift];
|
|
|
|
}
|
2021-09-09 21:37:32 +00:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static inline void accumulate_lsb(InfraredCommonDecoder* decoder, bool bit) {
|
2021-09-09 21:37:32 +00:00
|
|
|
uint16_t index = decoder->databit_cnt / 8;
|
2022-01-05 16:10:18 +00:00
|
|
|
uint8_t shift = decoder->databit_cnt % 8; // LSB first
|
2021-09-09 21:37:32 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(!shift) decoder->data[index] = 0;
|
2021-09-09 21:37:32 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(bit) {
|
|
|
|
decoder->data[index] |= (0x1 << shift); // add 1
|
2021-09-09 21:37:32 +00:00
|
|
|
} else {
|
2022-01-05 16:10:18 +00:00
|
|
|
(void)decoder->data[index]; // add 0
|
2021-09-09 21:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++decoder->databit_cnt;
|
|
|
|
}
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static bool infrared_check_preamble(InfraredCommonDecoder* decoder) {
|
2021-05-18 10:51:00 +00:00
|
|
|
furi_assert(decoder);
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(decoder->timings_cnt == 0) return false;
|
2021-09-15 17:22:58 +00:00
|
|
|
|
2021-05-18 10:51:00 +00:00
|
|
|
// align to start at Mark timing
|
2022-01-05 16:10:18 +00:00
|
|
|
if(!start_level) {
|
2021-09-15 17:22:58 +00:00
|
|
|
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(decoder->protocol->timings.preamble_mark == 0) {
|
2021-08-20 20:51:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
while((!result) && (decoder->timings_cnt >= 2)) {
|
2021-05-18 10:51:00 +00:00
|
|
|
float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
|
|
|
|
uint16_t preamble_mark = decoder->protocol->timings.preamble_mark;
|
|
|
|
uint16_t preamble_space = decoder->protocol->timings.preamble_space;
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if((MATCH_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance)) &&
|
|
|
|
(MATCH_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
|
2021-05-18 10:51:00 +00:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
2021-09-09 21:37:32 +00:00
|
|
|
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 2);
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:22:58 +00:00
|
|
|
/**
|
|
|
|
* decoder->protocol->databit_len[0] contains biggest amount of bits, for this protocol.
|
|
|
|
* decoder->protocol->databit_len[1...] contains lesser values, but which can be decoded
|
|
|
|
* for some protocol modifications.
|
|
|
|
*/
|
2022-02-25 15:22:58 +00:00
|
|
|
static InfraredStatus infrared_common_decode_bits(InfraredCommonDecoder* decoder) {
|
2021-05-18 10:51:00 +00:00
|
|
|
furi_assert(decoder);
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredStatus status = InfraredStatusOk;
|
|
|
|
const InfraredTimings* timings = &decoder->protocol->timings;
|
2021-09-15 17:22:58 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
while(decoder->timings_cnt && (status == InfraredStatusOk)) {
|
2021-09-15 17:22:58 +00:00
|
|
|
bool level = (decoder->level + decoder->timings_cnt + 1) % 2;
|
|
|
|
uint32_t timing = decoder->timings[0];
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(timings->min_split_time && !level) {
|
|
|
|
if(timing > timings->min_split_time) {
|
2021-10-16 12:00:21 +00:00
|
|
|
/* long low timing - check if we're ready for any of protocol modification */
|
2022-11-05 15:07:24 +00:00
|
|
|
for(size_t i = 0; i < COUNT_OF(decoder->protocol->databit_len) &&
|
|
|
|
decoder->protocol->databit_len[i];
|
2022-01-05 16:10:18 +00:00
|
|
|
++i) {
|
|
|
|
if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
|
2022-02-25 15:22:58 +00:00
|
|
|
return InfraredStatusReady;
|
2021-10-16 12:00:21 +00:00
|
|
|
}
|
2021-09-15 17:22:58 +00:00
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
} else if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
|
2021-10-16 12:00:21 +00:00
|
|
|
/* short low timing for longest protocol - this is signal is longer than we expected */
|
2022-02-25 15:22:58 +00:00
|
|
|
return InfraredStatusError;
|
2021-09-15 17:22:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status = decoder->protocol->decode(decoder, level, timing);
|
2021-10-16 12:00:21 +00:00
|
|
|
furi_check(decoder->databit_cnt <= decoder->protocol->databit_len[0]);
|
2022-02-25 15:22:58 +00:00
|
|
|
furi_assert(status == InfraredStatusError || status == InfraredStatusOk);
|
|
|
|
if(status == InfraredStatusError) {
|
2021-09-15 17:22:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
|
|
|
|
|
|
|
|
/* check if largest protocol version can be decoded */
|
2022-01-05 16:10:18 +00:00
|
|
|
if(level && (decoder->protocol->databit_len[0] == decoder->databit_cnt) &&
|
|
|
|
!timings->min_split_time) {
|
2022-02-25 15:22:58 +00:00
|
|
|
status = InfraredStatusReady;
|
2021-09-15 17:22:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pulse Distance-Width Modulation */
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredStatus
|
|
|
|
infrared_common_decode_pdwm(InfraredCommonDecoder* decoder, bool level, uint32_t timing) {
|
2021-09-15 17:22:58 +00:00
|
|
|
furi_assert(decoder);
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredStatus status = InfraredStatusOk;
|
2021-05-18 10:51:00 +00:00
|
|
|
uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
|
|
|
|
uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
|
|
|
|
uint16_t bit1_space = decoder->protocol->timings.bit1_space;
|
|
|
|
uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
|
|
|
|
uint16_t bit0_space = decoder->protocol->timings.bit0_space;
|
|
|
|
|
2021-09-15 17:22:58 +00:00
|
|
|
bool analyze_timing = level ^ (bit1_mark == bit0_mark);
|
|
|
|
uint16_t bit1 = level ? bit1_mark : bit1_space;
|
|
|
|
uint16_t bit0 = level ? bit0_mark : bit0_space;
|
|
|
|
uint16_t no_info_timing = (bit1_mark == bit0_mark) ? bit1_mark : bit1_space;
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(analyze_timing) {
|
|
|
|
if(MATCH_TIMING(timing, bit1, bit_tolerance)) {
|
2021-09-15 17:22:58 +00:00
|
|
|
accumulate_lsb(decoder, 1);
|
2022-01-05 16:10:18 +00:00
|
|
|
} else if(MATCH_TIMING(timing, bit0, bit_tolerance)) {
|
2021-09-15 17:22:58 +00:00
|
|
|
accumulate_lsb(decoder, 0);
|
2021-05-18 10:51:00 +00:00
|
|
|
} else {
|
2022-02-25 15:22:58 +00:00
|
|
|
status = InfraredStatusError;
|
2021-09-15 17:22:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-05 16:10:18 +00:00
|
|
|
if(!MATCH_TIMING(timing, no_info_timing, bit_tolerance)) {
|
2022-02-25 15:22:58 +00:00
|
|
|
status = InfraredStatusError;
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-07-08 18:20:13 +00:00
|
|
|
/* level switch detection goes in middle of time-quant */
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredStatus
|
|
|
|
infrared_common_decode_manchester(InfraredCommonDecoder* decoder, bool level, uint32_t timing) {
|
2021-07-08 18:20:13 +00:00
|
|
|
furi_assert(decoder);
|
2022-05-06 13:37:10 +00:00
|
|
|
uint32_t bit = decoder->protocol->timings.bit1_mark;
|
|
|
|
uint32_t tolerance = decoder->protocol->timings.bit_tolerance;
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2021-09-15 17:22:58 +00:00
|
|
|
bool* switch_detect = &decoder->switch_detect;
|
|
|
|
furi_assert((*switch_detect == true) || (*switch_detect == false));
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2021-09-15 17:22:58 +00:00
|
|
|
bool single_timing = MATCH_TIMING(timing, bit, tolerance);
|
2022-01-05 16:10:18 +00:00
|
|
|
bool double_timing = MATCH_TIMING(timing, 2 * bit, tolerance);
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2021-09-15 17:22:58 +00:00
|
|
|
if(!single_timing && !double_timing) {
|
2022-02-25 15:22:58 +00:00
|
|
|
return InfraredStatusError;
|
2021-09-15 17:22:58 +00:00
|
|
|
}
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(decoder->protocol->manchester_start_from_space && (decoder->databit_cnt == 0)) {
|
2021-09-15 17:22:58 +00:00
|
|
|
*switch_detect = 1; /* fake as we were previously in the middle of time-quant */
|
|
|
|
accumulate_lsb(decoder, 0);
|
|
|
|
}
|
2021-08-20 20:51:15 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(*switch_detect == 0) {
|
|
|
|
if(double_timing) {
|
2022-02-25 15:22:58 +00:00
|
|
|
return InfraredStatusError;
|
2021-07-08 18:20:13 +00:00
|
|
|
}
|
2021-09-15 17:22:58 +00:00
|
|
|
/* only single timing - level switch required in the middle of time-quant */
|
|
|
|
*switch_detect = 1;
|
|
|
|
} else {
|
|
|
|
/* double timing means we're in the middle of time-quant again */
|
2022-01-05 16:10:18 +00:00
|
|
|
if(single_timing) *switch_detect = 0;
|
2021-09-15 17:22:58 +00:00
|
|
|
}
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(*switch_detect) {
|
|
|
|
if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
|
2022-02-25 15:22:58 +00:00
|
|
|
return InfraredStatusError;
|
2021-10-16 12:00:21 +00:00
|
|
|
}
|
2021-09-15 17:22:58 +00:00
|
|
|
accumulate_lsb(decoder, level);
|
2021-07-08 18:20:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
return InfraredStatusOk;
|
2021-07-08 18:20:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredMessage* infrared_common_decoder_check_ready(InfraredCommonDecoder* decoder) {
|
|
|
|
InfraredMessage* message = NULL;
|
2021-10-16 12:00:21 +00:00
|
|
|
bool found_length = false;
|
|
|
|
|
2022-05-06 13:37:10 +00:00
|
|
|
for(size_t i = 0;
|
2022-11-05 15:07:24 +00:00
|
|
|
i < COUNT_OF(decoder->protocol->databit_len) && decoder->protocol->databit_len[i];
|
2022-01-05 16:10:18 +00:00
|
|
|
++i) {
|
|
|
|
if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
|
2021-10-16 12:00:21 +00:00
|
|
|
found_length = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 21:37:32 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(found_length && decoder->protocol->interpret(decoder)) {
|
2021-09-15 17:22:58 +00:00
|
|
|
decoder->databit_cnt = 0;
|
|
|
|
message = &decoder->message;
|
2022-01-05 16:10:18 +00:00
|
|
|
if(decoder->protocol->decode_repeat) {
|
2022-02-25 15:22:58 +00:00
|
|
|
decoder->state = InfraredCommonDecoderStateProcessRepeat;
|
2021-09-09 21:37:32 +00:00
|
|
|
} else {
|
2022-02-25 15:22:58 +00:00
|
|
|
decoder->state = InfraredCommonDecoderStateWaitPreamble;
|
2021-09-09 21:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:22:58 +00:00
|
|
|
return message;
|
2021-09-09 21:37:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredMessage*
|
|
|
|
infrared_common_decode(InfraredCommonDecoder* decoder, bool level, uint32_t duration) {
|
2021-05-18 10:51:00 +00:00
|
|
|
furi_assert(decoder);
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredMessage* message = 0;
|
|
|
|
InfraredStatus status = InfraredStatusError;
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(decoder->level == level) {
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_common_decoder_reset(decoder);
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
decoder->level = level; // start with low level (Space timing)
|
2021-05-18 10:51:00 +00:00
|
|
|
|
|
|
|
decoder->timings[decoder->timings_cnt] = duration;
|
|
|
|
decoder->timings_cnt++;
|
|
|
|
furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
|
|
|
|
|
|
|
|
while(1) {
|
2022-01-05 16:10:18 +00:00
|
|
|
switch(decoder->state) {
|
2022-02-25 15:22:58 +00:00
|
|
|
case InfraredCommonDecoderStateWaitPreamble:
|
|
|
|
if(infrared_check_preamble(decoder)) {
|
|
|
|
decoder->state = InfraredCommonDecoderStateDecode;
|
2021-05-18 10:51:00 +00:00
|
|
|
decoder->databit_cnt = 0;
|
2021-07-08 18:20:13 +00:00
|
|
|
decoder->switch_detect = false;
|
2021-08-20 20:51:15 +00:00
|
|
|
continue;
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-02-25 15:22:58 +00:00
|
|
|
case InfraredCommonDecoderStateDecode:
|
|
|
|
status = infrared_common_decode_bits(decoder);
|
|
|
|
if(status == InfraredStatusReady) {
|
|
|
|
message = infrared_common_decoder_check_ready(decoder);
|
2022-01-05 16:10:18 +00:00
|
|
|
if(message) {
|
2021-09-15 17:22:58 +00:00
|
|
|
continue;
|
2022-01-05 16:10:18 +00:00
|
|
|
} else if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
|
2021-09-15 17:22:58 +00:00
|
|
|
/* error: can't decode largest protocol - begin decoding from start */
|
2022-02-25 15:22:58 +00:00
|
|
|
decoder->state = InfraredCommonDecoderStateWaitPreamble;
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
2022-02-25 15:22:58 +00:00
|
|
|
} else if(status == InfraredStatusError) {
|
|
|
|
infrared_common_decoder_reset_state(decoder);
|
2021-05-18 10:51:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2022-02-25 15:22:58 +00:00
|
|
|
case InfraredCommonDecoderStateProcessRepeat:
|
2021-05-18 10:51:00 +00:00
|
|
|
status = decoder->protocol->decode_repeat(decoder);
|
2022-02-25 15:22:58 +00:00
|
|
|
if(status == InfraredStatusError) {
|
|
|
|
infrared_common_decoder_reset_state(decoder);
|
2021-05-18 10:51:00 +00:00
|
|
|
continue;
|
2022-02-25 15:22:58 +00:00
|
|
|
} else if(status == InfraredStatusReady) {
|
2021-05-18 10:51:00 +00:00
|
|
|
decoder->message.repeat = true;
|
|
|
|
message = &decoder->message;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void* infrared_common_decoder_alloc(const InfraredCommonProtocolSpec* protocol) {
|
2021-05-18 10:51:00 +00:00
|
|
|
furi_assert(protocol);
|
|
|
|
|
2021-09-15 17:22:58 +00:00
|
|
|
/* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
|
2022-05-06 13:37:10 +00:00
|
|
|
for(size_t i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
|
2021-09-15 17:22:58 +00:00
|
|
|
furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
uint32_t alloc_size = sizeof(InfraredCommonDecoder) + protocol->databit_len[0] / 8 +
|
2022-01-05 16:10:18 +00:00
|
|
|
!!(protocol->databit_len[0] % 8);
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredCommonDecoder* decoder = malloc(alloc_size);
|
2021-05-18 10:51:00 +00:00
|
|
|
decoder->protocol = protocol;
|
2021-07-08 18:20:13 +00:00
|
|
|
decoder->level = true;
|
2021-05-18 10:51:00 +00:00
|
|
|
return decoder;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void infrared_common_decoder_free(InfraredCommonDecoder* decoder) {
|
2021-05-18 10:51:00 +00:00
|
|
|
furi_assert(decoder);
|
|
|
|
free(decoder);
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void infrared_common_decoder_reset_state(InfraredCommonDecoder* decoder) {
|
|
|
|
decoder->state = InfraredCommonDecoderStateWaitPreamble;
|
2021-09-09 21:37:32 +00:00
|
|
|
decoder->databit_cnt = 0;
|
|
|
|
decoder->switch_detect = false;
|
2022-02-25 15:22:58 +00:00
|
|
|
decoder->message.protocol = InfraredProtocolUnknown;
|
2022-01-05 16:10:18 +00:00
|
|
|
if(decoder->protocol->timings.preamble_mark == 0) {
|
|
|
|
if(decoder->timings_cnt > 0) {
|
2021-09-09 21:37:32 +00:00
|
|
|
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
|
|
|
|
}
|
2021-08-20 20:51:15 +00:00
|
|
|
}
|
2021-07-08 18:20:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void infrared_common_decoder_reset(InfraredCommonDecoder* decoder) {
|
2021-06-02 15:16:05 +00:00
|
|
|
furi_assert(decoder);
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_common_decoder_reset_state(decoder);
|
2021-09-09 21:37:32 +00:00
|
|
|
decoder->timings_cnt = 0;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|