[FL-1696, FL-1685] IRDA: Add RC5, decoder refactoring (#663)

* [FL-1696] IRDA: Split decoders and protocols
* IRDA: Restruct directories.
* IRDA: fix long timings
* [FL-1685] IRDA: Add RC5

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
Albert Kharisov
2021-08-20 23:51:15 +03:00
committed by GitHub
parent ecff31d228
commit e6d5f4038b
24 changed files with 677 additions and 242 deletions

View File

@@ -0,0 +1,273 @@
#include "furi/check.h"
#include "irda.h"
#include "irda_common_i.h"
#include <stdbool.h>
#include <furi.h>
#include "irda_i.h"
static void irda_common_decoder_reset_state(IrdaCommonDecoder* common_decoder);
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
if (!start_level) {
if (decoder->timings_cnt > 0) {
--decoder->timings_cnt;
shift_left_array(decoder->timings, decoder->timings_cnt, 1);
}
}
if (decoder->protocol->timings.preamble_mark == 0) {
return true;
}
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;
}
/* Pulse Distance-Width Modulation */
IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
uint32_t* timings = decoder->timings;
uint16_t index = 0;
uint8_t shift = 0;
IrdaStatus status = IrdaStatusError;
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;
status = IrdaStatusReady;
} else {
status = IrdaStatusError;
}
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 {
status = IrdaStatusError;
break;
}
++decoder->databit_cnt;
decoder->timings_cnt -= 2;
shift_left_array(decoder->timings, decoder->timings_cnt, 2);
} else {
status = IrdaStatusOk;
break;
}
}
return status;
}
/* level switch detection goes in middle of time-quant */
IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
IrdaStatus status = IrdaStatusOk;
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);
if(!single_timing && !double_timing) {
status = IrdaStatusError;
break;
}
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;
}
if (*switch_detect == 0) {
if (double_timing) {
status = IrdaStatusError;
break;
}
/* 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;
bool level = (decoder->level + decoder->timings_cnt) % 2;
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;
decoder->data[index] |= (level << shift);
++decoder->databit_cnt;
}
if (decoder->databit_cnt == decoder->protocol->databit_len) {
if (level) {
status = IrdaStatusReady;
break;
}
}
} else {
furi_assert(level);
/* cover case: sequence should be stopped after last bit was received */
if (single_timing) {
status = IrdaStatusReady;
break;
} else {
status = IrdaStatusError;
}
}
}
return status;
}
IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
furi_assert(decoder);
IrdaMessage* message = 0;
IrdaStatus status = IrdaStatusError;
if (decoder->level == level) {
decoder->timings_cnt = 0;
}
decoder->level = level; // start with low level (Space timing)
decoder->timings[decoder->timings_cnt] = duration;
decoder->timings_cnt++;
furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
while(1) {
switch (decoder->state) {
case IrdaCommonDecoderStateWaitPreamble:
if (irda_check_preamble(decoder)) {
decoder->state = IrdaCommonDecoderStateDecode;
decoder->databit_cnt = 0;
decoder->switch_detect = false;
continue;
}
break;
case IrdaCommonDecoderStateDecode:
status = decoder->protocol->decode(decoder);
if (status == IrdaStatusReady) {
if (decoder->protocol->interpret(decoder)) {
message = &decoder->message;
decoder->state = IrdaCommonDecoderStateProcessRepeat;
} else {
decoder->state = IrdaCommonDecoderStateWaitPreamble;
}
} else if (status == IrdaStatusError) {
irda_common_decoder_reset_state(decoder);
continue;
}
break;
case IrdaCommonDecoderStateProcessRepeat:
if (!decoder->protocol->decode_repeat) {
decoder->state = IrdaCommonDecoderStateWaitPreamble;
continue;
}
status = decoder->protocol->decode_repeat(decoder);
if (status == IrdaStatusError) {
irda_common_decoder_reset_state(decoder);
continue;
} else if (status == IrdaStatusReady) {
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;
decoder->level = true;
return decoder;
}
void irda_common_decoder_set_context(void* decoder, void* context) {
IrdaCommonDecoder* common_decoder = decoder;
common_decoder->context = context;
}
void irda_common_decoder_free(void* decoder) {
furi_assert(decoder);
free(decoder);
}
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;
if (common_decoder->protocol->timings.preamble_mark == 0) {
--common_decoder->timings_cnt;
shift_left_array(common_decoder->timings, common_decoder->timings_cnt, 1);
}
}
void irda_common_decoder_reset(void* decoder) {
furi_assert(decoder);
IrdaCommonDecoder* common_decoder = decoder;
irda_common_decoder_reset_state(common_decoder);
common_decoder->timings_cnt = 0;
}

View File

@@ -0,0 +1,160 @@
#include "furi/check.h"
#include "irda.h"
#include "irda_common_i.h"
#include <stdbool.h>
#include <furi.h>
#include "irda_i.h"
/*
*
* 3:
* even_timing = 0
* level = 0 ^ 1 = 1
* 4:
* even_timing = 1
* level = 1 ^ 1 = 0
* ++timing;
*
*
* 0 1 2 | 3 4 |
* _____-------_____---___
*/
IrdaStatus irda_common_encode_manchester(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
furi_assert(encoder);
furi_assert(duration);
furi_assert(level);
const IrdaTimings* timings = &encoder->protocol->timings;
uint8_t index = encoder->bits_encoded / 8;
uint8_t shift = encoder->bits_encoded % 8; // LSB first
bool logic_value = !!(encoder->data[index] & (0x01 << shift));
bool even_timing = !(encoder->timings_encoded % 2);
*level = even_timing ^ logic_value;
*duration = timings->bit1_mark;
if (even_timing) /* start encoding from space */
++encoder->bits_encoded;
++encoder->timings_encoded;
bool finish = (encoder->bits_encoded == encoder->protocol->databit_len);
finish |= (encoder->bits_encoded == (encoder->protocol->databit_len-1)) && *level && !even_timing;
return finish ? IrdaStatusDone : IrdaStatusOk;
}
IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
furi_assert(encoder);
furi_assert(duration);
furi_assert(level);
const IrdaTimings* timings = &encoder->protocol->timings;
uint8_t index = encoder->bits_encoded / 8;
uint8_t shift = encoder->bits_encoded % 8; // LSB first
bool logic_value = !!(encoder->data[index] & (0x01 << shift));
// stop bit
if (encoder->bits_encoded == encoder->protocol->databit_len) {
*duration = timings->bit1_mark;
*level = true;
++encoder->timings_encoded;
return IrdaStatusDone;
}
if (encoder->timings_encoded % 2) { /* start encoding from space */
*duration = logic_value ? timings->bit1_mark : timings->bit0_mark;
*level = true;
} else {
*duration = logic_value ? timings->bit1_space : timings->bit0_space;
*level = false;
++encoder->bits_encoded;
}
++encoder->timings_encoded;
return IrdaStatusOk;
}
IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
furi_assert(encoder);
furi_assert(duration);
furi_assert(level);
IrdaStatus status = IrdaStatusOk;
const IrdaTimings* timings = &encoder->protocol->timings;
switch (encoder->state) {
case IrdaCommonEncoderStateSpace:
*duration = encoder->protocol->timings.silence_time;
*level = false;
status = IrdaStatusOk;
encoder->state = IrdaCommonEncoderStatePreamble;
++encoder->timings_encoded;
break;
case IrdaCommonEncoderStatePreamble:
if (timings->preamble_mark) {
if (encoder->timings_encoded == 1) {
*duration = timings->preamble_mark;
*level = true;
} else {
*duration = timings->preamble_space;
*level = false;
encoder->state = IrdaCommonEncoderStateEncode;
}
++encoder->timings_encoded;
break;
} else {
encoder->state = IrdaCommonEncoderStateEncode;
}
/* FALLTHROUGH */
case IrdaCommonEncoderStateEncode:
status = encoder->protocol->encode(encoder, duration, level);
if (status == IrdaStatusDone) {
if (encoder->protocol->encode_repeat) {
encoder->state = IrdaCommonEncoderStateEncodeRepeat;
} else {
encoder->timings_encoded = 0;
encoder->bits_encoded = 0;
encoder->switch_detect = 0;
encoder->state = IrdaCommonEncoderStateSpace;
}
}
break;
case IrdaCommonEncoderStateEncodeRepeat:
status = encoder->protocol->encode_repeat(encoder, duration, level);
break;
}
return status;
}
void* irda_common_encoder_alloc(const IrdaCommonProtocolSpec* protocol) {
furi_assert(protocol);
uint32_t alloc_size = sizeof(IrdaCommonEncoder)
+ protocol->databit_len / 8
+ !!(protocol->databit_len % 8);
IrdaCommonEncoder* encoder = furi_alloc(alloc_size);
memset(encoder, 0, alloc_size);
encoder->protocol = protocol;
return encoder;
}
void irda_common_encoder_free(IrdaCommonEncoder* encoder) {
furi_assert(encoder);
free(encoder);
}
void irda_common_encoder_reset(IrdaCommonEncoder* encoder) {
furi_assert(encoder);
encoder->timings_encoded = 0;
encoder->bits_encoded = 0;
encoder->state = IrdaCommonEncoderStateSpace;
encoder->switch_detect = 0;
uint8_t bytes_to_clear = encoder->protocol->databit_len / 8
+ !!(encoder->protocol->databit_len % 8);
memset(encoder->data, 0, bytes_to_clear);
}
void irda_common_encoder_set_context(void* decoder, void* context) {
IrdaCommonEncoder* common_encoder = decoder;
common_encoder->context = context;
}

View File

@@ -0,0 +1,90 @@
#pragma once
#include <stdint.h>
#include "irda.h"
#include "irda_i.h"
#define MATCH_BIT_TIMING(x, v, delta) ( ((x) < (v + delta)) \
&& ((x) > (v - delta)))
#define MATCH_PREAMBLE_TIMING(x, v, delta) ( ((x) < ((v) * (1 + (delta)))) \
&& ((x) > ((v) * (1 - (delta)))))
typedef struct IrdaCommonDecoder IrdaCommonDecoder;
typedef struct IrdaCommonEncoder IrdaCommonEncoder;
typedef IrdaStatus (*IrdaCommonDecode)(IrdaCommonDecoder*);
typedef bool (*IrdaCommonInterpret)(IrdaCommonDecoder*);
typedef IrdaStatus (*IrdaCommonEncode)(IrdaCommonEncoder* encoder, uint32_t* out, bool* polarity);
typedef struct {
IrdaTimings timings;
bool manchester_start_from_space;
uint32_t databit_len;
IrdaCommonDecode decode;
IrdaCommonDecode decode_repeat;
IrdaCommonInterpret interpret;
IrdaCommonEncode encode;
IrdaCommonEncode encode_repeat;
} IrdaCommonProtocolSpec;
typedef enum {
IrdaCommonDecoderStateWaitPreamble,
IrdaCommonDecoderStateDecode,
IrdaCommonDecoderStateProcessRepeat,
} IrdaCommonStateDecoder;
typedef enum {
IrdaCommonEncoderStateSpace,
IrdaCommonEncoderStatePreamble,
IrdaCommonEncoderStateEncode,
IrdaCommonEncoderStateEncodeRepeat,
} IrdaCommonStateEncoder;
struct IrdaCommonDecoder {
const IrdaCommonProtocolSpec* protocol;
IrdaCommonStateDecoder state;
IrdaMessage message;
uint32_t timings[6];
uint8_t timings_cnt;
void* context;
bool switch_detect;
uint32_t level;
uint16_t databit_cnt;
uint8_t data[];
};
struct IrdaCommonEncoder {
const IrdaCommonProtocolSpec* protocol;
IrdaCommonStateEncoder state;
bool switch_detect;
uint32_t bits_encoded;
uint32_t timings_encoded;
void* context;
uint8_t data[];
};
static inline void shift_left_array(uint32_t *array, uint32_t len, uint32_t shift) {
for (int i = 0; i < len; ++i)
array[i] = array[i + shift];
}
IrdaMessage* irda_common_decode(IrdaCommonDecoder *decoder, bool level, uint32_t duration);
IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder);
IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder);
void irda_common_decoder_set_context(void* decoder, void* context);
void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec *protocol);
void irda_common_decoder_free(void* decoder);
void irda_common_decoder_reset(void* decoder);
IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bool* polarity);
IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duration, bool* polarity);
IrdaStatus irda_common_encode_manchester(IrdaCommonEncoder* encoder, uint32_t* duration, bool* polarity);
void irda_common_encoder_set_context(void* decoder, void* context);
void* irda_common_encoder_alloc(const IrdaCommonProtocolSpec* protocol);
void irda_common_encoder_free(IrdaCommonEncoder* encoder);
void irda_common_encoder_reset(IrdaCommonEncoder* encoder);

View File

@@ -0,0 +1,79 @@
#include "irda_common_i.h"
#include "irda_protocol_defs_i.h"
const IrdaCommonProtocolSpec protocol_nec = {
.timings = {
.preamble_mark = IRDA_NEC_PREAMBULE_MARK,
.preamble_space = IRDA_NEC_PREAMBULE_SPACE,
.bit1_mark = IRDA_NEC_BIT1_MARK,
.bit1_space = IRDA_NEC_BIT1_SPACE,
.bit0_mark = IRDA_NEC_BIT0_MARK,
.bit0_space = IRDA_NEC_BIT0_SPACE,
.preamble_tolerance = IRDA_NEC_PREAMBLE_TOLERANCE,
.bit_tolerance = IRDA_NEC_BIT_TOLERANCE,
.silence_time = IRDA_NEC_SILENCE,
},
.databit_len = 32,
.decode = irda_common_decode_pdwm,
.encode = irda_common_encode_pdwm,
.interpret = irda_decoder_nec_interpret,
.decode_repeat = irda_decoder_nec_decode_repeat,
.encode_repeat = irda_encoder_nec_encode_repeat,
};
const IrdaCommonProtocolSpec protocol_samsung32 = {
.timings = {
.preamble_mark = IRDA_SAMSUNG_PREAMBULE_MARK,
.preamble_space = IRDA_SAMSUNG_PREAMBULE_SPACE,
.bit1_mark = IRDA_SAMSUNG_BIT1_MARK,
.bit1_space = IRDA_SAMSUNG_BIT1_SPACE,
.bit0_mark = IRDA_SAMSUNG_BIT0_MARK,
.bit0_space = IRDA_SAMSUNG_BIT0_SPACE,
.preamble_tolerance = IRDA_SAMSUNG_PREAMBLE_TOLERANCE,
.bit_tolerance = IRDA_SAMSUNG_BIT_TOLERANCE,
.silence_time = IRDA_SAMSUNG_SILENCE,
},
.databit_len = 32,
.decode = irda_common_decode_pdwm,
.encode = irda_common_encode_pdwm,
.interpret = irda_decoder_samsung32_interpret,
.decode_repeat = irda_decoder_samsung32_decode_repeat,
.encode_repeat = irda_encoder_samsung32_encode_repeat,
};
const IrdaCommonProtocolSpec protocol_rc6 = {
.timings = {
.preamble_mark = IRDA_RC6_PREAMBULE_MARK,
.preamble_space = IRDA_RC6_PREAMBULE_SPACE,
.bit1_mark = IRDA_RC6_BIT,
.preamble_tolerance = IRDA_RC6_PREAMBLE_TOLERANCE,
.bit_tolerance = IRDA_RC6_BIT_TOLERANCE,
.silence_time = IRDA_RC6_SILENCE,
},
.databit_len = 1 + 3 + 1 + 8 + 8, // start_bit + 3 mode bits, + 1 toggle bit (x2 timing) + 8 address + 8 command
.manchester_start_from_space = false,
.decode = irda_decoder_rc6_decode_manchester,
.encode = irda_encoder_rc6_encode_manchester,
.interpret = irda_decoder_rc6_interpret,
.decode_repeat = NULL,
.encode_repeat = NULL,
};
const IrdaCommonProtocolSpec protocol_rc5 = {
.timings = {
.preamble_mark = 0,
.preamble_space = 0,
.bit1_mark = IRDA_RC5_BIT,
.preamble_tolerance = 0,
.bit_tolerance = IRDA_RC5_BIT_TOLERANCE,
.silence_time = IRDA_RC5_SILENCE,
},
.databit_len = 1 + 1 + 1 + 5 + 6, // start_bit + start_bit/command_bit + toggle_bit + 5 address + 6 command
.manchester_start_from_space = true,
.decode = irda_common_decode_manchester,
.encode = irda_common_encode_manchester,
.interpret = irda_decoder_rc5_interpret,
.decode_repeat = NULL,
.encode_repeat = NULL,
};