[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:
317
lib/infrared/encoder_decoder/common/infrared_common_decoder.c
Normal file
317
lib/infrared/encoder_decoder/common/infrared_common_decoder.c
Normal 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;
|
||||
}
|
183
lib/infrared/encoder_decoder/common/infrared_common_encoder.c
Normal file
183
lib/infrared/encoder_decoder/common/infrared_common_encoder.c
Normal 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);
|
||||
}
|
89
lib/infrared/encoder_decoder/common/infrared_common_i.h
Normal file
89
lib/infrared/encoder_decoder/common/infrared_common_i.h
Normal 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);
|
@@ -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,
|
||||
};
|
Reference in New Issue
Block a user