2021-07-08 21:20:13 +03:00
|
|
|
#include "furi/check.h"
|
|
|
|
#include "irda.h"
|
|
|
|
#include "irda_common_i.h"
|
2021-05-18 13:51:00 +03:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <furi.h>
|
|
|
|
#include "irda_i.h"
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
static void irda_common_decoder_reset_state(IrdaCommonDecoder* common_decoder);
|
2021-05-18 13:51:00 +03:00
|
|
|
|
|
|
|
static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
|
|
|
|
furi_assert(decoder);
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
|
|
|
|
|
|
|
|
// align to start at Mark timing
|
2021-07-08 21:20:13 +03:00
|
|
|
if (!start_level) {
|
2021-05-18 13:51:00 +03:00
|
|
|
if (decoder->timings_cnt > 0) {
|
|
|
|
--decoder->timings_cnt;
|
|
|
|
shift_left_array(decoder->timings, decoder->timings_cnt, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 23:51:15 +03:00
|
|
|
if (decoder->protocol->timings.preamble_mark == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-18 13:51:00 +03:00
|
|
|
while ((!result) && (decoder->timings_cnt >= 2)) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
if ((MATCH_PREAMBLE_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance))
|
|
|
|
&& (MATCH_PREAMBLE_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder->timings_cnt -= 2;
|
|
|
|
shift_left_array(decoder->timings, decoder->timings_cnt, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
/* Pulse Distance-Width Modulation */
|
|
|
|
IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder) {
|
2021-05-18 13:51:00 +03:00
|
|
|
furi_assert(decoder);
|
|
|
|
|
|
|
|
uint32_t* timings = decoder->timings;
|
|
|
|
uint16_t index = 0;
|
|
|
|
uint8_t shift = 0;
|
2021-07-08 21:20:13 +03:00
|
|
|
IrdaStatus status = IrdaStatusError;
|
2021-05-18 13:51:00 +03: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;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// Stop bit
|
|
|
|
if ((decoder->databit_cnt == decoder->protocol->databit_len) && (decoder->timings_cnt == 1)) {
|
|
|
|
if (MATCH_BIT_TIMING(timings[0], bit1_mark, bit_tolerance)) {
|
|
|
|
decoder->timings_cnt = 0;
|
2021-07-08 21:20:13 +03:00
|
|
|
status = IrdaStatusReady;
|
2021-05-18 13:51:00 +03:00
|
|
|
} else {
|
2021-07-08 21:20:13 +03:00
|
|
|
status = IrdaStatusError;
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decoder->timings_cnt >= 2) {
|
|
|
|
index = decoder->databit_cnt / 8;
|
|
|
|
shift = decoder->databit_cnt % 8; // LSB first
|
|
|
|
if (!shift)
|
|
|
|
decoder->data[index] = 0;
|
|
|
|
if (MATCH_BIT_TIMING(timings[0], bit1_mark, bit_tolerance)
|
|
|
|
&& MATCH_BIT_TIMING(timings[1], bit1_space, bit_tolerance)) {
|
|
|
|
decoder->data[index] |= (0x1 << shift); // add 1
|
|
|
|
} else if (MATCH_BIT_TIMING(timings[0], bit0_mark, bit_tolerance)
|
|
|
|
&& MATCH_BIT_TIMING(timings[1], bit0_space, bit_tolerance)) {
|
|
|
|
(void) decoder->data[index]; // add 0
|
|
|
|
} else {
|
2021-07-08 21:20:13 +03:00
|
|
|
status = IrdaStatusError;
|
2021-05-18 13:51:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++decoder->databit_cnt;
|
|
|
|
decoder->timings_cnt -= 2;
|
|
|
|
shift_left_array(decoder->timings, decoder->timings_cnt, 2);
|
|
|
|
} else {
|
2021-07-08 21:20:13 +03:00
|
|
|
status = IrdaStatusOk;
|
2021-05-18 13:51:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
/* level switch detection goes in middle of time-quant */
|
|
|
|
IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder) {
|
|
|
|
furi_assert(decoder);
|
2021-08-20 23:51:15 +03:00
|
|
|
IrdaStatus status = IrdaStatusOk;
|
2021-07-08 21:20:13 +03:00
|
|
|
uint16_t bit = decoder->protocol->timings.bit1_mark;
|
|
|
|
uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
|
|
|
|
|
|
|
|
while (decoder->timings_cnt) {
|
|
|
|
uint32_t timing = decoder->timings[0];
|
|
|
|
bool* switch_detect = &decoder->switch_detect;
|
|
|
|
furi_assert((*switch_detect == true) || (*switch_detect == false));
|
|
|
|
|
|
|
|
bool single_timing = MATCH_BIT_TIMING(timing, bit, tolerance);
|
|
|
|
bool double_timing = MATCH_BIT_TIMING(timing, 2*bit, tolerance);
|
|
|
|
|
2021-08-20 23:51:15 +03:00
|
|
|
if(!single_timing && !double_timing) {
|
2021-07-08 21:20:13 +03:00
|
|
|
status = IrdaStatusError;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-20 23:51:15 +03:00
|
|
|
if ((decoder->protocol->manchester_start_from_space) && (decoder->databit_cnt == 0)) {
|
|
|
|
*switch_detect = 1; /* fake as we were previously in the middle of time-quant */
|
|
|
|
decoder->data[0] = 0; /* first captured timing should be Mark */
|
|
|
|
++decoder->databit_cnt;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
if (*switch_detect == 0) {
|
2021-08-20 23:51:15 +03:00
|
|
|
if (double_timing) {
|
|
|
|
status = IrdaStatusError;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-08 21:20:13 +03:00
|
|
|
/* only single timing - level switch required in the middle of time-quant */
|
|
|
|
*switch_detect = 1;
|
|
|
|
} else {
|
|
|
|
/* double timing means we in the middle of time-quant again */
|
|
|
|
if (single_timing)
|
|
|
|
*switch_detect = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
--decoder->timings_cnt;
|
|
|
|
shift_left_array(decoder->timings, decoder->timings_cnt, 1);
|
|
|
|
status = IrdaStatusOk;
|
2021-08-20 23:51:15 +03:00
|
|
|
bool level = (decoder->level + decoder->timings_cnt) % 2;
|
2021-07-08 21:20:13 +03:00
|
|
|
|
|
|
|
if (decoder->databit_cnt < decoder->protocol->databit_len) {
|
|
|
|
if (*switch_detect) {
|
|
|
|
uint8_t index = decoder->databit_cnt / 8;
|
|
|
|
uint8_t shift = decoder->databit_cnt % 8; // LSB first
|
|
|
|
if (!shift)
|
|
|
|
decoder->data[index] = 0;
|
2021-08-20 23:51:15 +03:00
|
|
|
decoder->data[index] |= (level << shift);
|
2021-07-08 21:20:13 +03:00
|
|
|
++decoder->databit_cnt;
|
|
|
|
}
|
|
|
|
if (decoder->databit_cnt == decoder->protocol->databit_len) {
|
2021-08-20 23:51:15 +03:00
|
|
|
if (level) {
|
2021-07-08 21:20:13 +03:00
|
|
|
status = IrdaStatusReady;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-20 23:51:15 +03:00
|
|
|
furi_assert(level);
|
2021-07-08 21:20:13 +03:00
|
|
|
/* cover case: sequence should be stopped after last bit was received */
|
|
|
|
if (single_timing) {
|
|
|
|
status = IrdaStatusReady;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
status = IrdaStatusError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-05-18 13:51:00 +03:00
|
|
|
IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
|
|
|
|
furi_assert(decoder);
|
|
|
|
|
|
|
|
IrdaMessage* message = 0;
|
2021-07-08 21:20:13 +03:00
|
|
|
IrdaStatus status = IrdaStatusError;
|
2021-05-18 13:51:00 +03:00
|
|
|
|
|
|
|
if (decoder->level == level) {
|
|
|
|
decoder->timings_cnt = 0;
|
|
|
|
}
|
2021-07-08 21:20:13 +03:00
|
|
|
decoder->level = level; // start with low level (Space timing)
|
2021-05-18 13:51:00 +03:00
|
|
|
|
|
|
|
decoder->timings[decoder->timings_cnt] = duration;
|
|
|
|
decoder->timings_cnt++;
|
|
|
|
furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
switch (decoder->state) {
|
2021-07-08 21:20:13 +03:00
|
|
|
case IrdaCommonDecoderStateWaitPreamble:
|
2021-05-18 13:51:00 +03:00
|
|
|
if (irda_check_preamble(decoder)) {
|
2021-07-08 21:20:13 +03:00
|
|
|
decoder->state = IrdaCommonDecoderStateDecode;
|
2021-05-18 13:51:00 +03:00
|
|
|
decoder->databit_cnt = 0;
|
2021-07-08 21:20:13 +03:00
|
|
|
decoder->switch_detect = false;
|
2021-08-20 23:51:15 +03:00
|
|
|
continue;
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
|
|
|
break;
|
2021-07-08 21:20:13 +03:00
|
|
|
case IrdaCommonDecoderStateDecode:
|
2021-05-18 13:51:00 +03:00
|
|
|
status = decoder->protocol->decode(decoder);
|
2021-07-08 21:20:13 +03:00
|
|
|
if (status == IrdaStatusReady) {
|
2021-05-18 13:51:00 +03:00
|
|
|
if (decoder->protocol->interpret(decoder)) {
|
|
|
|
message = &decoder->message;
|
2021-07-08 21:20:13 +03:00
|
|
|
decoder->state = IrdaCommonDecoderStateProcessRepeat;
|
2021-05-18 13:51:00 +03:00
|
|
|
} else {
|
2021-07-08 21:20:13 +03:00
|
|
|
decoder->state = IrdaCommonDecoderStateWaitPreamble;
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
2021-07-08 21:20:13 +03:00
|
|
|
} else if (status == IrdaStatusError) {
|
|
|
|
irda_common_decoder_reset_state(decoder);
|
2021-05-18 13:51:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2021-07-08 21:20:13 +03:00
|
|
|
case IrdaCommonDecoderStateProcessRepeat:
|
2021-05-18 13:51:00 +03:00
|
|
|
if (!decoder->protocol->decode_repeat) {
|
2021-07-08 21:20:13 +03:00
|
|
|
decoder->state = IrdaCommonDecoderStateWaitPreamble;
|
2021-05-18 13:51:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
status = decoder->protocol->decode_repeat(decoder);
|
2021-07-08 21:20:13 +03:00
|
|
|
if (status == IrdaStatusError) {
|
|
|
|
irda_common_decoder_reset_state(decoder);
|
2021-05-18 13:51:00 +03:00
|
|
|
continue;
|
2021-07-08 21:20:13 +03:00
|
|
|
} else if (status == IrdaStatusReady) {
|
2021-05-18 13:51:00 +03:00
|
|
|
decoder->message.repeat = true;
|
|
|
|
message = &decoder->message;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
|
|
|
|
furi_assert(protocol);
|
|
|
|
|
|
|
|
uint32_t alloc_size = sizeof(IrdaCommonDecoder)
|
|
|
|
+ protocol->databit_len / 8
|
|
|
|
+ !!(protocol->databit_len % 8);
|
|
|
|
IrdaCommonDecoder* decoder = furi_alloc(alloc_size);
|
|
|
|
memset(decoder, 0, alloc_size);
|
|
|
|
decoder->protocol = protocol;
|
2021-07-08 21:20:13 +03:00
|
|
|
decoder->level = true;
|
2021-05-18 13:51:00 +03:00
|
|
|
return decoder;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
void irda_common_decoder_set_context(void* decoder, void* context) {
|
|
|
|
IrdaCommonDecoder* common_decoder = decoder;
|
|
|
|
common_decoder->context = context;
|
|
|
|
}
|
|
|
|
|
2021-05-18 13:51:00 +03:00
|
|
|
void irda_common_decoder_free(void* decoder) {
|
|
|
|
furi_assert(decoder);
|
|
|
|
free(decoder);
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
void irda_common_decoder_reset_state(IrdaCommonDecoder* common_decoder) {
|
|
|
|
common_decoder->state = IrdaCommonDecoderStateWaitPreamble;
|
|
|
|
common_decoder->databit_cnt = 0;
|
|
|
|
common_decoder->switch_detect = false;
|
|
|
|
common_decoder->message.protocol = IrdaProtocolUnknown;
|
2021-08-28 14:58:01 +03:00
|
|
|
if ((common_decoder->protocol->timings.preamble_mark == 0) && (common_decoder->timings_cnt > 0)) {
|
2021-08-20 23:51:15 +03:00
|
|
|
--common_decoder->timings_cnt;
|
|
|
|
shift_left_array(common_decoder->timings, common_decoder->timings_cnt, 1);
|
|
|
|
}
|
2021-07-08 21:20:13 +03:00
|
|
|
}
|
|
|
|
|
2021-06-02 18:16:05 +03:00
|
|
|
void irda_common_decoder_reset(void* decoder) {
|
|
|
|
furi_assert(decoder);
|
|
|
|
IrdaCommonDecoder* common_decoder = decoder;
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
irda_common_decoder_reset_state(common_decoder);
|
2021-06-02 18:16:05 +03:00
|
|
|
common_decoder->timings_cnt = 0;
|
|
|
|
}
|
|
|
|
|