[FL-2279] IR doxygen, rename irda -> infrared (#1010)

* IR: Doxygen docs, some rename
* Rename irda -> infrared
* Rollback collateral renames

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
Albert Kharisov
2022-02-25 19:22:58 +04:00
committed by GitHub
parent c42cce3c6c
commit 052237f8c9
159 changed files with 6387 additions and 5622 deletions

View File

@@ -0,0 +1,317 @@
#include "furi/check.h"
#include "furi/common_defines.h"
#include "infrared.h"
#include "infrared_common_i.h"
#include <stdbool.h>
#include <furi.h>
#include "infrared_i.h"
#include <stdint.h>
static void infrared_common_decoder_reset_state(InfraredCommonDecoder* 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(InfraredCommonDecoder* 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 infrared_check_preamble(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
if(decoder->timings_cnt == 0) return false;
// align to start at Mark timing
if(!start_level) {
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
}
if(decoder->protocol->timings.preamble_mark == 0) {
return true;
}
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_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance)) &&
(MATCH_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
result = true;
}
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 2);
}
return result;
}
/**
* decoder->protocol->databit_len[0] contains biggest amount of bits, for this protocol.
* decoder->protocol->databit_len[1...] contains lesser values, but which can be decoded
* for some protocol modifications.
*/
static InfraredStatus infrared_common_decode_bits(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
InfraredStatus status = InfraredStatusOk;
const InfraredTimings* timings = &decoder->protocol->timings;
while(decoder->timings_cnt && (status == InfraredStatusOk)) {
bool level = (decoder->level + decoder->timings_cnt + 1) % 2;
uint32_t timing = decoder->timings[0];
if(timings->min_split_time && !level) {
if(timing > timings->min_split_time) {
/* long low timing - check if we're ready for any of protocol modification */
for(int i = 0; decoder->protocol->databit_len[i] &&
(i < COUNT_OF(decoder->protocol->databit_len));
++i) {
if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
return InfraredStatusReady;
}
}
} else if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
/* short low timing for longest protocol - this is signal is longer than we expected */
return InfraredStatusError;
}
}
status = decoder->protocol->decode(decoder, level, timing);
furi_check(decoder->databit_cnt <= decoder->protocol->databit_len[0]);
furi_assert(status == InfraredStatusError || status == InfraredStatusOk);
if(status == InfraredStatusError) {
break;
}
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
/* check if largest protocol version can be decoded */
if(level && (decoder->protocol->databit_len[0] == decoder->databit_cnt) &&
!timings->min_split_time) {
status = InfraredStatusReady;
break;
}
}
return status;
}
/* Pulse Distance-Width Modulation */
InfraredStatus
infrared_common_decode_pdwm(InfraredCommonDecoder* decoder, bool level, uint32_t timing) {
furi_assert(decoder);
InfraredStatus status = InfraredStatusOk;
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;
bool analyze_timing = level ^ (bit1_mark == bit0_mark);
uint16_t bit1 = level ? bit1_mark : bit1_space;
uint16_t bit0 = level ? bit0_mark : bit0_space;
uint16_t no_info_timing = (bit1_mark == bit0_mark) ? bit1_mark : bit1_space;
if(analyze_timing) {
if(MATCH_TIMING(timing, bit1, bit_tolerance)) {
accumulate_lsb(decoder, 1);
} else if(MATCH_TIMING(timing, bit0, bit_tolerance)) {
accumulate_lsb(decoder, 0);
} else {
status = InfraredStatusError;
}
} else {
if(!MATCH_TIMING(timing, no_info_timing, bit_tolerance)) {
status = InfraredStatusError;
}
}
return status;
}
/* level switch detection goes in middle of time-quant */
InfraredStatus
infrared_common_decode_manchester(InfraredCommonDecoder* decoder, bool level, uint32_t timing) {
furi_assert(decoder);
uint16_t bit = decoder->protocol->timings.bit1_mark;
uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
bool* switch_detect = &decoder->switch_detect;
furi_assert((*switch_detect == true) || (*switch_detect == false));
bool single_timing = MATCH_TIMING(timing, bit, tolerance);
bool double_timing = MATCH_TIMING(timing, 2 * bit, tolerance);
if(!single_timing && !double_timing) {
return InfraredStatusError;
}
if(decoder->protocol->manchester_start_from_space && (decoder->databit_cnt == 0)) {
*switch_detect = 1; /* fake as we were previously in the middle of time-quant */
accumulate_lsb(decoder, 0);
}
if(*switch_detect == 0) {
if(double_timing) {
return InfraredStatusError;
}
/* only single timing - level switch required in the middle of time-quant */
*switch_detect = 1;
} else {
/* double timing means we're in the middle of time-quant again */
if(single_timing) *switch_detect = 0;
}
if(*switch_detect) {
if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
return InfraredStatusError;
}
accumulate_lsb(decoder, level);
}
return InfraredStatusOk;
}
InfraredMessage* infrared_common_decoder_check_ready(InfraredCommonDecoder* decoder) {
InfraredMessage* message = NULL;
bool found_length = false;
for(int i = 0;
decoder->protocol->databit_len[i] && (i < COUNT_OF(decoder->protocol->databit_len));
++i) {
if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
found_length = true;
break;
}
}
if(found_length && decoder->protocol->interpret(decoder)) {
decoder->databit_cnt = 0;
message = &decoder->message;
if(decoder->protocol->decode_repeat) {
decoder->state = InfraredCommonDecoderStateProcessRepeat;
} else {
decoder->state = InfraredCommonDecoderStateWaitPreamble;
}
}
return message;
}
InfraredMessage*
infrared_common_decode(InfraredCommonDecoder* decoder, bool level, uint32_t duration) {
furi_assert(decoder);
InfraredMessage* message = 0;
InfraredStatus status = InfraredStatusError;
if(decoder->level == level) {
infrared_common_decoder_reset(decoder);
}
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 InfraredCommonDecoderStateWaitPreamble:
if(infrared_check_preamble(decoder)) {
decoder->state = InfraredCommonDecoderStateDecode;
decoder->databit_cnt = 0;
decoder->switch_detect = false;
continue;
}
break;
case InfraredCommonDecoderStateDecode:
status = infrared_common_decode_bits(decoder);
if(status == InfraredStatusReady) {
message = infrared_common_decoder_check_ready(decoder);
if(message) {
continue;
} else if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
/* error: can't decode largest protocol - begin decoding from start */
decoder->state = InfraredCommonDecoderStateWaitPreamble;
}
} else if(status == InfraredStatusError) {
infrared_common_decoder_reset_state(decoder);
continue;
}
break;
case InfraredCommonDecoderStateProcessRepeat:
status = decoder->protocol->decode_repeat(decoder);
if(status == InfraredStatusError) {
infrared_common_decoder_reset_state(decoder);
continue;
} else if(status == InfraredStatusReady) {
decoder->message.repeat = true;
message = &decoder->message;
}
break;
}
break;
}
return message;
}
void* infrared_common_decoder_alloc(const InfraredCommonProtocolSpec* protocol) {
furi_assert(protocol);
/* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
for(int i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
}
uint32_t alloc_size = sizeof(InfraredCommonDecoder) + protocol->databit_len[0] / 8 +
!!(protocol->databit_len[0] % 8);
InfraredCommonDecoder* decoder = malloc(alloc_size);
decoder->protocol = protocol;
decoder->level = true;
return decoder;
}
void infrared_common_decoder_free(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
free(decoder);
}
void infrared_common_decoder_reset_state(InfraredCommonDecoder* decoder) {
decoder->state = InfraredCommonDecoderStateWaitPreamble;
decoder->databit_cnt = 0;
decoder->switch_detect = false;
decoder->message.protocol = InfraredProtocolUnknown;
if(decoder->protocol->timings.preamble_mark == 0) {
if(decoder->timings_cnt > 0) {
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
}
}
}
void infrared_common_decoder_reset(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
infrared_common_decoder_reset_state(decoder);
decoder->timings_cnt = 0;
}

View File

@@ -0,0 +1,183 @@
#include "furi/check.h"
#include "infrared.h"
#include "infrared_common_i.h"
#include <stdbool.h>
#include <furi.h>
#include "infrared_i.h"
#include <stdint.h>
static InfraredStatus
infrared_common_encode_bits(InfraredCommonEncoder* encoder, uint32_t* duration, bool* level) {
InfraredStatus status = encoder->protocol->encode(encoder, duration, level);
furi_assert(status == InfraredStatusOk);
++encoder->timings_encoded;
encoder->timings_sum += *duration;
if((encoder->bits_encoded == encoder->bits_to_encode) && *level) {
status = InfraredStatusDone;
}
return status;
}
/*
*
* 3:
* even_timing = 0
* level = 0 ^ 1 = 1
* 4:
* even_timing = 1
* level = 1 ^ 1 = 0
* ++timing;
*
*
* 0 1 2 | 3 4 |
* _____-------_____---___
*/
InfraredStatus infrared_common_encode_manchester(
InfraredCommonEncoder* encoder,
uint32_t* duration,
bool* level) {
furi_assert(encoder);
furi_assert(duration);
furi_assert(level);
const InfraredTimings* 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 even_timing = !(encoder->timings_encoded % 2);
*level = even_timing ^ logic_value;
*duration = timings->bit1_mark;
if(even_timing)
++encoder->bits_encoded;
else if(*level && (encoder->bits_encoded + 1 == encoder->bits_to_encode))
++encoder->bits_encoded; /* don't encode last space */
return InfraredStatusOk;
}
InfraredStatus
infrared_common_encode_pdwm(InfraredCommonEncoder* encoder, uint32_t* duration, bool* level) {
furi_assert(encoder);
furi_assert(duration);
furi_assert(level);
const InfraredTimings* 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 pwm = timings->bit1_space == timings->bit0_space;
if(encoder->timings_encoded % 2) { /* start encoding from space */
*duration = logic_value ? timings->bit1_mark : timings->bit0_mark;
*level = true;
if(pwm) ++encoder->bits_encoded;
} else {
*duration = logic_value ? timings->bit1_space : timings->bit0_space;
*level = false;
if(!pwm) ++encoder->bits_encoded;
}
return InfraredStatusOk;
}
InfraredStatus
infrared_common_encode(InfraredCommonEncoder* encoder, uint32_t* duration, bool* level) {
furi_assert(encoder);
furi_assert(duration);
furi_assert(level);
InfraredStatus status = InfraredStatusOk;
const InfraredTimings* timings = &encoder->protocol->timings;
switch(encoder->state) {
case InfraredCommonEncoderStateSilence:
*duration = encoder->protocol->timings.silence_time;
*level = false;
status = InfraredStatusOk;
encoder->state = InfraredCommonEncoderStatePreamble;
++encoder->timings_encoded;
encoder->timings_sum = 0;
break;
case InfraredCommonEncoderStatePreamble:
if(timings->preamble_mark) {
if(encoder->timings_encoded == 1) {
*duration = timings->preamble_mark;
*level = true;
} else {
*duration = timings->preamble_space;
*level = false;
encoder->state = InfraredCommonEncoderStateEncode;
}
++encoder->timings_encoded;
encoder->timings_sum += *duration;
break;
} else {
encoder->state = InfraredCommonEncoderStateEncode;
}
/* FALLTHROUGH */
case InfraredCommonEncoderStateEncode:
status = infrared_common_encode_bits(encoder, duration, level);
if(status == InfraredStatusDone) {
if(encoder->protocol->encode_repeat) {
encoder->state = InfraredCommonEncoderStateEncodeRepeat;
} else {
encoder->timings_encoded = 0;
encoder->timings_sum = 0;
encoder->bits_encoded = 0;
encoder->switch_detect = 0;
encoder->state = InfraredCommonEncoderStateSilence;
}
}
break;
case InfraredCommonEncoderStateEncodeRepeat:
status = encoder->protocol->encode_repeat(encoder, duration, level);
break;
}
return status;
}
void* infrared_common_encoder_alloc(const InfraredCommonProtocolSpec* protocol) {
furi_assert(protocol);
if(protocol->decode == infrared_common_decode_pdwm) {
furi_assert(
(protocol->timings.bit1_mark == protocol->timings.bit0_mark) ^
(protocol->timings.bit1_space == protocol->timings.bit0_space));
}
/* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
for(int i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
}
uint32_t alloc_size = sizeof(InfraredCommonDecoder) + protocol->databit_len[0] / 8 +
!!(protocol->databit_len[0] % 8);
InfraredCommonEncoder* encoder = malloc(alloc_size);
memset(encoder, 0, alloc_size);
encoder->protocol = protocol;
return encoder;
}
void infrared_common_encoder_free(InfraredCommonEncoder* encoder) {
furi_assert(encoder);
free(encoder);
}
void infrared_common_encoder_reset(InfraredCommonEncoder* encoder) {
furi_assert(encoder);
encoder->timings_encoded = 0;
encoder->timings_sum = 0;
encoder->bits_encoded = 0;
encoder->state = InfraredCommonEncoderStateSilence;
encoder->switch_detect = 0;
uint8_t max_databit_len = 0;
for(int i = 0; i < COUNT_OF(encoder->protocol->databit_len); ++i) {
max_databit_len = MAX(max_databit_len, encoder->protocol->databit_len[i]);
}
uint8_t bytes_to_clear = max_databit_len / 8 + !!(max_databit_len % 8);
memset(encoder->data, 0, bytes_to_clear);
}

View File

@@ -0,0 +1,89 @@
#pragma once
#include <stdint.h>
#include "infrared.h"
#include "infrared_i.h"
#define MATCH_TIMING(x, v, delta) (((x) < (v + delta)) && ((x) > (v - delta)))
typedef struct InfraredCommonDecoder InfraredCommonDecoder;
typedef struct InfraredCommonEncoder InfraredCommonEncoder;
typedef InfraredStatus (*InfraredCommonDecode)(InfraredCommonDecoder*, bool, uint32_t);
typedef InfraredStatus (*InfraredCommonDecodeRepeat)(InfraredCommonDecoder*);
typedef bool (*InfraredCommonInterpret)(InfraredCommonDecoder*);
typedef InfraredStatus (
*InfraredCommonEncode)(InfraredCommonEncoder* encoder, uint32_t* out, bool* polarity);
typedef struct {
InfraredTimings timings;
bool manchester_start_from_space;
bool no_stop_bit;
uint8_t databit_len[4];
InfraredCommonDecode decode;
InfraredCommonDecodeRepeat decode_repeat;
InfraredCommonInterpret interpret;
InfraredCommonEncode encode;
InfraredCommonEncode encode_repeat;
} InfraredCommonProtocolSpec;
typedef enum {
InfraredCommonDecoderStateWaitPreamble,
InfraredCommonDecoderStateDecode,
InfraredCommonDecoderStateProcessRepeat,
} InfraredCommonStateDecoder;
typedef enum {
InfraredCommonEncoderStateSilence,
InfraredCommonEncoderStatePreamble,
InfraredCommonEncoderStateEncode,
InfraredCommonEncoderStateEncodeRepeat,
} InfraredCommonStateEncoder;
struct InfraredCommonDecoder {
const InfraredCommonProtocolSpec* protocol;
void* context;
uint32_t timings[6];
InfraredMessage message;
InfraredCommonStateDecoder state;
uint8_t timings_cnt;
bool switch_detect;
bool level;
uint16_t databit_cnt;
uint8_t data[];
};
struct InfraredCommonEncoder {
const InfraredCommonProtocolSpec* protocol;
InfraredCommonStateEncoder state;
bool switch_detect;
uint8_t bits_to_encode;
uint8_t bits_encoded;
uint32_t timings_sum;
uint32_t timings_encoded;
void* context;
uint8_t data[];
};
InfraredMessage*
infrared_common_decode(InfraredCommonDecoder* decoder, bool level, uint32_t duration);
InfraredStatus
infrared_common_decode_pdwm(InfraredCommonDecoder* decoder, bool level, uint32_t timing);
InfraredStatus
infrared_common_decode_manchester(InfraredCommonDecoder* decoder, bool level, uint32_t timing);
void* infrared_common_decoder_alloc(const InfraredCommonProtocolSpec* protocol);
void infrared_common_decoder_free(InfraredCommonDecoder* decoder);
void infrared_common_decoder_reset(InfraredCommonDecoder* decoder);
InfraredMessage* infrared_common_decoder_check_ready(InfraredCommonDecoder* decoder);
InfraredStatus
infrared_common_encode(InfraredCommonEncoder* encoder, uint32_t* duration, bool* polarity);
InfraredStatus
infrared_common_encode_pdwm(InfraredCommonEncoder* encoder, uint32_t* duration, bool* polarity);
InfraredStatus infrared_common_encode_manchester(
InfraredCommonEncoder* encoder,
uint32_t* duration,
bool* polarity);
void* infrared_common_encoder_alloc(const InfraredCommonProtocolSpec* protocol);
void infrared_common_encoder_free(InfraredCommonEncoder* encoder);
void infrared_common_encoder_reset(InfraredCommonEncoder* encoder);

View File

@@ -0,0 +1,117 @@
#include "infrared_common_i.h"
#include "infrared_protocol_defs_i.h"
const InfraredCommonProtocolSpec protocol_nec = {
.timings =
{
.preamble_mark = INFRARED_NEC_PREAMBLE_MARK,
.preamble_space = INFRARED_NEC_PREAMBLE_SPACE,
.bit1_mark = INFRARED_NEC_BIT1_MARK,
.bit1_space = INFRARED_NEC_BIT1_SPACE,
.bit0_mark = INFRARED_NEC_BIT0_MARK,
.bit0_space = INFRARED_NEC_BIT0_SPACE,
.preamble_tolerance = INFRARED_NEC_PREAMBLE_TOLERANCE,
.bit_tolerance = INFRARED_NEC_BIT_TOLERANCE,
.silence_time = INFRARED_NEC_SILENCE,
.min_split_time = INFRARED_NEC_MIN_SPLIT_TIME,
},
.databit_len[0] = 42,
.databit_len[1] = 32,
.no_stop_bit = false,
.decode = infrared_common_decode_pdwm,
.encode = infrared_common_encode_pdwm,
.interpret = infrared_decoder_nec_interpret,
.decode_repeat = infrared_decoder_nec_decode_repeat,
.encode_repeat = infrared_encoder_nec_encode_repeat,
};
const InfraredCommonProtocolSpec protocol_samsung32 = {
.timings =
{
.preamble_mark = INFRARED_SAMSUNG_PREAMBLE_MARK,
.preamble_space = INFRARED_SAMSUNG_PREAMBLE_SPACE,
.bit1_mark = INFRARED_SAMSUNG_BIT1_MARK,
.bit1_space = INFRARED_SAMSUNG_BIT1_SPACE,
.bit0_mark = INFRARED_SAMSUNG_BIT0_MARK,
.bit0_space = INFRARED_SAMSUNG_BIT0_SPACE,
.preamble_tolerance = INFRARED_SAMSUNG_PREAMBLE_TOLERANCE,
.bit_tolerance = INFRARED_SAMSUNG_BIT_TOLERANCE,
.silence_time = INFRARED_SAMSUNG_SILENCE,
.min_split_time = INFRARED_SAMSUNG_MIN_SPLIT_TIME,
},
.databit_len[0] = 32,
.no_stop_bit = false,
.decode = infrared_common_decode_pdwm,
.encode = infrared_common_encode_pdwm,
.interpret = infrared_decoder_samsung32_interpret,
.decode_repeat = infrared_decoder_samsung32_decode_repeat,
.encode_repeat = infrared_encoder_samsung32_encode_repeat,
};
const InfraredCommonProtocolSpec protocol_rc6 = {
.timings =
{
.preamble_mark = INFRARED_RC6_PREAMBLE_MARK,
.preamble_space = INFRARED_RC6_PREAMBLE_SPACE,
.bit1_mark = INFRARED_RC6_BIT,
.preamble_tolerance = INFRARED_RC6_PREAMBLE_TOLERANCE,
.bit_tolerance = INFRARED_RC6_BIT_TOLERANCE,
.silence_time = INFRARED_RC6_SILENCE,
.min_split_time = INFRARED_RC6_MIN_SPLIT_TIME,
},
.databit_len[0] =
1 + 3 + 1 + 8 +
8, // start_bit + 3 mode bits, + 1 toggle bit (x2 timing) + 8 address + 8 command
.manchester_start_from_space = false,
.decode = infrared_decoder_rc6_decode_manchester,
.encode = infrared_encoder_rc6_encode_manchester,
.interpret = infrared_decoder_rc6_interpret,
.decode_repeat = NULL,
.encode_repeat = NULL,
};
const InfraredCommonProtocolSpec protocol_rc5 = {
.timings =
{
.preamble_mark = 0,
.preamble_space = 0,
.bit1_mark = INFRARED_RC5_BIT,
.preamble_tolerance = 0,
.bit_tolerance = INFRARED_RC5_BIT_TOLERANCE,
.silence_time = INFRARED_RC5_SILENCE,
.min_split_time = INFRARED_RC5_MIN_SPLIT_TIME,
},
.databit_len[0] = 1 + 1 + 1 + 5 +
6, // start_bit + start_bit/command_bit + toggle_bit + 5 address + 6 command
.manchester_start_from_space = true,
.decode = infrared_common_decode_manchester,
.encode = infrared_common_encode_manchester,
.interpret = infrared_decoder_rc5_interpret,
.decode_repeat = NULL,
.encode_repeat = NULL,
};
const InfraredCommonProtocolSpec protocol_sirc = {
.timings =
{
.preamble_mark = INFRARED_SIRC_PREAMBLE_MARK,
.preamble_space = INFRARED_SIRC_PREAMBLE_SPACE,
.bit1_mark = INFRARED_SIRC_BIT1_MARK,
.bit1_space = INFRARED_SIRC_BIT1_SPACE,
.bit0_mark = INFRARED_SIRC_BIT0_MARK,
.bit0_space = INFRARED_SIRC_BIT0_SPACE,
.preamble_tolerance = INFRARED_SIRC_PREAMBLE_TOLERANCE,
.bit_tolerance = INFRARED_SIRC_BIT_TOLERANCE,
.silence_time = INFRARED_SIRC_SILENCE,
.min_split_time = INFRARED_SIRC_MIN_SPLIT_TIME,
},
.databit_len[0] = 20,
.databit_len[1] = 15,
.databit_len[2] = 12,
.no_stop_bit = true,
.decode = infrared_common_decode_pdwm,
.encode = infrared_common_encode_pdwm,
.interpret = infrared_decoder_sirc_interpret,
.decode_repeat = NULL,
.encode_repeat = infrared_encoder_sirc_encode_repeat,
};

View File

@@ -0,0 +1,301 @@
#include "infrared.h"
#include "furi/check.h"
#include "common/infrared_common_i.h"
#include "infrared_protocol_defs_i.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <furi.h>
#include "infrared_i.h"
#include <furi_hal_infrared.h>
typedef struct {
InfraredAlloc alloc;
InfraredDecode decode;
InfraredDecoderReset reset;
InfraredFree free;
InfraredDecoderCheckReady check_ready;
} InfraredDecoders;
typedef struct {
InfraredAlloc alloc;
InfraredEncode encode;
InfraredEncoderReset reset;
InfraredFree free;
} InfraredEncoders;
struct InfraredDecoderHandler {
void** ctx;
};
struct InfraredEncoderHandler {
void* handler;
const InfraredEncoders* encoder;
};
typedef struct {
InfraredEncoders encoder;
InfraredDecoders decoder;
InfraredGetProtocolSpec get_protocol_spec;
} InfraredEncoderDecoder;
static const InfraredEncoderDecoder infrared_encoder_decoder[] = {
{
.decoder =
{.alloc = infrared_decoder_nec_alloc,
.decode = infrared_decoder_nec_decode,
.reset = infrared_decoder_nec_reset,
.check_ready = infrared_decoder_nec_check_ready,
.free = infrared_decoder_nec_free},
.encoder =
{.alloc = infrared_encoder_nec_alloc,
.encode = infrared_encoder_nec_encode,
.reset = infrared_encoder_nec_reset,
.free = infrared_encoder_nec_free},
.get_protocol_spec = infrared_nec_get_spec,
},
{
.decoder =
{.alloc = infrared_decoder_samsung32_alloc,
.decode = infrared_decoder_samsung32_decode,
.reset = infrared_decoder_samsung32_reset,
.check_ready = infrared_decoder_samsung32_check_ready,
.free = infrared_decoder_samsung32_free},
.encoder =
{.alloc = infrared_encoder_samsung32_alloc,
.encode = infrared_encoder_samsung32_encode,
.reset = infrared_encoder_samsung32_reset,
.free = infrared_encoder_samsung32_free},
.get_protocol_spec = infrared_samsung32_get_spec,
},
{
.decoder =
{.alloc = infrared_decoder_rc5_alloc,
.decode = infrared_decoder_rc5_decode,
.reset = infrared_decoder_rc5_reset,
.check_ready = infrared_decoder_rc5_check_ready,
.free = infrared_decoder_rc5_free},
.encoder =
{.alloc = infrared_encoder_rc5_alloc,
.encode = infrared_encoder_rc5_encode,
.reset = infrared_encoder_rc5_reset,
.free = infrared_encoder_rc5_free},
.get_protocol_spec = infrared_rc5_get_spec,
},
{
.decoder =
{.alloc = infrared_decoder_rc6_alloc,
.decode = infrared_decoder_rc6_decode,
.reset = infrared_decoder_rc6_reset,
.check_ready = infrared_decoder_rc6_check_ready,
.free = infrared_decoder_rc6_free},
.encoder =
{.alloc = infrared_encoder_rc6_alloc,
.encode = infrared_encoder_rc6_encode,
.reset = infrared_encoder_rc6_reset,
.free = infrared_encoder_rc6_free},
.get_protocol_spec = infrared_rc6_get_spec,
},
{
.decoder =
{.alloc = infrared_decoder_sirc_alloc,
.decode = infrared_decoder_sirc_decode,
.reset = infrared_decoder_sirc_reset,
.check_ready = infrared_decoder_sirc_check_ready,
.free = infrared_decoder_sirc_free},
.encoder =
{.alloc = infrared_encoder_sirc_alloc,
.encode = infrared_encoder_sirc_encode,
.reset = infrared_encoder_sirc_reset,
.free = infrared_encoder_sirc_free},
.get_protocol_spec = infrared_sirc_get_spec,
},
};
static int infrared_find_index_by_protocol(InfraredProtocol protocol);
static const InfraredProtocolSpecification*
infrared_get_spec_by_protocol(InfraredProtocol protocol);
const InfraredMessage*
infrared_decode(InfraredDecoderHandler* handler, bool level, uint32_t duration) {
furi_assert(handler);
InfraredMessage* message = NULL;
InfraredMessage* result = NULL;
for(int i = 0; i < COUNT_OF(infrared_encoder_decoder); ++i) {
if(infrared_encoder_decoder[i].decoder.decode) {
message = infrared_encoder_decoder[i].decoder.decode(handler->ctx[i], level, duration);
if(!result && message) {
result = message;
}
}
}
return result;
}
InfraredDecoderHandler* infrared_alloc_decoder(void) {
InfraredDecoderHandler* handler = malloc(sizeof(InfraredDecoderHandler));
handler->ctx = malloc(sizeof(void*) * COUNT_OF(infrared_encoder_decoder));
for(int i = 0; i < COUNT_OF(infrared_encoder_decoder); ++i) {
handler->ctx[i] = 0;
if(infrared_encoder_decoder[i].decoder.alloc)
handler->ctx[i] = infrared_encoder_decoder[i].decoder.alloc();
}
infrared_reset_decoder(handler);
return handler;
}
void infrared_free_decoder(InfraredDecoderHandler* handler) {
furi_assert(handler);
furi_assert(handler->ctx);
for(int i = 0; i < COUNT_OF(infrared_encoder_decoder); ++i) {
if(infrared_encoder_decoder[i].decoder.free)
infrared_encoder_decoder[i].decoder.free(handler->ctx[i]);
}
free(handler->ctx);
free(handler);
}
void infrared_reset_decoder(InfraredDecoderHandler* handler) {
for(int i = 0; i < COUNT_OF(infrared_encoder_decoder); ++i) {
if(infrared_encoder_decoder[i].decoder.reset)
infrared_encoder_decoder[i].decoder.reset(handler->ctx[i]);
}
}
const InfraredMessage* infrared_check_decoder_ready(InfraredDecoderHandler* handler) {
furi_assert(handler);
InfraredMessage* message = NULL;
InfraredMessage* result = NULL;
for(int i = 0; i < COUNT_OF(infrared_encoder_decoder); ++i) {
if(infrared_encoder_decoder[i].decoder.check_ready) {
message = infrared_encoder_decoder[i].decoder.check_ready(handler->ctx[i]);
if(!result && message) {
result = message;
}
}
}
return result;
}
InfraredEncoderHandler* infrared_alloc_encoder(void) {
InfraredEncoderHandler* handler = malloc(sizeof(InfraredEncoderHandler));
handler->handler = NULL;
handler->encoder = NULL;
return handler;
}
void infrared_free_encoder(InfraredEncoderHandler* handler) {
furi_assert(handler);
const InfraredEncoders* encoder = handler->encoder;
if(encoder || handler->handler) {
furi_assert(encoder);
furi_assert(handler->handler);
furi_assert(encoder->free);
encoder->free(handler->handler);
}
free(handler);
}
static int infrared_find_index_by_protocol(InfraredProtocol protocol) {
for(int i = 0; i < COUNT_OF(infrared_encoder_decoder); ++i) {
if(infrared_encoder_decoder[i].get_protocol_spec(protocol)) {
return i;
}
}
return -1;
}
void infrared_reset_encoder(InfraredEncoderHandler* handler, const InfraredMessage* message) {
furi_assert(handler);
furi_assert(message);
int index = infrared_find_index_by_protocol(message->protocol);
furi_check(index >= 0);
const InfraredEncoders* required_encoder = &infrared_encoder_decoder[index].encoder;
furi_assert(required_encoder);
furi_assert(required_encoder->reset);
furi_assert(required_encoder->alloc);
/* Realloc encoder if different protocol set */
if(required_encoder != handler->encoder) {
if(handler->handler != NULL) {
furi_assert(handler->encoder->free);
handler->encoder->free(handler->handler);
}
handler->encoder = required_encoder;
handler->handler = handler->encoder->alloc();
}
handler->encoder->reset(handler->handler, message);
}
InfraredStatus infrared_encode(InfraredEncoderHandler* handler, uint32_t* duration, bool* level) {
furi_assert(handler);
furi_assert(duration);
furi_assert(level);
const InfraredEncoders* encoder = handler->encoder;
furi_assert(encoder);
furi_assert(encoder->encode);
InfraredStatus status = encoder->encode(handler->handler, duration, level);
furi_assert(status != InfraredStatusError);
return status;
}
bool infrared_is_protocol_valid(InfraredProtocol protocol) {
return infrared_find_index_by_protocol(protocol) >= 0;
}
InfraredProtocol infrared_get_protocol_by_name(const char* protocol_name) {
for(InfraredProtocol protocol = 0; protocol < InfraredProtocolMAX; ++protocol) {
const char* name = infrared_get_protocol_name(protocol);
if(!strcmp(name, protocol_name)) return protocol;
}
return InfraredProtocolUnknown;
}
static const InfraredProtocolSpecification*
infrared_get_spec_by_protocol(InfraredProtocol protocol) {
int index = infrared_find_index_by_protocol(protocol);
const InfraredProtocolSpecification* spec = NULL;
if(index >= 0) {
spec = infrared_encoder_decoder[index].get_protocol_spec(protocol);
}
furi_assert(spec);
return spec;
}
const char* infrared_get_protocol_name(InfraredProtocol protocol) {
return infrared_get_spec_by_protocol(protocol)->name;
}
uint8_t infrared_get_protocol_address_length(InfraredProtocol protocol) {
return infrared_get_spec_by_protocol(protocol)->address_length;
}
uint8_t infrared_get_protocol_command_length(InfraredProtocol protocol) {
return infrared_get_spec_by_protocol(protocol)->command_length;
}
uint32_t infrared_get_protocol_frequency(InfraredProtocol protocol) {
return infrared_get_spec_by_protocol(protocol)->frequency;
}
float infrared_get_protocol_duty_cycle(InfraredProtocol protocol) {
return infrared_get_spec_by_protocol(protocol)->duty_cycle;
}

View File

@@ -0,0 +1,205 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define INFRARED_COMMON_CARRIER_FREQUENCY ((uint32_t)38000)
#define INFRARED_COMMON_DUTY_CYCLE ((float)0.33)
/* if we want to see splitted raw signals during brutforce,
* we have to have RX raw timing delay less than TX */
#define INFRARED_RAW_RX_TIMING_DELAY_US 150000
#define INFRARED_RAW_TX_TIMING_DELAY_US 180000
typedef struct InfraredDecoderHandler InfraredDecoderHandler;
typedef struct InfraredEncoderHandler InfraredEncoderHandler;
typedef enum {
InfraredProtocolUnknown = -1,
InfraredProtocolNEC = 0,
InfraredProtocolNECext,
InfraredProtocolNEC42,
InfraredProtocolNEC42ext,
InfraredProtocolSamsung32,
InfraredProtocolRC6,
InfraredProtocolRC5,
InfraredProtocolRC5X,
InfraredProtocolSIRC,
InfraredProtocolSIRC15,
InfraredProtocolSIRC20,
InfraredProtocolMAX,
} InfraredProtocol;
typedef struct {
InfraredProtocol protocol;
uint32_t address;
uint32_t command;
bool repeat;
} InfraredMessage;
typedef enum {
InfraredStatusError,
InfraredStatusOk,
InfraredStatusDone,
InfraredStatusReady,
} InfraredStatus;
/**
* Initialize decoder.
*
* \return returns pointer to INFRARED decoder handler if success, otherwise - error.
*/
InfraredDecoderHandler* infrared_alloc_decoder(void);
/**
* Provide to decoder next timing.
*
* \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_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.
* Note: ownership of returned ptr belongs to handler. So pointer is valid
* up to next infrared_free_decoder(), infrared_reset_decoder(),
* infrared_decode(), infrared_check_decoder_ready() calls.
*/
const InfraredMessage*
infrared_decode(InfraredDecoderHandler* handler, bool level, uint32_t duration);
/**
* Check whether decoder is ready.
* Functionality is quite similar to infrared_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 infrared_check_decoder_ready() after some timeout to
* retrieve decoded message, if so.
*
* \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_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 infrared_free_decoder(), infrared_reset_decoder(),
* infrared_decode(), infrared_check_decoder_ready() calls.
*/
const InfraredMessage* infrared_check_decoder_ready(InfraredDecoderHandler* handler);
/**
* Deinitialize decoder and free allocated memory.
*
* \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_alloc_decoder().
*/
void infrared_free_decoder(InfraredDecoderHandler* handler);
/**
* Reset INFRARED decoder.
*
* \param[in] handler - handler to INFRARED decoders. Should be acquired with \c infrared_alloc_decoder().
*/
void infrared_reset_decoder(InfraredDecoderHandler* handler);
/**
* Get protocol name by protocol enum.
*
* \param[in] protocol - protocol identifier.
* \return string to protocol name.
*/
const char* infrared_get_protocol_name(InfraredProtocol protocol);
/**
* Get protocol enum by protocol name.
*
* \param[in] protocol_name - string to protocol name.
* \return protocol identifier.
*/
InfraredProtocol infrared_get_protocol_by_name(const char* protocol_name);
/**
* Get address length by protocol enum.
*
* \param[in] protocol - protocol identifier.
* \return length of address in bits.
*/
uint8_t infrared_get_protocol_address_length(InfraredProtocol protocol);
/**
* Get command length by protocol enum.
*
* \param[in] protocol - protocol identifier.
* \return length of command in bits.
*/
uint8_t infrared_get_protocol_command_length(InfraredProtocol protocol);
/**
* Checks whether protocol valid.
*
* \param[in] protocol - protocol identifier.
* \return true if protocol is valid, false otherwise.
*/
bool infrared_is_protocol_valid(InfraredProtocol protocol);
/**
* Allocate INFRARED encoder.
*
* \return encoder handler.
*/
InfraredEncoderHandler* infrared_alloc_encoder(void);
/**
* Free encoder handler previously allocated with \c infrared_alloc_encoder().
*
* \param[in] handler - handler to INFRARED encoder. Should be acquired with \c infrared_alloc_encoder().
*/
void infrared_free_encoder(InfraredEncoderHandler* handler);
/**
* Encode previously set INFRARED message.
* Usage:
* 1) alloc with \c infrared_alloc_encoder()
* 2) set message to encode with \c infrared_reset_encoder()
* 3) call for \c infrared_encode() to continuously get one at a time timings.
* 4) when \c infrared_encode() returns InfraredStatusDone, it means new message is fully encoded.
* 5) to encode additional timings, just continue calling \c infrared_encode().
*
* \param[in] handler - handler to INFRARED encoder. Should be acquired with \c infrared_alloc_encoder().
* \param[out] duration - encoded timing.
* \param[out] level - encoded level.
*
* \return status of encode operation.
*/
InfraredStatus infrared_encode(InfraredEncoderHandler* handler, uint32_t* duration, bool* level);
/**
* Reset INFRARED encoder and set new message to encode. If it's not called after receiveing
* InfraredStatusDone in \c infrared_encode(), encoder will encode repeat messages
* till the end of time.
*
* \param[in] handler - handler to INFRARED encoder. Should be acquired with \c infrared_alloc_encoder().
* \param[in] message - message to encode.
*/
void infrared_reset_encoder(InfraredEncoderHandler* handler, const InfraredMessage* message);
/**
* Get PWM frequency value for selected protocol
*
* \param[in] protocol - protocol to get from PWM frequency
*
* \return frequency
*/
uint32_t infrared_get_protocol_frequency(InfraredProtocol protocol);
/**
* Get PWM duty cycle value for selected protocol
*
* \param[in] protocol - protocol to get from PWM duty cycle
*
* \return duty cycle
*/
float infrared_get_protocol_duty_cycle(InfraredProtocol protocol);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,46 @@
#pragma once
#include "infrared.h"
#include <stddef.h>
#include <stdint.h>
typedef struct {
uint32_t min_split_time;
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;
uint32_t preamble_tolerance;
uint32_t bit_tolerance;
} InfraredTimings;
typedef struct {
const char* name;
uint8_t address_length;
uint8_t command_length;
uint32_t frequency;
float duty_cycle;
} InfraredProtocolSpecification;
typedef const InfraredProtocolSpecification* (*InfraredGetProtocolSpec)(InfraredProtocol protocol);
typedef void* (*InfraredAlloc)(void);
typedef void (*InfraredFree)(void*);
typedef void (*InfraredDecoderReset)(void*);
typedef InfraredMessage* (*InfraredDecode)(void* ctx, bool level, uint32_t duration);
typedef InfraredMessage* (*InfraredDecoderCheckReady)(void*);
typedef void (*InfraredEncoderReset)(void* encoder, const InfraredMessage* message);
typedef InfraredStatus (*InfraredEncode)(void* encoder, uint32_t* out, bool* polarity);
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;
}

View File

@@ -0,0 +1,269 @@
#pragma once
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include "infrared.h"
#include "common/infrared_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 up to period repeat repeat bit
* mark space
*
* 9000 4500 32 bit + stop bit ...110000 9000 2250
* __________ _ _ _ _ _ _ _ _ _ _ _ _ _ ___________ _
* ____ __________ _ _ _ __ __ __ _ _ __ __ _ _ ________________ ____________ ___
*
***************************************************************************************************/
#define INFRARED_NEC_PREAMBLE_MARK 9000
#define INFRARED_NEC_PREAMBLE_SPACE 4500
#define INFRARED_NEC_BIT1_MARK 560
#define INFRARED_NEC_BIT1_SPACE 1690
#define INFRARED_NEC_BIT0_MARK 560
#define INFRARED_NEC_BIT0_SPACE 560
#define INFRARED_NEC_REPEAT_PERIOD 110000
#define INFRARED_NEC_SILENCE INFRARED_NEC_REPEAT_PERIOD
#define INFRARED_NEC_MIN_SPLIT_TIME INFRARED_NEC_REPEAT_PAUSE_MIN
#define INFRARED_NEC_REPEAT_PAUSE_MIN 4000
#define INFRARED_NEC_REPEAT_PAUSE_MAX 150000
#define INFRARED_NEC_REPEAT_MARK 9000
#define INFRARED_NEC_REPEAT_SPACE 2250
#define INFRARED_NEC_PREAMBLE_TOLERANCE 200 // us
#define INFRARED_NEC_BIT_TOLERANCE 120 // us
void* infrared_decoder_nec_alloc(void);
void infrared_decoder_nec_reset(void* decoder);
void infrared_decoder_nec_free(void* decoder);
InfraredMessage* infrared_decoder_nec_check_ready(void* decoder);
InfraredMessage* infrared_decoder_nec_decode(void* decoder, bool level, uint32_t duration);
void* infrared_encoder_nec_alloc(void);
InfraredStatus infrared_encoder_nec_encode(void* encoder_ptr, uint32_t* duration, bool* level);
void infrared_encoder_nec_reset(void* encoder_ptr, const InfraredMessage* message);
void infrared_encoder_nec_free(void* encoder_ptr);
bool infrared_decoder_nec_interpret(InfraredCommonDecoder* decoder);
InfraredStatus infrared_decoder_nec_decode_repeat(InfraredCommonDecoder* decoder);
InfraredStatus infrared_encoder_nec_encode_repeat(
InfraredCommonEncoder* encoder,
uint32_t* duration,
bool* level);
const InfraredProtocolSpecification* infrared_nec_get_spec(InfraredProtocol protocol);
extern const InfraredCommonProtocolSpec 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 INFRARED_SAMSUNG_PREAMBLE_MARK 4500
#define INFRARED_SAMSUNG_PREAMBLE_SPACE 4500
#define INFRARED_SAMSUNG_BIT1_MARK 550
#define INFRARED_SAMSUNG_BIT1_SPACE 1650
#define INFRARED_SAMSUNG_BIT0_MARK 550
#define INFRARED_SAMSUNG_BIT0_SPACE 550
#define INFRARED_SAMSUNG_REPEAT_PAUSE_MIN 30000
#define INFRARED_SAMSUNG_REPEAT_PAUSE1 46000
#define INFRARED_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 INFRARED_SAMSUNG_MIN_SPLIT_TIME 5000
#define INFRARED_SAMSUNG_SILENCE 145000
#define INFRARED_SAMSUNG_REPEAT_PAUSE_MAX 140000
#define INFRARED_SAMSUNG_REPEAT_MARK 4500
#define INFRARED_SAMSUNG_REPEAT_SPACE 4500
#define INFRARED_SAMSUNG_PREAMBLE_TOLERANCE 200 // us
#define INFRARED_SAMSUNG_BIT_TOLERANCE 120 // us
void* infrared_decoder_samsung32_alloc(void);
void infrared_decoder_samsung32_reset(void* decoder);
void infrared_decoder_samsung32_free(void* decoder);
InfraredMessage* infrared_decoder_samsung32_check_ready(void* ctx);
InfraredMessage* infrared_decoder_samsung32_decode(void* decoder, bool level, uint32_t duration);
InfraredStatus
infrared_encoder_samsung32_encode(void* encoder_ptr, uint32_t* duration, bool* level);
void infrared_encoder_samsung32_reset(void* encoder_ptr, const InfraredMessage* message);
void* infrared_encoder_samsung32_alloc(void);
void infrared_encoder_samsung32_free(void* encoder_ptr);
bool infrared_decoder_samsung32_interpret(InfraredCommonDecoder* decoder);
InfraredStatus infrared_decoder_samsung32_decode_repeat(InfraredCommonDecoder* decoder);
InfraredStatus infrared_encoder_samsung32_encode_repeat(
InfraredCommonEncoder* encoder,
uint32_t* duration,
bool* level);
const InfraredProtocolSpecification* infrared_samsung32_get_spec(InfraredProtocol protocol);
extern const InfraredCommonProtocolSpec 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 INFRARED_RC6_CARRIER_FREQUENCY 36000
#define INFRARED_RC6_DUTY_CYCLE 0.33
#define INFRARED_RC6_PREAMBLE_MARK 2666
#define INFRARED_RC6_PREAMBLE_SPACE 889
#define INFRARED_RC6_BIT 444 // half of time-quant for 1 bit
#define INFRARED_RC6_PREAMBLE_TOLERANCE 200 // us
#define INFRARED_RC6_BIT_TOLERANCE 120 // us
/* protocol allows 2700 silence, but it is hard to send 1 message without repeat */
#define INFRARED_RC6_SILENCE (2700 * 10)
#define INFRARED_RC6_MIN_SPLIT_TIME 2700
void* infrared_decoder_rc6_alloc(void);
void infrared_decoder_rc6_reset(void* decoder);
void infrared_decoder_rc6_free(void* decoder);
InfraredMessage* infrared_decoder_rc6_check_ready(void* ctx);
InfraredMessage* infrared_decoder_rc6_decode(void* decoder, bool level, uint32_t duration);
void* infrared_encoder_rc6_alloc(void);
void infrared_encoder_rc6_reset(void* encoder_ptr, const InfraredMessage* message);
void infrared_encoder_rc6_free(void* decoder);
InfraredStatus infrared_encoder_rc6_encode(void* encoder_ptr, uint32_t* duration, bool* polarity);
bool infrared_decoder_rc6_interpret(InfraredCommonDecoder* decoder);
InfraredStatus infrared_decoder_rc6_decode_manchester(
InfraredCommonDecoder* decoder,
bool level,
uint32_t timing);
InfraredStatus infrared_encoder_rc6_encode_manchester(
InfraredCommonEncoder* encoder_ptr,
uint32_t* duration,
bool* polarity);
const InfraredProtocolSpecification* infrared_rc6_get_spec(InfraredProtocol protocol);
extern const InfraredCommonProtocolSpec protocol_rc6;
/***************************************************************************************************
* RC5 protocol description
* https://www.mikrocontroller.net/articles/IRMP_-_english#RC5_.2B_RC5X
****************************************************************************************************
* Manchester/biphase
* Modulation
*
* 888/1776 - bit (x2 for toggle bit)
*
* __ ____ __ __ __ __ __ __ __ __
* __ __ ____ __ __ __ __ __ __ __ _
* | 1 | 1 | 0 | ... | ... |
* s si T address (MSB) command (MSB)
*
* Note: manchester starts from space timing, so it have to be handled properly
* 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 - 5 bit
* command - 6/7 bit
***************************************************************************************************/
#define INFRARED_RC5_CARRIER_FREQUENCY 36000
#define INFRARED_RC5_DUTY_CYCLE 0.33
#define INFRARED_RC5_PREAMBLE_MARK 0
#define INFRARED_RC5_PREAMBLE_SPACE 0
#define INFRARED_RC5_BIT 888 // half of time-quant for 1 bit
#define INFRARED_RC5_PREAMBLE_TOLERANCE 200 // us
#define INFRARED_RC5_BIT_TOLERANCE 120 // us
/* protocol allows 2700 silence, but it is hard to send 1 message without repeat */
#define INFRARED_RC5_SILENCE (2700 * 10)
#define INFRARED_RC5_MIN_SPLIT_TIME 2700
void* infrared_decoder_rc5_alloc(void);
void infrared_decoder_rc5_reset(void* decoder);
void infrared_decoder_rc5_free(void* decoder);
InfraredMessage* infrared_decoder_rc5_check_ready(void* ctx);
InfraredMessage* infrared_decoder_rc5_decode(void* decoder, bool level, uint32_t duration);
void* infrared_encoder_rc5_alloc(void);
void infrared_encoder_rc5_reset(void* encoder_ptr, const InfraredMessage* message);
void infrared_encoder_rc5_free(void* decoder);
InfraredStatus infrared_encoder_rc5_encode(void* encoder_ptr, uint32_t* duration, bool* polarity);
bool infrared_decoder_rc5_interpret(InfraredCommonDecoder* decoder);
const InfraredProtocolSpecification* infrared_rc5_get_spec(InfraredProtocol protocol);
extern const InfraredCommonProtocolSpec 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 INFRARED_SIRC_CARRIER_FREQUENCY 40000
#define INFRARED_SIRC_DUTY_CYCLE 0.33
#define INFRARED_SIRC_PREAMBLE_MARK 2400
#define INFRARED_SIRC_PREAMBLE_SPACE 600
#define INFRARED_SIRC_BIT1_MARK 1200
#define INFRARED_SIRC_BIT1_SPACE 600
#define INFRARED_SIRC_BIT0_MARK 600
#define INFRARED_SIRC_BIT0_SPACE 600
#define INFRARED_SIRC_PREAMBLE_TOLERANCE 200 // us
#define INFRARED_SIRC_BIT_TOLERANCE 120 // us
#define INFRARED_SIRC_SILENCE 10000
#define INFRARED_SIRC_MIN_SPLIT_TIME (INFRARED_SIRC_SILENCE - 1000)
#define INFRARED_SIRC_REPEAT_PERIOD 45000
void* infrared_decoder_sirc_alloc(void);
void infrared_decoder_sirc_reset(void* decoder);
InfraredMessage* infrared_decoder_sirc_check_ready(void* decoder);
uint32_t infrared_decoder_sirc_get_timeout(void* decoder);
void infrared_decoder_sirc_free(void* decoder);
InfraredMessage* infrared_decoder_sirc_decode(void* decoder, bool level, uint32_t duration);
void* infrared_encoder_sirc_alloc(void);
void infrared_encoder_sirc_reset(void* encoder_ptr, const InfraredMessage* message);
void infrared_encoder_sirc_free(void* decoder);
InfraredStatus infrared_encoder_sirc_encode(void* encoder_ptr, uint32_t* duration, bool* polarity);
bool infrared_decoder_sirc_interpret(InfraredCommonDecoder* decoder);
const InfraredProtocolSpecification* infrared_sirc_get_spec(InfraredProtocol protocol);
InfraredStatus infrared_encoder_sirc_encode_repeat(
InfraredCommonEncoder* encoder,
uint32_t* duration,
bool* level);
extern const InfraredCommonProtocolSpec protocol_sirc;

View File

@@ -0,0 +1,100 @@
#include "common/infrared_common_i.h"
#include "infrared.h"
#include "infrared_protocol_defs_i.h"
#include <stdbool.h>
#include <stdint.h>
#include <furi.h>
#include "../infrared_i.h"
InfraredMessage* infrared_decoder_nec_check_ready(void* ctx) {
return infrared_common_decoder_check_ready(ctx);
}
bool infrared_decoder_nec_interpret(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
if(decoder->databit_cnt == 32) {
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.protocol = InfraredProtocolNEC;
decoder->message.address = address;
decoder->message.command = command;
decoder->message.repeat = false;
result = true;
} else {
decoder->message.protocol = InfraredProtocolNECext;
decoder->message.address = decoder->data[0] | (decoder->data[1] << 8);
decoder->message.command = decoder->data[2] | (decoder->data[3] << 8);
decoder->message.repeat = false;
result = true;
}
} else if(decoder->databit_cnt == 42) {
uint32_t* data1 = (void*)decoder->data;
uint16_t* data2 = (void*)(data1 + 1);
uint16_t address = *data1 & 0x1FFF;
uint16_t address_inverse = (*data1 >> 13) & 0x1FFF;
uint16_t command = ((*data1 >> 26) & 0x3F) | ((*data2 & 0x3) << 6);
uint16_t command_inverse = (*data2 >> 2) & 0xFF;
if((address == (~address_inverse & 0x1FFF)) && (command == (~command_inverse & 0xFF))) {
decoder->message.protocol = InfraredProtocolNEC42;
decoder->message.address = address;
decoder->message.command = command;
decoder->message.repeat = false;
result = true;
} else {
decoder->message.protocol = InfraredProtocolNEC42ext;
decoder->message.address = address | (address_inverse << 13);
decoder->message.command = command | (command_inverse << 8);
decoder->message.repeat = false;
result = true;
}
}
return result;
}
// timings start from Space (delay between message and repeat)
InfraredStatus infrared_decoder_nec_decode_repeat(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
InfraredStatus status = InfraredStatusError;
if(decoder->timings_cnt < 4) return InfraredStatusOk;
if((decoder->timings[0] > INFRARED_NEC_REPEAT_PAUSE_MIN) &&
(decoder->timings[0] < INFRARED_NEC_REPEAT_PAUSE_MAX) &&
MATCH_TIMING(decoder->timings[1], INFRARED_NEC_REPEAT_MARK, preamble_tolerance) &&
MATCH_TIMING(decoder->timings[2], INFRARED_NEC_REPEAT_SPACE, preamble_tolerance) &&
MATCH_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)) {
status = InfraredStatusReady;
decoder->timings_cnt = 0;
} else {
status = InfraredStatusError;
}
return status;
}
void* infrared_decoder_nec_alloc(void) {
return infrared_common_decoder_alloc(&protocol_nec);
}
InfraredMessage* infrared_decoder_nec_decode(void* decoder, bool level, uint32_t duration) {
return infrared_common_decode(decoder, level, duration);
}
void infrared_decoder_nec_free(void* decoder) {
infrared_common_decoder_free(decoder);
}
void infrared_decoder_nec_reset(void* decoder) {
infrared_common_decoder_reset(decoder);
}

View File

@@ -0,0 +1,93 @@
#include "furi/check.h"
#include "infrared.h"
#include "common/infrared_common_i.h"
#include <stdint.h>
#include "../infrared_i.h"
#include "infrared_protocol_defs_i.h"
#include <furi.h>
static const uint32_t repeat_timings[] = {
INFRARED_NEC_REPEAT_PERIOD - INFRARED_NEC_REPEAT_MARK - INFRARED_NEC_REPEAT_SPACE -
INFRARED_NEC_BIT1_MARK,
INFRARED_NEC_REPEAT_MARK,
INFRARED_NEC_REPEAT_SPACE,
INFRARED_NEC_BIT1_MARK,
};
void infrared_encoder_nec_reset(void* encoder_ptr, const InfraredMessage* message) {
furi_assert(encoder_ptr);
furi_assert(message);
InfraredCommonEncoder* encoder = encoder_ptr;
infrared_common_encoder_reset(encoder);
uint32_t* data1 = (void*)encoder->data;
uint32_t* data2 = data1 + 1;
if(message->protocol == InfraredProtocolNEC) {
uint8_t address = message->address;
uint8_t address_inverse = ~address;
uint8_t command = message->command;
uint8_t command_inverse = ~command;
*data1 = address;
*data1 |= address_inverse << 8;
*data1 |= command << 16;
*data1 |= command_inverse << 24;
encoder->bits_to_encode = 32;
} else if(message->protocol == InfraredProtocolNECext) {
*data1 = (uint16_t)message->address;
*data1 |= (message->command & 0xFFFF) << 16;
encoder->bits_to_encode = 32;
} else if(message->protocol == InfraredProtocolNEC42) {
/* 13 address + 13 inverse address + 8 command + 8 inv command */
*data1 = message->address & 0x1FFFUL;
*data1 |= (~message->address & 0x1FFFUL) << 13;
*data1 |= ((message->command & 0x3FUL) << 26);
*data2 = (message->command & 0xC0UL) >> 6;
*data2 |= (~message->command & 0xFFUL) << 2;
encoder->bits_to_encode = 42;
} else if(message->protocol == InfraredProtocolNEC42ext) {
*data1 = message->address & 0x3FFFFFF;
*data1 |= ((message->command & 0x3F) << 26);
*data2 = (message->command & 0xFFC0) >> 6;
encoder->bits_to_encode = 42;
} else {
furi_assert(0);
}
}
InfraredStatus infrared_encoder_nec_encode_repeat(
InfraredCommonEncoder* 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->bits_to_encode * 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 = INFRARED_NEC_REPEAT_PERIOD - encoder->timings_sum;
}
*level = repeat_cnt % 2;
++encoder->timings_encoded;
bool done = (!((repeat_cnt + 1) % COUNT_OF(repeat_timings)));
return done ? InfraredStatusDone : InfraredStatusOk;
}
void* infrared_encoder_nec_alloc(void) {
return infrared_common_encoder_alloc(&protocol_nec);
}
void infrared_encoder_nec_free(void* encoder_ptr) {
infrared_common_encoder_free(encoder_ptr);
}
InfraredStatus infrared_encoder_nec_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
return infrared_common_encode(encoder_ptr, duration, level);
}

View File

@@ -0,0 +1,47 @@
#include "../infrared_i.h"
#include "infrared_protocol_defs_i.h"
static const InfraredProtocolSpecification infrared_nec_protocol_specification = {
.name = "NEC",
.address_length = 8,
.command_length = 8,
.frequency = INFRARED_COMMON_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_COMMON_DUTY_CYCLE,
};
static const InfraredProtocolSpecification infrared_necext_protocol_specification = {
.name = "NECext",
.address_length = 16,
.command_length = 16,
.frequency = INFRARED_COMMON_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_COMMON_DUTY_CYCLE,
};
static const InfraredProtocolSpecification infrared_nec42_protocol_specification = {
.name = "NEC42",
.address_length = 13,
.command_length = 8,
.frequency = INFRARED_COMMON_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_COMMON_DUTY_CYCLE,
};
static const InfraredProtocolSpecification infrared_nec42ext_protocol_specification = {
.name = "NEC42ext",
.address_length = 26,
.command_length = 16,
.frequency = INFRARED_COMMON_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_COMMON_DUTY_CYCLE,
};
const InfraredProtocolSpecification* infrared_nec_get_spec(InfraredProtocol protocol) {
if(protocol == InfraredProtocolNEC)
return &infrared_nec_protocol_specification;
else if(protocol == InfraredProtocolNECext)
return &infrared_necext_protocol_specification;
else if(protocol == InfraredProtocolNEC42)
return &infrared_nec42_protocol_specification;
else if(protocol == InfraredProtocolNEC42ext)
return &infrared_nec42ext_protocol_specification;
else
return NULL;
}

View File

@@ -0,0 +1,82 @@
#include "infrared.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <furi.h>
#include "../infrared_i.h"
#include "../infrared_protocol_defs_i.h"
typedef struct {
InfraredCommonDecoder* common_decoder;
bool toggle;
} InfraredRc5Decoder;
InfraredMessage* infrared_decoder_rc5_check_ready(void* ctx) {
InfraredRc5Decoder* decoder = ctx;
return infrared_common_decoder_check_ready(decoder->common_decoder);
}
bool infrared_decoder_rc5_interpret(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
uint32_t* data = (void*)&decoder->data[0];
/* Manchester (inverse):
* 0->1 : 1
* 1->0 : 0
*/
decoder->data[0] = ~decoder->data[0];
decoder->data[1] = ~decoder->data[1];
// MSB first
uint8_t address = reverse((uint8_t)decoder->data[0]) & 0x1F;
uint8_t command = (reverse((uint8_t)decoder->data[1]) >> 2) & 0x3F;
bool start_bit1 = *data & 0x01;
bool start_bit2 = *data & 0x02;
bool toggle = !!(*data & 0x04);
if(start_bit1 == 1) {
InfraredProtocol protocol = start_bit2 ? InfraredProtocolRC5 : InfraredProtocolRC5X;
InfraredMessage* message = &decoder->message;
InfraredRc5Decoder* rc5_decoder = decoder->context;
bool* prev_toggle = &rc5_decoder->toggle;
if((message->address == address) && (message->command == command) &&
(message->protocol == protocol)) {
message->repeat = (toggle == *prev_toggle);
} else {
message->repeat = false;
}
*prev_toggle = toggle;
message->command = command;
message->address = address;
message->protocol = protocol;
result = true;
}
return result;
}
void* infrared_decoder_rc5_alloc(void) {
InfraredRc5Decoder* decoder = malloc(sizeof(InfraredRc5Decoder));
decoder->toggle = false;
decoder->common_decoder = infrared_common_decoder_alloc(&protocol_rc5);
decoder->common_decoder->context = decoder;
return decoder;
}
InfraredMessage* infrared_decoder_rc5_decode(void* decoder, bool level, uint32_t duration) {
InfraredRc5Decoder* decoder_rc5 = decoder;
return infrared_common_decode(decoder_rc5->common_decoder, level, duration);
}
void infrared_decoder_rc5_free(void* decoder) {
InfraredRc5Decoder* decoder_rc5 = decoder;
infrared_common_decoder_free(decoder_rc5->common_decoder);
free(decoder_rc5);
}
void infrared_decoder_rc5_reset(void* decoder) {
InfraredRc5Decoder* decoder_rc5 = decoder;
infrared_common_decoder_reset(decoder_rc5->common_decoder);
}

View File

@@ -0,0 +1,55 @@
#include "furi/memmgr.h"
#include "infrared.h"
#include "common/infrared_common_i.h"
#include "infrared_protocol_defs_i.h"
#include <stdint.h>
#include "../infrared_i.h"
typedef struct InfraredEncoderRC5 {
InfraredCommonEncoder* common_encoder;
bool toggle_bit;
} InfraredEncoderRC5;
void infrared_encoder_rc5_reset(void* encoder_ptr, const InfraredMessage* message) {
furi_assert(encoder_ptr);
InfraredEncoderRC5* encoder = encoder_ptr;
InfraredCommonEncoder* common_encoder = encoder->common_encoder;
infrared_common_encoder_reset(common_encoder);
uint32_t* data = (void*)common_encoder->data;
/* RC5 */
*data |= 0x01; // start bit
if(message->protocol == InfraredProtocolRC5) {
*data |= 0x02; // start bit
}
*data |= encoder->toggle_bit ? 0x04 : 0;
*data |= (reverse(message->address) >> 3) << 3; /* address 5 bit */
*data |= (reverse(message->command) >> 2) << 8; /* command 6 bit */
common_encoder->data[0] = ~common_encoder->data[0];
common_encoder->data[1] = ~common_encoder->data[1];
common_encoder->bits_to_encode = common_encoder->protocol->databit_len[0];
encoder->toggle_bit ^= 1;
}
InfraredStatus infrared_encoder_rc5_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
InfraredEncoderRC5* encoder = encoder_ptr;
return infrared_common_encode(encoder->common_encoder, duration, level);
}
void* infrared_encoder_rc5_alloc(void) {
InfraredEncoderRC5* encoder = malloc(sizeof(InfraredEncoderRC5));
encoder->common_encoder = infrared_common_encoder_alloc(&protocol_rc5);
encoder->toggle_bit = false;
return encoder;
}
void infrared_encoder_rc5_free(void* encoder_ptr) {
furi_assert(encoder_ptr);
InfraredEncoderRC5* encoder = encoder_ptr;
free(encoder->common_encoder);
free(encoder);
}

View File

@@ -0,0 +1,27 @@
#include "../infrared_i.h"
#include "infrared_protocol_defs_i.h"
static const InfraredProtocolSpecification infrared_rc5_protocol_specification = {
.name = "RC5",
.address_length = 5,
.command_length = 6,
.frequency = INFRARED_RC5_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_RC5_DUTY_CYCLE,
};
static const InfraredProtocolSpecification infrared_rc5x_protocol_specification = {
.name = "RC5X",
.address_length = 5,
.command_length = 7,
.frequency = INFRARED_RC5_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_RC5_DUTY_CYCLE,
};
const InfraredProtocolSpecification* infrared_rc5_get_spec(InfraredProtocol protocol) {
if(protocol == InfraredProtocolRC5)
return &infrared_rc5_protocol_specification;
else if(protocol == InfraredProtocolRC5X)
return &infrared_rc5x_protocol_specification;
else
return NULL;
}

View File

@@ -0,0 +1,115 @@
#include "infrared.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <furi.h>
#include "../infrared_i.h"
#include "../infrared_protocol_defs_i.h"
typedef struct {
InfraredCommonDecoder* common_decoder;
bool toggle;
} InfraredRc6Decoder;
InfraredMessage* infrared_decoder_rc6_check_ready(void* ctx) {
InfraredRc6Decoder* decoder_rc6 = ctx;
return infrared_common_decoder_check_ready(decoder_rc6->common_decoder);
}
bool infrared_decoder_rc6_interpret(InfraredCommonDecoder* 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)) {
InfraredMessage* message = &decoder->message;
InfraredRc6Decoder* rc6_decoder = decoder->context;
bool* prev_toggle = &rc6_decoder->toggle;
if((message->address == address) && (message->command == command) &&
(message->protocol == InfraredProtocolRC6)) {
message->repeat = (toggle == *prev_toggle);
} else {
message->repeat = false;
}
*prev_toggle = toggle;
message->command = command;
message->address = address;
message->protocol = InfraredProtocolRC6;
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.
*/
InfraredStatus infrared_decoder_rc6_decode_manchester(
InfraredCommonDecoder* decoder,
bool level,
uint32_t timing) {
// 4th bit lasts 2x times more
InfraredStatus status = InfraredStatusError;
uint16_t bit = decoder->protocol->timings.bit1_mark;
uint16_t tolerance = decoder->protocol->timings.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->switch_detect == true);
if(single_timing ^ triple_timing) {
++decoder->databit_cnt;
decoder->data[0] |= (single_timing ? !level : level) << 4;
status = InfraredStatusOk;
}
} else if(decoder->databit_cnt == 5) {
if(single_timing || triple_timing) {
if(triple_timing) timing = bit;
decoder->switch_detect = false;
status = infrared_common_decode_manchester(decoder, level, timing);
} else if(double_timing) {
status = InfraredStatusOk;
}
} else {
status = infrared_common_decode_manchester(decoder, level, timing);
}
return status;
}
void* infrared_decoder_rc6_alloc(void) {
InfraredRc6Decoder* decoder = malloc(sizeof(InfraredRc6Decoder));
decoder->toggle = false;
decoder->common_decoder = infrared_common_decoder_alloc(&protocol_rc6);
decoder->common_decoder->context = decoder;
return decoder;
}
InfraredMessage* infrared_decoder_rc6_decode(void* decoder, bool level, uint32_t duration) {
InfraredRc6Decoder* decoder_rc6 = decoder;
return infrared_common_decode(decoder_rc6->common_decoder, level, duration);
}
void infrared_decoder_rc6_free(void* decoder) {
InfraredRc6Decoder* decoder_rc6 = decoder;
infrared_common_decoder_free(decoder_rc6->common_decoder);
free(decoder_rc6);
}
void infrared_decoder_rc6_reset(void* decoder) {
InfraredRc6Decoder* decoder_rc6 = decoder;
infrared_common_decoder_reset(decoder_rc6->common_decoder);
}

View File

@@ -0,0 +1,61 @@
#include "furi/memmgr.h"
#include "infrared.h"
#include "common/infrared_common_i.h"
#include "infrared_protocol_defs_i.h"
#include <stdint.h>
#include "../infrared_i.h"
typedef struct InfraredEncoderRC6 {
InfraredCommonEncoder* common_encoder;
bool toggle_bit;
} InfraredEncoderRC6;
void infrared_encoder_rc6_reset(void* encoder_ptr, const InfraredMessage* message) {
furi_assert(encoder_ptr);
InfraredEncoderRC6* encoder = encoder_ptr;
InfraredCommonEncoder* common_encoder = encoder->common_encoder;
infrared_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;
common_encoder->bits_to_encode = common_encoder->protocol->databit_len[0];
encoder->toggle_bit ^= 1;
}
InfraredStatus infrared_encoder_rc6_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
InfraredEncoderRC6* encoder = encoder_ptr;
return infrared_common_encode(encoder->common_encoder, duration, level);
}
void* infrared_encoder_rc6_alloc(void) {
InfraredEncoderRC6* encoder = malloc(sizeof(InfraredEncoderRC6));
encoder->common_encoder = infrared_common_encoder_alloc(&protocol_rc6);
encoder->toggle_bit = false;
return encoder;
}
void infrared_encoder_rc6_free(void* encoder_ptr) {
furi_assert(encoder_ptr);
InfraredEncoderRC6* encoder = encoder_ptr;
free(encoder->common_encoder);
free(encoder);
}
InfraredStatus infrared_encoder_rc6_encode_manchester(
InfraredCommonEncoder* common_encoder,
uint32_t* duration,
bool* polarity) {
InfraredStatus status = InfraredStatusError;
bool toggle_bit = (common_encoder->bits_encoded == 4);
status = infrared_common_encode_manchester(common_encoder, duration, polarity);
if(toggle_bit) *duration *= 2;
return status;
}

View File

@@ -0,0 +1,17 @@
#include "../infrared_i.h"
#include "infrared_protocol_defs_i.h"
static const InfraredProtocolSpecification infrared_rc6_protocol_specification = {
.name = "RC6",
.address_length = 8,
.command_length = 8,
.frequency = INFRARED_RC6_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_RC6_DUTY_CYCLE,
};
const InfraredProtocolSpecification* infrared_rc6_get_spec(InfraredProtocol protocol) {
if(protocol == InfraredProtocolRC6)
return &infrared_rc6_protocol_specification;
else
return NULL;
}

View File

@@ -0,0 +1,72 @@
#include "infrared.h"
#include "infrared_protocol_defs_i.h"
#include <stdbool.h>
#include <stdint.h>
#include <furi.h>
#include "../infrared_i.h"
InfraredMessage* infrared_decoder_samsung32_check_ready(void* ctx) {
return infrared_common_decoder_check_ready(ctx);
}
bool infrared_decoder_samsung32_interpret(InfraredCommonDecoder* 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.protocol = InfraredProtocolSamsung32;
decoder->message.repeat = false;
result = true;
}
return result;
}
// timings start from Space (delay between message and repeat)
InfraredStatus infrared_decoder_samsung32_decode_repeat(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
InfraredStatus status = InfraredStatusError;
if(decoder->timings_cnt < 6) return InfraredStatusOk;
if((decoder->timings[0] > INFRARED_SAMSUNG_REPEAT_PAUSE_MIN) &&
(decoder->timings[0] < INFRARED_SAMSUNG_REPEAT_PAUSE_MAX) &&
MATCH_TIMING(decoder->timings[1], INFRARED_SAMSUNG_REPEAT_MARK, preamble_tolerance) &&
MATCH_TIMING(decoder->timings[2], INFRARED_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 = InfraredStatusReady;
decoder->timings_cnt = 0;
} else {
status = InfraredStatusError;
}
return status;
}
void* infrared_decoder_samsung32_alloc(void) {
return infrared_common_decoder_alloc(&protocol_samsung32);
}
InfraredMessage* infrared_decoder_samsung32_decode(void* decoder, bool level, uint32_t duration) {
return infrared_common_decode(decoder, level, duration);
}
void infrared_decoder_samsung32_free(void* decoder) {
infrared_common_decoder_free(decoder);
}
void infrared_decoder_samsung32_reset(void* decoder) {
infrared_common_decoder_reset(decoder);
}

View File

@@ -0,0 +1,71 @@
#include "furi/check.h"
#include "common/infrared_common_i.h"
#include <stdint.h>
#include "../infrared_i.h"
#include "infrared_protocol_defs_i.h"
#include <furi.h>
static const uint32_t repeat_timings[] = {
INFRARED_SAMSUNG_REPEAT_PAUSE2,
INFRARED_SAMSUNG_REPEAT_MARK,
INFRARED_SAMSUNG_REPEAT_SPACE,
INFRARED_SAMSUNG_BIT1_MARK,
INFRARED_SAMSUNG_BIT1_SPACE,
INFRARED_SAMSUNG_BIT1_MARK,
};
void infrared_encoder_samsung32_reset(void* encoder_ptr, const InfraredMessage* message) {
furi_assert(encoder_ptr);
InfraredCommonEncoder* encoder = encoder_ptr;
infrared_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;
encoder->bits_to_encode = encoder->protocol->databit_len[0];
}
InfraredStatus infrared_encoder_samsung32_encode_repeat(
InfraredCommonEncoder* 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->bits_encoded * 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 = INFRARED_SAMSUNG_REPEAT_PAUSE1;
*level = repeat_cnt % 2;
++encoder->timings_encoded;
bool done = (!((repeat_cnt + 1) % COUNT_OF(repeat_timings)));
return done ? InfraredStatusDone : InfraredStatusOk;
}
void* infrared_encoder_samsung32_alloc(void) {
return infrared_common_encoder_alloc(&protocol_samsung32);
}
void infrared_encoder_samsung32_free(void* encoder_ptr) {
infrared_common_encoder_free(encoder_ptr);
}
InfraredStatus
infrared_encoder_samsung32_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
return infrared_common_encode(encoder_ptr, duration, level);
}

View File

@@ -0,0 +1,17 @@
#include "../infrared_i.h"
#include "infrared_protocol_defs_i.h"
static const InfraredProtocolSpecification infrared_samsung32_protocol_specification = {
.name = "Samsung32",
.address_length = 8,
.command_length = 8,
.frequency = INFRARED_COMMON_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_COMMON_DUTY_CYCLE,
};
const InfraredProtocolSpecification* infrared_samsung32_get_spec(InfraredProtocol protocol) {
if(protocol == InfraredProtocolSamsung32)
return &infrared_samsung32_protocol_specification;
else
return NULL;
}

View File

@@ -0,0 +1,60 @@
#include "common/infrared_common_i.h"
#include "infrared.h"
#include "infrared_protocol_defs_i.h"
#include <stdbool.h>
#include <stdint.h>
#include <furi.h>
#include "../infrared_i.h"
InfraredMessage* infrared_decoder_sirc_check_ready(void* ctx) {
return infrared_common_decoder_check_ready(ctx);
}
bool infrared_decoder_sirc_interpret(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
uint32_t* data = (void*)&decoder->data[0];
uint16_t address = 0;
uint8_t command = 0;
InfraredProtocol protocol = InfraredProtocolUnknown;
if(decoder->databit_cnt == 12) {
address = (*data >> 7) & 0x1F;
command = *data & 0x7F;
protocol = InfraredProtocolSIRC;
} else if(decoder->databit_cnt == 15) {
address = (*data >> 7) & 0xFF;
command = *data & 0x7F;
protocol = InfraredProtocolSIRC15;
} else if(decoder->databit_cnt == 20) {
address = (*data >> 7) & 0x1FFF;
command = *data & 0x7F;
protocol = InfraredProtocolSIRC20;
} 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* infrared_decoder_sirc_alloc(void) {
return infrared_common_decoder_alloc(&protocol_sirc);
}
InfraredMessage* infrared_decoder_sirc_decode(void* decoder, bool level, uint32_t duration) {
return infrared_common_decode(decoder, level, duration);
}
void infrared_decoder_sirc_free(void* decoder) {
infrared_common_decoder_free(decoder);
}
void infrared_decoder_sirc_reset(void* decoder) {
infrared_common_decoder_reset(decoder);
}

View File

@@ -0,0 +1,73 @@
#include "furi/check.h"
#include "infrared.h"
#include "common/infrared_common_i.h"
#include <stdint.h>
#include "../infrared_i.h"
#include "infrared_protocol_defs_i.h"
#include <furi.h>
void infrared_encoder_sirc_reset(void* encoder_ptr, const InfraredMessage* message) {
furi_assert(encoder_ptr);
furi_assert(message);
InfraredCommonEncoder* encoder = encoder_ptr;
infrared_common_encoder_reset(encoder);
uint32_t* data = (void*)encoder->data;
if(message->protocol == InfraredProtocolSIRC) {
*data = (message->command & 0x7F);
*data |= (message->address & 0x1F) << 7;
encoder->bits_to_encode = 12;
} else if(message->protocol == InfraredProtocolSIRC15) {
*data = (message->command & 0x7F);
*data |= (message->address & 0xFF) << 7;
encoder->bits_to_encode = 15;
} else if(message->protocol == InfraredProtocolSIRC20) {
*data = (message->command & 0x7F);
*data |= (message->address & 0x1FFF) << 7;
encoder->bits_to_encode = 20;
} else {
furi_assert(0);
}
}
InfraredStatus infrared_encoder_sirc_encode_repeat(
InfraredCommonEncoder* encoder,
uint32_t* duration,
bool* level) {
furi_assert(encoder);
furi_assert(encoder->timings_encoded == (1 + 2 + encoder->bits_to_encode * 2 - 1));
furi_assert(encoder->timings_sum < INFRARED_SIRC_REPEAT_PERIOD);
*duration = INFRARED_SIRC_REPEAT_PERIOD - encoder->timings_sum;
*level = false;
encoder->timings_sum = 0;
encoder->timings_encoded = 1;
encoder->bits_encoded = 0;
encoder->state = InfraredCommonEncoderStatePreamble;
return InfraredStatusOk;
}
void* infrared_encoder_sirc_alloc(void) {
return infrared_common_encoder_alloc(&protocol_sirc);
}
void infrared_encoder_sirc_free(void* encoder_ptr) {
infrared_common_encoder_free(encoder_ptr);
}
InfraredStatus infrared_encoder_sirc_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
InfraredCommonEncoder* encoder = encoder_ptr;
InfraredStatus status = infrared_common_encode(encoder, duration, level);
if((status == InfraredStatusOk) && (encoder->bits_encoded == encoder->bits_to_encode)) {
furi_assert(!*level);
status = InfraredStatusDone;
encoder->state = InfraredCommonEncoderStateEncodeRepeat;
}
return status;
}

View File

@@ -0,0 +1,37 @@
#include "../infrared_i.h"
#include "infrared_protocol_defs_i.h"
static const InfraredProtocolSpecification infrared_sirc_protocol_specification = {
.name = "SIRC",
.address_length = 5,
.command_length = 7,
.frequency = INFRARED_SIRC_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_SIRC_DUTY_CYCLE,
};
static const InfraredProtocolSpecification infrared_sirc15_protocol_specification = {
.name = "SIRC15",
.address_length = 8,
.command_length = 7,
.frequency = INFRARED_SIRC_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_SIRC_DUTY_CYCLE,
};
static const InfraredProtocolSpecification infrared_sirc20_protocol_specification = {
.name = "SIRC20",
.address_length = 13,
.command_length = 7,
.frequency = INFRARED_SIRC_CARRIER_FREQUENCY,
.duty_cycle = INFRARED_SIRC_DUTY_CYCLE,
};
const InfraredProtocolSpecification* infrared_sirc_get_spec(InfraredProtocol protocol) {
if(protocol == InfraredProtocolSIRC)
return &infrared_sirc_protocol_specification;
else if(protocol == InfraredProtocolSIRC15)
return &infrared_sirc15_protocol_specification;
else if(protocol == InfraredProtocolSIRC20)
return &infrared_sirc20_protocol_specification;
else
return NULL;
}