[FL-1684] IRDA Add SIRC protocol (#693)
* IRDA HAL: Fill buffer refactoring * IRDA: Add SIRC protocol * IRDA: correct adr/cmd bit length * Disable Unit tests Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
92
lib/irda/encoder_decoder/sirc/irda_decoder_sirc.c
Normal file
92
lib/irda/encoder_decoder/sirc/irda_decoder_sirc.c
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "common/irda_common_i.h"
|
||||
#include "irda.h"
|
||||
#include "irda_protocol_defs_i.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <furi.h>
|
||||
#include "../irda_i.h"
|
||||
|
||||
|
||||
IrdaMessage* irda_decoder_sirc_check_ready(void* ctx) {
|
||||
IrdaMessage* message = NULL;
|
||||
IrdaCommonDecoder* decoder = ctx;
|
||||
|
||||
if (irda_decoder_sirc_interpret(decoder)) {
|
||||
message = &decoder->message;
|
||||
decoder->timings_cnt = 0;
|
||||
decoder->databit_cnt = 0;
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
bool irda_decoder_sirc_interpret(IrdaCommonDecoder* decoder) {
|
||||
furi_assert(decoder);
|
||||
|
||||
uint32_t* data = (void*) &decoder->data[0];
|
||||
uint16_t address = 0;
|
||||
uint8_t command = 0;
|
||||
IrdaProtocol protocol = IrdaProtocolUnknown;
|
||||
|
||||
if (decoder->databit_cnt == 12) {
|
||||
address = (*data >> 7) & 0x1F;
|
||||
command = *data & 0x7F;
|
||||
protocol = IrdaProtocolSIRC;
|
||||
} else if (decoder->databit_cnt == 15) {
|
||||
address = (*data >> 7) & 0xFF;
|
||||
command = *data & 0x7F;
|
||||
protocol = IrdaProtocolSIRC15;
|
||||
} else if (decoder->databit_cnt == 20) {
|
||||
address = (*data >> 7) & 0x1FFF;
|
||||
command = *data & 0x7F;
|
||||
protocol = IrdaProtocolSIRC20;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
decoder->message.protocol = protocol;
|
||||
decoder->message.address = address;
|
||||
decoder->message.command = command;
|
||||
/* SIRC doesn't specify repeat detection */
|
||||
decoder->message.repeat = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void* irda_decoder_sirc_alloc(void) {
|
||||
return irda_common_decoder_alloc(&protocol_sirc);
|
||||
}
|
||||
|
||||
IrdaMessage* irda_decoder_sirc_decode(void* context, bool level, uint32_t duration) {
|
||||
IrdaCommonDecoder* decoder = context;
|
||||
IrdaMessage* message = NULL;
|
||||
|
||||
if ((decoder->databit_cnt == 12) || (decoder->databit_cnt == 15)) {
|
||||
if (!level && (duration >= IRDA_SIRC_MIN_SILENCE)) {
|
||||
if (irda_decoder_sirc_interpret(decoder)) {
|
||||
message = &decoder->message;
|
||||
decoder->timings_cnt = 0;
|
||||
decoder->databit_cnt = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!message) {
|
||||
message = irda_common_decode(decoder, level, duration);
|
||||
if (message) { /* 20 bit */
|
||||
decoder->timings_cnt = 0;
|
||||
decoder->databit_cnt = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
void irda_decoder_sirc_free(void* decoder) {
|
||||
irda_common_decoder_free(decoder);
|
||||
}
|
||||
|
||||
void irda_decoder_sirc_reset(void* decoder) {
|
||||
irda_common_decoder_reset(decoder);
|
||||
}
|
||||
|
89
lib/irda/encoder_decoder/sirc/irda_encoder_sirc.c
Normal file
89
lib/irda/encoder_decoder/sirc/irda_encoder_sirc.c
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "furi/check.h"
|
||||
#include "irda.h"
|
||||
#include "common/irda_common_i.h"
|
||||
#include <stdint.h>
|
||||
#include "../irda_i.h"
|
||||
#include "irda_protocol_defs_i.h"
|
||||
#include <furi.h>
|
||||
|
||||
|
||||
typedef struct {
|
||||
IrdaCommonEncoder* common_encoder;
|
||||
uint8_t databits;
|
||||
} IrdaSircEncoder;
|
||||
|
||||
|
||||
void irda_encoder_sirc_reset(void* encoder_ptr, const IrdaMessage* message) {
|
||||
furi_assert(encoder_ptr);
|
||||
furi_assert(message);
|
||||
|
||||
IrdaCommonEncoder* encoder = encoder_ptr;
|
||||
IrdaSircEncoder* encoder_sirc = encoder->context;
|
||||
irda_common_encoder_reset(encoder);
|
||||
|
||||
uint32_t* data = (void*) encoder->data;
|
||||
|
||||
if (message->protocol == IrdaProtocolSIRC) {
|
||||
encoder_sirc->databits = 12;
|
||||
*data = (message->command & 0x7F);
|
||||
*data |= (message->address & 0x1F) << 7;
|
||||
} else if (message->protocol == IrdaProtocolSIRC15) {
|
||||
encoder_sirc->databits = 15;
|
||||
*data = (message->command & 0x7F);
|
||||
*data |= (message->address & 0xFF) << 7;
|
||||
} else if (message->protocol == IrdaProtocolSIRC20) {
|
||||
encoder_sirc->databits = 20;
|
||||
*data = (message->command & 0x7F);
|
||||
*data |= (message->address & 0x1FFF) << 7;
|
||||
} else {
|
||||
furi_assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
IrdaStatus irda_encoder_sirc_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
|
||||
furi_assert(encoder);
|
||||
|
||||
IrdaSircEncoder* encoder_sirc = encoder->context;
|
||||
|
||||
uint32_t timings_in_message = 1 + 2 + encoder_sirc->databits * 2;
|
||||
furi_assert(encoder->timings_encoded == timings_in_message);
|
||||
|
||||
furi_assert(encoder->timings_sum < IRDA_SIRC_REPEAT_PERIOD);
|
||||
*duration = IRDA_SIRC_REPEAT_PERIOD - encoder->timings_sum;
|
||||
*level = false;
|
||||
|
||||
encoder->timings_sum = 0;
|
||||
encoder->timings_encoded = 1;
|
||||
encoder->bits_encoded = 0;
|
||||
encoder->state = IrdaCommonEncoderStatePreamble;
|
||||
|
||||
return IrdaStatusOk;
|
||||
}
|
||||
|
||||
void* irda_encoder_sirc_alloc(void) {
|
||||
IrdaCommonEncoder* encoder_common = irda_common_encoder_alloc(&protocol_sirc);
|
||||
IrdaSircEncoder* encoder_sirc = furi_alloc(sizeof(IrdaSircEncoder));
|
||||
encoder_sirc->common_encoder = encoder_common;
|
||||
encoder_common->context = encoder_sirc;
|
||||
return encoder_common;
|
||||
}
|
||||
|
||||
void irda_encoder_sirc_free(void* encoder_ptr) {
|
||||
IrdaCommonEncoder* encoder = encoder_ptr;
|
||||
free(encoder->context);
|
||||
irda_common_encoder_free(encoder);
|
||||
}
|
||||
|
||||
IrdaStatus irda_encoder_sirc_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
|
||||
IrdaCommonEncoder* encoder_common = encoder_ptr;
|
||||
IrdaSircEncoder* encoder_sirc = encoder_common->context;
|
||||
|
||||
IrdaStatus status = irda_common_encode(encoder_ptr, duration, level);
|
||||
if ((status == IrdaStatusOk) && (encoder_common->bits_encoded == encoder_sirc->databits)) {
|
||||
furi_assert(!*level);
|
||||
status = IrdaStatusDone;
|
||||
encoder_common->state = IrdaCommonEncoderStateEncodeRepeat;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
38
lib/irda/encoder_decoder/sirc/irda_sirc_spec.c
Normal file
38
lib/irda/encoder_decoder/sirc/irda_sirc_spec.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "../irda_i.h"
|
||||
#include "irda_protocol_defs_i.h"
|
||||
|
||||
static const IrdaProtocolSpecification irda_sirc_protocol_specification = {
|
||||
.name = "SIRC",
|
||||
.address_length = 5,
|
||||
.command_length = 7,
|
||||
.frequency = IRDA_SIRC_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_SIRC_DUTY_CYCLE,
|
||||
};
|
||||
|
||||
static const IrdaProtocolSpecification irda_sirc15_protocol_specification = {
|
||||
.name = "SIRC15",
|
||||
.address_length = 8,
|
||||
.command_length = 7,
|
||||
.frequency = IRDA_SIRC_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_SIRC_DUTY_CYCLE,
|
||||
};
|
||||
|
||||
static const IrdaProtocolSpecification irda_sirc20_protocol_specification = {
|
||||
.name = "SIRC20",
|
||||
.address_length = 13,
|
||||
.command_length = 7,
|
||||
.frequency = IRDA_SIRC_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_SIRC_DUTY_CYCLE,
|
||||
};
|
||||
|
||||
const IrdaProtocolSpecification* irda_sirc_get_spec(IrdaProtocol protocol) {
|
||||
if (protocol == IrdaProtocolSIRC)
|
||||
return &irda_sirc_protocol_specification;
|
||||
else if (protocol == IrdaProtocolSIRC15)
|
||||
return &irda_sirc15_protocol_specification;
|
||||
else if (protocol == IrdaProtocolSIRC20)
|
||||
return &irda_sirc20_protocol_specification;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
Reference in New Issue
Block a user