[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,79 @@
#include "irda.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <furi.h>
#include "../irda_i.h"
#include "../irda_protocol_defs_i.h"
typedef struct {
IrdaCommonDecoder* common_decoder;
bool toggle;
} IrdaRc5Decoder;
bool irda_decoder_rc5_interpret(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
uint32_t* data = (void*) &decoder->data[0];
/* Manchester (inverse):
* 0->1 : 1
* 1->0 : 0
*/
decoder->data[0] = ~decoder->data[0];
decoder->data[1] = ~decoder->data[1];
// MSB first
uint8_t address = reverse((uint8_t) decoder->data[0]) & 0x1F;
uint8_t command = (reverse((uint8_t) decoder->data[1]) >> 2) & 0x3F;
bool start_bit1 = *data & 0x01;
bool start_bit2 = *data & 0x02;
bool toggle = !!(*data & 0x04);
if (start_bit1 == 1) {
IrdaProtocol protocol = start_bit2 ? IrdaProtocolRC5 : IrdaProtocolRC5X;
IrdaMessage* message = &decoder->message;
IrdaRc5Decoder *rc5_decoder = decoder->context;
bool *prev_toggle = &rc5_decoder->toggle;
if ((message->address == address)
&& (message->command == command)
&& (message->protocol == protocol)) {
message->repeat = (toggle == *prev_toggle);
} else {
message->repeat = false;
}
*prev_toggle = toggle;
message->command = command;
message->address = address;
message->protocol = protocol;
result = true;
}
return result;
}
void* irda_decoder_rc5_alloc(void) {
IrdaRc5Decoder* decoder = furi_alloc(sizeof(IrdaRc5Decoder));
decoder->toggle = false;
decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc5);
irda_common_decoder_set_context(decoder->common_decoder, decoder);
return decoder;
}
IrdaMessage* irda_decoder_rc5_decode(void* decoder, bool level, uint32_t duration) {
IrdaRc5Decoder* decoder_rc5 = decoder;
return irda_common_decode(decoder_rc5->common_decoder, level, duration);
}
void irda_decoder_rc5_free(void* decoder) {
IrdaRc5Decoder* decoder_rc5 = decoder;
irda_common_decoder_free(decoder_rc5->common_decoder);
free(decoder_rc5);
}
void irda_decoder_rc5_reset(void* decoder) {
IrdaRc5Decoder* decoder_rc5 = decoder;
irda_common_decoder_reset(decoder_rc5->common_decoder);
}

View File

@@ -0,0 +1,55 @@
#include "furi/memmgr.h"
#include "irda.h"
#include "common/irda_common_i.h"
#include "irda_protocol_defs_i.h"
#include <stdint.h>
#include "../irda_i.h"
typedef struct IrdaEncoderRC5 {
IrdaCommonEncoder* common_encoder;
bool toggle_bit;
} IrdaEncoderRC5;
void irda_encoder_rc5_reset(void* encoder_ptr, const IrdaMessage* message) {
furi_assert(encoder_ptr);
IrdaEncoderRC5* encoder = encoder_ptr;
IrdaCommonEncoder* common_encoder = encoder->common_encoder;
irda_common_encoder_reset(common_encoder);
uint32_t* data = (void*) common_encoder->data;
/* RC5 */
*data |= 0x01; // start bit
if (message->protocol == IrdaProtocolRC5) {
*data |= 0x02; // start bit
}
*data |= encoder->toggle_bit ? 0x04 : 0;
*data |= (reverse(message->address) >> 3) << 3; /* address 5 bit */
*data |= (reverse(message->command) >> 2) << 8; /* command 6 bit */
common_encoder->data[0] = ~common_encoder->data[0];
common_encoder->data[1] = ~common_encoder->data[1];
encoder->toggle_bit ^= 1;
}
IrdaStatus irda_encoder_rc5_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
IrdaEncoderRC5* encoder = encoder_ptr;
return irda_common_encode(encoder->common_encoder, duration, level);
}
void* irda_encoder_rc5_alloc(void) {
IrdaEncoderRC5* encoder = furi_alloc(sizeof(IrdaEncoderRC5));
encoder->common_encoder = irda_common_encoder_alloc(&protocol_rc5);
encoder->toggle_bit = false;
return encoder;
}
void irda_encoder_rc5_free(void* encoder_ptr) {
furi_assert(encoder_ptr);
IrdaEncoderRC5* encoder = encoder_ptr;
free(encoder->common_encoder);
free(encoder);
}

View File

@@ -0,0 +1,28 @@
#include "../irda_i.h"
#include "irda_protocol_defs_i.h"
static const IrdaProtocolSpecification irda_rc5_protocol_specification = {
.name = "RC5",
.address_length = 2,
.command_length = 2,
.frequency = IRDA_RC5_CARRIER_FREQUENCY,
.duty_cycle = IRDA_RC5_DUTY_CYCLE,
};
static const IrdaProtocolSpecification irda_rc5x_protocol_specification = {
.name = "RC5X",
.address_length = 2,
.command_length = 2,
.frequency = IRDA_RC5_CARRIER_FREQUENCY,
.duty_cycle = IRDA_RC5_DUTY_CYCLE,
};
const IrdaProtocolSpecification* irda_rc5_get_spec(IrdaProtocol protocol) {
if (protocol == IrdaProtocolRC5)
return &irda_rc5_protocol_specification;
else if (protocol == IrdaProtocolRC5X)
return &irda_rc5x_protocol_specification;
else
return NULL;
}