[FL-1398] IRDA: Implement timings encoder, add RC-6 (#570)

* Add RC-6 protocol
* Implement timings Encoder
* Remove Unit-tests from build
This commit is contained in:
Albert Kharisov
2021-07-08 21:20:13 +03:00
committed by GitHub
parent 4ce41a3e6f
commit 9f6e14d005
32 changed files with 1563 additions and 489 deletions

View File

@@ -1,49 +1,11 @@
#include "irda_protocol_defs_i.h"
#include <stdbool.h>
#include <stdint.h>
#include <furi.h>
#include "../irda_i.h"
static bool interpret_nec(IrdaCommonDecoder* decoder);
static bool interpret_necext(IrdaCommonDecoder* decoder);
static DecodeStatus decode_repeat_nec(IrdaCommonDecoder* decoder);
static const IrdaCommonProtocolSpec protocol_nec = {
{
IRDA_NEC_PREAMBULE_MARK,
IRDA_NEC_PREAMBULE_SPACE,
IRDA_NEC_BIT1_MARK,
IRDA_NEC_BIT1_SPACE,
IRDA_NEC_BIT0_MARK,
IRDA_NEC_BIT0_SPACE,
IRDA_NEC_PREAMBLE_TOLERANCE,
IRDA_NEC_BIT_TOLERANCE,
},
32,
irda_common_decode_pdwm,
interpret_nec,
decode_repeat_nec,
};
static const IrdaCommonProtocolSpec protocol_necext = {
{
IRDA_NEC_PREAMBULE_MARK,
IRDA_NEC_PREAMBULE_SPACE,
IRDA_NEC_BIT1_MARK,
IRDA_NEC_BIT1_SPACE,
IRDA_NEC_BIT0_MARK,
IRDA_NEC_BIT0_SPACE,
IRDA_NEC_PREAMBLE_TOLERANCE,
IRDA_NEC_BIT_TOLERANCE,
},
32,
irda_common_decode_pdwm,
interpret_necext,
decode_repeat_nec,
};
static bool interpret_nec(IrdaCommonDecoder* decoder) {
bool irda_decoder_nec_interpret(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
@@ -63,7 +25,7 @@ static bool interpret_nec(IrdaCommonDecoder* decoder) {
}
// Some NEC's extensions allow 16 bit address
static bool interpret_necext(IrdaCommonDecoder* decoder) {
bool irda_decoder_necext_interpret(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
@@ -81,24 +43,24 @@ static bool interpret_necext(IrdaCommonDecoder* decoder) {
}
// timings start from Space (delay between message and repeat)
static DecodeStatus decode_repeat_nec(IrdaCommonDecoder* decoder) {
IrdaStatus irda_decoder_nec_decode_repeat(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
DecodeStatus status = DecodeStatusError;
IrdaStatus status = IrdaStatusError;
if(decoder->timings_cnt < 4) return DecodeStatusOk;
if(decoder->timings_cnt < 4) return IrdaStatusOk;
if((decoder->timings[0] > IRDA_NEC_REPEAT_PAUSE_MIN) &&
(decoder->timings[0] < IRDA_NEC_REPEAT_PAUSE_MAX) &&
MATCH_PREAMBLE_TIMING(decoder->timings[1], IRDA_NEC_REPEAT_MARK, preamble_tolerance) &&
MATCH_PREAMBLE_TIMING(decoder->timings[2], IRDA_NEC_REPEAT_SPACE, preamble_tolerance) &&
MATCH_BIT_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)) {
status = DecodeStatusReady;
status = IrdaStatusReady;
decoder->timings_cnt = 0;
} else {
status = DecodeStatusError;
status = IrdaStatusError;
}
return status;

View File

@@ -1,62 +1,85 @@
#include "furi/check.h"
#include "irda_common_i.h"
#include <stdint.h>
#include "../irda_i.h"
#include "irda_protocol_defs_i.h"
#include <furi.h>
static const IrdaEncoderTimings encoder_timings = {
.bit1_mark = IRDA_NEC_BIT1_MARK,
.bit1_space = IRDA_NEC_BIT1_SPACE,
.bit0_mark =IRDA_NEC_BIT0_MARK,
.bit0_space = IRDA_NEC_BIT0_SPACE,
.duty_cycle = IRDA_NEC_DUTY_CYCLE,
.carrier_frequency = IRDA_NEC_CARRIER_FREQUENCY,
static const uint32_t repeat_timings[] = {
IRDA_NEC_REPEAT_PAUSE2,
IRDA_NEC_REPEAT_MARK,
IRDA_NEC_REPEAT_SPACE,
IRDA_NEC_BIT1_MARK,
};
void irda_encoder_nec_reset(void* encoder_ptr, const IrdaMessage* message) {
furi_assert(encoder_ptr);
static void irda_encode_nec_preamble(void) {
irda_encode_mark(&encoder_timings, IRDA_NEC_PREAMBULE_MARK);
irda_encode_space(&encoder_timings, IRDA_NEC_PREAMBULE_SPACE);
IrdaCommonEncoder* encoder = encoder_ptr;
irda_common_encoder_reset(encoder);
uint8_t address = message->address;
uint8_t address_inverse = ~address;
uint8_t command = message->command;
uint8_t command_inverse = ~command;
uint32_t* data = (void*) encoder->data;
*data |= address;
*data |= address_inverse << 8;
*data |= command << 16;
*data |= command_inverse << 24;
}
static void irda_encode_nec_repeat(void) {
irda_encode_space(&encoder_timings, IRDA_NEC_REPEAT_PAUSE);
irda_encode_mark(&encoder_timings, IRDA_NEC_REPEAT_MARK);
irda_encode_space(&encoder_timings, IRDA_NEC_REPEAT_SPACE);
irda_encode_bit(&encoder_timings, 1);
void irda_encoder_necext_reset(void* encoder_ptr, const IrdaMessage* message) {
furi_assert(encoder_ptr);
IrdaCommonEncoder* encoder = encoder_ptr;
irda_common_encoder_reset(encoder);
uint16_t address = message->address;
uint8_t command = message->command;
uint8_t command_inverse = ~command;
uint32_t* data = (void*) encoder->data;
*data |= address;
*data |= command << 16;
*data |= command_inverse << 24;
}
void irda_encoder_nec_encode(uint32_t addr, uint32_t cmd, bool repeat) {
uint8_t address = addr & 0xFF;
uint8_t command = cmd & 0xFF;
uint8_t address_inverse = (uint8_t) ~address;
uint8_t command_inverse = (uint8_t) ~command;
IrdaStatus irda_encoder_nec_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
furi_assert(encoder);
if (!repeat) {
irda_encode_nec_preamble();
irda_encode_byte(&encoder_timings, address);
irda_encode_byte(&encoder_timings, address_inverse);
irda_encode_byte(&encoder_timings, command);
irda_encode_byte(&encoder_timings, command_inverse);
irda_encode_bit(&encoder_timings, 1);
} else {
irda_encode_nec_repeat();
}
/* space + 2 timings preambule + payload + stop bit */
uint32_t timings_encoded_up_to_repeat = 1 + 2 + encoder->protocol->databit_len * 2 + 1;
uint32_t repeat_cnt = encoder->timings_encoded - timings_encoded_up_to_repeat;
furi_assert(encoder->timings_encoded >= timings_encoded_up_to_repeat);
if (repeat_cnt > 0)
*duration = repeat_timings[repeat_cnt % COUNT_OF(repeat_timings)];
else
*duration = IRDA_NEC_REPEAT_PAUSE1;
*level = repeat_cnt % 2;
++encoder->timings_encoded;
bool done = (!((repeat_cnt + 1) % COUNT_OF(repeat_timings)));
return done ? IrdaStatusDone : IrdaStatusOk;
}
// Some NEC's extensions allow 16 bit address
void irda_encoder_necext_encode(uint32_t addr, uint32_t cmd, bool repeat) {
uint16_t address = addr & 0xFFFF;
uint8_t command = cmd & 0xFF;
uint8_t command_inverse = (uint8_t) ~command;
if (!repeat) {
irda_encode_nec_preamble();
irda_encode_byte(&encoder_timings, (uint8_t) address);
irda_encode_byte(&encoder_timings, (uint8_t) (address >> 8));
irda_encode_byte(&encoder_timings, command);
irda_encode_byte(&encoder_timings, command_inverse);
irda_encode_bit(&encoder_timings, 1);
} else {
irda_encode_nec_repeat();
}
void* irda_encoder_necext_alloc(void) {
return irda_common_encoder_alloc(&protocol_necext);
}
void* irda_encoder_nec_alloc(void) {
return irda_common_encoder_alloc(&protocol_nec);
}
void irda_encoder_nec_free(void* encoder_ptr) {
irda_common_encoder_free(encoder_ptr);
}
IrdaStatus irda_encoder_nec_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
return irda_common_encode(encoder_ptr, duration, level);
}