[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

@@ -9,7 +9,7 @@
- `fatfs` - External storage file system
- `flipper_file` - Flipper File Format library
- `fnv1a-hash` - Fnv1a hash library
- `irda` - Irda library
- `infrared` - Infrared library
- `libusb_stm32` - STM32 USB library
- `littlefs` - Internal storage file system
- `micro-ecc` - Elyptic Curve Crpytography library

View File

@@ -1,13 +1,13 @@
#include "furi/check.h"
#include "furi/common_defines.h"
#include "irda.h"
#include "irda_common_i.h"
#include "infrared.h"
#include "infrared_common_i.h"
#include <stdbool.h>
#include <furi.h>
#include "irda_i.h"
#include "infrared_i.h"
#include <stdint.h>
static void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder);
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);
@@ -17,7 +17,7 @@ static inline size_t consume_samples(uint32_t* array, size_t len, size_t shift)
return len;
}
static inline void accumulate_lsb(IrdaCommonDecoder* decoder, bool bit) {
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
@@ -32,7 +32,7 @@ static inline void accumulate_lsb(IrdaCommonDecoder* decoder, bool bit) {
++decoder->databit_cnt;
}
static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
static bool infrared_check_preamble(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
@@ -70,13 +70,13 @@ static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
* decoder->protocol->databit_len[1...] contains lesser values, but which can be decoded
* for some protocol modifications.
*/
static IrdaStatus irda_common_decode_bits(IrdaCommonDecoder* decoder) {
static InfraredStatus infrared_common_decode_bits(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
IrdaStatus status = IrdaStatusOk;
const IrdaTimings* timings = &decoder->protocol->timings;
InfraredStatus status = InfraredStatusOk;
const InfraredTimings* timings = &decoder->protocol->timings;
while(decoder->timings_cnt && (status == IrdaStatusOk)) {
while(decoder->timings_cnt && (status == InfraredStatusOk)) {
bool level = (decoder->level + decoder->timings_cnt + 1) % 2;
uint32_t timing = decoder->timings[0];
@@ -87,19 +87,19 @@ static IrdaStatus irda_common_decode_bits(IrdaCommonDecoder* decoder) {
(i < COUNT_OF(decoder->protocol->databit_len));
++i) {
if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
return IrdaStatusReady;
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 IrdaStatusError;
return InfraredStatusError;
}
}
status = decoder->protocol->decode(decoder, level, timing);
furi_check(decoder->databit_cnt <= decoder->protocol->databit_len[0]);
furi_assert(status == IrdaStatusError || status == IrdaStatusOk);
if(status == IrdaStatusError) {
furi_assert(status == InfraredStatusError || status == InfraredStatusOk);
if(status == InfraredStatusError) {
break;
}
decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
@@ -107,7 +107,7 @@ static IrdaStatus irda_common_decode_bits(IrdaCommonDecoder* decoder) {
/* check if largest protocol version can be decoded */
if(level && (decoder->protocol->databit_len[0] == decoder->databit_cnt) &&
!timings->min_split_time) {
status = IrdaStatusReady;
status = InfraredStatusReady;
break;
}
}
@@ -116,10 +116,11 @@ static IrdaStatus irda_common_decode_bits(IrdaCommonDecoder* decoder) {
}
/* Pulse Distance-Width Modulation */
IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder, bool level, uint32_t timing) {
InfraredStatus
infrared_common_decode_pdwm(InfraredCommonDecoder* decoder, bool level, uint32_t timing) {
furi_assert(decoder);
IrdaStatus status = IrdaStatusOk;
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;
@@ -137,11 +138,11 @@ IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder, bool level, uint3
} else if(MATCH_TIMING(timing, bit0, bit_tolerance)) {
accumulate_lsb(decoder, 0);
} else {
status = IrdaStatusError;
status = InfraredStatusError;
}
} else {
if(!MATCH_TIMING(timing, no_info_timing, bit_tolerance)) {
status = IrdaStatusError;
status = InfraredStatusError;
}
}
@@ -149,7 +150,8 @@ IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder, bool level, uint3
}
/* level switch detection goes in middle of time-quant */
IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder, bool level, uint32_t timing) {
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;
@@ -161,7 +163,7 @@ IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder, bool level,
bool double_timing = MATCH_TIMING(timing, 2 * bit, tolerance);
if(!single_timing && !double_timing) {
return IrdaStatusError;
return InfraredStatusError;
}
if(decoder->protocol->manchester_start_from_space && (decoder->databit_cnt == 0)) {
@@ -171,7 +173,7 @@ IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder, bool level,
if(*switch_detect == 0) {
if(double_timing) {
return IrdaStatusError;
return InfraredStatusError;
}
/* only single timing - level switch required in the middle of time-quant */
*switch_detect = 1;
@@ -182,16 +184,16 @@ IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder, bool level,
if(*switch_detect) {
if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
return IrdaStatusError;
return InfraredStatusError;
}
accumulate_lsb(decoder, level);
}
return IrdaStatusOk;
return InfraredStatusOk;
}
IrdaMessage* irda_common_decoder_check_ready(IrdaCommonDecoder* decoder) {
IrdaMessage* message = NULL;
InfraredMessage* infrared_common_decoder_check_ready(InfraredCommonDecoder* decoder) {
InfraredMessage* message = NULL;
bool found_length = false;
for(int i = 0;
@@ -207,23 +209,24 @@ IrdaMessage* irda_common_decoder_check_ready(IrdaCommonDecoder* decoder) {
decoder->databit_cnt = 0;
message = &decoder->message;
if(decoder->protocol->decode_repeat) {
decoder->state = IrdaCommonDecoderStateProcessRepeat;
decoder->state = InfraredCommonDecoderStateProcessRepeat;
} else {
decoder->state = IrdaCommonDecoderStateWaitPreamble;
decoder->state = InfraredCommonDecoderStateWaitPreamble;
}
}
return message;
}
IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
InfraredMessage*
infrared_common_decode(InfraredCommonDecoder* decoder, bool level, uint32_t duration) {
furi_assert(decoder);
IrdaMessage* message = 0;
IrdaStatus status = IrdaStatusError;
InfraredMessage* message = 0;
InfraredStatus status = InfraredStatusError;
if(decoder->level == level) {
irda_common_decoder_reset(decoder);
infrared_common_decoder_reset(decoder);
}
decoder->level = level; // start with low level (Space timing)
@@ -233,35 +236,35 @@ IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t
while(1) {
switch(decoder->state) {
case IrdaCommonDecoderStateWaitPreamble:
if(irda_check_preamble(decoder)) {
decoder->state = IrdaCommonDecoderStateDecode;
case InfraredCommonDecoderStateWaitPreamble:
if(infrared_check_preamble(decoder)) {
decoder->state = InfraredCommonDecoderStateDecode;
decoder->databit_cnt = 0;
decoder->switch_detect = false;
continue;
}
break;
case IrdaCommonDecoderStateDecode:
status = irda_common_decode_bits(decoder);
if(status == IrdaStatusReady) {
message = irda_common_decoder_check_ready(decoder);
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 = IrdaCommonDecoderStateWaitPreamble;
decoder->state = InfraredCommonDecoderStateWaitPreamble;
}
} else if(status == IrdaStatusError) {
irda_common_decoder_reset_state(decoder);
} else if(status == InfraredStatusError) {
infrared_common_decoder_reset_state(decoder);
continue;
}
break;
case IrdaCommonDecoderStateProcessRepeat:
case InfraredCommonDecoderStateProcessRepeat:
status = decoder->protocol->decode_repeat(decoder);
if(status == IrdaStatusError) {
irda_common_decoder_reset_state(decoder);
if(status == InfraredStatusError) {
infrared_common_decoder_reset_state(decoder);
continue;
} else if(status == IrdaStatusReady) {
} else if(status == InfraredStatusReady) {
decoder->message.repeat = true;
message = &decoder->message;
}
@@ -273,7 +276,7 @@ IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t
return message;
}
void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
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 */
@@ -281,24 +284,24 @@ void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
}
uint32_t alloc_size = sizeof(IrdaCommonDecoder) + protocol->databit_len[0] / 8 +
uint32_t alloc_size = sizeof(InfraredCommonDecoder) + protocol->databit_len[0] / 8 +
!!(protocol->databit_len[0] % 8);
IrdaCommonDecoder* decoder = malloc(alloc_size);
InfraredCommonDecoder* decoder = malloc(alloc_size);
decoder->protocol = protocol;
decoder->level = true;
return decoder;
}
void irda_common_decoder_free(IrdaCommonDecoder* decoder) {
void infrared_common_decoder_free(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
free(decoder);
}
void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder) {
decoder->state = IrdaCommonDecoderStateWaitPreamble;
void infrared_common_decoder_reset_state(InfraredCommonDecoder* decoder) {
decoder->state = InfraredCommonDecoderStateWaitPreamble;
decoder->databit_cnt = 0;
decoder->switch_detect = false;
decoder->message.protocol = IrdaProtocolUnknown;
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);
@@ -306,9 +309,9 @@ void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder) {
}
}
void irda_common_decoder_reset(IrdaCommonDecoder* decoder) {
void infrared_common_decoder_reset(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
irda_common_decoder_reset_state(decoder);
infrared_common_decoder_reset_state(decoder);
decoder->timings_cnt = 0;
}

View File

@@ -1,19 +1,19 @@
#include "furi/check.h"
#include "irda.h"
#include "irda_common_i.h"
#include "infrared.h"
#include "infrared_common_i.h"
#include <stdbool.h>
#include <furi.h>
#include "irda_i.h"
#include "infrared_i.h"
#include <stdint.h>
static IrdaStatus
irda_common_encode_bits(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
IrdaStatus status = encoder->protocol->encode(encoder, duration, level);
furi_assert(status == IrdaStatusOk);
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 = IrdaStatusDone;
status = InfraredStatusDone;
}
return status;
@@ -33,13 +33,15 @@ static IrdaStatus
* 0 1 2 | 3 4 |
* _____-------_____---___
*/
IrdaStatus
irda_common_encode_manchester(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
InfraredStatus infrared_common_encode_manchester(
InfraredCommonEncoder* encoder,
uint32_t* duration,
bool* level) {
furi_assert(encoder);
furi_assert(duration);
furi_assert(level);
const IrdaTimings* timings = &encoder->protocol->timings;
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));
@@ -52,15 +54,16 @@ IrdaStatus
else if(*level && (encoder->bits_encoded + 1 == encoder->bits_to_encode))
++encoder->bits_encoded; /* don't encode last space */
return IrdaStatusOk;
return InfraredStatusOk;
}
IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
InfraredStatus
infrared_common_encode_pdwm(InfraredCommonEncoder* encoder, uint32_t* duration, bool* level) {
furi_assert(encoder);
furi_assert(duration);
furi_assert(level);
const IrdaTimings* timings = &encoder->protocol->timings;
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));
@@ -76,26 +79,27 @@ IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duratio
if(!pwm) ++encoder->bits_encoded;
}
return IrdaStatusOk;
return InfraredStatusOk;
}
IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
InfraredStatus
infrared_common_encode(InfraredCommonEncoder* encoder, uint32_t* duration, bool* level) {
furi_assert(encoder);
furi_assert(duration);
furi_assert(level);
IrdaStatus status = IrdaStatusOk;
const IrdaTimings* timings = &encoder->protocol->timings;
InfraredStatus status = InfraredStatusOk;
const InfraredTimings* timings = &encoder->protocol->timings;
switch(encoder->state) {
case IrdaCommonEncoderStateSilence:
case InfraredCommonEncoderStateSilence:
*duration = encoder->protocol->timings.silence_time;
*level = false;
status = IrdaStatusOk;
encoder->state = IrdaCommonEncoderStatePreamble;
status = InfraredStatusOk;
encoder->state = InfraredCommonEncoderStatePreamble;
++encoder->timings_encoded;
encoder->timings_sum = 0;
break;
case IrdaCommonEncoderStatePreamble:
case InfraredCommonEncoderStatePreamble:
if(timings->preamble_mark) {
if(encoder->timings_encoded == 1) {
*duration = timings->preamble_mark;
@@ -103,39 +107,39 @@ IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bo
} else {
*duration = timings->preamble_space;
*level = false;
encoder->state = IrdaCommonEncoderStateEncode;
encoder->state = InfraredCommonEncoderStateEncode;
}
++encoder->timings_encoded;
encoder->timings_sum += *duration;
break;
} else {
encoder->state = IrdaCommonEncoderStateEncode;
encoder->state = InfraredCommonEncoderStateEncode;
}
/* FALLTHROUGH */
case IrdaCommonEncoderStateEncode:
status = irda_common_encode_bits(encoder, duration, level);
if(status == IrdaStatusDone) {
case InfraredCommonEncoderStateEncode:
status = infrared_common_encode_bits(encoder, duration, level);
if(status == InfraredStatusDone) {
if(encoder->protocol->encode_repeat) {
encoder->state = IrdaCommonEncoderStateEncodeRepeat;
encoder->state = InfraredCommonEncoderStateEncodeRepeat;
} else {
encoder->timings_encoded = 0;
encoder->timings_sum = 0;
encoder->bits_encoded = 0;
encoder->switch_detect = 0;
encoder->state = IrdaCommonEncoderStateSilence;
encoder->state = InfraredCommonEncoderStateSilence;
}
}
break;
case IrdaCommonEncoderStateEncodeRepeat:
case InfraredCommonEncoderStateEncodeRepeat:
status = encoder->protocol->encode_repeat(encoder, duration, level);
break;
}
return status;
}
void* irda_common_encoder_alloc(const IrdaCommonProtocolSpec* protocol) {
void* infrared_common_encoder_alloc(const InfraredCommonProtocolSpec* protocol) {
furi_assert(protocol);
if(protocol->decode == irda_common_decode_pdwm) {
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));
@@ -146,26 +150,26 @@ void* irda_common_encoder_alloc(const IrdaCommonProtocolSpec* protocol) {
furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
}
uint32_t alloc_size = sizeof(IrdaCommonDecoder) + protocol->databit_len[0] / 8 +
uint32_t alloc_size = sizeof(InfraredCommonDecoder) + protocol->databit_len[0] / 8 +
!!(protocol->databit_len[0] % 8);
IrdaCommonEncoder* encoder = malloc(alloc_size);
InfraredCommonEncoder* encoder = malloc(alloc_size);
memset(encoder, 0, alloc_size);
encoder->protocol = protocol;
return encoder;
}
void irda_common_encoder_free(IrdaCommonEncoder* encoder) {
void infrared_common_encoder_free(InfraredCommonEncoder* encoder) {
furi_assert(encoder);
free(encoder);
}
void irda_common_encoder_reset(IrdaCommonEncoder* 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 = IrdaCommonEncoderStateSilence;
encoder->state = InfraredCommonEncoderStateSilence;
encoder->switch_detect = 0;
uint8_t max_databit_len = 0;

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

@@ -1,5 +1,5 @@
#pragma once
#include "irda.h"
#include "infrared.h"
#include <stddef.h>
#include <stdint.h>
@@ -14,7 +14,7 @@ typedef struct {
uint16_t bit0_space;
uint32_t preamble_tolerance;
uint32_t bit_tolerance;
} IrdaTimings;
} InfraredTimings;
typedef struct {
const char* name;
@@ -22,19 +22,19 @@ typedef struct {
uint8_t command_length;
uint32_t frequency;
float duty_cycle;
} IrdaProtocolSpecification;
} InfraredProtocolSpecification;
typedef const IrdaProtocolSpecification* (*IrdaGetProtocolSpec)(IrdaProtocol protocol);
typedef const InfraredProtocolSpecification* (*InfraredGetProtocolSpec)(InfraredProtocol protocol);
typedef void* (*IrdaAlloc)(void);
typedef void (*IrdaFree)(void*);
typedef void* (*InfraredAlloc)(void);
typedef void (*InfraredFree)(void*);
typedef void (*IrdaDecoderReset)(void*);
typedef IrdaMessage* (*IrdaDecode)(void* ctx, bool level, uint32_t duration);
typedef IrdaMessage* (*IrdaDecoderCheckReady)(void*);
typedef void (*InfraredDecoderReset)(void*);
typedef InfraredMessage* (*InfraredDecode)(void* ctx, bool level, uint32_t duration);
typedef InfraredMessage* (*InfraredDecoderCheckReady)(void*);
typedef void (*IrdaEncoderReset)(void* encoder, const IrdaMessage* message);
typedef IrdaStatus (*IrdaEncode)(void* encoder, uint32_t* out, bool* polarity);
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;

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

@@ -1,16 +1,16 @@
#include "common/irda_common_i.h"
#include "irda.h"
#include "irda_protocol_defs_i.h"
#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 "../irda_i.h"
#include "../infrared_i.h"
IrdaMessage* irda_decoder_nec_check_ready(void* ctx) {
return irda_common_decoder_check_ready(ctx);
InfraredMessage* infrared_decoder_nec_check_ready(void* ctx) {
return infrared_common_decoder_check_ready(ctx);
}
bool irda_decoder_nec_interpret(IrdaCommonDecoder* decoder) {
bool infrared_decoder_nec_interpret(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
@@ -21,13 +21,13 @@ bool irda_decoder_nec_interpret(IrdaCommonDecoder* decoder) {
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 = IrdaProtocolNEC;
decoder->message.protocol = InfraredProtocolNEC;
decoder->message.address = address;
decoder->message.command = command;
decoder->message.repeat = false;
result = true;
} else {
decoder->message.protocol = IrdaProtocolNECext;
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;
@@ -42,13 +42,13 @@ bool irda_decoder_nec_interpret(IrdaCommonDecoder* decoder) {
uint16_t command_inverse = (*data2 >> 2) & 0xFF;
if((address == (~address_inverse & 0x1FFF)) && (command == (~command_inverse & 0xFF))) {
decoder->message.protocol = IrdaProtocolNEC42;
decoder->message.protocol = InfraredProtocolNEC42;
decoder->message.address = address;
decoder->message.command = command;
decoder->message.repeat = false;
result = true;
} else {
decoder->message.protocol = IrdaProtocolNEC42ext;
decoder->message.protocol = InfraredProtocolNEC42ext;
decoder->message.address = address | (address_inverse << 13);
decoder->message.command = command | (command_inverse << 8);
decoder->message.repeat = false;
@@ -60,41 +60,41 @@ bool irda_decoder_nec_interpret(IrdaCommonDecoder* decoder) {
}
// timings start from Space (delay between message and repeat)
IrdaStatus irda_decoder_nec_decode_repeat(IrdaCommonDecoder* decoder) {
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;
IrdaStatus status = IrdaStatusError;
InfraredStatus status = InfraredStatusError;
if(decoder->timings_cnt < 4) return IrdaStatusOk;
if(decoder->timings_cnt < 4) return InfraredStatusOk;
if((decoder->timings[0] > IRDA_NEC_REPEAT_PAUSE_MIN) &&
(decoder->timings[0] < IRDA_NEC_REPEAT_PAUSE_MAX) &&
MATCH_TIMING(decoder->timings[1], IRDA_NEC_REPEAT_MARK, preamble_tolerance) &&
MATCH_TIMING(decoder->timings[2], IRDA_NEC_REPEAT_SPACE, preamble_tolerance) &&
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 = IrdaStatusReady;
status = InfraredStatusReady;
decoder->timings_cnt = 0;
} else {
status = IrdaStatusError;
status = InfraredStatusError;
}
return status;
}
void* irda_decoder_nec_alloc(void) {
return irda_common_decoder_alloc(&protocol_nec);
void* infrared_decoder_nec_alloc(void) {
return infrared_common_decoder_alloc(&protocol_nec);
}
IrdaMessage* irda_decoder_nec_decode(void* decoder, bool level, uint32_t duration) {
return irda_common_decode(decoder, level, duration);
InfraredMessage* infrared_decoder_nec_decode(void* decoder, bool level, uint32_t duration) {
return infrared_common_decode(decoder, level, duration);
}
void irda_decoder_nec_free(void* decoder) {
irda_common_decoder_free(decoder);
void infrared_decoder_nec_free(void* decoder) {
infrared_common_decoder_free(decoder);
}
void irda_decoder_nec_reset(void* decoder) {
irda_common_decoder_reset(decoder);
void infrared_decoder_nec_reset(void* decoder) {
infrared_common_decoder_reset(decoder);
}

View File

@@ -1,28 +1,29 @@
#include "furi/check.h"
#include "irda.h"
#include "common/irda_common_i.h"
#include "infrared.h"
#include "common/infrared_common_i.h"
#include <stdint.h>
#include "../irda_i.h"
#include "irda_protocol_defs_i.h"
#include "../infrared_i.h"
#include "infrared_protocol_defs_i.h"
#include <furi.h>
static const uint32_t repeat_timings[] = {
IRDA_NEC_REPEAT_PERIOD - IRDA_NEC_REPEAT_MARK - IRDA_NEC_REPEAT_SPACE - IRDA_NEC_BIT1_MARK,
IRDA_NEC_REPEAT_MARK,
IRDA_NEC_REPEAT_SPACE,
IRDA_NEC_BIT1_MARK,
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 irda_encoder_nec_reset(void* encoder_ptr, const IrdaMessage* message) {
void infrared_encoder_nec_reset(void* encoder_ptr, const InfraredMessage* message) {
furi_assert(encoder_ptr);
furi_assert(message);
IrdaCommonEncoder* encoder = encoder_ptr;
irda_common_encoder_reset(encoder);
InfraredCommonEncoder* encoder = encoder_ptr;
infrared_common_encoder_reset(encoder);
uint32_t* data1 = (void*)encoder->data;
uint32_t* data2 = data1 + 1;
if(message->protocol == IrdaProtocolNEC) {
if(message->protocol == InfraredProtocolNEC) {
uint8_t address = message->address;
uint8_t address_inverse = ~address;
uint8_t command = message->command;
@@ -32,11 +33,11 @@ void irda_encoder_nec_reset(void* encoder_ptr, const IrdaMessage* message) {
*data1 |= command << 16;
*data1 |= command_inverse << 24;
encoder->bits_to_encode = 32;
} else if(message->protocol == IrdaProtocolNECext) {
} else if(message->protocol == InfraredProtocolNECext) {
*data1 = (uint16_t)message->address;
*data1 |= (message->command & 0xFFFF) << 16;
encoder->bits_to_encode = 32;
} else if(message->protocol == IrdaProtocolNEC42) {
} else if(message->protocol == InfraredProtocolNEC42) {
/* 13 address + 13 inverse address + 8 command + 8 inv command */
*data1 = message->address & 0x1FFFUL;
*data1 |= (~message->address & 0x1FFFUL) << 13;
@@ -44,7 +45,7 @@ void irda_encoder_nec_reset(void* encoder_ptr, const IrdaMessage* message) {
*data2 = (message->command & 0xC0UL) >> 6;
*data2 |= (~message->command & 0xFFUL) << 2;
encoder->bits_to_encode = 42;
} else if(message->protocol == IrdaProtocolNEC42ext) {
} else if(message->protocol == InfraredProtocolNEC42ext) {
*data1 = message->address & 0x3FFFFFF;
*data1 |= ((message->command & 0x3F) << 26);
*data2 = (message->command & 0xFFC0) >> 6;
@@ -54,8 +55,10 @@ void irda_encoder_nec_reset(void* encoder_ptr, const IrdaMessage* message) {
}
}
IrdaStatus
irda_encoder_nec_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
InfraredStatus infrared_encoder_nec_encode_repeat(
InfraredCommonEncoder* encoder,
uint32_t* duration,
bool* level) {
furi_assert(encoder);
/* space + 2 timings preambule + payload + stop bit */
@@ -67,24 +70,24 @@ IrdaStatus
if(repeat_cnt > 0) {
*duration = repeat_timings[repeat_cnt % COUNT_OF(repeat_timings)];
} else {
*duration = IRDA_NEC_REPEAT_PERIOD - encoder->timings_sum;
*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 ? IrdaStatusDone : IrdaStatusOk;
return done ? InfraredStatusDone : InfraredStatusOk;
}
void* irda_encoder_nec_alloc(void) {
return irda_common_encoder_alloc(&protocol_nec);
void* infrared_encoder_nec_alloc(void) {
return infrared_common_encoder_alloc(&protocol_nec);
}
void irda_encoder_nec_free(void* encoder_ptr) {
irda_common_encoder_free(encoder_ptr);
void infrared_encoder_nec_free(void* encoder_ptr) {
infrared_common_encoder_free(encoder_ptr);
}
IrdaStatus irda_encoder_nec_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
return irda_common_encode(encoder_ptr, duration, level);
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

@@ -1,22 +1,22 @@
#include "irda.h"
#include "infrared.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <furi.h>
#include "../irda_i.h"
#include "../irda_protocol_defs_i.h"
#include "../infrared_i.h"
#include "../infrared_protocol_defs_i.h"
typedef struct {
IrdaCommonDecoder* common_decoder;
InfraredCommonDecoder* common_decoder;
bool toggle;
} IrdaRc6Decoder;
} InfraredRc6Decoder;
IrdaMessage* irda_decoder_rc6_check_ready(void* ctx) {
IrdaRc6Decoder* decoder_rc6 = ctx;
return irda_common_decoder_check_ready(decoder_rc6->common_decoder);
InfraredMessage* infrared_decoder_rc6_check_ready(void* ctx) {
InfraredRc6Decoder* decoder_rc6 = ctx;
return infrared_common_decoder_check_ready(decoder_rc6->common_decoder);
}
bool irda_decoder_rc6_interpret(IrdaCommonDecoder* decoder) {
bool infrared_decoder_rc6_interpret(InfraredCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
@@ -29,11 +29,11 @@ bool irda_decoder_rc6_interpret(IrdaCommonDecoder* decoder) {
uint8_t mode = (*data >> 1) & 0x7;
if((start_bit == 1) && (mode == 0)) {
IrdaMessage* message = &decoder->message;
IrdaRc6Decoder* rc6_decoder = decoder->context;
InfraredMessage* message = &decoder->message;
InfraredRc6Decoder* rc6_decoder = decoder->context;
bool* prev_toggle = &rc6_decoder->toggle;
if((message->address == address) && (message->command == command) &&
(message->protocol == IrdaProtocolRC6)) {
(message->protocol == InfraredProtocolRC6)) {
message->repeat = (toggle == *prev_toggle);
} else {
message->repeat = false;
@@ -41,7 +41,7 @@ bool irda_decoder_rc6_interpret(IrdaCommonDecoder* decoder) {
*prev_toggle = toggle;
message->command = command;
message->address = address;
message->protocol = IrdaProtocolRC6;
message->protocol = InfraredProtocolRC6;
result = true;
}
@@ -54,10 +54,12 @@ bool irda_decoder_rc6_interpret(IrdaCommonDecoder* decoder) {
* it separately and than pass decoding for other bits to
* common manchester decode function.
*/
IrdaStatus
irda_decoder_rc6_decode_manchester(IrdaCommonDecoder* decoder, bool level, uint32_t timing) {
InfraredStatus infrared_decoder_rc6_decode_manchester(
InfraredCommonDecoder* decoder,
bool level,
uint32_t timing) {
// 4th bit lasts 2x times more
IrdaStatus status = IrdaStatusError;
InfraredStatus status = InfraredStatusError;
uint16_t bit = decoder->protocol->timings.bit1_mark;
uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
@@ -71,43 +73,43 @@ IrdaStatus
if(single_timing ^ triple_timing) {
++decoder->databit_cnt;
decoder->data[0] |= (single_timing ? !level : level) << 4;
status = IrdaStatusOk;
status = InfraredStatusOk;
}
} else if(decoder->databit_cnt == 5) {
if(single_timing || triple_timing) {
if(triple_timing) timing = bit;
decoder->switch_detect = false;
status = irda_common_decode_manchester(decoder, level, timing);
status = infrared_common_decode_manchester(decoder, level, timing);
} else if(double_timing) {
status = IrdaStatusOk;
status = InfraredStatusOk;
}
} else {
status = irda_common_decode_manchester(decoder, level, timing);
status = infrared_common_decode_manchester(decoder, level, timing);
}
return status;
}
void* irda_decoder_rc6_alloc(void) {
IrdaRc6Decoder* decoder = malloc(sizeof(IrdaRc6Decoder));
void* infrared_decoder_rc6_alloc(void) {
InfraredRc6Decoder* decoder = malloc(sizeof(InfraredRc6Decoder));
decoder->toggle = false;
decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc6);
decoder->common_decoder = infrared_common_decoder_alloc(&protocol_rc6);
decoder->common_decoder->context = decoder;
return decoder;
}
IrdaMessage* irda_decoder_rc6_decode(void* decoder, bool level, uint32_t duration) {
IrdaRc6Decoder* decoder_rc6 = decoder;
return irda_common_decode(decoder_rc6->common_decoder, level, duration);
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 irda_decoder_rc6_free(void* decoder) {
IrdaRc6Decoder* decoder_rc6 = decoder;
irda_common_decoder_free(decoder_rc6->common_decoder);
void infrared_decoder_rc6_free(void* decoder) {
InfraredRc6Decoder* decoder_rc6 = decoder;
infrared_common_decoder_free(decoder_rc6->common_decoder);
free(decoder_rc6);
}
void irda_decoder_rc6_reset(void* decoder) {
IrdaRc6Decoder* decoder_rc6 = decoder;
irda_common_decoder_reset(decoder_rc6->common_decoder);
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

@@ -1,24 +1,24 @@
#include "furi/check.h"
#include "common/irda_common_i.h"
#include "common/infrared_common_i.h"
#include <stdint.h>
#include "../irda_i.h"
#include "irda_protocol_defs_i.h"
#include "../infrared_i.h"
#include "infrared_protocol_defs_i.h"
#include <furi.h>
static const uint32_t repeat_timings[] = {
IRDA_SAMSUNG_REPEAT_PAUSE2,
IRDA_SAMSUNG_REPEAT_MARK,
IRDA_SAMSUNG_REPEAT_SPACE,
IRDA_SAMSUNG_BIT1_MARK,
IRDA_SAMSUNG_BIT1_SPACE,
IRDA_SAMSUNG_BIT1_MARK,
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 irda_encoder_samsung32_reset(void* encoder_ptr, const IrdaMessage* message) {
void infrared_encoder_samsung32_reset(void* encoder_ptr, const InfraredMessage* message) {
furi_assert(encoder_ptr);
IrdaCommonEncoder* encoder = encoder_ptr;
irda_common_encoder_reset(encoder);
InfraredCommonEncoder* encoder = encoder_ptr;
infrared_common_encoder_reset(encoder);
uint8_t address = message->address;
uint8_t command = message->command;
@@ -33,8 +33,8 @@ void irda_encoder_samsung32_reset(void* encoder_ptr, const IrdaMessage* message)
encoder->bits_to_encode = encoder->protocol->databit_len[0];
}
IrdaStatus irda_encoder_samsung32_encode_repeat(
IrdaCommonEncoder* encoder,
InfraredStatus infrared_encoder_samsung32_encode_repeat(
InfraredCommonEncoder* encoder,
uint32_t* duration,
bool* level) {
furi_assert(encoder);
@@ -48,23 +48,24 @@ IrdaStatus irda_encoder_samsung32_encode_repeat(
if(repeat_cnt > 0)
*duration = repeat_timings[repeat_cnt % COUNT_OF(repeat_timings)];
else
*duration = IRDA_SAMSUNG_REPEAT_PAUSE1;
*duration = INFRARED_SAMSUNG_REPEAT_PAUSE1;
*level = repeat_cnt % 2;
++encoder->timings_encoded;
bool done = (!((repeat_cnt + 1) % COUNT_OF(repeat_timings)));
return done ? IrdaStatusDone : IrdaStatusOk;
return done ? InfraredStatusDone : InfraredStatusOk;
}
void* irda_encoder_samsung32_alloc(void) {
return irda_common_encoder_alloc(&protocol_samsung32);
void* infrared_encoder_samsung32_alloc(void) {
return infrared_common_encoder_alloc(&protocol_samsung32);
}
void irda_encoder_samsung32_free(void* encoder_ptr) {
irda_common_encoder_free(encoder_ptr);
void infrared_encoder_samsung32_free(void* encoder_ptr) {
infrared_common_encoder_free(encoder_ptr);
}
IrdaStatus irda_encoder_samsung32_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
return irda_common_encode(encoder_ptr, duration, level);
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;
}

View File

@@ -0,0 +1,116 @@
#include "infrared.h"
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <furi.h>
#include <furi_hal_infrared.h>
#include <furi_hal_delay.h>
static uint32_t infrared_tx_number_of_transmissions = 0;
static uint32_t infrared_tx_raw_timings_index = 0;
static uint32_t infrared_tx_raw_timings_number = 0;
static uint32_t infrared_tx_raw_start_from_mark = 0;
static bool infrared_tx_raw_add_silence = false;
FuriHalInfraredTxGetDataState
infrared_get_raw_data_callback(void* context, uint32_t* duration, bool* level) {
furi_assert(duration);
furi_assert(level);
furi_assert(context);
FuriHalInfraredTxGetDataState state = FuriHalInfraredTxGetDataStateOk;
const uint32_t* timings = context;
if(infrared_tx_raw_add_silence && (infrared_tx_raw_timings_index == 0)) {
infrared_tx_raw_add_silence = false;
*level = false;
*duration = INFRARED_RAW_TX_TIMING_DELAY_US;
} else {
*level = infrared_tx_raw_start_from_mark ^ (infrared_tx_raw_timings_index % 2);
*duration = timings[infrared_tx_raw_timings_index++];
}
if(infrared_tx_raw_timings_number == infrared_tx_raw_timings_index) {
state = FuriHalInfraredTxGetDataStateLastDone;
}
return state;
}
void infrared_send_raw_ext(
const uint32_t timings[],
uint32_t timings_cnt,
bool start_from_mark,
uint32_t frequency,
float duty_cycle) {
furi_assert(timings);
infrared_tx_raw_start_from_mark = start_from_mark;
infrared_tx_raw_timings_index = 0;
infrared_tx_raw_timings_number = timings_cnt;
infrared_tx_raw_add_silence = start_from_mark;
furi_hal_infrared_async_tx_set_data_isr_callback(
infrared_get_raw_data_callback, (void*)timings);
furi_hal_infrared_async_tx_start(frequency, duty_cycle);
furi_hal_infrared_async_tx_wait_termination();
furi_assert(!furi_hal_infrared_is_busy());
}
void infrared_send_raw(const uint32_t timings[], uint32_t timings_cnt, bool start_from_mark) {
infrared_send_raw_ext(
timings,
timings_cnt,
start_from_mark,
INFRARED_COMMON_CARRIER_FREQUENCY,
INFRARED_COMMON_DUTY_CYCLE);
}
FuriHalInfraredTxGetDataState
infrared_get_data_callback(void* context, uint32_t* duration, bool* level) {
FuriHalInfraredTxGetDataState state = FuriHalInfraredTxGetDataStateLastDone;
InfraredEncoderHandler* handler = context;
InfraredStatus status = InfraredStatusError;
if(infrared_tx_number_of_transmissions > 0) {
status = infrared_encode(handler, duration, level);
}
if(status == InfraredStatusError) {
state = FuriHalInfraredTxGetDataStateLastDone;
*duration = 0;
*level = 0;
} else if(status == InfraredStatusOk) {
state = FuriHalInfraredTxGetDataStateOk;
} else if(status == InfraredStatusDone) {
state = FuriHalInfraredTxGetDataStateDone;
if(--infrared_tx_number_of_transmissions == 0) {
state = FuriHalInfraredTxGetDataStateLastDone;
}
} else {
furi_crash(NULL);
}
return state;
}
void infrared_send(const InfraredMessage* message, int times) {
furi_assert(message);
furi_assert(times);
furi_assert(infrared_is_protocol_valid(message->protocol));
InfraredEncoderHandler* handler = infrared_alloc_encoder();
infrared_reset_encoder(handler, message);
infrared_tx_number_of_transmissions = times;
uint32_t frequency = infrared_get_protocol_frequency(message->protocol);
float duty_cycle = infrared_get_protocol_duty_cycle(message->protocol);
furi_hal_infrared_async_tx_set_data_isr_callback(infrared_get_data_callback, handler);
furi_hal_infrared_async_tx_start(frequency, duty_cycle);
furi_hal_infrared_async_tx_wait_termination();
infrared_free_encoder(handler);
furi_assert(!furi_hal_infrared_is_busy());
}

View File

@@ -1,5 +1,5 @@
#include <furi_hal_irda.h>
#include <irda.h>
#include <furi_hal_infrared.h>
#include <infrared.h>
#include <stdint.h>
#ifdef __cplusplus
@@ -7,12 +7,12 @@ extern "C" {
#endif
/**
* Send message over IRDA.
* Send message over INFRARED.
*
* \param[in] message - message to send.
* \param[in] times - number of times message should be sent.
*/
void irda_send(const IrdaMessage* message, int times);
void infrared_send(const InfraredMessage* message, int times);
/**
* Send raw data through infrared port.
@@ -22,7 +22,7 @@ void irda_send(const IrdaMessage* message, int times);
* \param[in] start_from_mark - true if timings starts from mark,
* otherwise from space
*/
void irda_send_raw(const uint32_t timings[], uint32_t timings_cnt, bool start_from_mark);
void infrared_send_raw(const uint32_t timings[], uint32_t timings_cnt, bool start_from_mark);
/**
* Send raw data through infrared port, with additional settings.
@@ -34,7 +34,7 @@ void irda_send_raw(const uint32_t timings[], uint32_t timings_cnt, bool start_fr
* \param[in] duty_cycle - duty cycle to generate on PWM
* \param[in] frequency - frequency to generate on PWM
*/
void irda_send_raw_ext(
void infrared_send_raw_ext(
const uint32_t timings[],
uint32_t timings_cnt,
bool start_from_mark,

View File

@@ -0,0 +1,608 @@
#include "furi/check.h"
#include "furi/common_defines.h"
#include "sys/_stdint.h"
#include "infrared_worker.h"
#include <infrared.h>
#include <furi_hal_infrared.h>
#include <limits.h>
#include <stdint.h>
#include <furi.h>
#include <notification/notification_messages.h>
#include <stream_buffer.h>
#define INFRARED_WORKER_RX_TIMEOUT INFRARED_RAW_RX_TIMING_DELAY_US
#define INFRARED_WORKER_RX_RECEIVED 0x01
#define INFRARED_WORKER_RX_TIMEOUT_RECEIVED 0x02
#define INFRARED_WORKER_OVERRUN 0x04
#define INFRARED_WORKER_EXIT 0x08
#define INFRARED_WORKER_TX_FILL_BUFFER 0x10
#define INFRARED_WORKER_TX_MESSAGE_SENT 0x20
#define INFRARED_WORKER_ALL_RX_EVENTS \
(INFRARED_WORKER_RX_RECEIVED | INFRARED_WORKER_RX_TIMEOUT_RECEIVED | \
INFRARED_WORKER_OVERRUN | INFRARED_WORKER_EXIT)
#define INFRARED_WORKER_ALL_TX_EVENTS \
(INFRARED_WORKER_TX_FILL_BUFFER | INFRARED_WORKER_TX_MESSAGE_SENT | INFRARED_WORKER_EXIT)
#define INFRARED_WORKER_ALL_EVENTS (INFRARED_WORKER_ALL_RX_EVENTS | INFRARED_WORKER_ALL_TX_EVENTS)
typedef enum {
InfraredWorkerStateIdle,
InfraredWorkerStateRunRx,
InfraredWorkerStateRunTx,
InfraredWorkerStateWaitTxEnd,
InfraredWorkerStateStopTx,
InfraredWorkerStateStartTx,
} InfraredWorkerState;
struct InfraredWorkerSignal {
bool decoded;
size_t timings_cnt;
union {
InfraredMessage message;
/* +1 is for pause we add at the beginning */
uint32_t timings[MAX_TIMINGS_AMOUNT + 1];
};
};
struct InfraredWorker {
FuriThread* thread;
StreamBufferHandle_t stream;
osEventFlagsId_t events;
InfraredWorkerSignal signal;
InfraredWorkerState state;
InfraredEncoderHandler* infrared_encoder;
InfraredDecoderHandler* infrared_decoder;
NotificationApp* notification;
bool blink_enable;
union {
struct {
InfraredWorkerGetSignalCallback get_signal_callback;
InfraredWorkerMessageSentCallback message_sent_callback;
void* get_signal_context;
void* message_sent_context;
uint32_t frequency;
float duty_cycle;
uint32_t tx_raw_cnt;
bool need_reinitialization;
bool steady_signal_sent;
} tx;
struct {
InfraredWorkerReceivedSignalCallback received_signal_callback;
void* received_signal_context;
bool overrun;
} rx;
};
};
typedef struct {
uint32_t duration;
bool level;
FuriHalInfraredTxGetDataState state;
} InfraredWorkerTiming;
static int32_t infrared_worker_tx_thread(void* context);
static FuriHalInfraredTxGetDataState
infrared_worker_furi_hal_data_isr_callback(void* context, uint32_t* duration, bool* level);
static void infrared_worker_furi_hal_message_sent_isr_callback(void* context);
static void infrared_worker_rx_timeout_callback(void* context) {
InfraredWorker* instance = context;
uint32_t flags_set = osEventFlagsSet(instance->events, INFRARED_WORKER_RX_TIMEOUT_RECEIVED);
furi_check(flags_set & INFRARED_WORKER_RX_TIMEOUT_RECEIVED);
}
static void infrared_worker_rx_callback(void* context, bool level, uint32_t duration) {
InfraredWorker* instance = context;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
furi_assert(duration != 0);
LevelDuration level_duration = level_duration_make(level, duration);
size_t ret = xStreamBufferSendFromISR(
instance->stream, &level_duration, sizeof(LevelDuration), &xHigherPriorityTaskWoken);
uint32_t events = (ret == sizeof(LevelDuration)) ? INFRARED_WORKER_RX_RECEIVED :
INFRARED_WORKER_OVERRUN;
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
uint32_t flags_set = osEventFlagsSet(instance->events, events);
furi_check(flags_set & events);
}
static void infrared_worker_process_timeout(InfraredWorker* instance) {
if(instance->signal.timings_cnt < 2) return;
const InfraredMessage* message_decoded =
infrared_check_decoder_ready(instance->infrared_decoder);
if(message_decoded) {
instance->signal.message = *message_decoded;
instance->signal.timings_cnt = 0;
instance->signal.decoded = true;
} else {
instance->signal.decoded = false;
}
if(instance->rx.received_signal_callback)
instance->rx.received_signal_callback(
instance->rx.received_signal_context, &instance->signal);
}
static void
infrared_worker_process_timings(InfraredWorker* instance, uint32_t duration, bool level) {
const InfraredMessage* message_decoded =
infrared_decode(instance->infrared_decoder, level, duration);
if(message_decoded) {
instance->signal.message = *message_decoded;
instance->signal.timings_cnt = 0;
instance->signal.decoded = true;
if(instance->rx.received_signal_callback)
instance->rx.received_signal_callback(
instance->rx.received_signal_context, &instance->signal);
} else {
/* Skip first timing if it starts from Space */
if((instance->signal.timings_cnt == 0) && !level) {
return;
}
if(instance->signal.timings_cnt < MAX_TIMINGS_AMOUNT) {
instance->signal.timings[instance->signal.timings_cnt] = duration;
++instance->signal.timings_cnt;
} else {
uint32_t flags_set = osEventFlagsSet(instance->events, INFRARED_WORKER_OVERRUN);
furi_check(flags_set & INFRARED_WORKER_OVERRUN);
instance->rx.overrun = true;
}
}
}
static int32_t infrared_worker_rx_thread(void* thread_context) {
InfraredWorker* instance = thread_context;
uint32_t events = 0;
LevelDuration level_duration;
TickType_t last_blink_time = 0;
while(1) {
events =
osEventFlagsWait(instance->events, INFRARED_WORKER_ALL_RX_EVENTS, 0, osWaitForever);
furi_check(events & INFRARED_WORKER_ALL_RX_EVENTS); /* at least one caught */
if(events & INFRARED_WORKER_RX_RECEIVED) {
if(!instance->rx.overrun && instance->blink_enable &&
((xTaskGetTickCount() - last_blink_time) > 80)) {
last_blink_time = xTaskGetTickCount();
notification_message(instance->notification, &sequence_blink_blue_10);
}
if(instance->signal.timings_cnt == 0)
notification_message(instance->notification, &sequence_display_on);
while(sizeof(LevelDuration) ==
xStreamBufferReceive(
instance->stream, &level_duration, sizeof(LevelDuration), 0)) {
if(!instance->rx.overrun) {
bool level = level_duration_get_level(level_duration);
uint32_t duration = level_duration_get_duration(level_duration);
infrared_worker_process_timings(instance, duration, level);
}
}
}
if(events & INFRARED_WORKER_OVERRUN) {
printf("#");
infrared_reset_decoder(instance->infrared_decoder);
instance->signal.timings_cnt = 0;
if(instance->blink_enable)
notification_message(instance->notification, &sequence_set_red_255);
}
if(events & INFRARED_WORKER_RX_TIMEOUT_RECEIVED) {
if(instance->rx.overrun) {
printf("\nOVERRUN, max samples: %d\n", MAX_TIMINGS_AMOUNT);
instance->rx.overrun = false;
if(instance->blink_enable)
notification_message(instance->notification, &sequence_reset_red);
} else {
infrared_worker_process_timeout(instance);
}
instance->signal.timings_cnt = 0;
}
if(events & INFRARED_WORKER_EXIT) break;
}
return 0;
}
void infrared_worker_rx_set_received_signal_callback(
InfraredWorker* instance,
InfraredWorkerReceivedSignalCallback callback,
void* context) {
furi_assert(instance);
instance->rx.received_signal_callback = callback;
instance->rx.received_signal_context = context;
}
InfraredWorker* infrared_worker_alloc() {
InfraredWorker* instance = malloc(sizeof(InfraredWorker));
instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "InfraredWorker");
furi_thread_set_stack_size(instance->thread, 2048);
furi_thread_set_context(instance->thread, instance);
size_t buffer_size =
MAX(sizeof(InfraredWorkerTiming) * (MAX_TIMINGS_AMOUNT + 1),
sizeof(LevelDuration) * MAX_TIMINGS_AMOUNT);
instance->stream = xStreamBufferCreate(buffer_size, sizeof(InfraredWorkerTiming));
instance->infrared_decoder = infrared_alloc_decoder();
instance->infrared_encoder = infrared_alloc_encoder();
instance->blink_enable = false;
instance->notification = furi_record_open("notification");
instance->state = InfraredWorkerStateIdle;
instance->events = osEventFlagsNew(NULL);
return instance;
}
void infrared_worker_free(InfraredWorker* instance) {
furi_assert(instance);
furi_assert(instance->state == InfraredWorkerStateIdle);
furi_record_close("notification");
infrared_free_decoder(instance->infrared_decoder);
infrared_free_encoder(instance->infrared_encoder);
vStreamBufferDelete(instance->stream);
furi_thread_free(instance->thread);
osEventFlagsDelete(instance->events);
free(instance);
}
void infrared_worker_rx_start(InfraredWorker* instance) {
furi_assert(instance);
furi_assert(instance->state == InfraredWorkerStateIdle);
xStreamBufferSetTriggerLevel(instance->stream, sizeof(LevelDuration));
osEventFlagsClear(instance->events, INFRARED_WORKER_ALL_EVENTS);
furi_thread_set_callback(instance->thread, infrared_worker_rx_thread);
furi_thread_start(instance->thread);
furi_hal_infrared_async_rx_set_capture_isr_callback(infrared_worker_rx_callback, instance);
furi_hal_infrared_async_rx_set_timeout_isr_callback(
infrared_worker_rx_timeout_callback, instance);
furi_hal_infrared_async_rx_start();
furi_hal_infrared_async_rx_set_timeout(INFRARED_WORKER_RX_TIMEOUT);
instance->rx.overrun = false;
instance->state = InfraredWorkerStateRunRx;
}
void infrared_worker_rx_stop(InfraredWorker* instance) {
furi_assert(instance);
furi_assert(instance->state == InfraredWorkerStateRunRx);
furi_hal_infrared_async_rx_set_timeout_isr_callback(NULL, NULL);
furi_hal_infrared_async_rx_set_capture_isr_callback(NULL, NULL);
furi_hal_infrared_async_rx_stop();
osEventFlagsSet(instance->events, INFRARED_WORKER_EXIT);
furi_thread_join(instance->thread);
BaseType_t xReturn = xStreamBufferReset(instance->stream);
furi_assert(xReturn == pdPASS);
(void)xReturn;
instance->state = InfraredWorkerStateIdle;
}
bool infrared_worker_signal_is_decoded(const InfraredWorkerSignal* signal) {
furi_assert(signal);
return signal->decoded;
}
void infrared_worker_get_raw_signal(
const InfraredWorkerSignal* signal,
const uint32_t** timings,
size_t* timings_cnt) {
furi_assert(signal);
furi_assert(timings);
furi_assert(timings_cnt);
*timings = signal->timings;
*timings_cnt = signal->timings_cnt;
}
const InfraredMessage* infrared_worker_get_decoded_signal(const InfraredWorkerSignal* signal) {
furi_assert(signal);
return &signal->message;
}
void infrared_worker_rx_enable_blink_on_receiving(InfraredWorker* instance, bool enable) {
furi_assert(instance);
instance->blink_enable = enable;
}
void infrared_worker_tx_start(InfraredWorker* instance) {
furi_assert(instance);
furi_assert(instance->state == InfraredWorkerStateIdle);
furi_assert(instance->tx.get_signal_callback);
// size have to be greater than api hal infrared async tx buffer size
xStreamBufferSetTriggerLevel(instance->stream, sizeof(InfraredWorkerTiming));
osEventFlagsClear(instance->events, INFRARED_WORKER_ALL_EVENTS);
furi_thread_set_callback(instance->thread, infrared_worker_tx_thread);
instance->tx.steady_signal_sent = false;
instance->tx.need_reinitialization = false;
furi_hal_infrared_async_tx_set_data_isr_callback(
infrared_worker_furi_hal_data_isr_callback, instance);
furi_hal_infrared_async_tx_set_signal_sent_isr_callback(
infrared_worker_furi_hal_message_sent_isr_callback, instance);
instance->state = InfraredWorkerStateStartTx;
furi_thread_start(instance->thread);
}
static void infrared_worker_furi_hal_message_sent_isr_callback(void* context) {
InfraredWorker* instance = context;
uint32_t flags_set = osEventFlagsSet(instance->events, INFRARED_WORKER_TX_MESSAGE_SENT);
furi_check(flags_set & INFRARED_WORKER_TX_MESSAGE_SENT);
}
static FuriHalInfraredTxGetDataState
infrared_worker_furi_hal_data_isr_callback(void* context, uint32_t* duration, bool* level) {
furi_assert(context);
furi_assert(duration);
furi_assert(level);
InfraredWorker* instance = context;
InfraredWorkerTiming timing;
FuriHalInfraredTxGetDataState state;
if(sizeof(InfraredWorkerTiming) ==
xStreamBufferReceiveFromISR(instance->stream, &timing, sizeof(InfraredWorkerTiming), 0)) {
*level = timing.level;
*duration = timing.duration;
state = timing.state;
} else {
furi_assert(0);
*level = 0;
*duration = 100;
state = FuriHalInfraredTxGetDataStateDone;
}
uint32_t flags_set = osEventFlagsSet(instance->events, INFRARED_WORKER_TX_FILL_BUFFER);
furi_check(flags_set & INFRARED_WORKER_TX_FILL_BUFFER);
return state;
}
static bool infrared_get_new_signal(InfraredWorker* instance) {
bool new_signal_obtained = false;
InfraredWorkerGetSignalResponse response =
instance->tx.get_signal_callback(instance->tx.get_signal_context, instance);
if(response == InfraredWorkerGetSignalResponseNew) {
uint32_t new_tx_frequency = 0;
float new_tx_duty_cycle = 0;
if(instance->signal.decoded) {
new_tx_frequency = infrared_get_protocol_frequency(instance->signal.message.protocol);
new_tx_duty_cycle =
infrared_get_protocol_duty_cycle(instance->signal.message.protocol);
} else {
furi_assert(instance->signal.timings_cnt > 1);
new_tx_frequency = INFRARED_COMMON_CARRIER_FREQUENCY;
new_tx_duty_cycle = INFRARED_COMMON_DUTY_CYCLE;
}
instance->tx.tx_raw_cnt = 0;
instance->tx.need_reinitialization = (new_tx_frequency != instance->tx.frequency) ||
(new_tx_duty_cycle != instance->tx.duty_cycle);
instance->tx.frequency = new_tx_frequency;
instance->tx.duty_cycle = new_tx_duty_cycle;
if(instance->signal.decoded) {
infrared_reset_encoder(instance->infrared_encoder, &instance->signal.message);
}
new_signal_obtained = true;
} else if(response == InfraredWorkerGetSignalResponseSame) {
new_signal_obtained = true;
/* no need to reinit */
} else if(response == InfraredWorkerGetSignalResponseStop) {
new_signal_obtained = false;
} else {
furi_assert(0);
}
return new_signal_obtained;
}
static bool infrared_worker_tx_fill_buffer(InfraredWorker* instance) {
bool new_data_available = true;
InfraredWorkerTiming timing;
InfraredStatus status = InfraredStatusError;
while(!xStreamBufferIsFull(instance->stream) && !instance->tx.need_reinitialization &&
new_data_available) {
if(instance->signal.decoded) {
status = infrared_encode(instance->infrared_encoder, &timing.duration, &timing.level);
} else {
timing.duration = instance->signal.timings[instance->tx.tx_raw_cnt];
/* raw always starts from Mark, but we fill it with space delay at start */
timing.level = (instance->tx.tx_raw_cnt % 2);
++instance->tx.tx_raw_cnt;
if(instance->tx.tx_raw_cnt >= instance->signal.timings_cnt) {
instance->tx.tx_raw_cnt = 0;
status = InfraredStatusDone;
} else {
status = InfraredStatusOk;
}
}
if(status == InfraredStatusError) {
furi_assert(0);
new_data_available = false;
break;
} else if(status == InfraredStatusOk) {
timing.state = FuriHalInfraredTxGetDataStateOk;
} else if(status == InfraredStatusDone) {
timing.state = FuriHalInfraredTxGetDataStateDone;
new_data_available = infrared_get_new_signal(instance);
if(instance->tx.need_reinitialization || !new_data_available) {
timing.state = FuriHalInfraredTxGetDataStateLastDone;
}
} else {
furi_assert(0);
}
uint32_t written_size =
xStreamBufferSend(instance->stream, &timing, sizeof(InfraredWorkerTiming), 0);
furi_assert(sizeof(InfraredWorkerTiming) == written_size);
(void)written_size;
}
return new_data_available;
}
static int32_t infrared_worker_tx_thread(void* thread_context) {
InfraredWorker* instance = thread_context;
furi_assert(instance->state == InfraredWorkerStateStartTx);
furi_assert(thread_context);
uint32_t events = 0;
bool new_data_available = true;
bool exit = false;
exit = !infrared_get_new_signal(instance);
furi_assert(!exit);
while(!exit) {
switch(instance->state) {
case InfraredWorkerStateStartTx:
instance->tx.need_reinitialization = false;
new_data_available = infrared_worker_tx_fill_buffer(instance);
furi_hal_infrared_async_tx_start(instance->tx.frequency, instance->tx.duty_cycle);
if(!new_data_available) {
instance->state = InfraredWorkerStateStopTx;
} else if(instance->tx.need_reinitialization) {
instance->state = InfraredWorkerStateWaitTxEnd;
} else {
instance->state = InfraredWorkerStateRunTx;
}
break;
case InfraredWorkerStateStopTx:
furi_hal_infrared_async_tx_stop();
exit = true;
break;
case InfraredWorkerStateWaitTxEnd:
furi_hal_infrared_async_tx_wait_termination();
instance->state = InfraredWorkerStateStartTx;
events = osEventFlagsGet(instance->events);
if(events & INFRARED_WORKER_EXIT) {
exit = true;
break;
}
break;
case InfraredWorkerStateRunTx:
events = osEventFlagsWait(
instance->events, INFRARED_WORKER_ALL_TX_EVENTS, 0, osWaitForever);
furi_check(events & INFRARED_WORKER_ALL_TX_EVENTS); /* at least one caught */
if(events & INFRARED_WORKER_EXIT) {
instance->state = InfraredWorkerStateStopTx;
break;
}
if(events & INFRARED_WORKER_TX_FILL_BUFFER) {
infrared_worker_tx_fill_buffer(instance);
if(instance->tx.need_reinitialization) {
instance->state = InfraredWorkerStateWaitTxEnd;
}
}
if(events & INFRARED_WORKER_TX_MESSAGE_SENT) {
if(instance->tx.message_sent_callback)
instance->tx.message_sent_callback(instance->tx.message_sent_context);
}
break;
default:
furi_assert(0);
break;
}
}
return 0;
}
void infrared_worker_tx_set_get_signal_callback(
InfraredWorker* instance,
InfraredWorkerGetSignalCallback callback,
void* context) {
furi_assert(instance);
instance->tx.get_signal_callback = callback;
instance->tx.get_signal_context = context;
}
void infrared_worker_tx_set_signal_sent_callback(
InfraredWorker* instance,
InfraredWorkerMessageSentCallback callback,
void* context) {
furi_assert(instance);
instance->tx.message_sent_callback = callback;
instance->tx.message_sent_context = context;
}
void infrared_worker_tx_stop(InfraredWorker* instance) {
furi_assert(instance);
furi_assert(instance->state != InfraredWorkerStateRunRx);
osEventFlagsSet(instance->events, INFRARED_WORKER_EXIT);
furi_thread_join(instance->thread);
furi_hal_infrared_async_tx_set_data_isr_callback(NULL, NULL);
furi_hal_infrared_async_tx_set_signal_sent_isr_callback(NULL, NULL);
instance->signal.timings_cnt = 0;
BaseType_t xReturn = pdFAIL;
xReturn = xStreamBufferReset(instance->stream);
furi_assert(xReturn == pdPASS);
(void)xReturn;
instance->state = InfraredWorkerStateIdle;
}
void infrared_worker_set_decoded_signal(InfraredWorker* instance, const InfraredMessage* message) {
furi_assert(instance);
furi_assert(message);
instance->signal.decoded = true;
instance->signal.message = *message;
}
void infrared_worker_set_raw_signal(
InfraredWorker* instance,
const uint32_t* timings,
size_t timings_cnt) {
furi_assert(instance);
furi_assert(timings);
furi_assert(timings_cnt > 0);
size_t max_copy_num = COUNT_OF(instance->signal.timings) - 1;
furi_check(timings_cnt <= max_copy_num);
instance->signal.timings[0] = INFRARED_RAW_TX_TIMING_DELAY_US;
memcpy(&instance->signal.timings[1], timings, timings_cnt * sizeof(uint32_t));
instance->signal.decoded = false;
instance->signal.timings_cnt = timings_cnt + 1;
}
InfraredWorkerGetSignalResponse
infrared_worker_tx_get_signal_steady_callback(void* context, InfraredWorker* instance) {
InfraredWorkerGetSignalResponse response = instance->tx.steady_signal_sent ?
InfraredWorkerGetSignalResponseSame :
InfraredWorkerGetSignalResponseNew;
instance->tx.steady_signal_sent = true;
return response;
}

View File

@@ -0,0 +1,175 @@
#pragma once
#include <infrared.h>
#include <furi_hal.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_TIMINGS_AMOUNT 512
/** Interface struct of infrared worker */
typedef struct InfraredWorker InfraredWorker;
/** Interface struct of received signal */
typedef struct InfraredWorkerSignal InfraredWorkerSignal;
typedef enum {
InfraredWorkerGetSignalResponseNew, /** Signal, provided by callback is new and encoder should be reseted */
InfraredWorkerGetSignalResponseSame, /** Signal, provided by callback is same. No encoder resetting. */
InfraredWorkerGetSignalResponseStop, /** No more signals available. */
} InfraredWorkerGetSignalResponse;
/** Callback type for providing next signal to send. Should be used with
* infrared_worker_make_decoded_signal() or infrared_worker_make_raw_signal()
*/
typedef InfraredWorkerGetSignalResponse (
*InfraredWorkerGetSignalCallback)(void* context, InfraredWorker* instance);
/** Callback type for 'message is sent' event */
typedef void (*InfraredWorkerMessageSentCallback)(void* context);
/** Callback type to call by InfraredWorker thread when new signal is received */
typedef void (
*InfraredWorkerReceivedSignalCallback)(void* context, InfraredWorkerSignal* received_signal);
/** Allocate InfraredWorker
*
* @return just created instance of InfraredWorker
*/
InfraredWorker* infrared_worker_alloc();
/** Free InfraredWorker
*
* @param[in] instance - InfraredWorker instance
*/
void infrared_worker_free(InfraredWorker* instance);
/** Start InfraredWorker thread, initialise furi_hal, prepare all work.
*
* @param[in] instance - InfraredWorker instance
*/
void infrared_worker_rx_start(InfraredWorker* instance);
/** Stop InfraredWorker thread, deinitialize furi_hal.
*
* @param[in] instance - InfraredWorker instance
*/
void infrared_worker_rx_stop(InfraredWorker* instance);
/** Set received data callback InfraredWorker
*
* @param[in] instance - InfraredWorker instance
* @param[in] context - context to pass to callbacks
* @param[in] callback - InfraredWorkerReceivedSignalCallback callback
*/
void infrared_worker_rx_set_received_signal_callback(
InfraredWorker* instance,
InfraredWorkerReceivedSignalCallback callback,
void* context);
/** Enable blinking on receiving any signal on IR port.
*
* @param[in] instance - instance of InfraredWorker
* @param[in] enable - true if you want to enable blinking
* false otherwise
*/
void infrared_worker_rx_enable_blink_on_receiving(InfraredWorker* instance, bool enable);
/** Clarify is received signal either decoded or raw
*
* @param[in] signal - received signal
* @return true if signal is decoded, false if signal is raw
*/
bool infrared_worker_signal_is_decoded(const InfraredWorkerSignal* signal);
/** Start transmitting signal. Callback InfraredWorkerGetSignalCallback should be
* set before this function is called, as it calls for it to fill buffer before
* starting transmission.
*
* @param[in] instance - InfraredWorker instance
*/
void infrared_worker_tx_start(InfraredWorker* instance);
/** Stop transmitting signal. Waits for end of current signal and stops transmission.
*
* @param[in] instance - InfraredWorker instance
*/
void infrared_worker_tx_stop(InfraredWorker* instance);
/** Set callback for providing next signal to send
*
* @param[in] instance - InfraredWorker instance
* @param[in] context - context to pass to callbacks
* @param[in] callback - InfraredWorkerGetSignalCallback callback
*/
void infrared_worker_tx_set_get_signal_callback(
InfraredWorker* instance,
InfraredWorkerGetSignalCallback callback,
void* context);
/** Set callback for end of signal transmitting
*
* @param[in] instance - InfraredWorker instance
* @param[in] context - context to pass to callbacks
* @param[in] callback - InfraredWorkerMessageSentCallback callback
*/
void infrared_worker_tx_set_signal_sent_callback(
InfraredWorker* instance,
InfraredWorkerMessageSentCallback callback,
void* context);
/** Callback to pass to infrared_worker_tx_set_get_signal_callback() if signal
* is steady and will not be changed between infrared_worker start and stop.
* Before starting transmission, desired steady signal must be set with
* infrared_worker_make_decoded_signal() or infrared_worker_make_raw_signal().
*
* This function should not be implicitly called.
*
* @param[in] context - context
* @param[out] instance - InfraredWorker instance
*/
InfraredWorkerGetSignalResponse
infrared_worker_tx_get_signal_steady_callback(void* context, InfraredWorker* instance);
/** Acquire raw signal from interface struct 'InfraredWorkerSignal'.
* First, you have to ensure that signal is raw.
*
* @param[in] signal - received signal
* @param[out] timings - pointer to array of timings
* @param[out] timings_cnt - pointer to amount of timings
*/
void infrared_worker_get_raw_signal(
const InfraredWorkerSignal* signal,
const uint32_t** timings,
size_t* timings_cnt);
/** Acquire decoded message from interface struct 'InfraredWorkerSignal'.
* First, you have to ensure that signal is decoded.
*
* @param[in] signal - received signal
* @return decoded INFRARED message
*/
const InfraredMessage* infrared_worker_get_decoded_signal(const InfraredWorkerSignal* signal);
/** Set current decoded signal for InfraredWorker instance
*
* @param[out] instance - InfraredWorker instance
* @param[in] message - decoded signal
*/
void infrared_worker_set_decoded_signal(InfraredWorker* instance, const InfraredMessage* message);
/** Set current raw signal for InfraredWorker instance
*
* @param[out] instance - InfraredWorker instance
* @param[in] timings - array of raw timings
* @param[in] timings_cnt - size of array of raw timings
*/
void infrared_worker_set_raw_signal(
InfraredWorker* instance,
const uint32_t* timings,
size_t timings_cnt);
#ifdef __cplusplus
}
#endif

View File

@@ -1,81 +0,0 @@
#pragma once
#include <stdint.h>
#include "irda.h"
#include "irda_i.h"
#define MATCH_TIMING(x, v, delta) (((x) < (v + delta)) && ((x) > (v - delta)))
typedef struct IrdaCommonDecoder IrdaCommonDecoder;
typedef struct IrdaCommonEncoder IrdaCommonEncoder;
typedef IrdaStatus (*IrdaCommonDecode)(IrdaCommonDecoder*, bool, uint32_t);
typedef IrdaStatus (*IrdaCommonDecodeRepeat)(IrdaCommonDecoder*);
typedef bool (*IrdaCommonInterpret)(IrdaCommonDecoder*);
typedef IrdaStatus (*IrdaCommonEncode)(IrdaCommonEncoder* encoder, uint32_t* out, bool* polarity);
typedef struct {
IrdaTimings timings;
bool manchester_start_from_space;
bool no_stop_bit;
uint8_t databit_len[4];
IrdaCommonDecode decode;
IrdaCommonDecodeRepeat decode_repeat;
IrdaCommonInterpret interpret;
IrdaCommonEncode encode;
IrdaCommonEncode encode_repeat;
} IrdaCommonProtocolSpec;
typedef enum {
IrdaCommonDecoderStateWaitPreamble,
IrdaCommonDecoderStateDecode,
IrdaCommonDecoderStateProcessRepeat,
} IrdaCommonStateDecoder;
typedef enum {
IrdaCommonEncoderStateSilence,
IrdaCommonEncoderStatePreamble,
IrdaCommonEncoderStateEncode,
IrdaCommonEncoderStateEncodeRepeat,
} IrdaCommonStateEncoder;
struct IrdaCommonDecoder {
const IrdaCommonProtocolSpec* protocol;
void* context;
uint32_t timings[6];
IrdaMessage message;
IrdaCommonStateDecoder state;
uint8_t timings_cnt;
bool switch_detect;
bool level;
uint16_t databit_cnt;
uint8_t data[];
};
struct IrdaCommonEncoder {
const IrdaCommonProtocolSpec* protocol;
IrdaCommonStateEncoder 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[];
};
IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration);
IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder, bool level, uint32_t timing);
IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder, bool level, uint32_t timing);
void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol);
void irda_common_decoder_free(IrdaCommonDecoder* decoder);
void irda_common_decoder_reset(IrdaCommonDecoder* decoder);
IrdaMessage* irda_common_decoder_check_ready(IrdaCommonDecoder* decoder);
IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bool* polarity);
IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duration, bool* polarity);
IrdaStatus
irda_common_encode_manchester(IrdaCommonEncoder* encoder, uint32_t* duration, bool* polarity);
void* irda_common_encoder_alloc(const IrdaCommonProtocolSpec* protocol);
void irda_common_encoder_free(IrdaCommonEncoder* encoder);
void irda_common_encoder_reset(IrdaCommonEncoder* encoder);

View File

@@ -1,117 +0,0 @@
#include "irda_common_i.h"
#include "irda_protocol_defs_i.h"
const IrdaCommonProtocolSpec protocol_nec = {
.timings =
{
.preamble_mark = IRDA_NEC_PREAMBLE_MARK,
.preamble_space = IRDA_NEC_PREAMBLE_SPACE,
.bit1_mark = IRDA_NEC_BIT1_MARK,
.bit1_space = IRDA_NEC_BIT1_SPACE,
.bit0_mark = IRDA_NEC_BIT0_MARK,
.bit0_space = IRDA_NEC_BIT0_SPACE,
.preamble_tolerance = IRDA_NEC_PREAMBLE_TOLERANCE,
.bit_tolerance = IRDA_NEC_BIT_TOLERANCE,
.silence_time = IRDA_NEC_SILENCE,
.min_split_time = IRDA_NEC_MIN_SPLIT_TIME,
},
.databit_len[0] = 42,
.databit_len[1] = 32,
.no_stop_bit = false,
.decode = irda_common_decode_pdwm,
.encode = irda_common_encode_pdwm,
.interpret = irda_decoder_nec_interpret,
.decode_repeat = irda_decoder_nec_decode_repeat,
.encode_repeat = irda_encoder_nec_encode_repeat,
};
const IrdaCommonProtocolSpec protocol_samsung32 = {
.timings =
{
.preamble_mark = IRDA_SAMSUNG_PREAMBLE_MARK,
.preamble_space = IRDA_SAMSUNG_PREAMBLE_SPACE,
.bit1_mark = IRDA_SAMSUNG_BIT1_MARK,
.bit1_space = IRDA_SAMSUNG_BIT1_SPACE,
.bit0_mark = IRDA_SAMSUNG_BIT0_MARK,
.bit0_space = IRDA_SAMSUNG_BIT0_SPACE,
.preamble_tolerance = IRDA_SAMSUNG_PREAMBLE_TOLERANCE,
.bit_tolerance = IRDA_SAMSUNG_BIT_TOLERANCE,
.silence_time = IRDA_SAMSUNG_SILENCE,
.min_split_time = IRDA_SAMSUNG_MIN_SPLIT_TIME,
},
.databit_len[0] = 32,
.no_stop_bit = false,
.decode = irda_common_decode_pdwm,
.encode = irda_common_encode_pdwm,
.interpret = irda_decoder_samsung32_interpret,
.decode_repeat = irda_decoder_samsung32_decode_repeat,
.encode_repeat = irda_encoder_samsung32_encode_repeat,
};
const IrdaCommonProtocolSpec protocol_rc6 = {
.timings =
{
.preamble_mark = IRDA_RC6_PREAMBLE_MARK,
.preamble_space = IRDA_RC6_PREAMBLE_SPACE,
.bit1_mark = IRDA_RC6_BIT,
.preamble_tolerance = IRDA_RC6_PREAMBLE_TOLERANCE,
.bit_tolerance = IRDA_RC6_BIT_TOLERANCE,
.silence_time = IRDA_RC6_SILENCE,
.min_split_time = IRDA_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 = irda_decoder_rc6_decode_manchester,
.encode = irda_encoder_rc6_encode_manchester,
.interpret = irda_decoder_rc6_interpret,
.decode_repeat = NULL,
.encode_repeat = NULL,
};
const IrdaCommonProtocolSpec protocol_rc5 = {
.timings =
{
.preamble_mark = 0,
.preamble_space = 0,
.bit1_mark = IRDA_RC5_BIT,
.preamble_tolerance = 0,
.bit_tolerance = IRDA_RC5_BIT_TOLERANCE,
.silence_time = IRDA_RC5_SILENCE,
.min_split_time = IRDA_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 = irda_common_decode_manchester,
.encode = irda_common_encode_manchester,
.interpret = irda_decoder_rc5_interpret,
.decode_repeat = NULL,
.encode_repeat = NULL,
};
const IrdaCommonProtocolSpec protocol_sirc = {
.timings =
{
.preamble_mark = IRDA_SIRC_PREAMBLE_MARK,
.preamble_space = IRDA_SIRC_PREAMBLE_SPACE,
.bit1_mark = IRDA_SIRC_BIT1_MARK,
.bit1_space = IRDA_SIRC_BIT1_SPACE,
.bit0_mark = IRDA_SIRC_BIT0_MARK,
.bit0_space = IRDA_SIRC_BIT0_SPACE,
.preamble_tolerance = IRDA_SIRC_PREAMBLE_TOLERANCE,
.bit_tolerance = IRDA_SIRC_BIT_TOLERANCE,
.silence_time = IRDA_SIRC_SILENCE,
.min_split_time = IRDA_SIRC_MIN_SPLIT_TIME,
},
.databit_len[0] = 20,
.databit_len[1] = 15,
.databit_len[2] = 12,
.no_stop_bit = true,
.decode = irda_common_decode_pdwm,
.encode = irda_common_encode_pdwm,
.interpret = irda_decoder_sirc_interpret,
.decode_repeat = NULL,
.encode_repeat = irda_encoder_sirc_encode_repeat,
};

View File

@@ -1,298 +0,0 @@
#include "irda.h"
#include "furi/check.h"
#include "common/irda_common_i.h"
#include "irda_protocol_defs_i.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <furi.h>
#include "irda_i.h"
#include <furi_hal_irda.h>
typedef struct {
IrdaAlloc alloc;
IrdaDecode decode;
IrdaDecoderReset reset;
IrdaFree free;
IrdaDecoderCheckReady check_ready;
} IrdaDecoders;
typedef struct {
IrdaAlloc alloc;
IrdaEncode encode;
IrdaEncoderReset reset;
IrdaFree free;
} IrdaEncoders;
struct IrdaDecoderHandler {
void** ctx;
};
struct IrdaEncoderHandler {
void* handler;
const IrdaEncoders* encoder;
};
typedef struct {
IrdaEncoders encoder;
IrdaDecoders decoder;
IrdaGetProtocolSpec get_protocol_spec;
} IrdaEncoderDecoder;
static const IrdaEncoderDecoder irda_encoder_decoder[] = {
{
.decoder =
{.alloc = irda_decoder_nec_alloc,
.decode = irda_decoder_nec_decode,
.reset = irda_decoder_nec_reset,
.check_ready = irda_decoder_nec_check_ready,
.free = irda_decoder_nec_free},
.encoder =
{.alloc = irda_encoder_nec_alloc,
.encode = irda_encoder_nec_encode,
.reset = irda_encoder_nec_reset,
.free = irda_encoder_nec_free},
.get_protocol_spec = irda_nec_get_spec,
},
{
.decoder =
{.alloc = irda_decoder_samsung32_alloc,
.decode = irda_decoder_samsung32_decode,
.reset = irda_decoder_samsung32_reset,
.check_ready = irda_decoder_samsung32_check_ready,
.free = irda_decoder_samsung32_free},
.encoder =
{.alloc = irda_encoder_samsung32_alloc,
.encode = irda_encoder_samsung32_encode,
.reset = irda_encoder_samsung32_reset,
.free = irda_encoder_samsung32_free},
.get_protocol_spec = irda_samsung32_get_spec,
},
{
.decoder =
{.alloc = irda_decoder_rc5_alloc,
.decode = irda_decoder_rc5_decode,
.reset = irda_decoder_rc5_reset,
.check_ready = irda_decoder_rc5_check_ready,
.free = irda_decoder_rc5_free},
.encoder =
{.alloc = irda_encoder_rc5_alloc,
.encode = irda_encoder_rc5_encode,
.reset = irda_encoder_rc5_reset,
.free = irda_encoder_rc5_free},
.get_protocol_spec = irda_rc5_get_spec,
},
{
.decoder =
{.alloc = irda_decoder_rc6_alloc,
.decode = irda_decoder_rc6_decode,
.reset = irda_decoder_rc6_reset,
.check_ready = irda_decoder_rc6_check_ready,
.free = irda_decoder_rc6_free},
.encoder =
{.alloc = irda_encoder_rc6_alloc,
.encode = irda_encoder_rc6_encode,
.reset = irda_encoder_rc6_reset,
.free = irda_encoder_rc6_free},
.get_protocol_spec = irda_rc6_get_spec,
},
{
.decoder =
{.alloc = irda_decoder_sirc_alloc,
.decode = irda_decoder_sirc_decode,
.reset = irda_decoder_sirc_reset,
.check_ready = irda_decoder_sirc_check_ready,
.free = irda_decoder_sirc_free},
.encoder =
{.alloc = irda_encoder_sirc_alloc,
.encode = irda_encoder_sirc_encode,
.reset = irda_encoder_sirc_reset,
.free = irda_encoder_sirc_free},
.get_protocol_spec = irda_sirc_get_spec,
},
};
static int irda_find_index_by_protocol(IrdaProtocol protocol);
static const IrdaProtocolSpecification* irda_get_spec_by_protocol(IrdaProtocol protocol);
const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration) {
furi_assert(handler);
IrdaMessage* message = NULL;
IrdaMessage* result = NULL;
for(int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
if(irda_encoder_decoder[i].decoder.decode) {
message = irda_encoder_decoder[i].decoder.decode(handler->ctx[i], level, duration);
if(!result && message) {
result = message;
}
}
}
return result;
}
IrdaDecoderHandler* irda_alloc_decoder(void) {
IrdaDecoderHandler* handler = malloc(sizeof(IrdaDecoderHandler));
handler->ctx = malloc(sizeof(void*) * COUNT_OF(irda_encoder_decoder));
for(int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
handler->ctx[i] = 0;
if(irda_encoder_decoder[i].decoder.alloc)
handler->ctx[i] = irda_encoder_decoder[i].decoder.alloc();
}
irda_reset_decoder(handler);
return handler;
}
void irda_free_decoder(IrdaDecoderHandler* handler) {
furi_assert(handler);
furi_assert(handler->ctx);
for(int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
if(irda_encoder_decoder[i].decoder.free)
irda_encoder_decoder[i].decoder.free(handler->ctx[i]);
}
free(handler->ctx);
free(handler);
}
void irda_reset_decoder(IrdaDecoderHandler* handler) {
for(int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
if(irda_encoder_decoder[i].decoder.reset)
irda_encoder_decoder[i].decoder.reset(handler->ctx[i]);
}
}
const IrdaMessage* irda_check_decoder_ready(IrdaDecoderHandler* handler) {
furi_assert(handler);
IrdaMessage* message = NULL;
IrdaMessage* result = NULL;
for(int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
if(irda_encoder_decoder[i].decoder.check_ready) {
message = irda_encoder_decoder[i].decoder.check_ready(handler->ctx[i]);
if(!result && message) {
result = message;
}
}
}
return result;
}
IrdaEncoderHandler* irda_alloc_encoder(void) {
IrdaEncoderHandler* handler = malloc(sizeof(IrdaEncoderHandler));
handler->handler = NULL;
handler->encoder = NULL;
return handler;
}
void irda_free_encoder(IrdaEncoderHandler* handler) {
furi_assert(handler);
const IrdaEncoders* 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 irda_find_index_by_protocol(IrdaProtocol protocol) {
for(int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
if(irda_encoder_decoder[i].get_protocol_spec(protocol)) {
return i;
}
}
return -1;
}
void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message) {
furi_assert(handler);
furi_assert(message);
int index = irda_find_index_by_protocol(message->protocol);
furi_check(index >= 0);
const IrdaEncoders* required_encoder = &irda_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);
}
IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level) {
furi_assert(handler);
furi_assert(duration);
furi_assert(level);
const IrdaEncoders* encoder = handler->encoder;
furi_assert(encoder);
furi_assert(encoder->encode);
IrdaStatus status = encoder->encode(handler->handler, duration, level);
furi_assert(status != IrdaStatusError);
return status;
}
bool irda_is_protocol_valid(IrdaProtocol protocol) {
return irda_find_index_by_protocol(protocol) >= 0;
}
IrdaProtocol irda_get_protocol_by_name(const char* protocol_name) {
for(IrdaProtocol protocol = 0; protocol < IrdaProtocolMAX; ++protocol) {
const char* name = irda_get_protocol_name(protocol);
if(!strcmp(name, protocol_name)) return protocol;
}
return IrdaProtocolUnknown;
}
static const IrdaProtocolSpecification* irda_get_spec_by_protocol(IrdaProtocol protocol) {
int index = irda_find_index_by_protocol(protocol);
const IrdaProtocolSpecification* spec = NULL;
if(index >= 0) {
spec = irda_encoder_decoder[index].get_protocol_spec(protocol);
}
furi_assert(spec);
return spec;
}
const char* irda_get_protocol_name(IrdaProtocol protocol) {
return irda_get_spec_by_protocol(protocol)->name;
}
uint8_t irda_get_protocol_address_length(IrdaProtocol protocol) {
return irda_get_spec_by_protocol(protocol)->address_length;
}
uint8_t irda_get_protocol_command_length(IrdaProtocol protocol) {
return irda_get_spec_by_protocol(protocol)->command_length;
}
uint32_t irda_get_protocol_frequency(IrdaProtocol protocol) {
return irda_get_spec_by_protocol(protocol)->frequency;
}
float irda_get_protocol_duty_cycle(IrdaProtocol protocol) {
return irda_get_spec_by_protocol(protocol)->duty_cycle;
}

View File

@@ -1,204 +0,0 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define IRDA_COMMON_CARRIER_FREQUENCY ((uint32_t)38000)
#define IRDA_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 IRDA_RAW_RX_TIMING_DELAY_US 150000
#define IRDA_RAW_TX_TIMING_DELAY_US 180000
typedef struct IrdaDecoderHandler IrdaDecoderHandler;
typedef struct IrdaEncoderHandler IrdaEncoderHandler;
typedef enum {
IrdaProtocolUnknown = -1,
IrdaProtocolNEC = 0,
IrdaProtocolNECext,
IrdaProtocolNEC42,
IrdaProtocolNEC42ext,
IrdaProtocolSamsung32,
IrdaProtocolRC6,
IrdaProtocolRC5,
IrdaProtocolRC5X,
IrdaProtocolSIRC,
IrdaProtocolSIRC15,
IrdaProtocolSIRC20,
IrdaProtocolMAX,
} IrdaProtocol;
typedef struct {
IrdaProtocol protocol;
uint32_t address;
uint32_t command;
bool repeat;
} IrdaMessage;
typedef enum {
IrdaStatusError,
IrdaStatusOk,
IrdaStatusDone,
IrdaStatusReady,
} IrdaStatus;
/**
* Initialize decoder.
*
* \return returns pointer to IRDA decoder handler if success, otherwise - error.
*/
IrdaDecoderHandler* irda_alloc_decoder(void);
/**
* Provide to decoder next timing.
*
* \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
* \param[in] level - high(true) or low(false) level of input signal to analyze.
* it should alternate every call, otherwise it is an error case,
* and decoder resets its state and start decoding from the start.
* \param[in] duration - duration of steady high/low input signal.
* \return if message is ready, returns pointer to decoded message, returns NULL.
* Note: ownership of returned ptr belongs to handler. So pointer is valid
* up to next irda_free_decoder(), irda_reset_decoder(),
* irda_decode(), irda_check_decoder_ready() calls.
*/
const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration);
/**
* Check whether decoder is ready.
* Functionality is quite similar to irda_decode(), but with no timing providing.
* Some protocols (e.g. Sony SIRC) has variable payload length, which means we
* can't recognize end of message right after receiving last bit. That's why
* application should call to irda_check_decoder_ready() after some timeout to
* retrieve decoded message, if so.
*
* \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
* \return if message is ready, returns pointer to decoded message, returns NULL.
* Note: ownership of returned ptr belongs to handler. So pointer is valid
* up to next irda_free_decoder(), irda_reset_decoder(),
* irda_decode(), irda_check_decoder_ready() calls.
*/
const IrdaMessage* irda_check_decoder_ready(IrdaDecoderHandler* handler);
/**
* Deinitialize decoder and free allocated memory.
*
* \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
*/
void irda_free_decoder(IrdaDecoderHandler* handler);
/**
* Reset IRDA decoder.
*
* \param[in] handler - handler to IRDA decoders. Should be acquired with \c irda_alloc_decoder().
*/
void irda_reset_decoder(IrdaDecoderHandler* handler);
/**
* Get protocol name by protocol enum.
*
* \param[in] protocol - protocol identifier.
* \return string to protocol name.
*/
const char* irda_get_protocol_name(IrdaProtocol protocol);
/**
* Get protocol enum by protocol name.
*
* \param[in] protocol_name - string to protocol name.
* \return protocol identifier.
*/
IrdaProtocol irda_get_protocol_by_name(const char* protocol_name);
/**
* Get address length by protocol enum.
*
* \param[in] protocol - protocol identifier.
* \return length of address in bits.
*/
uint8_t irda_get_protocol_address_length(IrdaProtocol protocol);
/**
* Get command length by protocol enum.
*
* \param[in] protocol - protocol identifier.
* \return length of command in bits.
*/
uint8_t irda_get_protocol_command_length(IrdaProtocol protocol);
/**
* Checks whether protocol valid.
*
* \param[in] protocol - protocol identifier.
* \return true if protocol is valid, false otherwise.
*/
bool irda_is_protocol_valid(IrdaProtocol protocol);
/**
* Allocate IRDA encoder.
*
* \return encoder handler.
*/
IrdaEncoderHandler* irda_alloc_encoder(void);
/**
* Free encoder handler previously allocated with \c irda_alloc_encoder().
*
* \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
*/
void irda_free_encoder(IrdaEncoderHandler* handler);
/**
* Encode previously set IRDA message.
* Usage:
* 1) alloc with \c irda_alloc_encoder()
* 2) set message to encode with \c irda_reset_encoder()
* 3) call for \c irda_encode() to continuously get one at a time timings.
* 4) when \c irda_encode() returns IrdaStatusDone, it means new message is fully encoded.
* 5) to encode additional timings, just continue calling \c irda_encode().
*
* \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
* \param[out] duration - encoded timing.
* \param[out] level - encoded level.
*
* \return status of encode operation.
*/
IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level);
/**
* Reset IRDA encoder and set new message to encode. If it's not called after receiveing
* IrdaStatusDone in \c irda_encode(), encoder will encode repeat messages
* till the end of time.
*
* \param[in] handler - handler to IRDA encoder. Should be acquired with \c irda_alloc_encoder().
* \param[in] message - message to encode.
*/
void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message);
/**
* Get PWM frequency value for selected protocol
*
* \param[in] protocol - protocol to get from PWM frequency
*
* \return frequency
*/
uint32_t irda_get_protocol_frequency(IrdaProtocol protocol);
/**
* Get PWM duty cycle value for selected protocol
*
* \param[in] protocol - protocol to get from PWM duty cycle
*
* \return duty cycle
*/
float irda_get_protocol_duty_cycle(IrdaProtocol protocol);
#ifdef __cplusplus
}
#endif

View File

@@ -1,262 +0,0 @@
#pragma once
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include "irda.h"
#include "common/irda_common_i.h"
/***************************************************************************************************
* NEC protocol description
* https://radioparty.ru/manuals/encyclopedia/213-ircontrol?start=1
****************************************************************************************************
* Preamble Preamble Pulse Distance/Width Pause Preamble Preamble Stop
* mark space Modulation up to period repeat repeat bit
* mark space
*
* 9000 4500 32 bit + stop bit ...110000 9000 2250
* __________ _ _ _ _ _ _ _ _ _ _ _ _ _ ___________ _
* ____ __________ _ _ _ __ __ __ _ _ __ __ _ _ ________________ ____________ ___
*
***************************************************************************************************/
#define IRDA_NEC_PREAMBLE_MARK 9000
#define IRDA_NEC_PREAMBLE_SPACE 4500
#define IRDA_NEC_BIT1_MARK 560
#define IRDA_NEC_BIT1_SPACE 1690
#define IRDA_NEC_BIT0_MARK 560
#define IRDA_NEC_BIT0_SPACE 560
#define IRDA_NEC_REPEAT_PERIOD 110000
#define IRDA_NEC_SILENCE IRDA_NEC_REPEAT_PERIOD
#define IRDA_NEC_MIN_SPLIT_TIME IRDA_NEC_REPEAT_PAUSE_MIN
#define IRDA_NEC_REPEAT_PAUSE_MIN 4000
#define IRDA_NEC_REPEAT_PAUSE_MAX 150000
#define IRDA_NEC_REPEAT_MARK 9000
#define IRDA_NEC_REPEAT_SPACE 2250
#define IRDA_NEC_PREAMBLE_TOLERANCE 200 // us
#define IRDA_NEC_BIT_TOLERANCE 120 // us
void* irda_decoder_nec_alloc(void);
void irda_decoder_nec_reset(void* decoder);
void irda_decoder_nec_free(void* decoder);
IrdaMessage* irda_decoder_nec_check_ready(void* decoder);
IrdaMessage* irda_decoder_nec_decode(void* decoder, bool level, uint32_t duration);
void* irda_encoder_nec_alloc(void);
IrdaStatus irda_encoder_nec_encode(void* encoder_ptr, uint32_t* duration, bool* level);
void irda_encoder_nec_reset(void* encoder_ptr, const IrdaMessage* message);
void irda_encoder_nec_free(void* encoder_ptr);
bool irda_decoder_nec_interpret(IrdaCommonDecoder* decoder);
IrdaStatus irda_decoder_nec_decode_repeat(IrdaCommonDecoder* decoder);
IrdaStatus
irda_encoder_nec_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level);
const IrdaProtocolSpecification* irda_nec_get_spec(IrdaProtocol protocol);
extern const IrdaCommonProtocolSpec protocol_nec;
/***************************************************************************************************
* SAMSUNG32 protocol description
* https://www.mikrocontroller.net/articles/IRMP_-_english#SAMSUNG
****************************************************************************************************
* Preamble Preamble Pulse Distance/Width Pause Preamble Preamble Bit1 Stop
* mark space Modulation repeat repeat bit
* mark space
*
* 4500 4500 32 bit + stop bit 40000/100000 4500 4500
* __________ _ _ _ _ _ _ _ _ _ _ _ ___________ _ _
* _ __________ __ _ __ __ __ _ _ __ __ _ ________________ ____________ ____ ___
*
***************************************************************************************************/
#define IRDA_SAMSUNG_PREAMBLE_MARK 4500
#define IRDA_SAMSUNG_PREAMBLE_SPACE 4500
#define IRDA_SAMSUNG_BIT1_MARK 550
#define IRDA_SAMSUNG_BIT1_SPACE 1650
#define IRDA_SAMSUNG_BIT0_MARK 550
#define IRDA_SAMSUNG_BIT0_SPACE 550
#define IRDA_SAMSUNG_REPEAT_PAUSE_MIN 30000
#define IRDA_SAMSUNG_REPEAT_PAUSE1 46000
#define IRDA_SAMSUNG_REPEAT_PAUSE2 97000
/* Samsung silence have to be greater than REPEAT MAX
* otherwise there can be problems during unit tests parsing
* of some data. Real tolerances we don't know, but in real life
* silence time should be greater than max repeat time. This is
* because of similar preambule timings for repeat and first messages. */
#define IRDA_SAMSUNG_MIN_SPLIT_TIME 5000
#define IRDA_SAMSUNG_SILENCE 145000
#define IRDA_SAMSUNG_REPEAT_PAUSE_MAX 140000
#define IRDA_SAMSUNG_REPEAT_MARK 4500
#define IRDA_SAMSUNG_REPEAT_SPACE 4500
#define IRDA_SAMSUNG_PREAMBLE_TOLERANCE 200 // us
#define IRDA_SAMSUNG_BIT_TOLERANCE 120 // us
void* irda_decoder_samsung32_alloc(void);
void irda_decoder_samsung32_reset(void* decoder);
void irda_decoder_samsung32_free(void* decoder);
IrdaMessage* irda_decoder_samsung32_check_ready(void* ctx);
IrdaMessage* irda_decoder_samsung32_decode(void* decoder, bool level, uint32_t duration);
IrdaStatus irda_encoder_samsung32_encode(void* encoder_ptr, uint32_t* duration, bool* level);
void irda_encoder_samsung32_reset(void* encoder_ptr, const IrdaMessage* message);
void* irda_encoder_samsung32_alloc(void);
void irda_encoder_samsung32_free(void* encoder_ptr);
bool irda_decoder_samsung32_interpret(IrdaCommonDecoder* decoder);
IrdaStatus irda_decoder_samsung32_decode_repeat(IrdaCommonDecoder* decoder);
IrdaStatus irda_encoder_samsung32_encode_repeat(
IrdaCommonEncoder* encoder,
uint32_t* duration,
bool* level);
const IrdaProtocolSpecification* irda_samsung32_get_spec(IrdaProtocol protocol);
extern const IrdaCommonProtocolSpec protocol_samsung32;
/***************************************************************************************************
* RC6 protocol description
* https://www.mikrocontroller.net/articles/IRMP_-_english#RC6_.2B_RC6A
****************************************************************************************************
* Preamble Manchester/biphase Silence
* mark/space Modulation
*
* 2666 889 444/888 - bit (x2 for toggle bit) 2666
*
* ________ __ __ __ __ ____ __ __ __ __ __ __ __ __
* _ _________ ____ __ __ ____ __ __ __ __ __ __ __ __ _______________
* | 1 | 0 | 0 | 0 | 0 | ... | ... | |
* s m2 m1 m0 T address (MSB) command (MSB)
*
* s - start bit (always 1)
* m0-2 - mode (000 for RC6)
* T - toggle bit, twice longer
* address - 8 bit
* command - 8 bit
***************************************************************************************************/
#define IRDA_RC6_CARRIER_FREQUENCY 36000
#define IRDA_RC6_DUTY_CYCLE 0.33
#define IRDA_RC6_PREAMBLE_MARK 2666
#define IRDA_RC6_PREAMBLE_SPACE 889
#define IRDA_RC6_BIT 444 // half of time-quant for 1 bit
#define IRDA_RC6_PREAMBLE_TOLERANCE 200 // us
#define IRDA_RC6_BIT_TOLERANCE 120 // us
/* protocol allows 2700 silence, but it is hard to send 1 message without repeat */
#define IRDA_RC6_SILENCE (2700 * 10)
#define IRDA_RC6_MIN_SPLIT_TIME 2700
void* irda_decoder_rc6_alloc(void);
void irda_decoder_rc6_reset(void* decoder);
void irda_decoder_rc6_free(void* decoder);
IrdaMessage* irda_decoder_rc6_check_ready(void* ctx);
IrdaMessage* irda_decoder_rc6_decode(void* decoder, bool level, uint32_t duration);
void* irda_encoder_rc6_alloc(void);
void irda_encoder_rc6_reset(void* encoder_ptr, const IrdaMessage* message);
void irda_encoder_rc6_free(void* decoder);
IrdaStatus irda_encoder_rc6_encode(void* encoder_ptr, uint32_t* duration, bool* polarity);
bool irda_decoder_rc6_interpret(IrdaCommonDecoder* decoder);
IrdaStatus
irda_decoder_rc6_decode_manchester(IrdaCommonDecoder* decoder, bool level, uint32_t timing);
IrdaStatus irda_encoder_rc6_encode_manchester(
IrdaCommonEncoder* encoder_ptr,
uint32_t* duration,
bool* polarity);
const IrdaProtocolSpecification* irda_rc6_get_spec(IrdaProtocol protocol);
extern const IrdaCommonProtocolSpec 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 IRDA_RC5_CARRIER_FREQUENCY 36000
#define IRDA_RC5_DUTY_CYCLE 0.33
#define IRDA_RC5_PREAMBLE_MARK 0
#define IRDA_RC5_PREAMBLE_SPACE 0
#define IRDA_RC5_BIT 888 // half of time-quant for 1 bit
#define IRDA_RC5_PREAMBLE_TOLERANCE 200 // us
#define IRDA_RC5_BIT_TOLERANCE 120 // us
/* protocol allows 2700 silence, but it is hard to send 1 message without repeat */
#define IRDA_RC5_SILENCE (2700 * 10)
#define IRDA_RC5_MIN_SPLIT_TIME 2700
void* irda_decoder_rc5_alloc(void);
void irda_decoder_rc5_reset(void* decoder);
void irda_decoder_rc5_free(void* decoder);
IrdaMessage* irda_decoder_rc5_check_ready(void* ctx);
IrdaMessage* irda_decoder_rc5_decode(void* decoder, bool level, uint32_t duration);
void* irda_encoder_rc5_alloc(void);
void irda_encoder_rc5_reset(void* encoder_ptr, const IrdaMessage* message);
void irda_encoder_rc5_free(void* decoder);
IrdaStatus irda_encoder_rc5_encode(void* encoder_ptr, uint32_t* duration, bool* polarity);
bool irda_decoder_rc5_interpret(IrdaCommonDecoder* decoder);
const IrdaProtocolSpecification* irda_rc5_get_spec(IrdaProtocol protocol);
extern const IrdaCommonProtocolSpec protocol_rc5;
/***************************************************************************************************
* Sony SIRC protocol description
* https://www.sbprojects.net/knowledge/ir/sirc.php
* http://picprojects.org.uk/
****************************************************************************************************
* Preamble Preamble Pulse Width Modulation Pause Entirely repeat
* mark space up to period message..
*
* 2400 600 12/15/20 bits (600,1200) ...45000 2400 600
* __________ _ _ _ _ _ _ _ _ _ _ _ _ _ __________ _ _
* ____ __________ _ _ _ __ __ __ _ _ __ __ _ _ ____________________ __________ _
* | command | address |
* SIRC | 7b LSB | 5b LSB |
* SIRC15 | 7b LSB | 8b LSB |
* SIRC20 | 7b LSB | 13b LSB |
*
* No way to determine either next message is repeat or not,
* so recognize only fact message received. Sony remotes always send at least 3 messages.
* Assume 8 last extended bits for SIRC20 are address bits.
***************************************************************************************************/
#define IRDA_SIRC_CARRIER_FREQUENCY 40000
#define IRDA_SIRC_DUTY_CYCLE 0.33
#define IRDA_SIRC_PREAMBLE_MARK 2400
#define IRDA_SIRC_PREAMBLE_SPACE 600
#define IRDA_SIRC_BIT1_MARK 1200
#define IRDA_SIRC_BIT1_SPACE 600
#define IRDA_SIRC_BIT0_MARK 600
#define IRDA_SIRC_BIT0_SPACE 600
#define IRDA_SIRC_PREAMBLE_TOLERANCE 200 // us
#define IRDA_SIRC_BIT_TOLERANCE 120 // us
#define IRDA_SIRC_SILENCE 10000
#define IRDA_SIRC_MIN_SPLIT_TIME (IRDA_SIRC_SILENCE - 1000)
#define IRDA_SIRC_REPEAT_PERIOD 45000
void* irda_decoder_sirc_alloc(void);
void irda_decoder_sirc_reset(void* decoder);
IrdaMessage* irda_decoder_sirc_check_ready(void* decoder);
uint32_t irda_decoder_sirc_get_timeout(void* decoder);
void irda_decoder_sirc_free(void* decoder);
IrdaMessage* irda_decoder_sirc_decode(void* decoder, bool level, uint32_t duration);
void* irda_encoder_sirc_alloc(void);
void irda_encoder_sirc_reset(void* encoder_ptr, const IrdaMessage* message);
void irda_encoder_sirc_free(void* decoder);
IrdaStatus irda_encoder_sirc_encode(void* encoder_ptr, uint32_t* duration, bool* polarity);
bool irda_decoder_sirc_interpret(IrdaCommonDecoder* decoder);
const IrdaProtocolSpecification* irda_sirc_get_spec(IrdaProtocol protocol);
IrdaStatus
irda_encoder_sirc_encode_repeat(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level);
extern const IrdaCommonProtocolSpec protocol_sirc;

View File

@@ -1,47 +0,0 @@
#include "../irda_i.h"
#include "irda_protocol_defs_i.h"
static const IrdaProtocolSpecification irda_nec_protocol_specification = {
.name = "NEC",
.address_length = 8,
.command_length = 8,
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
};
static const IrdaProtocolSpecification irda_necext_protocol_specification = {
.name = "NECext",
.address_length = 16,
.command_length = 16,
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
};
static const IrdaProtocolSpecification irda_nec42_protocol_specification = {
.name = "NEC42",
.address_length = 13,
.command_length = 8,
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
};
static const IrdaProtocolSpecification irda_nec42ext_protocol_specification = {
.name = "NEC42ext",
.address_length = 26,
.command_length = 16,
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
};
const IrdaProtocolSpecification* irda_nec_get_spec(IrdaProtocol protocol) {
if(protocol == IrdaProtocolNEC)
return &irda_nec_protocol_specification;
else if(protocol == IrdaProtocolNECext)
return &irda_necext_protocol_specification;
else if(protocol == IrdaProtocolNEC42)
return &irda_nec42_protocol_specification;
else if(protocol == IrdaProtocolNEC42ext)
return &irda_nec42ext_protocol_specification;
else
return NULL;
}

View File

@@ -1,82 +0,0 @@
#include "irda.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <furi.h>
#include "../irda_i.h"
#include "../irda_protocol_defs_i.h"
typedef struct {
IrdaCommonDecoder* common_decoder;
bool toggle;
} IrdaRc5Decoder;
IrdaMessage* irda_decoder_rc5_check_ready(void* ctx) {
IrdaRc5Decoder* decoder = ctx;
return irda_common_decoder_check_ready(decoder->common_decoder);
}
bool irda_decoder_rc5_interpret(IrdaCommonDecoder* 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) {
IrdaProtocol protocol = start_bit2 ? IrdaProtocolRC5 : IrdaProtocolRC5X;
IrdaMessage* message = &decoder->message;
IrdaRc5Decoder* 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* irda_decoder_rc5_alloc(void) {
IrdaRc5Decoder* decoder = malloc(sizeof(IrdaRc5Decoder));
decoder->toggle = false;
decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc5);
decoder->common_decoder->context = decoder;
return decoder;
}
IrdaMessage* irda_decoder_rc5_decode(void* decoder, bool level, uint32_t duration) {
IrdaRc5Decoder* decoder_rc5 = decoder;
return irda_common_decode(decoder_rc5->common_decoder, level, duration);
}
void irda_decoder_rc5_free(void* decoder) {
IrdaRc5Decoder* decoder_rc5 = decoder;
irda_common_decoder_free(decoder_rc5->common_decoder);
free(decoder_rc5);
}
void irda_decoder_rc5_reset(void* decoder) {
IrdaRc5Decoder* decoder_rc5 = decoder;
irda_common_decoder_reset(decoder_rc5->common_decoder);
}

View File

@@ -1,55 +0,0 @@
#include "furi/memmgr.h"
#include "irda.h"
#include "common/irda_common_i.h"
#include "irda_protocol_defs_i.h"
#include <stdint.h>
#include "../irda_i.h"
typedef struct IrdaEncoderRC5 {
IrdaCommonEncoder* common_encoder;
bool toggle_bit;
} IrdaEncoderRC5;
void irda_encoder_rc5_reset(void* encoder_ptr, const IrdaMessage* message) {
furi_assert(encoder_ptr);
IrdaEncoderRC5* encoder = encoder_ptr;
IrdaCommonEncoder* common_encoder = encoder->common_encoder;
irda_common_encoder_reset(common_encoder);
uint32_t* data = (void*)common_encoder->data;
/* RC5 */
*data |= 0x01; // start bit
if(message->protocol == IrdaProtocolRC5) {
*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;
}
IrdaStatus irda_encoder_rc5_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
IrdaEncoderRC5* encoder = encoder_ptr;
return irda_common_encode(encoder->common_encoder, duration, level);
}
void* irda_encoder_rc5_alloc(void) {
IrdaEncoderRC5* encoder = malloc(sizeof(IrdaEncoderRC5));
encoder->common_encoder = irda_common_encoder_alloc(&protocol_rc5);
encoder->toggle_bit = false;
return encoder;
}
void irda_encoder_rc5_free(void* encoder_ptr) {
furi_assert(encoder_ptr);
IrdaEncoderRC5* encoder = encoder_ptr;
free(encoder->common_encoder);
free(encoder);
}

View File

@@ -1,27 +0,0 @@
#include "../irda_i.h"
#include "irda_protocol_defs_i.h"
static const IrdaProtocolSpecification irda_rc5_protocol_specification = {
.name = "RC5",
.address_length = 5,
.command_length = 6,
.frequency = IRDA_RC5_CARRIER_FREQUENCY,
.duty_cycle = IRDA_RC5_DUTY_CYCLE,
};
static const IrdaProtocolSpecification irda_rc5x_protocol_specification = {
.name = "RC5X",
.address_length = 5,
.command_length = 7,
.frequency = IRDA_RC5_CARRIER_FREQUENCY,
.duty_cycle = IRDA_RC5_DUTY_CYCLE,
};
const IrdaProtocolSpecification* irda_rc5_get_spec(IrdaProtocol protocol) {
if(protocol == IrdaProtocolRC5)
return &irda_rc5_protocol_specification;
else if(protocol == IrdaProtocolRC5X)
return &irda_rc5x_protocol_specification;
else
return NULL;
}

View File

@@ -1,61 +0,0 @@
#include "furi/memmgr.h"
#include "irda.h"
#include "common/irda_common_i.h"
#include "irda_protocol_defs_i.h"
#include <stdint.h>
#include "../irda_i.h"
typedef struct IrdaEncoderRC6 {
IrdaCommonEncoder* common_encoder;
bool toggle_bit;
} IrdaEncoderRC6;
void irda_encoder_rc6_reset(void* encoder_ptr, const IrdaMessage* message) {
furi_assert(encoder_ptr);
IrdaEncoderRC6* encoder = encoder_ptr;
IrdaCommonEncoder* common_encoder = encoder->common_encoder;
irda_common_encoder_reset(common_encoder);
uint32_t* data = (void*)common_encoder->data;
*data |= 0x01; // start bit
(void)*data; // 3 bits for mode == 0
*data |= encoder->toggle_bit ? 0x10 : 0;
*data |= reverse(message->address) << 5;
*data |= reverse(message->command) << 13;
common_encoder->bits_to_encode = common_encoder->protocol->databit_len[0];
encoder->toggle_bit ^= 1;
}
IrdaStatus irda_encoder_rc6_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
IrdaEncoderRC6* encoder = encoder_ptr;
return irda_common_encode(encoder->common_encoder, duration, level);
}
void* irda_encoder_rc6_alloc(void) {
IrdaEncoderRC6* encoder = malloc(sizeof(IrdaEncoderRC6));
encoder->common_encoder = irda_common_encoder_alloc(&protocol_rc6);
encoder->toggle_bit = false;
return encoder;
}
void irda_encoder_rc6_free(void* encoder_ptr) {
furi_assert(encoder_ptr);
IrdaEncoderRC6* encoder = encoder_ptr;
free(encoder->common_encoder);
free(encoder);
}
IrdaStatus irda_encoder_rc6_encode_manchester(
IrdaCommonEncoder* common_encoder,
uint32_t* duration,
bool* polarity) {
IrdaStatus status = IrdaStatusError;
bool toggle_bit = (common_encoder->bits_encoded == 4);
status = irda_common_encode_manchester(common_encoder, duration, polarity);
if(toggle_bit) *duration *= 2;
return status;
}

View File

@@ -1,17 +0,0 @@
#include "../irda_i.h"
#include "irda_protocol_defs_i.h"
static const IrdaProtocolSpecification irda_rc6_protocol_specification = {
.name = "RC6",
.address_length = 8,
.command_length = 8,
.frequency = IRDA_RC6_CARRIER_FREQUENCY,
.duty_cycle = IRDA_RC6_DUTY_CYCLE,
};
const IrdaProtocolSpecification* irda_rc6_get_spec(IrdaProtocol protocol) {
if(protocol == IrdaProtocolRC6)
return &irda_rc6_protocol_specification;
else
return NULL;
}

View File

@@ -1,72 +0,0 @@
#include "irda.h"
#include "irda_protocol_defs_i.h"
#include <stdbool.h>
#include <stdint.h>
#include <furi.h>
#include "../irda_i.h"
IrdaMessage* irda_decoder_samsung32_check_ready(void* ctx) {
return irda_common_decoder_check_ready(ctx);
}
bool irda_decoder_samsung32_interpret(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
uint8_t address1 = decoder->data[0];
uint8_t address2 = decoder->data[1];
uint8_t command = decoder->data[2];
uint8_t command_inverse = decoder->data[3];
if((address1 == address2) && (command == (uint8_t)~command_inverse)) {
decoder->message.command = command;
decoder->message.address = address1;
decoder->message.protocol = IrdaProtocolSamsung32;
decoder->message.repeat = false;
result = true;
}
return result;
}
// timings start from Space (delay between message and repeat)
IrdaStatus irda_decoder_samsung32_decode_repeat(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
IrdaStatus status = IrdaStatusError;
if(decoder->timings_cnt < 6) return IrdaStatusOk;
if((decoder->timings[0] > IRDA_SAMSUNG_REPEAT_PAUSE_MIN) &&
(decoder->timings[0] < IRDA_SAMSUNG_REPEAT_PAUSE_MAX) &&
MATCH_TIMING(decoder->timings[1], IRDA_SAMSUNG_REPEAT_MARK, preamble_tolerance) &&
MATCH_TIMING(decoder->timings[2], IRDA_SAMSUNG_REPEAT_SPACE, preamble_tolerance) &&
MATCH_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance) &&
MATCH_TIMING(decoder->timings[4], decoder->protocol->timings.bit1_space, bit_tolerance) &&
MATCH_TIMING(decoder->timings[5], decoder->protocol->timings.bit1_mark, bit_tolerance)) {
status = IrdaStatusReady;
decoder->timings_cnt = 0;
} else {
status = IrdaStatusError;
}
return status;
}
void* irda_decoder_samsung32_alloc(void) {
return irda_common_decoder_alloc(&protocol_samsung32);
}
IrdaMessage* irda_decoder_samsung32_decode(void* decoder, bool level, uint32_t duration) {
return irda_common_decode(decoder, level, duration);
}
void irda_decoder_samsung32_free(void* decoder) {
irda_common_decoder_free(decoder);
}
void irda_decoder_samsung32_reset(void* decoder) {
irda_common_decoder_reset(decoder);
}

View File

@@ -1,17 +0,0 @@
#include "../irda_i.h"
#include "irda_protocol_defs_i.h"
static const IrdaProtocolSpecification irda_samsung32_protocol_specification = {
.name = "Samsung32",
.address_length = 8,
.command_length = 8,
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
};
const IrdaProtocolSpecification* irda_samsung32_get_spec(IrdaProtocol protocol) {
if(protocol == IrdaProtocolSamsung32)
return &irda_samsung32_protocol_specification;
else
return NULL;
}

View File

@@ -1,60 +0,0 @@
#include "common/irda_common_i.h"
#include "irda.h"
#include "irda_protocol_defs_i.h"
#include <stdbool.h>
#include <stdint.h>
#include <furi.h>
#include "../irda_i.h"
IrdaMessage* irda_decoder_sirc_check_ready(void* ctx) {
return irda_common_decoder_check_ready(ctx);
}
bool irda_decoder_sirc_interpret(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
uint32_t* data = (void*)&decoder->data[0];
uint16_t address = 0;
uint8_t command = 0;
IrdaProtocol protocol = IrdaProtocolUnknown;
if(decoder->databit_cnt == 12) {
address = (*data >> 7) & 0x1F;
command = *data & 0x7F;
protocol = IrdaProtocolSIRC;
} else if(decoder->databit_cnt == 15) {
address = (*data >> 7) & 0xFF;
command = *data & 0x7F;
protocol = IrdaProtocolSIRC15;
} else if(decoder->databit_cnt == 20) {
address = (*data >> 7) & 0x1FFF;
command = *data & 0x7F;
protocol = IrdaProtocolSIRC20;
} else {
return false;
}
decoder->message.protocol = protocol;
decoder->message.address = address;
decoder->message.command = command;
/* SIRC doesn't specify repeat detection */
decoder->message.repeat = false;
return true;
}
void* irda_decoder_sirc_alloc(void) {
return irda_common_decoder_alloc(&protocol_sirc);
}
IrdaMessage* irda_decoder_sirc_decode(void* decoder, bool level, uint32_t duration) {
return irda_common_decode(decoder, level, duration);
}
void irda_decoder_sirc_free(void* decoder) {
irda_common_decoder_free(decoder);
}
void irda_decoder_sirc_reset(void* decoder) {
irda_common_decoder_reset(decoder);
}

View File

@@ -1,71 +0,0 @@
#include "furi/check.h"
#include "irda.h"
#include "common/irda_common_i.h"
#include <stdint.h>
#include "../irda_i.h"
#include "irda_protocol_defs_i.h"
#include <furi.h>
void irda_encoder_sirc_reset(void* encoder_ptr, const IrdaMessage* message) {
furi_assert(encoder_ptr);
furi_assert(message);
IrdaCommonEncoder* encoder = encoder_ptr;
irda_common_encoder_reset(encoder);
uint32_t* data = (void*)encoder->data;
if(message->protocol == IrdaProtocolSIRC) {
*data = (message->command & 0x7F);
*data |= (message->address & 0x1F) << 7;
encoder->bits_to_encode = 12;
} else if(message->protocol == IrdaProtocolSIRC15) {
*data = (message->command & 0x7F);
*data |= (message->address & 0xFF) << 7;
encoder->bits_to_encode = 15;
} else if(message->protocol == IrdaProtocolSIRC20) {
*data = (message->command & 0x7F);
*data |= (message->address & 0x1FFF) << 7;
encoder->bits_to_encode = 20;
} else {
furi_assert(0);
}
}
IrdaStatus
irda_encoder_sirc_encode_repeat(IrdaCommonEncoder* 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 < IRDA_SIRC_REPEAT_PERIOD);
*duration = IRDA_SIRC_REPEAT_PERIOD - encoder->timings_sum;
*level = false;
encoder->timings_sum = 0;
encoder->timings_encoded = 1;
encoder->bits_encoded = 0;
encoder->state = IrdaCommonEncoderStatePreamble;
return IrdaStatusOk;
}
void* irda_encoder_sirc_alloc(void) {
return irda_common_encoder_alloc(&protocol_sirc);
}
void irda_encoder_sirc_free(void* encoder_ptr) {
irda_common_encoder_free(encoder_ptr);
}
IrdaStatus irda_encoder_sirc_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
IrdaCommonEncoder* encoder = encoder_ptr;
IrdaStatus status = irda_common_encode(encoder, duration, level);
if((status == IrdaStatusOk) && (encoder->bits_encoded == encoder->bits_to_encode)) {
furi_assert(!*level);
status = IrdaStatusDone;
encoder->state = IrdaCommonEncoderStateEncodeRepeat;
}
return status;
}

View File

@@ -1,37 +0,0 @@
#include "../irda_i.h"
#include "irda_protocol_defs_i.h"
static const IrdaProtocolSpecification irda_sirc_protocol_specification = {
.name = "SIRC",
.address_length = 5,
.command_length = 7,
.frequency = IRDA_SIRC_CARRIER_FREQUENCY,
.duty_cycle = IRDA_SIRC_DUTY_CYCLE,
};
static const IrdaProtocolSpecification irda_sirc15_protocol_specification = {
.name = "SIRC15",
.address_length = 8,
.command_length = 7,
.frequency = IRDA_SIRC_CARRIER_FREQUENCY,
.duty_cycle = IRDA_SIRC_DUTY_CYCLE,
};
static const IrdaProtocolSpecification irda_sirc20_protocol_specification = {
.name = "SIRC20",
.address_length = 13,
.command_length = 7,
.frequency = IRDA_SIRC_CARRIER_FREQUENCY,
.duty_cycle = IRDA_SIRC_DUTY_CYCLE,
};
const IrdaProtocolSpecification* irda_sirc_get_spec(IrdaProtocol protocol) {
if(protocol == IrdaProtocolSIRC)
return &irda_sirc_protocol_specification;
else if(protocol == IrdaProtocolSIRC15)
return &irda_sirc15_protocol_specification;
else if(protocol == IrdaProtocolSIRC20)
return &irda_sirc20_protocol_specification;
else
return NULL;
}

View File

@@ -1,114 +0,0 @@
#include "irda.h"
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <furi.h>
#include <furi_hal_irda.h>
#include <furi_hal_delay.h>
static uint32_t irda_tx_number_of_transmissions = 0;
static uint32_t irda_tx_raw_timings_index = 0;
static uint32_t irda_tx_raw_timings_number = 0;
static uint32_t irda_tx_raw_start_from_mark = 0;
static bool irda_tx_raw_add_silence = false;
FuriHalIrdaTxGetDataState
irda_get_raw_data_callback(void* context, uint32_t* duration, bool* level) {
furi_assert(duration);
furi_assert(level);
furi_assert(context);
FuriHalIrdaTxGetDataState state = FuriHalIrdaTxGetDataStateOk;
const uint32_t* timings = context;
if(irda_tx_raw_add_silence && (irda_tx_raw_timings_index == 0)) {
irda_tx_raw_add_silence = false;
*level = false;
*duration = IRDA_RAW_TX_TIMING_DELAY_US;
} else {
*level = irda_tx_raw_start_from_mark ^ (irda_tx_raw_timings_index % 2);
*duration = timings[irda_tx_raw_timings_index++];
}
if(irda_tx_raw_timings_number == irda_tx_raw_timings_index) {
state = FuriHalIrdaTxGetDataStateLastDone;
}
return state;
}
void irda_send_raw_ext(
const uint32_t timings[],
uint32_t timings_cnt,
bool start_from_mark,
uint32_t frequency,
float duty_cycle) {
furi_assert(timings);
irda_tx_raw_start_from_mark = start_from_mark;
irda_tx_raw_timings_index = 0;
irda_tx_raw_timings_number = timings_cnt;
irda_tx_raw_add_silence = start_from_mark;
furi_hal_irda_async_tx_set_data_isr_callback(irda_get_raw_data_callback, (void*)timings);
furi_hal_irda_async_tx_start(frequency, duty_cycle);
furi_hal_irda_async_tx_wait_termination();
furi_assert(!furi_hal_irda_is_busy());
}
void irda_send_raw(const uint32_t timings[], uint32_t timings_cnt, bool start_from_mark) {
irda_send_raw_ext(
timings,
timings_cnt,
start_from_mark,
IRDA_COMMON_CARRIER_FREQUENCY,
IRDA_COMMON_DUTY_CYCLE);
}
FuriHalIrdaTxGetDataState irda_get_data_callback(void* context, uint32_t* duration, bool* level) {
FuriHalIrdaTxGetDataState state = FuriHalIrdaTxGetDataStateLastDone;
IrdaEncoderHandler* handler = context;
IrdaStatus status = IrdaStatusError;
if(irda_tx_number_of_transmissions > 0) {
status = irda_encode(handler, duration, level);
}
if(status == IrdaStatusError) {
state = FuriHalIrdaTxGetDataStateLastDone;
*duration = 0;
*level = 0;
} else if(status == IrdaStatusOk) {
state = FuriHalIrdaTxGetDataStateOk;
} else if(status == IrdaStatusDone) {
state = FuriHalIrdaTxGetDataStateDone;
if(--irda_tx_number_of_transmissions == 0) {
state = FuriHalIrdaTxGetDataStateLastDone;
}
} else {
furi_crash(NULL);
}
return state;
}
void irda_send(const IrdaMessage* message, int times) {
furi_assert(message);
furi_assert(times);
furi_assert(irda_is_protocol_valid(message->protocol));
IrdaEncoderHandler* handler = irda_alloc_encoder();
irda_reset_encoder(handler, message);
irda_tx_number_of_transmissions = times;
uint32_t frequency = irda_get_protocol_frequency(message->protocol);
float duty_cycle = irda_get_protocol_duty_cycle(message->protocol);
furi_hal_irda_async_tx_set_data_isr_callback(irda_get_data_callback, handler);
furi_hal_irda_async_tx_start(frequency, duty_cycle);
furi_hal_irda_async_tx_wait_termination();
irda_free_encoder(handler);
furi_assert(!furi_hal_irda_is_busy());
}

View File

@@ -1,598 +0,0 @@
#include "furi/check.h"
#include "furi/common_defines.h"
#include "sys/_stdint.h"
#include "irda_worker.h"
#include <irda.h>
#include <furi_hal_irda.h>
#include <limits.h>
#include <stdint.h>
#include <furi.h>
#include <notification/notification_messages.h>
#include <stream_buffer.h>
#define IRDA_WORKER_RX_TIMEOUT IRDA_RAW_RX_TIMING_DELAY_US
#define IRDA_WORKER_RX_RECEIVED 0x01
#define IRDA_WORKER_RX_TIMEOUT_RECEIVED 0x02
#define IRDA_WORKER_OVERRUN 0x04
#define IRDA_WORKER_EXIT 0x08
#define IRDA_WORKER_TX_FILL_BUFFER 0x10
#define IRDA_WORKER_TX_MESSAGE_SENT 0x20
#define IRDA_WORKER_ALL_RX_EVENTS \
(IRDA_WORKER_RX_RECEIVED | IRDA_WORKER_RX_TIMEOUT_RECEIVED | IRDA_WORKER_OVERRUN | \
IRDA_WORKER_EXIT)
#define IRDA_WORKER_ALL_TX_EVENTS \
(IRDA_WORKER_TX_FILL_BUFFER | IRDA_WORKER_TX_MESSAGE_SENT | IRDA_WORKER_EXIT)
#define IRDA_WORKER_ALL_EVENTS (IRDA_WORKER_ALL_RX_EVENTS | IRDA_WORKER_ALL_TX_EVENTS)
typedef enum {
IrdaWorkerStateIdle,
IrdaWorkerStateRunRx,
IrdaWorkerStateRunTx,
IrdaWorkerStateWaitTxEnd,
IrdaWorkerStateStopTx,
IrdaWorkerStateStartTx,
} IrdaWorkerState;
struct IrdaWorkerSignal {
bool decoded;
size_t timings_cnt;
union {
IrdaMessage message;
/* +1 is for pause we add at the beginning */
uint32_t timings[MAX_TIMINGS_AMOUNT + 1];
};
};
struct IrdaWorker {
FuriThread* thread;
StreamBufferHandle_t stream;
osEventFlagsId_t events;
IrdaWorkerSignal signal;
IrdaWorkerState state;
IrdaEncoderHandler* irda_encoder;
IrdaDecoderHandler* irda_decoder;
NotificationApp* notification;
bool blink_enable;
union {
struct {
IrdaWorkerGetSignalCallback get_signal_callback;
IrdaWorkerMessageSentCallback message_sent_callback;
void* get_signal_context;
void* message_sent_context;
uint32_t frequency;
float duty_cycle;
uint32_t tx_raw_cnt;
bool need_reinitialization;
bool steady_signal_sent;
} tx;
struct {
IrdaWorkerReceivedSignalCallback received_signal_callback;
void* received_signal_context;
bool overrun;
} rx;
};
};
typedef struct {
uint32_t duration;
bool level;
FuriHalIrdaTxGetDataState state;
} IrdaWorkerTiming;
static int32_t irda_worker_tx_thread(void* context);
static FuriHalIrdaTxGetDataState
irda_worker_furi_hal_data_isr_callback(void* context, uint32_t* duration, bool* level);
static void irda_worker_furi_hal_message_sent_isr_callback(void* context);
static void irda_worker_rx_timeout_callback(void* context) {
IrdaWorker* instance = context;
uint32_t flags_set = osEventFlagsSet(instance->events, IRDA_WORKER_RX_TIMEOUT_RECEIVED);
furi_check(flags_set & IRDA_WORKER_RX_TIMEOUT_RECEIVED);
}
static void irda_worker_rx_callback(void* context, bool level, uint32_t duration) {
IrdaWorker* instance = context;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
furi_assert(duration != 0);
LevelDuration level_duration = level_duration_make(level, duration);
size_t ret = xStreamBufferSendFromISR(
instance->stream, &level_duration, sizeof(LevelDuration), &xHigherPriorityTaskWoken);
uint32_t events = (ret == sizeof(LevelDuration)) ? IRDA_WORKER_RX_RECEIVED :
IRDA_WORKER_OVERRUN;
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
uint32_t flags_set = osEventFlagsSet(instance->events, events);
furi_check(flags_set & events);
}
static void irda_worker_process_timeout(IrdaWorker* instance) {
if(instance->signal.timings_cnt < 2) return;
const IrdaMessage* message_decoded = irda_check_decoder_ready(instance->irda_decoder);
if(message_decoded) {
instance->signal.message = *message_decoded;
instance->signal.timings_cnt = 0;
instance->signal.decoded = true;
} else {
instance->signal.decoded = false;
}
if(instance->rx.received_signal_callback)
instance->rx.received_signal_callback(
instance->rx.received_signal_context, &instance->signal);
}
static void irda_worker_process_timings(IrdaWorker* instance, uint32_t duration, bool level) {
const IrdaMessage* message_decoded = irda_decode(instance->irda_decoder, level, duration);
if(message_decoded) {
instance->signal.message = *message_decoded;
instance->signal.timings_cnt = 0;
instance->signal.decoded = true;
if(instance->rx.received_signal_callback)
instance->rx.received_signal_callback(
instance->rx.received_signal_context, &instance->signal);
} else {
/* Skip first timing if it starts from Space */
if((instance->signal.timings_cnt == 0) && !level) {
return;
}
if(instance->signal.timings_cnt < MAX_TIMINGS_AMOUNT) {
instance->signal.timings[instance->signal.timings_cnt] = duration;
++instance->signal.timings_cnt;
} else {
uint32_t flags_set = osEventFlagsSet(instance->events, IRDA_WORKER_OVERRUN);
furi_check(flags_set & IRDA_WORKER_OVERRUN);
instance->rx.overrun = true;
}
}
}
static int32_t irda_worker_rx_thread(void* thread_context) {
IrdaWorker* instance = thread_context;
uint32_t events = 0;
LevelDuration level_duration;
TickType_t last_blink_time = 0;
while(1) {
events = osEventFlagsWait(instance->events, IRDA_WORKER_ALL_RX_EVENTS, 0, osWaitForever);
furi_check(events & IRDA_WORKER_ALL_RX_EVENTS); /* at least one caught */
if(events & IRDA_WORKER_RX_RECEIVED) {
if(!instance->rx.overrun && instance->blink_enable &&
((xTaskGetTickCount() - last_blink_time) > 80)) {
last_blink_time = xTaskGetTickCount();
notification_message(instance->notification, &sequence_blink_blue_10);
}
if(instance->signal.timings_cnt == 0)
notification_message(instance->notification, &sequence_display_on);
while(sizeof(LevelDuration) ==
xStreamBufferReceive(
instance->stream, &level_duration, sizeof(LevelDuration), 0)) {
if(!instance->rx.overrun) {
bool level = level_duration_get_level(level_duration);
uint32_t duration = level_duration_get_duration(level_duration);
irda_worker_process_timings(instance, duration, level);
}
}
}
if(events & IRDA_WORKER_OVERRUN) {
printf("#");
irda_reset_decoder(instance->irda_decoder);
instance->signal.timings_cnt = 0;
if(instance->blink_enable)
notification_message(instance->notification, &sequence_set_red_255);
}
if(events & IRDA_WORKER_RX_TIMEOUT_RECEIVED) {
if(instance->rx.overrun) {
printf("\nOVERRUN, max samples: %d\n", MAX_TIMINGS_AMOUNT);
instance->rx.overrun = false;
if(instance->blink_enable)
notification_message(instance->notification, &sequence_reset_red);
} else {
irda_worker_process_timeout(instance);
}
instance->signal.timings_cnt = 0;
}
if(events & IRDA_WORKER_EXIT) break;
}
return 0;
}
void irda_worker_rx_set_received_signal_callback(
IrdaWorker* instance,
IrdaWorkerReceivedSignalCallback callback,
void* context) {
furi_assert(instance);
instance->rx.received_signal_callback = callback;
instance->rx.received_signal_context = context;
}
IrdaWorker* irda_worker_alloc() {
IrdaWorker* instance = malloc(sizeof(IrdaWorker));
instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "IrdaWorker");
furi_thread_set_stack_size(instance->thread, 2048);
furi_thread_set_context(instance->thread, instance);
size_t buffer_size =
MAX(sizeof(IrdaWorkerTiming) * (MAX_TIMINGS_AMOUNT + 1),
sizeof(LevelDuration) * MAX_TIMINGS_AMOUNT);
instance->stream = xStreamBufferCreate(buffer_size, sizeof(IrdaWorkerTiming));
instance->irda_decoder = irda_alloc_decoder();
instance->irda_encoder = irda_alloc_encoder();
instance->blink_enable = false;
instance->notification = furi_record_open("notification");
instance->state = IrdaWorkerStateIdle;
instance->events = osEventFlagsNew(NULL);
return instance;
}
void irda_worker_free(IrdaWorker* instance) {
furi_assert(instance);
furi_assert(instance->state == IrdaWorkerStateIdle);
furi_record_close("notification");
irda_free_decoder(instance->irda_decoder);
irda_free_encoder(instance->irda_encoder);
vStreamBufferDelete(instance->stream);
furi_thread_free(instance->thread);
osEventFlagsDelete(instance->events);
free(instance);
}
void irda_worker_rx_start(IrdaWorker* instance) {
furi_assert(instance);
furi_assert(instance->state == IrdaWorkerStateIdle);
xStreamBufferSetTriggerLevel(instance->stream, sizeof(LevelDuration));
osEventFlagsClear(instance->events, IRDA_WORKER_ALL_EVENTS);
furi_thread_set_callback(instance->thread, irda_worker_rx_thread);
furi_thread_start(instance->thread);
furi_hal_irda_async_rx_set_capture_isr_callback(irda_worker_rx_callback, instance);
furi_hal_irda_async_rx_set_timeout_isr_callback(irda_worker_rx_timeout_callback, instance);
furi_hal_irda_async_rx_start();
furi_hal_irda_async_rx_set_timeout(IRDA_WORKER_RX_TIMEOUT);
instance->rx.overrun = false;
instance->state = IrdaWorkerStateRunRx;
}
void irda_worker_rx_stop(IrdaWorker* instance) {
furi_assert(instance);
furi_assert(instance->state == IrdaWorkerStateRunRx);
furi_hal_irda_async_rx_set_timeout_isr_callback(NULL, NULL);
furi_hal_irda_async_rx_set_capture_isr_callback(NULL, NULL);
furi_hal_irda_async_rx_stop();
osEventFlagsSet(instance->events, IRDA_WORKER_EXIT);
furi_thread_join(instance->thread);
BaseType_t xReturn = xStreamBufferReset(instance->stream);
furi_assert(xReturn == pdPASS);
(void)xReturn;
instance->state = IrdaWorkerStateIdle;
}
bool irda_worker_signal_is_decoded(const IrdaWorkerSignal* signal) {
furi_assert(signal);
return signal->decoded;
}
void irda_worker_get_raw_signal(
const IrdaWorkerSignal* signal,
const uint32_t** timings,
size_t* timings_cnt) {
furi_assert(signal);
furi_assert(timings);
furi_assert(timings_cnt);
*timings = signal->timings;
*timings_cnt = signal->timings_cnt;
}
const IrdaMessage* irda_worker_get_decoded_signal(const IrdaWorkerSignal* signal) {
furi_assert(signal);
return &signal->message;
}
void irda_worker_rx_enable_blink_on_receiving(IrdaWorker* instance, bool enable) {
furi_assert(instance);
instance->blink_enable = enable;
}
void irda_worker_tx_start(IrdaWorker* instance) {
furi_assert(instance);
furi_assert(instance->state == IrdaWorkerStateIdle);
furi_assert(instance->tx.get_signal_callback);
// size have to be greater than api hal irda async tx buffer size
xStreamBufferSetTriggerLevel(instance->stream, sizeof(IrdaWorkerTiming));
osEventFlagsClear(instance->events, IRDA_WORKER_ALL_EVENTS);
furi_thread_set_callback(instance->thread, irda_worker_tx_thread);
instance->tx.steady_signal_sent = false;
instance->tx.need_reinitialization = false;
furi_hal_irda_async_tx_set_data_isr_callback(irda_worker_furi_hal_data_isr_callback, instance);
furi_hal_irda_async_tx_set_signal_sent_isr_callback(
irda_worker_furi_hal_message_sent_isr_callback, instance);
instance->state = IrdaWorkerStateStartTx;
furi_thread_start(instance->thread);
}
static void irda_worker_furi_hal_message_sent_isr_callback(void* context) {
IrdaWorker* instance = context;
uint32_t flags_set = osEventFlagsSet(instance->events, IRDA_WORKER_TX_MESSAGE_SENT);
furi_check(flags_set & IRDA_WORKER_TX_MESSAGE_SENT);
}
static FuriHalIrdaTxGetDataState
irda_worker_furi_hal_data_isr_callback(void* context, uint32_t* duration, bool* level) {
furi_assert(context);
furi_assert(duration);
furi_assert(level);
IrdaWorker* instance = context;
IrdaWorkerTiming timing;
FuriHalIrdaTxGetDataState state;
if(sizeof(IrdaWorkerTiming) ==
xStreamBufferReceiveFromISR(instance->stream, &timing, sizeof(IrdaWorkerTiming), 0)) {
*level = timing.level;
*duration = timing.duration;
state = timing.state;
} else {
furi_assert(0);
*level = 0;
*duration = 100;
state = FuriHalIrdaTxGetDataStateDone;
}
uint32_t flags_set = osEventFlagsSet(instance->events, IRDA_WORKER_TX_FILL_BUFFER);
furi_check(flags_set & IRDA_WORKER_TX_FILL_BUFFER);
return state;
}
static bool irda_get_new_signal(IrdaWorker* instance) {
bool new_signal_obtained = false;
IrdaWorkerGetSignalResponse response =
instance->tx.get_signal_callback(instance->tx.get_signal_context, instance);
if(response == IrdaWorkerGetSignalResponseNew) {
uint32_t new_tx_frequency = 0;
float new_tx_duty_cycle = 0;
if(instance->signal.decoded) {
new_tx_frequency = irda_get_protocol_frequency(instance->signal.message.protocol);
new_tx_duty_cycle = irda_get_protocol_duty_cycle(instance->signal.message.protocol);
} else {
furi_assert(instance->signal.timings_cnt > 1);
new_tx_frequency = IRDA_COMMON_CARRIER_FREQUENCY;
new_tx_duty_cycle = IRDA_COMMON_DUTY_CYCLE;
}
instance->tx.tx_raw_cnt = 0;
instance->tx.need_reinitialization = (new_tx_frequency != instance->tx.frequency) ||
(new_tx_duty_cycle != instance->tx.duty_cycle);
instance->tx.frequency = new_tx_frequency;
instance->tx.duty_cycle = new_tx_duty_cycle;
if(instance->signal.decoded) {
irda_reset_encoder(instance->irda_encoder, &instance->signal.message);
}
new_signal_obtained = true;
} else if(response == IrdaWorkerGetSignalResponseSame) {
new_signal_obtained = true;
/* no need to reinit */
} else if(response == IrdaWorkerGetSignalResponseStop) {
new_signal_obtained = false;
} else {
furi_assert(0);
}
return new_signal_obtained;
}
static bool irda_worker_tx_fill_buffer(IrdaWorker* instance) {
bool new_data_available = true;
IrdaWorkerTiming timing;
IrdaStatus status = IrdaStatusError;
while(!xStreamBufferIsFull(instance->stream) && !instance->tx.need_reinitialization &&
new_data_available) {
if(instance->signal.decoded) {
status = irda_encode(instance->irda_encoder, &timing.duration, &timing.level);
} else {
timing.duration = instance->signal.timings[instance->tx.tx_raw_cnt];
/* raw always starts from Mark, but we fill it with space delay at start */
timing.level = (instance->tx.tx_raw_cnt % 2);
++instance->tx.tx_raw_cnt;
if(instance->tx.tx_raw_cnt >= instance->signal.timings_cnt) {
instance->tx.tx_raw_cnt = 0;
status = IrdaStatusDone;
} else {
status = IrdaStatusOk;
}
}
if(status == IrdaStatusError) {
furi_assert(0);
new_data_available = false;
break;
} else if(status == IrdaStatusOk) {
timing.state = FuriHalIrdaTxGetDataStateOk;
} else if(status == IrdaStatusDone) {
timing.state = FuriHalIrdaTxGetDataStateDone;
new_data_available = irda_get_new_signal(instance);
if(instance->tx.need_reinitialization || !new_data_available) {
timing.state = FuriHalIrdaTxGetDataStateLastDone;
}
} else {
furi_assert(0);
}
uint32_t written_size =
xStreamBufferSend(instance->stream, &timing, sizeof(IrdaWorkerTiming), 0);
furi_assert(sizeof(IrdaWorkerTiming) == written_size);
(void)written_size;
}
return new_data_available;
}
static int32_t irda_worker_tx_thread(void* thread_context) {
IrdaWorker* instance = thread_context;
furi_assert(instance->state == IrdaWorkerStateStartTx);
furi_assert(thread_context);
uint32_t events = 0;
bool new_data_available = true;
bool exit = false;
exit = !irda_get_new_signal(instance);
furi_assert(!exit);
while(!exit) {
switch(instance->state) {
case IrdaWorkerStateStartTx:
instance->tx.need_reinitialization = false;
new_data_available = irda_worker_tx_fill_buffer(instance);
furi_hal_irda_async_tx_start(instance->tx.frequency, instance->tx.duty_cycle);
if(!new_data_available) {
instance->state = IrdaWorkerStateStopTx;
} else if(instance->tx.need_reinitialization) {
instance->state = IrdaWorkerStateWaitTxEnd;
} else {
instance->state = IrdaWorkerStateRunTx;
}
break;
case IrdaWorkerStateStopTx:
furi_hal_irda_async_tx_stop();
exit = true;
break;
case IrdaWorkerStateWaitTxEnd:
furi_hal_irda_async_tx_wait_termination();
instance->state = IrdaWorkerStateStartTx;
events = osEventFlagsGet(instance->events);
if(events & IRDA_WORKER_EXIT) {
exit = true;
break;
}
break;
case IrdaWorkerStateRunTx:
events =
osEventFlagsWait(instance->events, IRDA_WORKER_ALL_TX_EVENTS, 0, osWaitForever);
furi_check(events & IRDA_WORKER_ALL_TX_EVENTS); /* at least one caught */
if(events & IRDA_WORKER_EXIT) {
instance->state = IrdaWorkerStateStopTx;
break;
}
if(events & IRDA_WORKER_TX_FILL_BUFFER) {
irda_worker_tx_fill_buffer(instance);
if(instance->tx.need_reinitialization) {
instance->state = IrdaWorkerStateWaitTxEnd;
}
}
if(events & IRDA_WORKER_TX_MESSAGE_SENT) {
if(instance->tx.message_sent_callback)
instance->tx.message_sent_callback(instance->tx.message_sent_context);
}
break;
default:
furi_assert(0);
break;
}
}
return 0;
}
void irda_worker_tx_set_get_signal_callback(
IrdaWorker* instance,
IrdaWorkerGetSignalCallback callback,
void* context) {
furi_assert(instance);
instance->tx.get_signal_callback = callback;
instance->tx.get_signal_context = context;
}
void irda_worker_tx_set_signal_sent_callback(
IrdaWorker* instance,
IrdaWorkerMessageSentCallback callback,
void* context) {
furi_assert(instance);
instance->tx.message_sent_callback = callback;
instance->tx.message_sent_context = context;
}
void irda_worker_tx_stop(IrdaWorker* instance) {
furi_assert(instance);
furi_assert(instance->state != IrdaWorkerStateRunRx);
osEventFlagsSet(instance->events, IRDA_WORKER_EXIT);
furi_thread_join(instance->thread);
furi_hal_irda_async_tx_set_data_isr_callback(NULL, NULL);
furi_hal_irda_async_tx_set_signal_sent_isr_callback(NULL, NULL);
instance->signal.timings_cnt = 0;
BaseType_t xReturn = pdFAIL;
xReturn = xStreamBufferReset(instance->stream);
furi_assert(xReturn == pdPASS);
(void)xReturn;
instance->state = IrdaWorkerStateIdle;
}
void irda_worker_set_decoded_signal(IrdaWorker* instance, const IrdaMessage* message) {
furi_assert(instance);
furi_assert(message);
instance->signal.decoded = true;
instance->signal.message = *message;
}
void irda_worker_set_raw_signal(IrdaWorker* instance, const uint32_t* timings, size_t timings_cnt) {
furi_assert(instance);
furi_assert(timings);
furi_assert(timings_cnt > 0);
size_t max_copy_num = COUNT_OF(instance->signal.timings) - 1;
furi_check(timings_cnt <= max_copy_num);
instance->signal.timings[0] = IRDA_RAW_TX_TIMING_DELAY_US;
memcpy(&instance->signal.timings[1], timings, timings_cnt * sizeof(uint32_t));
instance->signal.decoded = false;
instance->signal.timings_cnt = timings_cnt + 1;
}
IrdaWorkerGetSignalResponse
irda_worker_tx_get_signal_steady_callback(void* context, IrdaWorker* instance) {
IrdaWorkerGetSignalResponse response = instance->tx.steady_signal_sent ?
IrdaWorkerGetSignalResponseSame :
IrdaWorkerGetSignalResponseNew;
instance->tx.steady_signal_sent = true;
return response;
}

View File

@@ -1,171 +0,0 @@
#pragma once
#include <irda.h>
#include <furi_hal.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_TIMINGS_AMOUNT 512
/** Interface struct of irda worker */
typedef struct IrdaWorker IrdaWorker;
/** Interface struct of received signal */
typedef struct IrdaWorkerSignal IrdaWorkerSignal;
typedef enum {
IrdaWorkerGetSignalResponseNew, /** Signal, provided by callback is new and encoder should be reseted */
IrdaWorkerGetSignalResponseSame, /** Signal, provided by callback is same. No encoder resetting. */
IrdaWorkerGetSignalResponseStop, /** No more signals available. */
} IrdaWorkerGetSignalResponse;
/** Callback type for providing next signal to send. Should be used with
* irda_worker_make_decoded_signal() or irda_worker_make_raw_signal()
*/
typedef IrdaWorkerGetSignalResponse (
*IrdaWorkerGetSignalCallback)(void* context, IrdaWorker* instance);
/** Callback type for 'message is sent' event */
typedef void (*IrdaWorkerMessageSentCallback)(void* context);
/** Callback type to call by IrdaWorker thread when new signal is received */
typedef void (*IrdaWorkerReceivedSignalCallback)(void* context, IrdaWorkerSignal* received_signal);
/** Allocate IrdaWorker
*
* @return just created instance of IrdaWorker
*/
IrdaWorker* irda_worker_alloc();
/** Free IrdaWorker
*
* @param[in] instance - IrdaWorker instance
*/
void irda_worker_free(IrdaWorker* instance);
/** Start IrdaWorker thread, initialise furi_hal, prepare all work.
*
* @param[in] instance - IrdaWorker instance
*/
void irda_worker_rx_start(IrdaWorker* instance);
/** Stop IrdaWorker thread, deinitialize furi_hal.
*
* @param[in] instance - IrdaWorker instance
*/
void irda_worker_rx_stop(IrdaWorker* instance);
/** Set received data callback IrdaWorker
*
* @param[in] instance - IrdaWorker instance
* @param[in] context - context to pass to callbacks
* @param[in] callback - IrdaWorkerReceivedSignalCallback callback
*/
void irda_worker_rx_set_received_signal_callback(
IrdaWorker* instance,
IrdaWorkerReceivedSignalCallback callback,
void* context);
/** Enable blinking on receiving any signal on IR port.
*
* @param[in] instance - instance of IrdaWorker
* @param[in] enable - true if you want to enable blinking
* false otherwise
*/
void irda_worker_rx_enable_blink_on_receiving(IrdaWorker* instance, bool enable);
/** Clarify is received signal either decoded or raw
*
* @param[in] signal - received signal
* @return true if signal is decoded, false if signal is raw
*/
bool irda_worker_signal_is_decoded(const IrdaWorkerSignal* signal);
/** Start transmitting signal. Callback IrdaWorkerGetSignalCallback should be
* set before this function is called, as it calls for it to fill buffer before
* starting transmission.
*
* @param[in] instance - IrdaWorker instance
*/
void irda_worker_tx_start(IrdaWorker* instance);
/** Stop transmitting signal. Waits for end of current signal and stops transmission.
*
* @param[in] instance - IrdaWorker instance
*/
void irda_worker_tx_stop(IrdaWorker* instance);
/** Set callback for providing next signal to send
*
* @param[in] instance - IrdaWorker instance
* @param[in] context - context to pass to callbacks
* @param[in] callback - IrdaWorkerGetSignalCallback callback
*/
void irda_worker_tx_set_get_signal_callback(
IrdaWorker* instance,
IrdaWorkerGetSignalCallback callback,
void* context);
/** Set callback for end of signal transmitting
*
* @param[in] instance - IrdaWorker instance
* @param[in] context - context to pass to callbacks
* @param[in] callback - IrdaWorkerMessageSentCallback callback
*/
void irda_worker_tx_set_signal_sent_callback(
IrdaWorker* instance,
IrdaWorkerMessageSentCallback callback,
void* context);
/** Callback to pass to irda_worker_tx_set_get_signal_callback() if signal
* is steady and will not be changed between irda_worker start and stop.
* Before starting transmission, desired steady signal must be set with
* irda_worker_make_decoded_signal() or irda_worker_make_raw_signal().
*
* This function should not be implicitly called.
*
* @param[in] context - context
* @param[out] instance - IrdaWorker instance
*/
IrdaWorkerGetSignalResponse
irda_worker_tx_get_signal_steady_callback(void* context, IrdaWorker* instance);
/** Acquire raw signal from interface struct 'IrdaWorkerSignal'.
* First, you have to ensure that signal is raw.
*
* @param[in] signal - received signal
* @param[out] timings - pointer to array of timings
* @param[out] timings_cnt - pointer to amount of timings
*/
void irda_worker_get_raw_signal(
const IrdaWorkerSignal* signal,
const uint32_t** timings,
size_t* timings_cnt);
/** Acquire decoded message from interface struct 'IrdaWorkerSignal'.
* First, you have to ensure that signal is decoded.
*
* @param[in] signal - received signal
* @return decoded IRDA message
*/
const IrdaMessage* irda_worker_get_decoded_signal(const IrdaWorkerSignal* signal);
/** Set current decoded signal for IrdaWorker instance
*
* @param[out] instance - IrdaWorker instance
* @param[in] message - decoded signal
*/
void irda_worker_set_decoded_signal(IrdaWorker* instance, const IrdaMessage* message);
/** Set current raw signal for IrdaWorker instance
*
* @param[out] instance - IrdaWorker instance
* @param[in] timings - array of raw timings
* @param[in] timings_cnt - size of array of raw timings
*/
void irda_worker_set_raw_signal(IrdaWorker* instance, const uint32_t* timings, size_t timings_cnt);
#ifdef __cplusplus
}
#endif

View File

@@ -83,11 +83,11 @@ CFLAGS += -I$(LIB_DIR)/drivers
C_SOURCES += $(wildcard $(LIB_DIR)/drivers/*.c)
# IR lib
CFLAGS += -I$(LIB_DIR)/irda/encoder_decoder
CFLAGS += -I$(LIB_DIR)/irda/worker
C_SOURCES += $(wildcard $(LIB_DIR)/irda/encoder_decoder/*.c)
C_SOURCES += $(wildcard $(LIB_DIR)/irda/encoder_decoder/*/*.c)
C_SOURCES += $(wildcard $(LIB_DIR)/irda/worker/*.c)
CFLAGS += -I$(LIB_DIR)/infrared/encoder_decoder
CFLAGS += -I$(LIB_DIR)/infrared/worker
C_SOURCES += $(wildcard $(LIB_DIR)/infrared/encoder_decoder/*.c)
C_SOURCES += $(wildcard $(LIB_DIR)/infrared/encoder_decoder/*/*.c)
C_SOURCES += $(wildcard $(LIB_DIR)/infrared/worker/*.c)
# SubGhz
C_SOURCES += $(wildcard $(LIB_DIR)/subghz/*.c)