[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
+103 -30
View File
@@ -1,12 +1,15 @@
#include "irda.h"
#include "furi/check.h"
#include "irda_common_i.h"
#include "irda_protocol_defs_i.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <furi.h>
#include "irda_i.h"
#include <api-hal-irda.h>
struct IrdaHandler {
struct IrdaDecoderHandler {
void** ctx;
};
@@ -18,7 +21,10 @@ typedef struct {
} IrdaDecoders;
typedef struct {
IrdaEncoderReset reset;
IrdaAlloc alloc;
IrdaEncode encode;
IrdaFree free;
} IrdaEncoders;
typedef struct {
@@ -30,23 +36,14 @@ typedef struct {
uint8_t command_length;
} IrdaProtocolImplementation;
struct IrdaEncoderHandler {
void* encoder;
IrdaProtocol protocol;
};
// TODO: replace with key-value, Now we refer by enum index, which is dangerous.
static const IrdaProtocolImplementation irda_protocols[] = {
// #0
{ .protocol = IrdaProtocolSamsung32,
.name ="Samsung32",
.decoder = {
.alloc = irda_decoder_samsung32_alloc,
.decode = irda_decoder_samsung32_decode,
.reset = irda_decoder_samsung32_reset,
.free = irda_decoder_samsung32_free},
.encoder = {
.encode = irda_encoder_samsung32_encode},
.address_length = 2,
.command_length = 2,
},
// #1
{ .protocol = IrdaProtocolNEC,
.name = "NEC",
.decoder = {
@@ -55,11 +52,14 @@ static const IrdaProtocolImplementation irda_protocols[] = {
.reset = irda_decoder_nec_reset,
.free = irda_decoder_nec_free},
.encoder = {
.encode = irda_encoder_nec_encode},
.alloc = irda_encoder_nec_alloc,
.encode = irda_encoder_nec_encode,
.reset = irda_encoder_nec_reset,
.free = irda_encoder_nec_free},
.address_length = 2,
.command_length = 2,
},
// #2 - have to be after NEC
// #1 - have to be after NEC
{ .protocol = IrdaProtocolNECext,
.name = "NECext",
.decoder = {
@@ -68,14 +68,48 @@ static const IrdaProtocolImplementation irda_protocols[] = {
.reset = irda_decoder_nec_reset,
.free = irda_decoder_nec_free},
.encoder = {
.encode = irda_encoder_necext_encode},
.alloc = irda_encoder_necext_alloc,
.encode = irda_encoder_nec_encode,
.reset = irda_encoder_necext_reset,
.free = irda_encoder_nec_free},
.address_length = 4,
.command_length = 2,
},
// #2
{ .protocol = IrdaProtocolSamsung32,
.name ="Samsung32",
.decoder = {
.alloc = irda_decoder_samsung32_alloc,
.decode = irda_decoder_samsung32_decode,
.reset = irda_decoder_samsung32_reset,
.free = irda_decoder_samsung32_free},
.encoder = {
.alloc = irda_encoder_samsung32_alloc,
.encode = irda_encoder_samsung32_encode,
.reset = irda_encoder_samsung32_reset,
.free = irda_encoder_samsung32_free},
.address_length = 2,
.command_length = 2,
},
// #3
{ .protocol = IrdaProtocolRC6,
.name = "RC6",
.decoder = {
.alloc = irda_decoder_rc6_alloc,
.decode = irda_decoder_rc6_decode,
.reset = irda_decoder_rc6_reset,
.free = irda_decoder_rc6_free},
.encoder = {
.alloc = irda_encoder_rc6_alloc,
.encode = irda_encoder_rc6_encode,
.reset = irda_encoder_rc6_reset,
.free = irda_encoder_rc6_free},
.address_length = 2,
.command_length = 2,
},
};
const IrdaMessage* irda_decode(IrdaHandler* handler, bool level, uint32_t duration) {
const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration) {
furi_assert(handler);
IrdaMessage* message = NULL;
@@ -94,8 +128,8 @@ const IrdaMessage* irda_decode(IrdaHandler* handler, bool level, uint32_t durati
return result;
}
IrdaHandler* irda_alloc_decoder(void) {
IrdaHandler* handler = furi_alloc(sizeof(IrdaHandler));
IrdaDecoderHandler* irda_alloc_decoder(void) {
IrdaDecoderHandler* handler = furi_alloc(sizeof(IrdaDecoderHandler));
handler->ctx = furi_alloc(sizeof(void*) * COUNT_OF(irda_protocols));
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
@@ -107,7 +141,7 @@ IrdaHandler* irda_alloc_decoder(void) {
return handler;
}
void irda_free_decoder(IrdaHandler* handler) {
void irda_free_decoder(IrdaDecoderHandler* handler) {
furi_assert(handler);
furi_assert(handler->ctx);
@@ -120,26 +154,65 @@ void irda_free_decoder(IrdaHandler* handler) {
free(handler);
}
void irda_reset_decoder(IrdaHandler* handler) {
void irda_reset_decoder(IrdaDecoderHandler* handler) {
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
if (irda_protocols[i].decoder.reset)
irda_protocols[i].decoder.reset(handler->ctx[i]);
}
}
void irda_send(const IrdaMessage* message, int times) {
IrdaEncoderHandler* irda_alloc_encoder(void) {
IrdaEncoderHandler* handler = furi_alloc(sizeof(IrdaEncoderHandler));
handler->encoder = NULL;
handler->protocol = IrdaProtocolUnknown;
return handler;
}
void irda_free_encoder(IrdaEncoderHandler* handler) {
furi_assert(handler);
if (handler->encoder) {
furi_assert(irda_is_protocol_valid(handler->protocol));
furi_assert(irda_protocols[handler->protocol].encoder.free);
irda_protocols[handler->protocol].encoder.free(handler->encoder);
}
free(handler);
}
void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message) {
furi_assert(handler);
furi_assert(message);
furi_assert(irda_is_protocol_valid(message->protocol));
furi_assert(irda_protocols[message->protocol].encoder.reset);
furi_assert(irda_protocols[message->protocol].encoder.alloc);
for (int i = 0; i < times; ++i) {
if(irda_protocols[message->protocol].encoder.encode) {
__disable_irq();
irda_protocols[message->protocol].encoder.encode(message->address, message->command, !!i);
__enable_irq();
/* Realloc encoder if different protocol set */
if (message->protocol != handler->protocol) {
if (handler->encoder != NULL) {
furi_assert(handler->protocol != IrdaProtocolUnknown);
irda_protocols[handler->protocol].encoder.free(handler->encoder);
}
handler->encoder = irda_protocols[message->protocol].encoder.alloc();
handler->protocol = message->protocol;
}
irda_protocols[handler->protocol].encoder.reset(handler->encoder, message);
}
IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level) {
furi_assert(handler);
furi_assert(irda_is_protocol_valid(handler->protocol));
furi_assert(irda_protocols[handler->protocol].encoder.encode);
IrdaStatus status = irda_protocols[handler->protocol].encoder.encode(handler->encoder, duration, level);
furi_assert(status != IrdaStatusError);
return status;
}
bool irda_is_protocol_valid(IrdaProtocol protocol) {
return (protocol >= 0) && (protocol < COUNT_OF(irda_protocols));
}