[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:
@@ -4,8 +4,34 @@
|
||||
#include <stdbool.h>
|
||||
#include <furi.h>
|
||||
#include "irda_i.h"
|
||||
#include <stdint.h>
|
||||
|
||||
static void irda_common_decoder_reset_state(IrdaCommonDecoder* common_decoder);
|
||||
static void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder);
|
||||
|
||||
static inline size_t consume_samples(uint32_t* array, size_t len, size_t shift) {
|
||||
furi_assert(len >= shift);
|
||||
len -= shift;
|
||||
for (int i = 0; i < len; ++i)
|
||||
array[i] = array[i + shift];
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static inline void accumulate_lsb(IrdaCommonDecoder* decoder, bool bit) {
|
||||
uint16_t index = decoder->databit_cnt / 8;
|
||||
uint8_t shift = decoder->databit_cnt % 8; // LSB first
|
||||
|
||||
if (!shift)
|
||||
decoder->data[index] = 0;
|
||||
|
||||
if (bit) {
|
||||
decoder->data[index] |= (0x1 << shift); // add 1
|
||||
} else {
|
||||
(void) decoder->data[index]; // add 0
|
||||
}
|
||||
|
||||
++decoder->databit_cnt;
|
||||
}
|
||||
|
||||
static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
|
||||
furi_assert(decoder);
|
||||
@@ -16,8 +42,7 @@ static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
|
||||
// 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);
|
||||
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,25 +55,22 @@ static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
|
||||
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))) {
|
||||
if ((MATCH_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance))
|
||||
&& (MATCH_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
|
||||
result = true;
|
||||
}
|
||||
|
||||
decoder->timings_cnt -= 2;
|
||||
shift_left_array(decoder->timings, decoder->timings_cnt, 2);
|
||||
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 2);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Pulse Distance-Width Modulation */
|
||||
IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder) {
|
||||
/* Pulse Distance Modulation */
|
||||
IrdaStatus irda_common_decode_pdm(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;
|
||||
@@ -59,7 +81,7 @@ IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder) {
|
||||
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)) {
|
||||
if (MATCH_TIMING(timings[0], bit1_mark, bit_tolerance)) {
|
||||
decoder->timings_cnt = 0;
|
||||
status = IrdaStatusReady;
|
||||
} else {
|
||||
@@ -69,23 +91,17 @@ IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder) {
|
||||
}
|
||||
|
||||
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
|
||||
if (MATCH_TIMING(timings[0], bit1_mark, bit_tolerance)
|
||||
&& MATCH_TIMING(timings[1], bit1_space, bit_tolerance)) {
|
||||
accumulate_lsb(decoder, 1);
|
||||
} else if (MATCH_TIMING(timings[0], bit0_mark, bit_tolerance)
|
||||
&& MATCH_TIMING(timings[1], bit0_space, bit_tolerance)) {
|
||||
accumulate_lsb(decoder, 0);
|
||||
} else {
|
||||
status = IrdaStatusError;
|
||||
break;
|
||||
}
|
||||
++decoder->databit_cnt;
|
||||
decoder->timings_cnt -= 2;
|
||||
shift_left_array(decoder->timings, decoder->timings_cnt, 2);
|
||||
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 2);
|
||||
} else {
|
||||
status = IrdaStatusOk;
|
||||
break;
|
||||
@@ -107,8 +123,8 @@ IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder) {
|
||||
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);
|
||||
bool single_timing = MATCH_TIMING(timing, bit, tolerance);
|
||||
bool double_timing = MATCH_TIMING(timing, 2*bit, tolerance);
|
||||
|
||||
if(!single_timing && !double_timing) {
|
||||
status = IrdaStatusError;
|
||||
@@ -134,19 +150,13 @@ IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder) {
|
||||
*switch_detect = 0;
|
||||
}
|
||||
|
||||
--decoder->timings_cnt;
|
||||
shift_left_array(decoder->timings, decoder->timings_cnt, 1);
|
||||
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
|
||||
status = IrdaStatusOk;
|
||||
bool level = (decoder->level + decoder->timings_cnt) % 2;
|
||||
|
||||
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;
|
||||
decoder->data[index] |= (level << shift);
|
||||
++decoder->databit_cnt;
|
||||
accumulate_lsb(decoder, level);
|
||||
}
|
||||
if (decoder->databit_cnt == decoder->protocol->databit_len) {
|
||||
if (level) {
|
||||
@@ -169,6 +179,46 @@ IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Pulse Width Modulation */
|
||||
IrdaStatus irda_common_decode_pwm(IrdaCommonDecoder* decoder) {
|
||||
furi_assert(decoder);
|
||||
|
||||
uint32_t* timings = decoder->timings;
|
||||
IrdaStatus status = IrdaStatusOk;
|
||||
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;
|
||||
|
||||
while (decoder->timings_cnt) {
|
||||
bool level = (decoder->level + decoder->timings_cnt + 1) % 2;
|
||||
|
||||
if (level) {
|
||||
if (MATCH_TIMING(timings[0], bit1_mark, bit_tolerance)) {
|
||||
accumulate_lsb(decoder, 1);
|
||||
} else if (MATCH_TIMING(timings[0], bit0_mark, bit_tolerance)) {
|
||||
accumulate_lsb(decoder, 0);
|
||||
} else {
|
||||
status = IrdaStatusError;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (!MATCH_TIMING(timings[0], bit1_space, bit_tolerance)) {
|
||||
status = IrdaStatusError;
|
||||
break;
|
||||
}
|
||||
}
|
||||
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
|
||||
|
||||
if (decoder->databit_cnt == decoder->protocol->databit_len) {
|
||||
status = IrdaStatusReady;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
|
||||
furi_assert(decoder);
|
||||
|
||||
@@ -176,7 +226,7 @@ IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t
|
||||
IrdaStatus status = IrdaStatusError;
|
||||
|
||||
if (decoder->level == level) {
|
||||
decoder->timings_cnt = 0;
|
||||
irda_common_decoder_reset(decoder);
|
||||
}
|
||||
decoder->level = level; // start with low level (Space timing)
|
||||
|
||||
@@ -242,32 +292,27 @@ void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
|
||||
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) {
|
||||
void irda_common_decoder_free(IrdaCommonDecoder* 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;
|
||||
if ((common_decoder->protocol->timings.preamble_mark == 0) && (common_decoder->timings_cnt > 0)) {
|
||||
--common_decoder->timings_cnt;
|
||||
shift_left_array(common_decoder->timings, common_decoder->timings_cnt, 1);
|
||||
void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder) {
|
||||
decoder->state = IrdaCommonDecoderStateWaitPreamble;
|
||||
decoder->databit_cnt = 0;
|
||||
decoder->switch_detect = false;
|
||||
decoder->message.protocol = IrdaProtocolUnknown;
|
||||
if (decoder->protocol->timings.preamble_mark == 0) {
|
||||
if (decoder->timings_cnt > 0) {
|
||||
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void irda_common_decoder_reset(void* decoder) {
|
||||
void irda_common_decoder_reset(IrdaCommonDecoder* decoder) {
|
||||
furi_assert(decoder);
|
||||
IrdaCommonDecoder* common_decoder = decoder;
|
||||
|
||||
irda_common_decoder_reset_state(common_decoder);
|
||||
common_decoder->timings_cnt = 0;
|
||||
irda_common_decoder_reset_state(decoder);
|
||||
decoder->timings_cnt = 0;
|
||||
}
|
||||
|
||||
|
@@ -35,6 +35,7 @@ IrdaStatus irda_common_encode_manchester(IrdaCommonEncoder* encoder, uint32_t* d
|
||||
if (even_timing) /* start encoding from space */
|
||||
++encoder->bits_encoded;
|
||||
++encoder->timings_encoded;
|
||||
encoder->timings_sum += *duration;
|
||||
|
||||
bool finish = (encoder->bits_encoded == encoder->protocol->databit_len);
|
||||
finish |= (encoder->bits_encoded == (encoder->protocol->databit_len-1)) && *level && !even_timing;
|
||||
@@ -46,6 +47,7 @@ IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duratio
|
||||
furi_assert(duration);
|
||||
furi_assert(level);
|
||||
|
||||
bool done = false;
|
||||
const IrdaTimings* timings = &encoder->protocol->timings;
|
||||
uint8_t index = encoder->bits_encoded / 8;
|
||||
uint8_t shift = encoder->bits_encoded % 8; // LSB first
|
||||
@@ -53,9 +55,11 @@ IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duratio
|
||||
|
||||
// stop bit
|
||||
if (encoder->bits_encoded == encoder->protocol->databit_len) {
|
||||
furi_assert(!encoder->protocol->no_stop_bit);
|
||||
*duration = timings->bit1_mark;
|
||||
*level = true;
|
||||
++encoder->timings_encoded;
|
||||
encoder->timings_sum += *duration;
|
||||
return IrdaStatusDone;
|
||||
}
|
||||
|
||||
@@ -68,8 +72,14 @@ IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duratio
|
||||
++encoder->bits_encoded;
|
||||
}
|
||||
|
||||
if ((encoder->bits_encoded == encoder->protocol->databit_len)
|
||||
&& encoder->protocol->no_stop_bit) {
|
||||
done = true;
|
||||
}
|
||||
|
||||
++encoder->timings_encoded;
|
||||
return IrdaStatusOk;
|
||||
encoder->timings_sum += *duration;
|
||||
return done ? IrdaStatusDone : IrdaStatusOk;
|
||||
}
|
||||
|
||||
IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
|
||||
@@ -80,12 +90,13 @@ IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bo
|
||||
const IrdaTimings* timings = &encoder->protocol->timings;
|
||||
|
||||
switch (encoder->state) {
|
||||
case IrdaCommonEncoderStateSpace:
|
||||
case IrdaCommonEncoderStateSilence:
|
||||
*duration = encoder->protocol->timings.silence_time;
|
||||
*level = false;
|
||||
status = IrdaStatusOk;
|
||||
encoder->state = IrdaCommonEncoderStatePreamble;
|
||||
++encoder->timings_encoded;
|
||||
encoder->timings_sum = 0;
|
||||
break;
|
||||
case IrdaCommonEncoderStatePreamble:
|
||||
if (timings->preamble_mark) {
|
||||
@@ -98,6 +109,7 @@ IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bo
|
||||
encoder->state = IrdaCommonEncoderStateEncode;
|
||||
}
|
||||
++encoder->timings_encoded;
|
||||
encoder->timings_sum += *duration;
|
||||
break;
|
||||
} else {
|
||||
encoder->state = IrdaCommonEncoderStateEncode;
|
||||
@@ -110,9 +122,10 @@ IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bo
|
||||
encoder->state = IrdaCommonEncoderStateEncodeRepeat;
|
||||
} else {
|
||||
encoder->timings_encoded = 0;
|
||||
encoder->timings_sum = 0;
|
||||
encoder->bits_encoded = 0;
|
||||
encoder->switch_detect = 0;
|
||||
encoder->state = IrdaCommonEncoderStateSpace;
|
||||
encoder->state = IrdaCommonEncoderStateSilence;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -144,8 +157,9 @@ void irda_common_encoder_free(IrdaCommonEncoder* encoder) {
|
||||
void irda_common_encoder_reset(IrdaCommonEncoder* encoder) {
|
||||
furi_assert(encoder);
|
||||
encoder->timings_encoded = 0;
|
||||
encoder->timings_sum = 0;
|
||||
encoder->bits_encoded = 0;
|
||||
encoder->state = IrdaCommonEncoderStateSpace;
|
||||
encoder->state = IrdaCommonEncoderStateSilence;
|
||||
encoder->switch_detect = 0;
|
||||
|
||||
uint8_t bytes_to_clear = encoder->protocol->databit_len / 8
|
||||
@@ -153,8 +167,3 @@ void irda_common_encoder_reset(IrdaCommonEncoder* encoder) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
@@ -5,11 +5,8 @@
|
||||
#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)))))
|
||||
#define MATCH_TIMING(x, v, delta) ( ((x) < (v + delta)) \
|
||||
&& ((x) > (v - delta)))
|
||||
|
||||
typedef struct IrdaCommonDecoder IrdaCommonDecoder;
|
||||
typedef struct IrdaCommonEncoder IrdaCommonEncoder;
|
||||
@@ -21,6 +18,7 @@ typedef IrdaStatus (*IrdaCommonEncode)(IrdaCommonEncoder* encoder, uint32_t* out
|
||||
typedef struct {
|
||||
IrdaTimings timings;
|
||||
bool manchester_start_from_space;
|
||||
bool no_stop_bit;
|
||||
uint32_t databit_len;
|
||||
IrdaCommonDecode decode;
|
||||
IrdaCommonDecode decode_repeat;
|
||||
@@ -36,7 +34,7 @@ typedef enum {
|
||||
} IrdaCommonStateDecoder;
|
||||
|
||||
typedef enum {
|
||||
IrdaCommonEncoderStateSpace,
|
||||
IrdaCommonEncoderStateSilence,
|
||||
IrdaCommonEncoderStatePreamble,
|
||||
IrdaCommonEncoderStateEncode,
|
||||
IrdaCommonEncoderStateEncodeRepeat,
|
||||
@@ -44,13 +42,13 @@ typedef enum {
|
||||
|
||||
struct IrdaCommonDecoder {
|
||||
const IrdaCommonProtocolSpec* protocol;
|
||||
IrdaCommonStateDecoder state;
|
||||
IrdaMessage message;
|
||||
uint32_t timings[6];
|
||||
uint8_t timings_cnt;
|
||||
void* context;
|
||||
uint32_t timings[6];
|
||||
IrdaMessage message;
|
||||
IrdaCommonStateDecoder state;
|
||||
uint8_t timings_cnt;
|
||||
bool switch_detect;
|
||||
uint32_t level;
|
||||
bool level;
|
||||
uint16_t databit_cnt;
|
||||
uint8_t data[];
|
||||
};
|
||||
@@ -60,30 +58,23 @@ struct IrdaCommonEncoder {
|
||||
IrdaCommonStateEncoder state;
|
||||
bool switch_detect;
|
||||
uint32_t bits_encoded;
|
||||
uint32_t timings_sum;
|
||||
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_pdm(IrdaCommonDecoder* decoder);
|
||||
IrdaStatus irda_common_decode_pwm(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);
|
||||
void irda_common_decoder_free(IrdaCommonDecoder* decoder);
|
||||
void irda_common_decoder_reset(IrdaCommonDecoder* 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);
|
||||
|
@@ -3,8 +3,8 @@
|
||||
|
||||
const IrdaCommonProtocolSpec protocol_nec = {
|
||||
.timings = {
|
||||
.preamble_mark = IRDA_NEC_PREAMBULE_MARK,
|
||||
.preamble_space = IRDA_NEC_PREAMBULE_SPACE,
|
||||
.preamble_mark = IRDA_NEC_PREAMBLE_MARK,
|
||||
.preamble_space = IRDA_NEC_PREAMBLE_SPACE,
|
||||
.bit1_mark = IRDA_NEC_BIT1_MARK,
|
||||
.bit1_space = IRDA_NEC_BIT1_SPACE,
|
||||
.bit0_mark = IRDA_NEC_BIT0_MARK,
|
||||
@@ -14,7 +14,8 @@ const IrdaCommonProtocolSpec protocol_nec = {
|
||||
.silence_time = IRDA_NEC_SILENCE,
|
||||
},
|
||||
.databit_len = 32,
|
||||
.decode = irda_common_decode_pdwm,
|
||||
.no_stop_bit = false,
|
||||
.decode = irda_common_decode_pdm,
|
||||
.encode = irda_common_encode_pdwm,
|
||||
.interpret = irda_decoder_nec_interpret,
|
||||
.decode_repeat = irda_decoder_nec_decode_repeat,
|
||||
@@ -23,8 +24,8 @@ const IrdaCommonProtocolSpec protocol_nec = {
|
||||
|
||||
const IrdaCommonProtocolSpec protocol_samsung32 = {
|
||||
.timings = {
|
||||
.preamble_mark = IRDA_SAMSUNG_PREAMBULE_MARK,
|
||||
.preamble_space = IRDA_SAMSUNG_PREAMBULE_SPACE,
|
||||
.preamble_mark = IRDA_SAMSUNG_PREAMBLE_MARK,
|
||||
.preamble_space = IRDA_SAMSUNG_PREAMBLE_SPACE,
|
||||
.bit1_mark = IRDA_SAMSUNG_BIT1_MARK,
|
||||
.bit1_space = IRDA_SAMSUNG_BIT1_SPACE,
|
||||
.bit0_mark = IRDA_SAMSUNG_BIT0_MARK,
|
||||
@@ -34,7 +35,8 @@ const IrdaCommonProtocolSpec protocol_samsung32 = {
|
||||
.silence_time = IRDA_SAMSUNG_SILENCE,
|
||||
},
|
||||
.databit_len = 32,
|
||||
.decode = irda_common_decode_pdwm,
|
||||
.no_stop_bit = false,
|
||||
.decode = irda_common_decode_pdm,
|
||||
.encode = irda_common_encode_pdwm,
|
||||
.interpret = irda_decoder_samsung32_interpret,
|
||||
.decode_repeat = irda_decoder_samsung32_decode_repeat,
|
||||
@@ -43,8 +45,8 @@ const IrdaCommonProtocolSpec protocol_samsung32 = {
|
||||
|
||||
const IrdaCommonProtocolSpec protocol_rc6 = {
|
||||
.timings = {
|
||||
.preamble_mark = IRDA_RC6_PREAMBULE_MARK,
|
||||
.preamble_space = IRDA_RC6_PREAMBULE_SPACE,
|
||||
.preamble_mark = IRDA_RC6_PREAMBLE_MARK,
|
||||
.preamble_space = IRDA_RC6_PREAMBLE_SPACE,
|
||||
.bit1_mark = IRDA_RC6_BIT,
|
||||
.preamble_tolerance = IRDA_RC6_PREAMBLE_TOLERANCE,
|
||||
.bit_tolerance = IRDA_RC6_BIT_TOLERANCE,
|
||||
@@ -77,3 +79,24 @@ const IrdaCommonProtocolSpec protocol_rc5 = {
|
||||
.encode_repeat = NULL,
|
||||
};
|
||||
|
||||
const IrdaCommonProtocolSpec protocol_sirc = {
|
||||
.timings = {
|
||||
.preamble_mark = IRDA_SIRC_PREAMBLE_MARK,
|
||||
.preamble_space = IRDA_SIRC_PREAMBLE_SPACE,
|
||||
.bit1_mark = IRDA_SIRC_BIT1_MARK,
|
||||
.bit1_space = IRDA_SIRC_BIT1_SPACE,
|
||||
.bit0_mark = IRDA_SIRC_BIT0_MARK,
|
||||
.bit0_space = IRDA_SIRC_BIT0_SPACE,
|
||||
.preamble_tolerance = IRDA_SIRC_PREAMBLE_TOLERANCE,
|
||||
.bit_tolerance = IRDA_SIRC_BIT_TOLERANCE,
|
||||
.silence_time = IRDA_SIRC_SILENCE,
|
||||
},
|
||||
.databit_len = 20, /* 12/15/20 */
|
||||
.no_stop_bit = true,
|
||||
.decode = irda_common_decode_pwm,
|
||||
.encode = irda_common_encode_pdwm,
|
||||
.interpret = irda_decoder_sirc_interpret,
|
||||
.decode_repeat = NULL,
|
||||
.encode_repeat = irda_encoder_sirc_encode_repeat,
|
||||
};
|
||||
|
||||
|
@@ -12,14 +12,15 @@
|
||||
typedef struct {
|
||||
IrdaAlloc alloc;
|
||||
IrdaDecode decode;
|
||||
IrdaReset reset;
|
||||
IrdaDecoderReset reset;
|
||||
IrdaFree free;
|
||||
IrdaDecoderCheckReady check_ready;
|
||||
} IrdaDecoders;
|
||||
|
||||
typedef struct {
|
||||
IrdaEncoderReset reset;
|
||||
IrdaAlloc alloc;
|
||||
IrdaEncode encode;
|
||||
IrdaEncoderReset reset;
|
||||
IrdaFree free;
|
||||
} IrdaEncoders;
|
||||
|
||||
@@ -39,19 +40,6 @@ typedef struct {
|
||||
} IrdaEncoderDecoder;
|
||||
|
||||
static const IrdaEncoderDecoder irda_encoder_decoder[] = {
|
||||
{
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_rc5_alloc,
|
||||
.decode = irda_decoder_rc5_decode,
|
||||
.reset = irda_decoder_rc5_reset,
|
||||
.free = irda_decoder_rc5_free},
|
||||
.encoder = {
|
||||
.alloc = irda_encoder_rc5_alloc,
|
||||
.encode = irda_encoder_rc5_encode,
|
||||
.reset = irda_encoder_rc5_reset,
|
||||
.free = irda_encoder_rc5_free},
|
||||
.get_protocol_spec = irda_rc5_get_spec,
|
||||
},
|
||||
{
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_nec_alloc,
|
||||
@@ -78,6 +66,19 @@ static const IrdaEncoderDecoder irda_encoder_decoder[] = {
|
||||
.free = irda_encoder_samsung32_free},
|
||||
.get_protocol_spec = irda_samsung32_get_spec,
|
||||
},
|
||||
{
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_rc5_alloc,
|
||||
.decode = irda_decoder_rc5_decode,
|
||||
.reset = irda_decoder_rc5_reset,
|
||||
.free = irda_decoder_rc5_free},
|
||||
.encoder = {
|
||||
.alloc = irda_encoder_rc5_alloc,
|
||||
.encode = irda_encoder_rc5_encode,
|
||||
.reset = irda_encoder_rc5_reset,
|
||||
.free = irda_encoder_rc5_free},
|
||||
.get_protocol_spec = irda_rc5_get_spec,
|
||||
},
|
||||
{
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_rc6_alloc,
|
||||
@@ -91,8 +92,26 @@ static const IrdaEncoderDecoder irda_encoder_decoder[] = {
|
||||
.free = irda_encoder_rc6_free},
|
||||
.get_protocol_spec = irda_rc6_get_spec,
|
||||
},
|
||||
{
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_sirc_alloc,
|
||||
.decode = irda_decoder_sirc_decode,
|
||||
.reset = irda_decoder_sirc_reset,
|
||||
.check_ready = irda_decoder_sirc_check_ready,
|
||||
.free = irda_decoder_sirc_free},
|
||||
.encoder = {
|
||||
.alloc = irda_encoder_sirc_alloc,
|
||||
.encode = irda_encoder_sirc_encode,
|
||||
.reset = irda_encoder_sirc_reset,
|
||||
.free = irda_encoder_sirc_free},
|
||||
.get_protocol_spec = irda_sirc_get_spec,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
static int irda_find_index_by_protocol(IrdaProtocol protocol);
|
||||
static const IrdaProtocolSpecification* irda_get_spec_by_protocol(IrdaProtocol protocol);
|
||||
|
||||
const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration) {
|
||||
furi_assert(handler);
|
||||
|
||||
@@ -121,6 +140,7 @@ IrdaDecoderHandler* irda_alloc_decoder(void) {
|
||||
handler->ctx[i] = irda_encoder_decoder[i].decoder.alloc();
|
||||
}
|
||||
|
||||
irda_reset_decoder(handler);
|
||||
return handler;
|
||||
}
|
||||
|
||||
@@ -144,6 +164,25 @@ void irda_reset_decoder(IrdaDecoderHandler* handler) {
|
||||
}
|
||||
}
|
||||
|
||||
const IrdaMessage* irda_check_decoder_ready(IrdaDecoderHandler* handler) {
|
||||
furi_assert(handler);
|
||||
|
||||
IrdaMessage* message = NULL;
|
||||
IrdaMessage* result = NULL;
|
||||
|
||||
for (int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
|
||||
if (irda_encoder_decoder[i].decoder.check_ready) {
|
||||
message = irda_encoder_decoder[i].decoder.check_ready(handler->ctx[i]);
|
||||
if (!result && message) {
|
||||
result = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
IrdaEncoderHandler* irda_alloc_encoder(void) {
|
||||
IrdaEncoderHandler* handler = furi_alloc(sizeof(IrdaEncoderHandler));
|
||||
handler->handler = NULL;
|
||||
|
@@ -15,11 +15,9 @@ extern "C" {
|
||||
#define IRDA_RAW_RX_TIMING_DELAY_US 150000
|
||||
#define IRDA_RAW_TX_TIMING_DELAY_US 180000
|
||||
|
||||
|
||||
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,
|
||||
@@ -28,6 +26,9 @@ typedef enum {
|
||||
IrdaProtocolRC6 = 3,
|
||||
IrdaProtocolRC5 = 4,
|
||||
IrdaProtocolRC5X = 5,
|
||||
IrdaProtocolSIRC = 6,
|
||||
IrdaProtocolSIRC15 = 7,
|
||||
IrdaProtocolSIRC20 = 8,
|
||||
IrdaProtocolMAX,
|
||||
} IrdaProtocol;
|
||||
|
||||
@@ -62,10 +63,27 @@ IrdaDecoderHandler* irda_alloc_decoder(void);
|
||||
* \param[in] duration - duration of steady high/low input signal.
|
||||
* \return if message is ready, returns pointer to decoded message, returns NULL.
|
||||
* Note: ownership of returned ptr belongs to handler. So pointer is valid
|
||||
* up to next irda_free_decoder(), irda_reset_decoder(), irda_decode() calls.
|
||||
* up to next irda_free_decoder(), irda_reset_decoder(),
|
||||
* irda_decode(), irda_check_decoder_ready() calls.
|
||||
*/
|
||||
const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration);
|
||||
|
||||
/**
|
||||
* Check whether decoder is ready.
|
||||
* Functionality is quite similar to irda_decode(), but with no timing providing.
|
||||
* Some protocols (e.g. Sony SIRC) has variable payload length, which means we
|
||||
* can't recognize end of message right after receiving last bit. That's why
|
||||
* application should call to irda_check_decoder_ready() after some timeout to
|
||||
* retrieve decoded message, if so.
|
||||
*
|
||||
* \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
|
||||
* \return if message is ready, returns pointer to decoded message, returns NULL.
|
||||
* Note: ownership of returned ptr belongs to handler. So pointer is valid
|
||||
* up to next irda_free_decoder(), irda_reset_decoder(),
|
||||
* irda_decode(), irda_check_decoder_ready() calls.
|
||||
*/
|
||||
const IrdaMessage* irda_check_decoder_ready(IrdaDecoderHandler* handler);
|
||||
|
||||
/**
|
||||
* Deinitialize decoder and free allocated memory.
|
||||
*
|
||||
@@ -100,7 +118,7 @@ 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.
|
||||
* \return length of address in bits.
|
||||
*/
|
||||
uint8_t irda_get_protocol_address_length(IrdaProtocol protocol);
|
||||
|
||||
@@ -108,7 +126,7 @@ 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.
|
||||
* \return length of command in bits.
|
||||
*/
|
||||
uint8_t irda_get_protocol_command_length(IrdaProtocol protocol);
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include "irda.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t silence_time;
|
||||
@@ -10,7 +11,7 @@ typedef struct {
|
||||
uint16_t bit1_space;
|
||||
uint16_t bit0_mark;
|
||||
uint16_t bit0_space;
|
||||
float preamble_tolerance;
|
||||
uint32_t preamble_tolerance;
|
||||
uint32_t bit_tolerance;
|
||||
} IrdaTimings;
|
||||
|
||||
@@ -25,10 +26,12 @@ typedef struct {
|
||||
typedef const IrdaProtocolSpecification* (*IrdaGetProtocolSpec) (IrdaProtocol protocol);
|
||||
|
||||
typedef void* (*IrdaAlloc) (void);
|
||||
typedef IrdaMessage* (*IrdaDecode) (void* ctx, bool level, uint32_t duration);
|
||||
typedef void (*IrdaReset) (void*);
|
||||
typedef void (*IrdaFree) (void*);
|
||||
|
||||
typedef void (*IrdaDecoderReset) (void*);
|
||||
typedef IrdaMessage* (*IrdaDecode) (void* ctx, bool level, uint32_t duration);
|
||||
typedef IrdaMessage* (*IrdaDecoderCheckReady) (void*);
|
||||
|
||||
typedef void (*IrdaEncoderReset)(void* encoder, const IrdaMessage* message);
|
||||
typedef IrdaStatus (*IrdaEncode)(void* encoder, uint32_t* out, bool* polarity);
|
||||
|
||||
|
@@ -11,29 +11,28 @@
|
||||
* 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 Modulation up to period repeat repeat bit
|
||||
* mark space
|
||||
*
|
||||
* 9000 4500 32 bit + stop bit 40000/100000 9000 2250
|
||||
* 9000 4500 32 bit + stop bit ...110000 9000 2250
|
||||
* __________ _ _ _ _ _ _ _ _ _ _ _ _ _ ___________ _
|
||||
* ____ __________ _ _ _ __ __ __ _ _ __ __ _ _ ________________ ____________ ___
|
||||
*
|
||||
***************************************************************************************************/
|
||||
|
||||
#define IRDA_NEC_PREAMBULE_MARK 9000
|
||||
#define IRDA_NEC_PREAMBULE_SPACE 4500
|
||||
#define IRDA_NEC_PREAMBLE_MARK 9000
|
||||
#define IRDA_NEC_PREAMBLE_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_PERIOD 110000
|
||||
#define IRDA_NEC_SILENCE IRDA_NEC_REPEAT_PERIOD
|
||||
#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_PREAMBLE_TOLERANCE 200 // us
|
||||
#define IRDA_NEC_BIT_TOLERANCE 120 // us
|
||||
|
||||
void* irda_decoder_nec_alloc(void);
|
||||
@@ -66,8 +65,8 @@ extern const IrdaCommonProtocolSpec protocol_nec;
|
||||
*
|
||||
***************************************************************************************************/
|
||||
|
||||
#define IRDA_SAMSUNG_PREAMBULE_MARK 4500
|
||||
#define IRDA_SAMSUNG_PREAMBULE_SPACE 4500
|
||||
#define IRDA_SAMSUNG_PREAMBLE_MARK 4500
|
||||
#define IRDA_SAMSUNG_PREAMBLE_SPACE 4500
|
||||
#define IRDA_SAMSUNG_BIT1_MARK 550
|
||||
#define IRDA_SAMSUNG_BIT1_SPACE 1650
|
||||
#define IRDA_SAMSUNG_BIT0_MARK 550
|
||||
@@ -84,7 +83,7 @@ extern const IrdaCommonProtocolSpec protocol_nec;
|
||||
#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_PREAMBLE_TOLERANCE 200 // us
|
||||
#define IRDA_SAMSUNG_BIT_TOLERANCE 120 // us
|
||||
|
||||
void* irda_decoder_samsung32_alloc(void);
|
||||
@@ -127,10 +126,10 @@ extern const IrdaCommonProtocolSpec protocol_samsung32;
|
||||
#define IRDA_RC6_CARRIER_FREQUENCY 36000
|
||||
#define IRDA_RC6_DUTY_CYCLE 0.33
|
||||
|
||||
#define IRDA_RC6_PREAMBULE_MARK 2666
|
||||
#define IRDA_RC6_PREAMBULE_SPACE 889
|
||||
#define IRDA_RC6_PREAMBLE_MARK 2666
|
||||
#define IRDA_RC6_PREAMBLE_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_PREAMBLE_TOLERANCE 200 // us
|
||||
#define IRDA_RC6_BIT_TOLERANCE 120 // us
|
||||
/* protocol allows 2700 silence, but it is hard to send 1 message without repeat */
|
||||
#define IRDA_RC6_SILENCE (2700 * 10)
|
||||
@@ -169,17 +168,17 @@ extern const IrdaCommonProtocolSpec protocol_rc6;
|
||||
* s - start bit (always 1)
|
||||
* si - RC5: start bit (always 1), RC5X - 7-th bit of address (in our case always 0)
|
||||
* T - toggle bit, change it's value every button press
|
||||
* address - 8 bit
|
||||
* command - 8 bit
|
||||
* address - 5 bit
|
||||
* command - 6/7 bit
|
||||
***************************************************************************************************/
|
||||
|
||||
#define IRDA_RC5_CARRIER_FREQUENCY 36000
|
||||
#define IRDA_RC5_DUTY_CYCLE 0.33
|
||||
|
||||
#define IRDA_RC5_PREAMBULE_MARK 0
|
||||
#define IRDA_RC5_PREAMBULE_SPACE 0
|
||||
#define IRDA_RC5_PREAMBLE_MARK 0
|
||||
#define IRDA_RC5_PREAMBLE_SPACE 0
|
||||
#define IRDA_RC5_BIT 888 // half of time-quant for 1 bit
|
||||
#define IRDA_RC5_PREAMBLE_TOLERANCE 0.07 // percents
|
||||
#define IRDA_RC5_PREAMBLE_TOLERANCE 200 // us
|
||||
#define IRDA_RC5_BIT_TOLERANCE 120 // us
|
||||
/* protocol allows 2700 silence, but it is hard to send 1 message without repeat */
|
||||
#define IRDA_RC5_SILENCE (2700 * 10)
|
||||
@@ -197,3 +196,56 @@ const IrdaProtocolSpecification* irda_rc5_get_spec(IrdaProtocol protocol);
|
||||
|
||||
extern const IrdaCommonProtocolSpec protocol_rc5;
|
||||
|
||||
|
||||
/***************************************************************************************************
|
||||
* Sony SIRC protocol description
|
||||
* https://www.sbprojects.net/knowledge/ir/sirc.php
|
||||
* http://picprojects.org.uk/
|
||||
****************************************************************************************************
|
||||
* Preamble Preamble Pulse Width Modulation Pause Entirely repeat
|
||||
* mark space up to period message..
|
||||
*
|
||||
* 2400 600 12/15/20 bits (600,1200) ...45000 2400 600
|
||||
* __________ _ _ _ _ _ _ _ _ _ _ _ _ _ __________ _ _
|
||||
* ____ __________ _ _ _ __ __ __ _ _ __ __ _ _ ____________________ __________ _
|
||||
* | command | address |
|
||||
* SIRC | 7b LSB | 5b LSB |
|
||||
* SIRC15 | 7b LSB | 8b LSB |
|
||||
* SIRC20 | 7b LSB | 13b LSB |
|
||||
*
|
||||
* No way to determine either next message is repeat or not,
|
||||
* so recognize only fact message received. Sony remotes always send at least 3 messages.
|
||||
* Assume 8 last extended bits for SIRC20 are address bits.
|
||||
***************************************************************************************************/
|
||||
|
||||
#define IRDA_SIRC_CARRIER_FREQUENCY 40000
|
||||
#define IRDA_SIRC_DUTY_CYCLE 0.33
|
||||
#define IRDA_SIRC_PREAMBLE_MARK 2400
|
||||
#define IRDA_SIRC_PREAMBLE_SPACE 600
|
||||
#define IRDA_SIRC_BIT1_MARK 1200
|
||||
#define IRDA_SIRC_BIT1_SPACE 600
|
||||
#define IRDA_SIRC_BIT0_MARK 600
|
||||
#define IRDA_SIRC_BIT0_SPACE 600
|
||||
#define IRDA_SIRC_PREAMBLE_TOLERANCE 200 // us
|
||||
#define IRDA_SIRC_BIT_TOLERANCE 120 // us
|
||||
#define IRDA_SIRC_SILENCE 10000
|
||||
#define IRDA_SIRC_MIN_SILENCE (IRDA_SIRC_SILENCE - 1000)
|
||||
#define IRDA_SIRC_REPEAT_PERIOD 45000
|
||||
|
||||
|
||||
void* irda_decoder_sirc_alloc(void);
|
||||
void irda_decoder_sirc_reset(void* decoder);
|
||||
IrdaMessage* irda_decoder_sirc_check_ready(void* decoder);
|
||||
uint32_t irda_decoder_sirc_get_timeout(void* decoder);
|
||||
void irda_decoder_sirc_free(void* decoder);
|
||||
IrdaMessage* irda_decoder_sirc_decode(void* decoder, bool level, uint32_t duration);
|
||||
void* irda_encoder_sirc_alloc(void);
|
||||
void irda_encoder_sirc_reset(void* encoder_ptr, const IrdaMessage* message);
|
||||
void irda_encoder_sirc_free(void* decoder);
|
||||
IrdaStatus irda_encoder_sirc_encode(void* encoder_ptr, uint32_t* duration, bool* polarity);
|
||||
bool irda_decoder_sirc_interpret(IrdaCommonDecoder* decoder);
|
||||
const IrdaProtocolSpecification* irda_sirc_get_spec(IrdaProtocol protocol);
|
||||
IrdaStatus irda_encoder_sirc_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level);
|
||||
|
||||
extern const IrdaCommonProtocolSpec protocol_sirc;
|
||||
|
||||
|
@@ -43,9 +43,9 @@ IrdaStatus irda_decoder_nec_decode_repeat(IrdaCommonDecoder* decoder) {
|
||||
|
||||
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)) {
|
||||
MATCH_TIMING(decoder->timings[1], IRDA_NEC_REPEAT_MARK, preamble_tolerance) &&
|
||||
MATCH_TIMING(decoder->timings[2], IRDA_NEC_REPEAT_SPACE, preamble_tolerance) &&
|
||||
MATCH_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)) {
|
||||
status = IrdaStatusReady;
|
||||
decoder->timings_cnt = 0;
|
||||
} else {
|
||||
|
@@ -7,7 +7,7 @@
|
||||
#include <furi.h>
|
||||
|
||||
static const uint32_t repeat_timings[] = {
|
||||
IRDA_NEC_REPEAT_PAUSE2,
|
||||
IRDA_NEC_REPEAT_PERIOD - IRDA_NEC_REPEAT_MARK - IRDA_NEC_REPEAT_SPACE - IRDA_NEC_BIT1_MARK,
|
||||
IRDA_NEC_REPEAT_MARK,
|
||||
IRDA_NEC_REPEAT_SPACE,
|
||||
IRDA_NEC_BIT1_MARK,
|
||||
@@ -44,10 +44,11 @@ IrdaStatus irda_encoder_nec_encode_repeat(IrdaCommonEncoder* encoder, uint32_t*
|
||||
|
||||
furi_assert(encoder->timings_encoded >= timings_encoded_up_to_repeat);
|
||||
|
||||
if (repeat_cnt > 0)
|
||||
if (repeat_cnt > 0) {
|
||||
*duration = repeat_timings[repeat_cnt % COUNT_OF(repeat_timings)];
|
||||
else
|
||||
*duration = IRDA_NEC_REPEAT_PAUSE1;
|
||||
} else {
|
||||
*duration = IRDA_NEC_REPEAT_PERIOD - encoder->timings_sum;
|
||||
}
|
||||
|
||||
*level = repeat_cnt % 2;
|
||||
++encoder->timings_encoded;
|
||||
|
@@ -3,16 +3,16 @@
|
||||
|
||||
static const IrdaProtocolSpecification irda_nec_protocol_specification = {
|
||||
.name = "NEC",
|
||||
.address_length = 2,
|
||||
.command_length = 2,
|
||||
.address_length = 8,
|
||||
.command_length = 8,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
};
|
||||
|
||||
static const IrdaProtocolSpecification irda_necext_protocol_specification = {
|
||||
.name = "NECext",
|
||||
.address_length = 4,
|
||||
.command_length = 2,
|
||||
.address_length = 16,
|
||||
.command_length = 8,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
};
|
||||
|
@@ -57,7 +57,7 @@ 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);
|
||||
decoder->common_decoder->context = decoder;
|
||||
return decoder;
|
||||
}
|
||||
|
||||
|
@@ -3,16 +3,16 @@
|
||||
|
||||
static const IrdaProtocolSpecification irda_rc5_protocol_specification = {
|
||||
.name = "RC5",
|
||||
.address_length = 2,
|
||||
.command_length = 2,
|
||||
.address_length = 5,
|
||||
.command_length = 6,
|
||||
.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,
|
||||
.address_length = 5,
|
||||
.command_length = 7,
|
||||
.frequency = IRDA_RC5_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_RC5_DUTY_CYCLE,
|
||||
};
|
||||
|
@@ -57,9 +57,9 @@ IrdaStatus irda_decoder_rc6_decode_manchester(IrdaCommonDecoder* decoder) {
|
||||
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);
|
||||
bool single_timing = MATCH_TIMING(timing, bit, tolerance);
|
||||
bool double_timing = MATCH_TIMING(timing, 2*bit, tolerance);
|
||||
bool triple_timing = MATCH_TIMING(timing, 3*bit, tolerance);
|
||||
|
||||
if (decoder->databit_cnt == 4) {
|
||||
furi_assert(decoder->timings_cnt == 1);
|
||||
@@ -92,7 +92,7 @@ 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);
|
||||
decoder->common_decoder->context = decoder;
|
||||
return decoder;
|
||||
}
|
||||
|
||||
|
@@ -3,8 +3,8 @@
|
||||
|
||||
static const IrdaProtocolSpecification irda_rc6_protocol_specification = {
|
||||
.name = "RC6",
|
||||
.address_length = 2,
|
||||
.command_length = 2,
|
||||
.address_length = 8,
|
||||
.command_length = 8,
|
||||
.frequency = IRDA_RC6_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_RC6_DUTY_CYCLE,
|
||||
};
|
||||
|
@@ -39,11 +39,11 @@ IrdaStatus irda_decoder_samsung32_decode_repeat(IrdaCommonDecoder* decoder) {
|
||||
|
||||
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)
|
||||
&& MATCH_TIMING(decoder->timings[1], IRDA_SAMSUNG_REPEAT_MARK, preamble_tolerance)
|
||||
&& MATCH_TIMING(decoder->timings[2], IRDA_SAMSUNG_REPEAT_SPACE, preamble_tolerance)
|
||||
&& MATCH_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)
|
||||
&& MATCH_TIMING(decoder->timings[4], decoder->protocol->timings.bit1_space, bit_tolerance)
|
||||
&& MATCH_TIMING(decoder->timings[5], decoder->protocol->timings.bit1_mark, bit_tolerance)
|
||||
) {
|
||||
status = IrdaStatusReady;
|
||||
decoder->timings_cnt = 0;
|
||||
|
@@ -3,8 +3,8 @@
|
||||
|
||||
static const IrdaProtocolSpecification irda_samsung32_protocol_specification = {
|
||||
.name = "Samsung32",
|
||||
.address_length = 2,
|
||||
.command_length = 2,
|
||||
.address_length = 8,
|
||||
.command_length = 8,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
};
|
||||
|
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