[FL-1505] Add RAW format (#576)
* Add RAW format * F5 stubs for build to pass * Fix saving decoded signal error * Irda: set ISR before starting timer, remove explicit NVIC configuration Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
248
lib/irda/encoder_decoder/irda.c
Normal file
248
lib/irda/encoder_decoder/irda.c
Normal file
@@ -0,0 +1,248 @@
|
||||
#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 IrdaDecoderHandler {
|
||||
void** ctx;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
IrdaAlloc alloc;
|
||||
IrdaDecode decode;
|
||||
IrdaReset reset;
|
||||
IrdaFree free;
|
||||
} IrdaDecoders;
|
||||
|
||||
typedef struct {
|
||||
IrdaEncoderReset reset;
|
||||
IrdaAlloc alloc;
|
||||
IrdaEncode encode;
|
||||
IrdaFree free;
|
||||
} IrdaEncoders;
|
||||
|
||||
typedef struct {
|
||||
IrdaProtocol protocol;
|
||||
const char* name;
|
||||
IrdaDecoders decoder;
|
||||
IrdaEncoders encoder;
|
||||
uint8_t address_length;
|
||||
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 = IrdaProtocolNEC,
|
||||
.name = "NEC",
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_nec_alloc,
|
||||
.decode = irda_decoder_nec_decode,
|
||||
.reset = irda_decoder_nec_reset,
|
||||
.free = irda_decoder_nec_free},
|
||||
.encoder = {
|
||||
.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,
|
||||
},
|
||||
// #1 - have to be after NEC
|
||||
{ .protocol = IrdaProtocolNECext,
|
||||
.name = "NECext",
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_necext_alloc,
|
||||
.decode = irda_decoder_nec_decode,
|
||||
.reset = irda_decoder_nec_reset,
|
||||
.free = irda_decoder_nec_free},
|
||||
.encoder = {
|
||||
.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(IrdaDecoderHandler* handler, bool level, uint32_t duration) {
|
||||
furi_assert(handler);
|
||||
|
||||
IrdaMessage* message = NULL;
|
||||
IrdaMessage* result = NULL;
|
||||
|
||||
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
||||
if (irda_protocols[i].decoder.decode) {
|
||||
message = irda_protocols[i].decoder.decode(handler->ctx[i], level, duration);
|
||||
if (!result && message) {
|
||||
message->protocol = irda_protocols[i].protocol;
|
||||
result = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
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) {
|
||||
handler->ctx[i] = 0;
|
||||
if (irda_protocols[i].decoder.alloc)
|
||||
handler->ctx[i] = irda_protocols[i].decoder.alloc();
|
||||
}
|
||||
|
||||
return handler;
|
||||
}
|
||||
|
||||
void irda_free_decoder(IrdaDecoderHandler* handler) {
|
||||
furi_assert(handler);
|
||||
furi_assert(handler->ctx);
|
||||
|
||||
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
||||
if (irda_protocols[i].decoder.free)
|
||||
irda_protocols[i].decoder.free(handler->ctx[i]);
|
||||
}
|
||||
|
||||
free(handler->ctx);
|
||||
free(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]);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
/* 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));
|
||||
}
|
||||
|
||||
IrdaProtocol irda_get_protocol_by_name(const char* protocol_name) {
|
||||
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
||||
if (!strcmp(irda_protocols[i].name, protocol_name))
|
||||
return i;
|
||||
}
|
||||
return IrdaProtocolUnknown;
|
||||
}
|
||||
|
||||
const char* irda_get_protocol_name(IrdaProtocol protocol) {
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].name;
|
||||
else
|
||||
return "Invalid";
|
||||
}
|
||||
|
||||
uint8_t irda_get_protocol_address_length(IrdaProtocol protocol) {
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].address_length;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t irda_get_protocol_command_length(IrdaProtocol protocol) {
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].command_length;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
156
lib/irda/encoder_decoder/irda.h
Normal file
156
lib/irda/encoder_decoder/irda.h
Normal file
@@ -0,0 +1,156 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define IRDA_COMMON_CARRIER_FREQUENCY 38000
|
||||
#define IRDA_COMMON_DUTY_CYCLE 0.33
|
||||
|
||||
typedef struct IrdaDecoderHandler IrdaDecoderHandler;
|
||||
typedef struct IrdaEncoderHandler IrdaEncoderHandler;
|
||||
|
||||
// Do not change protocol order, as it can be saved into memory and fw update can be performed!
|
||||
typedef enum {
|
||||
IrdaProtocolUnknown = -1,
|
||||
IrdaProtocolNEC = 0,
|
||||
IrdaProtocolNECext = 1,
|
||||
IrdaProtocolSamsung32 = 2,
|
||||
IrdaProtocolRC6 = 3,
|
||||
} IrdaProtocol;
|
||||
|
||||
typedef struct {
|
||||
IrdaProtocol protocol;
|
||||
uint32_t address;
|
||||
uint32_t command;
|
||||
bool repeat;
|
||||
} IrdaMessage;
|
||||
|
||||
typedef enum {
|
||||
IrdaStatusError,
|
||||
IrdaStatusOk,
|
||||
IrdaStatusDone,
|
||||
IrdaStatusReady,
|
||||
} IrdaStatus;
|
||||
|
||||
/**
|
||||
* Initialize decoder.
|
||||
*
|
||||
* \return returns pointer to IRDA decoder handler if success, otherwise - error.
|
||||
*/
|
||||
IrdaDecoderHandler* irda_alloc_decoder(void);
|
||||
|
||||
/**
|
||||
* Provide to decoder next timing.
|
||||
*
|
||||
* \param[in] handler - handler to IRDA decoders. Should be aquired with \c irda_alloc_decoder().
|
||||
* \param[in] level - high(true) or low(false) level of input signal to analyze.
|
||||
* it should alternate every call, otherwise it is an error case,
|
||||
* and decoder resets its state and start decoding from the start.
|
||||
* \param[in] duration - duration of steady high/low input signal.
|
||||
* \return if message is ready, returns pointer to decoded message, returns NULL.
|
||||
*/
|
||||
const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration);
|
||||
|
||||
/**
|
||||
* Deinitialize decoder and free allocated memory.
|
||||
*
|
||||
* \param[in] handler - handler to IRDA decoders. Should be aquired with \c irda_alloc_decoder().
|
||||
*/
|
||||
void irda_free_decoder(IrdaDecoderHandler* handler);
|
||||
|
||||
/**
|
||||
* Reset IRDA decoder.
|
||||
*
|
||||
* \param[in] handler - handler to IRDA decoders. Should be aquired with \c irda_alloc_decoder().
|
||||
*/
|
||||
void irda_reset_decoder(IrdaDecoderHandler* handler);
|
||||
|
||||
/**
|
||||
* Get protocol name by protocol enum.
|
||||
*
|
||||
* \param[in] protocol - protocol identifier.
|
||||
* \return string to protocol name.
|
||||
*/
|
||||
const char* irda_get_protocol_name(IrdaProtocol protocol);
|
||||
|
||||
/**
|
||||
* Get protocol enum by protocol name.
|
||||
*
|
||||
* \param[in] protocol_name - string to protocol name.
|
||||
* \return protocol identifier.
|
||||
*/
|
||||
IrdaProtocol irda_get_protocol_by_name(const char* protocol_name);
|
||||
|
||||
/**
|
||||
* Get address length by protocol enum.
|
||||
*
|
||||
* \param[in] protocol - protocol identifier.
|
||||
* \return length of address in nibbles.
|
||||
*/
|
||||
uint8_t irda_get_protocol_address_length(IrdaProtocol protocol);
|
||||
|
||||
/**
|
||||
* Get command length by protocol enum.
|
||||
*
|
||||
* \param[in] protocol - protocol identifier.
|
||||
* \return length of command in nibbles.
|
||||
*/
|
||||
uint8_t irda_get_protocol_command_length(IrdaProtocol protocol);
|
||||
|
||||
/**
|
||||
* Checks whether protocol valid.
|
||||
*
|
||||
* \param[in] protocol - protocol identifier.
|
||||
* \return true if protocol is valid, false otherwise.
|
||||
*/
|
||||
bool irda_is_protocol_valid(IrdaProtocol protocol);
|
||||
|
||||
/**
|
||||
* Allocate IRDA encoder.
|
||||
*
|
||||
* \return encoder handler.
|
||||
*/
|
||||
IrdaEncoderHandler* irda_alloc_encoder(void);
|
||||
|
||||
/**
|
||||
* Free encoder handler previously allocated with \c irda_alloc_encoder().
|
||||
*
|
||||
* \param[in] handler - handler to IRDA encoder. Should be aquired with \c irda_alloc_encoder().
|
||||
*/
|
||||
void irda_free_encoder(IrdaEncoderHandler* handler);
|
||||
|
||||
/**
|
||||
* Encode previously set IRDA message.
|
||||
* Usage:
|
||||
* 1) alloc with \c irda_alloc_encoder()
|
||||
* 2) set message to encode with \c irda_reset_encoder()
|
||||
* 3) call for \c irda_encode() to continuously get one at a time timings.
|
||||
* 4) when \c irda_encode() returns IrdaStatusDone, it means new message is fully encoded.
|
||||
* 5) to encode additional timings, just continue calling \c irda_encode().
|
||||
*
|
||||
* \param[in] handler - handler to IRDA encoder. Should be aquired with \c irda_alloc_encoder().
|
||||
* \param[out] duration - encoded timing.
|
||||
* \param[out] level - encoded level.
|
||||
*
|
||||
* \return status of encode operation.
|
||||
*/
|
||||
IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level);
|
||||
|
||||
/**
|
||||
* Reset IRDA encoder and set new message to encode. If it's not called after receiveing
|
||||
* IrdaStatusDone in \c irda_encode(), encoder will encode repeat messages
|
||||
* till the end of time.
|
||||
*
|
||||
* \param[in] handler - handler to IRDA encoder. Should be aquired with \c irda_alloc_encoder().
|
||||
* \param[in] message - message to encode.
|
||||
*/
|
||||
void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
255
lib/irda/encoder_decoder/irda_common_decoder.c
Normal file
255
lib/irda/encoder_decoder/irda_common_decoder.c
Normal file
@@ -0,0 +1,255 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
||||
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 = IrdaStatusError;
|
||||
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) || (double_timing && !*switch_detect)) {
|
||||
status = IrdaStatusError;
|
||||
break;
|
||||
}
|
||||
|
||||
if (*switch_detect == 0) {
|
||||
/* 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;
|
||||
|
||||
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;
|
||||
bool inverse_level = decoder->protocol->manchester_inverse_level;
|
||||
uint8_t logic_value = inverse_level ? !decoder->level : decoder->level;
|
||||
decoder->data[index] |= (logic_value << shift);
|
||||
++decoder->databit_cnt;
|
||||
}
|
||||
if (decoder->databit_cnt == decoder->protocol->databit_len) {
|
||||
if (decoder->level) {
|
||||
status = IrdaStatusReady;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
furi_assert(decoder->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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
161
lib/irda/encoder_decoder/irda_common_encoder.c
Normal file
161
lib/irda/encoder_decoder/irda_common_encoder.c
Normal file
@@ -0,0 +1,161 @@
|
||||
#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 inverse = encoder->protocol->manchester_inverse_level;
|
||||
bool even_timing = !(encoder->timings_encoded % 2);
|
||||
|
||||
*level = even_timing ^ logic_value ^ inverse;
|
||||
*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;
|
||||
}
|
||||
|
90
lib/irda/encoder_decoder/irda_common_i.h
Normal file
90
lib/irda/encoder_decoder/irda_common_i.h
Normal 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_inverse_level;
|
||||
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);
|
||||
|
81
lib/irda/encoder_decoder/irda_common_protocol_defs.c
Normal file
81
lib/irda/encoder_decoder/irda_common_protocol_defs.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#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_necext = {
|
||||
.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_necext_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_inverse_level = false,
|
||||
.decode = irda_decoder_rc6_decode_manchester,
|
||||
.encode = irda_encoder_rc6_encode_manchester,
|
||||
.interpret = irda_decoder_rc6_interpret,
|
||||
.decode_repeat = NULL,
|
||||
.encode_repeat = NULL,
|
||||
};
|
||||
|
34
lib/irda/encoder_decoder/irda_i.h
Normal file
34
lib/irda/encoder_decoder/irda_i.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include "irda.h"
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t silence_time;
|
||||
uint16_t preamble_mark;
|
||||
uint16_t preamble_space;
|
||||
uint16_t bit1_mark;
|
||||
uint16_t bit1_space;
|
||||
uint16_t bit0_mark;
|
||||
uint16_t bit0_space;
|
||||
float preamble_tolerance;
|
||||
uint32_t bit_tolerance;
|
||||
} IrdaTimings;
|
||||
|
||||
typedef void* (*IrdaAlloc) (void);
|
||||
typedef IrdaMessage* (*IrdaDecode) (void* ctx, bool level, uint32_t duration);
|
||||
typedef void (*IrdaReset) (void*);
|
||||
typedef void (*IrdaFree) (void*);
|
||||
|
||||
typedef void (*IrdaEncoderReset)(void* encoder, const IrdaMessage* message);
|
||||
typedef IrdaStatus (*IrdaEncode)(void* encoder, uint32_t* out, bool* polarity);
|
||||
typedef IrdaTimings (*IrdaTimingsGet)(void);
|
||||
|
||||
static inline uint8_t reverse(uint8_t value) {
|
||||
uint8_t reverse_value = 0;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
reverse_value |= (value & (0x01 << i)) ? 1 << (7 - i) : 0;
|
||||
}
|
||||
|
||||
return reverse_value;
|
||||
}
|
||||
|
156
lib/irda/encoder_decoder/irda_protocol_defs_i.h
Normal file
156
lib/irda/encoder_decoder/irda_protocol_defs_i.h
Normal file
@@ -0,0 +1,156 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "irda.h"
|
||||
#include "irda_common_i.h"
|
||||
|
||||
/***************************************************************************************************
|
||||
* NEC protocol description
|
||||
* https://radioparty.ru/manuals/encyclopedia/213-ircontrol?start=1
|
||||
****************************************************************************************************
|
||||
* Preamble Preamble Pulse Distance/Width Pause Preamble Preamble Stop
|
||||
* mark space Modulation repeat repeat bit
|
||||
* mark space
|
||||
*
|
||||
* 9000 4500 32 bit + stop bit 40000/100000 9000 2250
|
||||
* __________ _ _ _ _ _ _ _ _ _ _ _ _ _ ___________ _
|
||||
* ____ __________ _ _ _ __ __ __ _ _ __ __ _ _ ________________ ____________ ___
|
||||
*
|
||||
***************************************************************************************************/
|
||||
|
||||
#define IRDA_NEC_PREAMBULE_MARK 9000
|
||||
#define IRDA_NEC_PREAMBULE_SPACE 4500
|
||||
#define IRDA_NEC_BIT1_MARK 560
|
||||
#define IRDA_NEC_BIT1_SPACE 1600
|
||||
#define IRDA_NEC_BIT0_MARK 560
|
||||
#define IRDA_NEC_BIT0_SPACE 560
|
||||
#define IRDA_NEC_REPEAT_PAUSE_MIN 30000
|
||||
#define IRDA_NEC_REPEAT_PAUSE1 46000
|
||||
#define IRDA_NEC_REPEAT_PAUSE2 97000
|
||||
#define IRDA_NEC_SILENCE IRDA_NEC_REPEAT_PAUSE2
|
||||
#define IRDA_NEC_REPEAT_PAUSE_MAX 150000
|
||||
#define IRDA_NEC_REPEAT_MARK 9000
|
||||
#define IRDA_NEC_REPEAT_SPACE 2250
|
||||
#define IRDA_NEC_PREAMBLE_TOLERANCE 0.07 // percents
|
||||
#define IRDA_NEC_BIT_TOLERANCE 120 // us
|
||||
|
||||
void* irda_decoder_nec_alloc(void);
|
||||
void irda_decoder_nec_reset(void* decoder);
|
||||
void irda_decoder_nec_free(void* decoder);
|
||||
IrdaMessage* irda_decoder_nec_decode(void* decoder, bool level, uint32_t duration);
|
||||
|
||||
void* irda_encoder_nec_alloc(void);
|
||||
IrdaStatus irda_encoder_nec_encode(void* encoder_ptr, uint32_t* duration, bool* level);
|
||||
void irda_encoder_nec_reset(void* encoder_ptr, const IrdaMessage* message);
|
||||
void irda_encoder_nec_free(void* encoder_ptr);
|
||||
|
||||
void* irda_decoder_necext_alloc(void);
|
||||
void* irda_encoder_necext_alloc(void);
|
||||
void irda_encoder_necext_reset(void* encoder_ptr, const IrdaMessage* message);
|
||||
|
||||
bool irda_decoder_nec_interpret(IrdaCommonDecoder* decoder);
|
||||
bool irda_decoder_necext_interpret(IrdaCommonDecoder* decoder);
|
||||
IrdaStatus irda_decoder_nec_decode_repeat(IrdaCommonDecoder* decoder);
|
||||
IrdaStatus irda_encoder_nec_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level);
|
||||
|
||||
extern const IrdaCommonProtocolSpec protocol_necext;
|
||||
extern const IrdaCommonProtocolSpec protocol_nec;
|
||||
|
||||
|
||||
/***************************************************************************************************
|
||||
* SAMSUNG32 protocol description
|
||||
* https://www.mikrocontroller.net/articles/IRMP_-_english#SAMSUNG
|
||||
****************************************************************************************************
|
||||
* Preamble Preamble Pulse Distance/Width Pause Preamble Preamble Bit1 Stop
|
||||
* mark space Modulation repeat repeat bit
|
||||
* mark space
|
||||
*
|
||||
* 4500 4500 32 bit + stop bit 40000/100000 4500 4500
|
||||
* __________ _ _ _ _ _ _ _ _ _ _ _ ___________ _ _
|
||||
* _ __________ __ _ __ __ __ _ _ __ __ _ ________________ ____________ ____ ___
|
||||
*
|
||||
***************************************************************************************************/
|
||||
|
||||
#define IRDA_SAMSUNG_PREAMBULE_MARK 4500
|
||||
#define IRDA_SAMSUNG_PREAMBULE_SPACE 4500
|
||||
#define IRDA_SAMSUNG_BIT1_MARK 550
|
||||
#define IRDA_SAMSUNG_BIT1_SPACE 1650
|
||||
#define IRDA_SAMSUNG_BIT0_MARK 550
|
||||
#define IRDA_SAMSUNG_BIT0_SPACE 550
|
||||
#define IRDA_SAMSUNG_REPEAT_PAUSE_MIN 30000
|
||||
#define IRDA_SAMSUNG_REPEAT_PAUSE1 46000
|
||||
#define IRDA_SAMSUNG_REPEAT_PAUSE2 97000
|
||||
/* Samsung silence have to be greater than REPEAT MAX
|
||||
* otherwise there can be problems during unit tests parsing
|
||||
* of some data. Real tolerances we don't know, but in real life
|
||||
* silence time should be greater than max repeat time. This is
|
||||
* because of similar preambule timings for repeat and first messages. */
|
||||
#define IRDA_SAMSUNG_SILENCE 145000
|
||||
#define IRDA_SAMSUNG_REPEAT_PAUSE_MAX 140000
|
||||
#define IRDA_SAMSUNG_REPEAT_MARK 4500
|
||||
#define IRDA_SAMSUNG_REPEAT_SPACE 4500
|
||||
#define IRDA_SAMSUNG_PREAMBLE_TOLERANCE 0.07 // percents
|
||||
#define IRDA_SAMSUNG_BIT_TOLERANCE 120 // us
|
||||
|
||||
void* irda_decoder_samsung32_alloc(void);
|
||||
void irda_decoder_samsung32_reset(void* decoder);
|
||||
void irda_decoder_samsung32_free(void* decoder);
|
||||
IrdaMessage* irda_decoder_samsung32_decode(void* decoder, bool level, uint32_t duration);
|
||||
|
||||
IrdaStatus irda_encoder_samsung32_encode(void* encoder_ptr, uint32_t* duration, bool* level);
|
||||
void irda_encoder_samsung32_reset(void* encoder_ptr, const IrdaMessage* message);
|
||||
void* irda_encoder_samsung32_alloc(void);
|
||||
void irda_encoder_samsung32_free(void* encoder_ptr);
|
||||
|
||||
bool irda_decoder_samsung32_interpret(IrdaCommonDecoder* decoder);
|
||||
IrdaStatus irda_decoder_samsung32_decode_repeat(IrdaCommonDecoder* decoder);
|
||||
IrdaStatus irda_encoder_samsung32_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level);
|
||||
|
||||
extern const IrdaCommonProtocolSpec protocol_samsung32;
|
||||
|
||||
|
||||
/***************************************************************************************************
|
||||
* RC6 protocol description
|
||||
* https://www.mikrocontroller.net/articles/IRMP_-_english#RC6_.2B_RC6A
|
||||
****************************************************************************************************
|
||||
* Preamble Manchester/biphase Silence
|
||||
* mark/space Modulation
|
||||
*
|
||||
* 2666 889 444/888 - bit (x2 for toggle bit) 2666
|
||||
*
|
||||
* ________ __ __ __ __ ____ __ __ __ __ __ __ __ __
|
||||
* _ _________ ____ __ __ ____ __ __ __ __ __ __ __ __ _______________
|
||||
* | 1 | 0 | 0 | 0 | 0 | ... | ... | |
|
||||
* s m2 m1 m0 T address (MSB) command (MSB)
|
||||
*
|
||||
* s - start bit (always 1)
|
||||
* m0-2 - mode (000 for RC6)
|
||||
* T - toggle bit, twice longer
|
||||
* address - 8 bit
|
||||
* command - 8 bit
|
||||
***************************************************************************************************/
|
||||
|
||||
#define IRDA_RC6_PREAMBULE_MARK 2666
|
||||
#define IRDA_RC6_PREAMBULE_SPACE 889
|
||||
#define IRDA_RC6_BIT 444 // half of time-quant for 1 bit
|
||||
#define IRDA_RC6_PREAMBLE_TOLERANCE 0.07 // percents
|
||||
#define IRDA_RC6_BIT_TOLERANCE 120 // us
|
||||
#define IRDA_RC6_SILENCE 2700
|
||||
|
||||
void* irda_decoder_rc6_alloc(void);
|
||||
void irda_decoder_rc6_reset(void* decoder);
|
||||
void irda_decoder_rc6_free(void* decoder);
|
||||
IrdaMessage* irda_decoder_rc6_decode(void* decoder, bool level, uint32_t duration);
|
||||
void* irda_encoder_rc6_alloc(void);
|
||||
void irda_encoder_rc6_reset(void* encoder_ptr, const IrdaMessage* message);
|
||||
void irda_encoder_rc6_free(void* decoder);
|
||||
IrdaStatus irda_encoder_rc6_encode(void* encoder_ptr, uint32_t* duration, bool* polarity);
|
||||
|
||||
bool irda_decoder_rc6_interpret(IrdaCommonDecoder* decoder);
|
||||
IrdaStatus irda_decoder_rc6_decode_manchester(IrdaCommonDecoder* decoder);
|
||||
IrdaStatus irda_encoder_rc6_encode_manchester(IrdaCommonEncoder* encoder_ptr, uint32_t* duration, bool* polarity);
|
||||
|
||||
extern const IrdaCommonProtocolSpec protocol_rc6;
|
||||
|
88
lib/irda/encoder_decoder/nec/irda_decoder_nec.c
Normal file
88
lib/irda/encoder_decoder/nec/irda_decoder_nec.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "irda_protocol_defs_i.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <furi.h>
|
||||
#include "../irda_i.h"
|
||||
|
||||
|
||||
bool irda_decoder_nec_interpret(IrdaCommonDecoder* decoder) {
|
||||
furi_assert(decoder);
|
||||
|
||||
bool result = false;
|
||||
uint8_t address = decoder->data[0];
|
||||
uint8_t address_inverse = decoder->data[1];
|
||||
uint8_t command = decoder->data[2];
|
||||
uint8_t command_inverse = decoder->data[3];
|
||||
|
||||
if ((command == (uint8_t) ~command_inverse) && (address == (uint8_t) ~address_inverse)) {
|
||||
decoder->message.command = command;
|
||||
decoder->message.address = address;
|
||||
decoder->message.repeat = false;
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Some NEC's extensions allow 16 bit address
|
||||
bool irda_decoder_necext_interpret(IrdaCommonDecoder* decoder) {
|
||||
furi_assert(decoder);
|
||||
|
||||
bool result = false;
|
||||
uint8_t command = decoder->data[2];
|
||||
uint8_t command_inverse = decoder->data[3];
|
||||
|
||||
if(command == (uint8_t)~command_inverse) {
|
||||
decoder->message.command = command;
|
||||
decoder->message.address = decoder->data[0] | (decoder->data[1] << 8);
|
||||
decoder->message.repeat = false;
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// timings start from Space (delay between message and repeat)
|
||||
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;
|
||||
IrdaStatus status = IrdaStatusError;
|
||||
|
||||
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 = IrdaStatusReady;
|
||||
decoder->timings_cnt = 0;
|
||||
} else {
|
||||
status = IrdaStatusError;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
void* irda_decoder_nec_alloc(void) {
|
||||
return irda_common_decoder_alloc(&protocol_nec);
|
||||
}
|
||||
|
||||
void* irda_decoder_necext_alloc(void) {
|
||||
return irda_common_decoder_alloc(&protocol_necext);
|
||||
}
|
||||
|
||||
IrdaMessage* irda_decoder_nec_decode(void* decoder, bool level, uint32_t duration) {
|
||||
return irda_common_decode(decoder, level, duration);
|
||||
}
|
||||
|
||||
void irda_decoder_nec_free(void* decoder) {
|
||||
irda_common_decoder_free(decoder);
|
||||
}
|
||||
|
||||
void irda_decoder_nec_reset(void* decoder) {
|
||||
irda_common_decoder_reset(decoder);
|
||||
}
|
||||
|
85
lib/irda/encoder_decoder/nec/irda_encoder_nec.c
Normal file
85
lib/irda/encoder_decoder/nec/irda_encoder_nec.c
Normal file
@@ -0,0 +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 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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
IrdaStatus irda_encoder_nec_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
|
||||
furi_assert(encoder);
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
113
lib/irda/encoder_decoder/rc6/irda_decoder_rc6.c
Normal file
113
lib/irda/encoder_decoder/rc6/irda_decoder_rc6.c
Normal file
@@ -0,0 +1,113 @@
|
||||
#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;
|
||||
} IrdaRc6Decoder;
|
||||
|
||||
bool irda_decoder_rc6_interpret(IrdaCommonDecoder* decoder) {
|
||||
furi_assert(decoder);
|
||||
|
||||
bool result = false;
|
||||
uint32_t* data = (void*) &decoder->data[0];
|
||||
// MSB first
|
||||
uint8_t address = reverse((uint8_t) (*data >> 5));
|
||||
uint8_t command = reverse((uint8_t) (*data >> 13));
|
||||
bool start_bit = *data & 0x01;
|
||||
bool toggle = !!(*data & 0x10);
|
||||
uint8_t mode = (*data >> 1) & 0x7;
|
||||
|
||||
if ((start_bit == 1) && (mode == 0)) {
|
||||
IrdaMessage* message = &decoder->message;
|
||||
IrdaRc6Decoder *rc6_decoder = decoder->context;
|
||||
bool *prev_toggle = &rc6_decoder->toggle;
|
||||
if ((message->address == address)
|
||||
&& (message->command == command)
|
||||
&& (message->protocol == IrdaProtocolRC6)) {
|
||||
message->repeat = (toggle == *prev_toggle);
|
||||
} else {
|
||||
message->repeat = false;
|
||||
}
|
||||
*prev_toggle = toggle;
|
||||
message->command = command;
|
||||
message->address = address;
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* RC6 Uses manchester encoding, but it has twice longer
|
||||
* 4-th bit (toggle bit) time quant, so we need to decode
|
||||
* it separately and than pass decoding for other bits to
|
||||
* common manchester decode function.
|
||||
*/
|
||||
IrdaStatus irda_decoder_rc6_decode_manchester(IrdaCommonDecoder* decoder) {
|
||||
// 4th bit lasts 2x times more
|
||||
IrdaStatus status = IrdaStatusError;
|
||||
uint16_t bit = decoder->protocol->timings.bit1_mark;
|
||||
uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
|
||||
uint16_t timing = decoder->timings[0];
|
||||
|
||||
bool single_timing = MATCH_BIT_TIMING(timing, bit, tolerance);
|
||||
bool double_timing = MATCH_BIT_TIMING(timing, 2*bit, tolerance);
|
||||
bool triple_timing = MATCH_BIT_TIMING(timing, 3*bit, tolerance);
|
||||
|
||||
if (decoder->databit_cnt == 4) {
|
||||
furi_assert(decoder->timings_cnt == 1);
|
||||
furi_assert(decoder->switch_detect == true);
|
||||
|
||||
if (single_timing ^ triple_timing) {
|
||||
--decoder->timings_cnt;
|
||||
++decoder->databit_cnt;
|
||||
decoder->data[0] |= (single_timing ? !decoder->level : decoder->level) << 4;
|
||||
status = IrdaStatusOk;
|
||||
}
|
||||
} else if (decoder->databit_cnt == 5) {
|
||||
if (single_timing || triple_timing) {
|
||||
if (triple_timing)
|
||||
decoder->timings[0] = bit;
|
||||
decoder->switch_detect = false;
|
||||
status = irda_common_decode_manchester(decoder);
|
||||
} else if (double_timing) {
|
||||
--decoder->timings_cnt;
|
||||
status = IrdaStatusOk;
|
||||
}
|
||||
} else {
|
||||
status = irda_common_decode_manchester(decoder);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
void* irda_decoder_rc6_alloc(void) {
|
||||
IrdaRc6Decoder* decoder = furi_alloc(sizeof(IrdaRc6Decoder));
|
||||
decoder->toggle = false;
|
||||
decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc6);
|
||||
irda_common_decoder_set_context(decoder->common_decoder, decoder);
|
||||
return decoder;
|
||||
}
|
||||
|
||||
IrdaMessage* irda_decoder_rc6_decode(void* decoder, bool level, uint32_t duration) {
|
||||
IrdaRc6Decoder* decoder_rc6 = decoder;
|
||||
return irda_common_decode(decoder_rc6->common_decoder, level, duration);
|
||||
}
|
||||
|
||||
void irda_decoder_rc6_free(void* decoder) {
|
||||
IrdaRc6Decoder* decoder_rc6 = decoder;
|
||||
irda_common_decoder_free(decoder_rc6->common_decoder);
|
||||
free(decoder_rc6);
|
||||
}
|
||||
|
||||
void irda_decoder_rc6_reset(void* decoder) {
|
||||
IrdaRc6Decoder* decoder_rc6 = decoder;
|
||||
irda_common_decoder_reset(decoder_rc6->common_decoder);
|
||||
}
|
||||
|
59
lib/irda/encoder_decoder/rc6/irda_encoder_rc6.c
Normal file
59
lib/irda/encoder_decoder/rc6/irda_encoder_rc6.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "furi/memmgr.h"
|
||||
#include "irda.h"
|
||||
#include "irda_common_i.h"
|
||||
#include "irda_protocol_defs_i.h"
|
||||
#include <stdint.h>
|
||||
#include "../irda_i.h"
|
||||
|
||||
typedef struct IrdaEncoderRC6 {
|
||||
IrdaCommonEncoder* common_encoder;
|
||||
bool toggle_bit;
|
||||
} IrdaEncoderRC6;
|
||||
|
||||
void irda_encoder_rc6_reset(void* encoder_ptr, const IrdaMessage* message) {
|
||||
furi_assert(encoder_ptr);
|
||||
|
||||
IrdaEncoderRC6* encoder = encoder_ptr;
|
||||
IrdaCommonEncoder* common_encoder = encoder->common_encoder;
|
||||
irda_common_encoder_reset(common_encoder);
|
||||
|
||||
uint32_t* data = (void*) common_encoder->data;
|
||||
*data |= 0x01; // start bit
|
||||
(void) *data; // 3 bits for mode == 0
|
||||
*data |= encoder->toggle_bit ? 0x10 : 0;
|
||||
*data |= reverse(message->address) << 5;
|
||||
*data |= reverse(message->command) << 13;
|
||||
|
||||
encoder->toggle_bit ^= 1;
|
||||
}
|
||||
|
||||
IrdaStatus irda_encoder_rc6_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
|
||||
IrdaEncoderRC6* encoder = encoder_ptr;
|
||||
return irda_common_encode(encoder->common_encoder, duration, level);
|
||||
}
|
||||
|
||||
void* irda_encoder_rc6_alloc(void) {
|
||||
IrdaEncoderRC6* encoder = furi_alloc(sizeof(IrdaEncoderRC6));
|
||||
encoder->common_encoder = irda_common_encoder_alloc(&protocol_rc6);
|
||||
encoder->toggle_bit = false;
|
||||
return encoder;
|
||||
}
|
||||
|
||||
void irda_encoder_rc6_free(void* encoder_ptr) {
|
||||
furi_assert(encoder_ptr);
|
||||
|
||||
IrdaEncoderRC6* encoder = encoder_ptr;
|
||||
free(encoder->common_encoder);
|
||||
free(encoder);
|
||||
}
|
||||
|
||||
IrdaStatus irda_encoder_rc6_encode_manchester(IrdaCommonEncoder* common_encoder, uint32_t* duration, bool* polarity) {
|
||||
IrdaStatus status = IrdaStatusError;
|
||||
|
||||
bool toggle_bit = (common_encoder->bits_encoded == 4);
|
||||
status = irda_common_encode_manchester(common_encoder, duration, polarity);
|
||||
if (toggle_bit)
|
||||
*duration *= 2;
|
||||
return status;
|
||||
}
|
||||
|
70
lib/irda/encoder_decoder/samsung/irda_decoder_samsung.c
Normal file
70
lib/irda/encoder_decoder/samsung/irda_decoder_samsung.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "irda_protocol_defs_i.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <furi.h>
|
||||
#include "../irda_i.h"
|
||||
|
||||
|
||||
bool irda_decoder_samsung32_interpret(IrdaCommonDecoder* decoder) {
|
||||
furi_assert(decoder);
|
||||
|
||||
bool result = false;
|
||||
uint8_t address1 = decoder->data[0];
|
||||
uint8_t address2 = decoder->data[1];
|
||||
uint8_t command = decoder->data[2];
|
||||
uint8_t command_inverse = decoder->data[3];
|
||||
|
||||
if ((address1 == address2) && (command == (uint8_t) ~command_inverse)) {
|
||||
decoder->message.command = command;
|
||||
decoder->message.address = address1;
|
||||
decoder->message.repeat = false;
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// timings start from Space (delay between message and repeat)
|
||||
IrdaStatus irda_decoder_samsung32_decode_repeat(IrdaCommonDecoder* decoder) {
|
||||
furi_assert(decoder);
|
||||
|
||||
float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
|
||||
uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
|
||||
IrdaStatus status = IrdaStatusError;
|
||||
|
||||
if (decoder->timings_cnt < 6)
|
||||
return IrdaStatusOk;
|
||||
|
||||
if ((decoder->timings[0] > IRDA_SAMSUNG_REPEAT_PAUSE_MIN)
|
||||
&& (decoder->timings[0] < IRDA_SAMSUNG_REPEAT_PAUSE_MAX)
|
||||
&& MATCH_PREAMBLE_TIMING(decoder->timings[1], IRDA_SAMSUNG_REPEAT_MARK, preamble_tolerance)
|
||||
&& MATCH_PREAMBLE_TIMING(decoder->timings[2], IRDA_SAMSUNG_REPEAT_SPACE, preamble_tolerance)
|
||||
&& MATCH_BIT_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)
|
||||
&& MATCH_BIT_TIMING(decoder->timings[4], decoder->protocol->timings.bit1_space, bit_tolerance)
|
||||
&& MATCH_BIT_TIMING(decoder->timings[5], decoder->protocol->timings.bit1_mark, bit_tolerance)
|
||||
) {
|
||||
status = IrdaStatusReady;
|
||||
decoder->timings_cnt = 0;
|
||||
} else {
|
||||
status = IrdaStatusError;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
void* irda_decoder_samsung32_alloc(void) {
|
||||
return irda_common_decoder_alloc(&protocol_samsung32);
|
||||
}
|
||||
|
||||
IrdaMessage* irda_decoder_samsung32_decode(void* decoder, bool level, uint32_t duration) {
|
||||
return irda_common_decode(decoder, level, duration);
|
||||
}
|
||||
|
||||
void irda_decoder_samsung32_free(void* decoder) {
|
||||
irda_common_decoder_free(decoder);
|
||||
}
|
||||
|
||||
void irda_decoder_samsung32_reset(void* decoder) {
|
||||
irda_common_decoder_reset(decoder);
|
||||
}
|
||||
|
67
lib/irda/encoder_decoder/samsung/irda_encoder_samsung.c
Normal file
67
lib/irda/encoder_decoder/samsung/irda_encoder_samsung.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#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 uint32_t repeat_timings[] = {
|
||||
IRDA_SAMSUNG_REPEAT_PAUSE2,
|
||||
IRDA_SAMSUNG_REPEAT_MARK,
|
||||
IRDA_SAMSUNG_REPEAT_SPACE,
|
||||
IRDA_SAMSUNG_BIT1_MARK,
|
||||
IRDA_SAMSUNG_BIT1_SPACE,
|
||||
IRDA_SAMSUNG_BIT1_MARK,
|
||||
};
|
||||
|
||||
void irda_encoder_samsung32_reset(void* encoder_ptr, const IrdaMessage* message) {
|
||||
furi_assert(encoder_ptr);
|
||||
|
||||
IrdaCommonEncoder* encoder = encoder_ptr;
|
||||
irda_common_encoder_reset(encoder);
|
||||
|
||||
uint8_t address = message->address;
|
||||
uint8_t command = message->command;
|
||||
uint8_t command_inverse = ~command;
|
||||
|
||||
uint32_t* data = (void*) encoder->data;
|
||||
*data |= address;
|
||||
*data |= address << 8;
|
||||
*data |= command << 16;
|
||||
*data |= command_inverse << 24;
|
||||
}
|
||||
|
||||
IrdaStatus irda_encoder_samsung32_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
|
||||
furi_assert(encoder);
|
||||
|
||||
/* 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_SAMSUNG_REPEAT_PAUSE1;
|
||||
|
||||
*level = repeat_cnt % 2;
|
||||
++encoder->timings_encoded;
|
||||
bool done = (!((repeat_cnt + 1) % COUNT_OF(repeat_timings)));
|
||||
|
||||
return done ? IrdaStatusDone : IrdaStatusOk;
|
||||
}
|
||||
|
||||
void* irda_encoder_samsung32_alloc(void) {
|
||||
return irda_common_encoder_alloc(&protocol_samsung32);
|
||||
}
|
||||
|
||||
void irda_encoder_samsung32_free(void* encoder_ptr) {
|
||||
irda_common_encoder_free(encoder_ptr);
|
||||
}
|
||||
|
||||
IrdaStatus irda_encoder_samsung32_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
|
||||
return irda_common_encode(encoder_ptr, duration, level);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user