[FL-1156, FL-1249] Add IRDA encoder/decoder library (#451)

* Add cscope db generation
* Add api-hal-irda, TIM2: HAL->LL
* Add libirda: pwm decoding
* Universal state machine
* Add irda decoder library
* Move IRDA capture to standalone tool
* Add encoder/decoder samsung32, NEC, fix bugs
* Port current App to new Irda lib
* Fix clang format for test data
* Port IRDA api-hal to f6

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
Albert Kharisov
2021-05-18 13:51:00 +03:00
committed by GitHub
parent ba0419276e
commit 3114a2d4b8
54 changed files with 1916 additions and 417 deletions

111
lib/irda/irda.c Normal file
View File

@@ -0,0 +1,111 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <furi.h>
#include "irda_i.h"
struct IrdaHandler {
void** ctx;
};
typedef struct {
IrdaAlloc alloc;
IrdaDecode decode;
IrdaFree free;
} IrdaDecoders;
typedef struct {
IrdaEncode encode;
} IrdaEncoders;
typedef struct {
IrdaProtocol protocol;
const char* name;
IrdaDecoders decoder;
IrdaEncoders encoder;
} IrdaProtocolImplementation;
// TODO: replace with key-value, Now we refer by enum index, which is dangerous.
static const IrdaProtocolImplementation irda_protocols[] = {
// #0
{ .protocol = IrdaProtocolSamsung32,
.name ="Samsung32",
.decoder = {
.alloc = irda_decoder_samsung32_alloc,
.decode = irda_decoder_samsung32_decode,
.free = irda_decoder_samsung32_free},
.encoder = {
.encode = irda_encoder_samsung32_encode}
},
// #1
{ .protocol = IrdaProtocolNEC,
.name = "NEC",
.decoder = {
.alloc = irda_decoder_nec_alloc,
.decode = irda_decoder_nec_decode,
.free = irda_decoder_nec_free},
.encoder = {
.encode = irda_encoder_nec_encode}
},
};
const IrdaMessage* irda_decode(IrdaHandler* handler, bool level, uint32_t duration) {
furi_assert(handler);
IrdaMessage* message = NULL;
IrdaMessage* result = NULL;
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
message = irda_protocols[i].decoder.decode(handler->ctx[i], level, duration);
if (!result && message) {
message->protocol = irda_protocols[i].protocol;
result = message;
}
}
return result;
}
IrdaHandler* irda_alloc_decoder(void) {
IrdaHandler* handler = furi_alloc(sizeof(IrdaHandler));
handler->ctx = furi_alloc(sizeof(void*) * COUNT_OF(irda_protocols));
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
handler->ctx[i] = irda_protocols[i].decoder.alloc();
furi_check(handler->ctx[i]);
}
return handler;
}
void irda_free_decoder(IrdaHandler* handler) {
furi_assert(handler);
furi_assert(handler->ctx);
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
irda_protocols[i].decoder.free(handler->ctx[i]);
}
free(handler->ctx);
free(handler);
}
void irda_send(const IrdaMessage* message, int times) {
furi_assert(message);
for (int i = 0; i < times; ++i) {
osKernelLock();
__disable_irq();
irda_protocols[message->protocol].encoder.encode(message->address, message->command, !!i);
__enable_irq();
osKernelUnlock();
}
}
const char* irda_get_protocol_name(IrdaProtocol protocol) {
return irda_protocols[protocol].name;
}

72
lib/irda/irda.h Normal file
View File

@@ -0,0 +1,72 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct IrdaHandler IrdaHandler;
// Do not change protocol order, as it can be saved into memory and fw update can be performed!
typedef enum {
IrdaProtocolSamsung32 = 0,
IrdaProtocolNEC = 1,
} IrdaProtocol;
typedef struct {
IrdaProtocol protocol;
uint32_t address;
uint32_t command;
bool repeat;
} IrdaMessage;
/**
* Initialize decoder.
*
* \return returns pointer to IRDA decoder handler if success, otherwise - error.
*/
IrdaHandler* irda_alloc_decoder(void);
/**
* Provide to decoder next timing. If message is ready, it returns decoded message,
* otherwise NULL.
*
* \param[in] handler - handler to irda decoders. Should be aquired with \c irda_alloc_decoder().
* \param[in] level - high(true) or low(false) level of input signal to analyze.
* it should alternate every call, otherwise it is an error case,
* and decoder resets its state and start decoding from the start.
* \param[in] duration - duration of steady high/low input signal.
* \return if message is ready, returns pointer to decoded message, returns NULL.
*/
const IrdaMessage* irda_decode(IrdaHandler* handler, bool level, uint32_t duration);
/**
* Deinitialize decoder and free allocated memory.
*
* \param[in] handler - handler to irda decoders. Should be aquired with \c irda_alloc_decoder().
*/
void irda_free_decoder(IrdaHandler* handler);
/**
* Send message over IRDA.
*
* \param[in] message - message to send.
* \param[in] times - number of times message should be sent.
*/
void irda_send(const IrdaMessage* message, int times);
/**
* Get protocol name by protocol enum.
*
* \param[in] protocol - protocol identifier.
* \return string to protocol name.
*/
const char* irda_get_protocol_name(IrdaProtocol protocol);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,166 @@
#include <stdbool.h>
#include <furi.h>
#include "irda_i.h"
static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
// align to start at Mark timing
if (start_level) {
if (decoder->timings_cnt > 0) {
--decoder->timings_cnt;
shift_left_array(decoder->timings, decoder->timings_cnt, 1);
}
}
while ((!result) && (decoder->timings_cnt >= 2)) {
float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
uint16_t preamble_mark = decoder->protocol->timings.preamble_mark;
uint16_t preamble_space = decoder->protocol->timings.preamble_space;
if ((MATCH_PREAMBLE_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance))
&& (MATCH_PREAMBLE_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
result = true;
}
decoder->timings_cnt -= 2;
shift_left_array(decoder->timings, decoder->timings_cnt, 2);
}
return result;
}
// Pulse Distance-Width Modulation
DecodeStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
uint32_t* timings = decoder->timings;
uint16_t index = 0;
uint8_t shift = 0;
DecodeStatus status = DecodeStatusError;
uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
uint16_t bit1_space = decoder->protocol->timings.bit1_space;
uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
uint16_t bit0_space = decoder->protocol->timings.bit0_space;
while (1) {
// Stop bit
if ((decoder->databit_cnt == decoder->protocol->databit_len) && (decoder->timings_cnt == 1)) {
if (MATCH_BIT_TIMING(timings[0], bit1_mark, bit_tolerance)) {
decoder->timings_cnt = 0;
status = DecodeStatusReady;
} else {
status = DecodeStatusError;
}
break;
}
if (decoder->timings_cnt >= 2) {
index = decoder->databit_cnt / 8;
shift = decoder->databit_cnt % 8; // LSB first
if (!shift)
decoder->data[index] = 0;
if (MATCH_BIT_TIMING(timings[0], bit1_mark, bit_tolerance)
&& MATCH_BIT_TIMING(timings[1], bit1_space, bit_tolerance)) {
decoder->data[index] |= (0x1 << shift); // add 1
} else if (MATCH_BIT_TIMING(timings[0], bit0_mark, bit_tolerance)
&& MATCH_BIT_TIMING(timings[1], bit0_space, bit_tolerance)) {
(void) decoder->data[index]; // add 0
} else {
status = DecodeStatusError;
break;
}
++decoder->databit_cnt;
decoder->timings_cnt -= 2;
shift_left_array(decoder->timings, decoder->timings_cnt, 2);
} else {
status = DecodeStatusOk;
break;
}
}
return status;
}
IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
furi_assert(decoder);
IrdaMessage* message = 0;
DecodeStatus status = DecodeStatusError;
if (decoder->level == level) {
furi_assert(0);
decoder->timings_cnt = 0;
}
decoder->level = level; // start with high level (Space timing)
decoder->timings[decoder->timings_cnt] = duration;
decoder->timings_cnt++;
furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
while(1) {
switch (decoder->state) {
case IrdaCommonStateWaitPreamble:
if (irda_check_preamble(decoder)) {
decoder->state = IrdaCommonStateDecode;
decoder->databit_cnt = 0;
}
break;
case IrdaCommonStateDecode:
status = decoder->protocol->decode(decoder);
if (status == DecodeStatusReady) {
if (decoder->protocol->interpret(decoder)) {
message = &decoder->message;
decoder->state = IrdaCommonStateProcessRepeat;
} else {
decoder->state = IrdaCommonStateWaitPreamble;
}
} else if (status == DecodeStatusError) {
decoder->state = IrdaCommonStateWaitPreamble;
continue;
}
break;
case IrdaCommonStateProcessRepeat:
if (!decoder->protocol->decode_repeat) {
decoder->state = IrdaCommonStateWaitPreamble;
continue;
}
status = decoder->protocol->decode_repeat(decoder);
if (status == DecodeStatusError) {
decoder->state = IrdaCommonStateWaitPreamble;
continue;
} else if (status == DecodeStatusReady) {
decoder->message.repeat = true;
message = &decoder->message;
}
break;
}
break;
}
return message;
}
void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
furi_assert(protocol);
uint32_t alloc_size = sizeof(IrdaCommonDecoder)
+ protocol->databit_len / 8
+ !!(protocol->databit_len % 8);
IrdaCommonDecoder* decoder = furi_alloc(alloc_size);
memset(decoder, 0, alloc_size);
decoder->protocol = protocol;
return decoder;
}
void irda_common_decoder_free(void* decoder) {
furi_assert(decoder);
free(decoder);
}

View File

@@ -0,0 +1,72 @@
#pragma once
#include <stdint.h>
#include "irda.h"
#define MATCH_BIT_TIMING(x, v, delta) ( ((x) < (v + delta)) \
&& ((x) > (v - delta)))
#define MATCH_PREAMBLE_TIMING(x, v, delta) ( ((x) < ((v) * (1 + (delta)))) \
&& ((x) > ((v) * (1 - (delta)))))
typedef enum {
DecodeStatusError,
DecodeStatusOk,
DecodeStatusReady,
} DecodeStatus;
typedef struct IrdaCommonDecoder IrdaCommonDecoder;
typedef DecodeStatus (*IrdaCommonDecode)(IrdaCommonDecoder*);
typedef bool (*IrdaCommonInterpret)(IrdaCommonDecoder*);
typedef DecodeStatus (*IrdaCommonDecodeRepeat)(IrdaCommonDecoder*);
typedef enum IrdaCommonState {
IrdaCommonStateWaitPreamble,
IrdaCommonStateDecode,
IrdaCommonStateProcessRepeat,
} IrdaCommonState;
typedef struct {
uint16_t preamble_mark;
uint16_t preamble_space;
uint16_t bit1_mark;
uint16_t bit1_space;
uint16_t bit0_mark;
uint16_t bit0_space;
float preamble_tolerance;
uint32_t bit_tolerance;
} IrdaCommonDecoderTimings;
typedef struct {
IrdaCommonDecoderTimings timings;
uint32_t databit_len;
IrdaCommonDecode decode;
IrdaCommonInterpret interpret;
IrdaCommonDecodeRepeat decode_repeat;
} IrdaCommonProtocolSpec;
struct IrdaCommonDecoder {
const IrdaCommonProtocolSpec* protocol;
IrdaCommonState state;
IrdaMessage message;
uint32_t timings[6];
uint8_t timings_cnt;
uint32_t level;
uint16_t databit_cnt;
uint8_t data[];
};
static inline void shift_left_array(uint32_t *array, uint32_t len, uint32_t shift) {
for (int i = 0; i < len; ++i)
array[i] = array[i + shift];
}
IrdaMessage* irda_common_decode(IrdaCommonDecoder *decoder, bool level, uint32_t duration);
void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec *protocol);
void irda_common_decoder_free(void* decoder);
DecodeStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder);

34
lib/irda/irda_encoder.c Normal file
View File

@@ -0,0 +1,34 @@
#include <stdint.h>
#include <stdbool.h>
#include <api-hal-irda.h>
#include <api-hal-delay.h>
#include "irda_i.h"
void irda_encode_mark(const IrdaEncoderTimings *timings, uint32_t duration) {
api_hal_irda_pwm_set(timings->duty_cycle, timings->carrier_frequency);
delay_us(duration);
}
void irda_encode_space(const IrdaEncoderTimings *timings, uint32_t duration) {
(void) timings;
api_hal_irda_pwm_stop();
delay_us(duration);
}
void irda_encode_bit(const IrdaEncoderTimings *timings, bool bit) {
if (bit) {
irda_encode_mark(timings, timings->bit1_mark);
irda_encode_space(timings, timings->bit1_space);
} else {
irda_encode_mark(timings, timings->bit0_mark);
irda_encode_space(timings, timings->bit0_space);
}
}
void irda_encode_byte(const IrdaEncoderTimings *timings, uint8_t data) {
for(uint8_t i = 0; i < 8; i++) {
irda_encode_bit(timings, !!(data & (1 << i)));
}
}

21
lib/irda/irda_encoder_i.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "irda.h"
typedef struct {
uint32_t bit1_mark;
uint32_t bit1_space;
uint32_t bit0_mark;
uint32_t bit0_space;
float duty_cycle;
uint32_t carrier_frequency;
} IrdaEncoderTimings;
void irda_encode_byte(const IrdaEncoderTimings *timings, uint8_t data);
void irda_encode_bit(const IrdaEncoderTimings *timings, bool bit);
void irda_encode_space(const IrdaEncoderTimings *timings, uint32_t duration);
void irda_encode_mark(const IrdaEncoderTimings *timings, uint32_t duration);

13
lib/irda/irda_i.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
#include "irda.h"
#include <stddef.h>
#include "irda_encoder_i.h"
#include "irda_common_decoder_i.h"
#include "irda_protocol_defs_i.h"
typedef void* (*IrdaAlloc) (void);
typedef IrdaMessage* (*IrdaDecode) (void* ctx, bool level, uint32_t duration);
typedef void (*IrdaFree) (void*);
typedef void (*IrdaEncode)(uint32_t address, uint32_t command, bool repeat);

View File

@@ -0,0 +1,78 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "irda.h"
/***************************************************************************************************
* NEC protocol description
* https://radioparty.ru/manuals/encyclopedia/213-ircontrol?start=1
****************************************************************************************************
* Preamble Preamble Pulse Distance/Width Pause Preamble Preamble Stop
* mark space Modulation repeat repeat bit
* mark space
*
* 9000 4500 32 bit + stop bit 40000/100000 9000 2250
* __________ _ _ _ _ _ _ _ _ _ _ _ _ _ ___________ _
* ____ __________ _ _ _ __ __ __ _ _ __ __ _ _ ________________ ____________ ___
*
***************************************************************************************************/
#define IRDA_NEC_PREAMBULE_MARK 9000
#define IRDA_NEC_PREAMBULE_SPACE 4500
#define IRDA_NEC_BIT1_MARK 560
#define IRDA_NEC_BIT1_SPACE 1600
#define IRDA_NEC_BIT0_MARK 560
#define IRDA_NEC_BIT0_SPACE 560
#define IRDA_NEC_REPEAT_PAUSE_MIN 30000
#define IRDA_NEC_REPEAT_PAUSE 40000
#define IRDA_NEC_REPEAT_PAUSE_MAX 150000
#define IRDA_NEC_REPEAT_MARK 9000
#define IRDA_NEC_REPEAT_SPACE 2250
#define IRDA_NEC_CARRIER_FREQUENCY 38000
#define IRDA_NEC_DUTY_CYCLE 0.33
#define IRDA_NEC_PREAMBLE_TOLERANCE 0.07 // percents
#define IRDA_NEC_BIT_TOLERANCE 120 // us
void* irda_decoder_nec_alloc(void);
void irda_encoder_nec_encode(uint32_t address, uint32_t command, bool repeat);
void irda_decoder_nec_free(void* decoder);
IrdaMessage* irda_decoder_nec_decode(void* decoder, bool level, uint32_t duration);
/***************************************************************************************************
* SAMSUNG32 protocol description
* https://www.mikrocontroller.net/articles/IRMP_-_english#SAMSUNG
****************************************************************************************************
* Preamble Preamble Pulse Distance/Width Pause Preamble Preamble Bit1 Stop
* mark space Modulation repeat repeat bit
* mark space
*
* 4500 4500 32 bit + stop bit 40000/100000 4500 4500
* __________ _ _ _ _ _ _ _ _ _ _ _ ___________ _ _
* _ __________ __ _ __ __ __ _ _ __ __ _ ________________ ____________ ____ ___
*
***************************************************************************************************/
#define IRDA_SAMSUNG_PREAMBULE_MARK 4500
#define IRDA_SAMSUNG_PREAMBULE_SPACE 4500
#define IRDA_SAMSUNG_BIT1_MARK 550
#define IRDA_SAMSUNG_BIT1_SPACE 1650
#define IRDA_SAMSUNG_BIT0_MARK 550
#define IRDA_SAMSUNG_BIT0_SPACE 550
#define IRDA_SAMSUNG_REPEAT_PAUSE_MIN 30000
#define IRDA_SAMSUNG_REPEAT_PAUSE 47000
#define IRDA_SAMSUNG_REPEAT_PAUSE_MAX 150000
#define IRDA_SAMSUNG_REPEAT_MARK 4500
#define IRDA_SAMSUNG_REPEAT_SPACE 4500
#define IRDA_SAMSUNG_CARRIER_FREQUENCY 38000
#define IRDA_SAMSUNG_DUTY_CYCLE 0.33
#define IRDA_SAMSUNG_PREAMBLE_TOLERANCE 0.07 // percents
#define IRDA_SAMSUNG_BIT_TOLERANCE 120 // us
void* irda_decoder_samsung32_alloc(void);
void irda_encoder_samsung32_encode(uint32_t address, uint32_t command, bool repeat);
void irda_decoder_samsung32_free(void* decoder);
IrdaMessage* irda_decoder_samsung32_decode(void* decoder, bool level, uint32_t duration);

View File

@@ -0,0 +1,84 @@
#include <stdbool.h>
#include <stdint.h>
#include <furi.h>
#include "../irda_i.h"
static bool interpret_nec(IrdaCommonDecoder* decoder);
static DecodeStatus decode_repeat_nec(IrdaCommonDecoder* decoder);
static const IrdaCommonProtocolSpec protocol_nec = {
{
IRDA_NEC_PREAMBULE_MARK,
IRDA_NEC_PREAMBULE_SPACE,
IRDA_NEC_BIT1_MARK,
IRDA_NEC_BIT1_SPACE,
IRDA_NEC_BIT0_MARK,
IRDA_NEC_BIT0_SPACE,
IRDA_NEC_PREAMBLE_TOLERANCE,
IRDA_NEC_BIT_TOLERANCE,
},
32,
irda_common_decode_pdwm,
interpret_nec,
decode_repeat_nec,
};
static bool interpret_nec(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
uint8_t address = decoder->data[0];
uint8_t address_inverse = decoder->data[1];
uint8_t command = decoder->data[2];
uint8_t command_inverse = decoder->data[3];
if ((command == (uint8_t) ~command_inverse) && (address == (uint8_t) ~address_inverse)) {
decoder->message.command = command;
decoder->message.address = address;
decoder->message.repeat = false;
result = true;
}
return result;
}
// timings start from Space (delay between message and repeat)
static DecodeStatus decode_repeat_nec(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
DecodeStatus status = DecodeStatusError;
if (decoder->timings_cnt < 4)
return DecodeStatusOk;
if ((decoder->timings[0] > IRDA_NEC_REPEAT_PAUSE_MIN)
&& (decoder->timings[0] < IRDA_NEC_REPEAT_PAUSE_MAX)
&& MATCH_PREAMBLE_TIMING(decoder->timings[1], IRDA_NEC_REPEAT_MARK, preamble_tolerance)
&& MATCH_PREAMBLE_TIMING(decoder->timings[2], IRDA_NEC_REPEAT_SPACE, preamble_tolerance)
&& MATCH_BIT_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)) {
status = DecodeStatusReady;
decoder->timings_cnt = 0;
} else {
status = DecodeStatusError;
}
return status;
}
void* irda_decoder_nec_alloc(void) {
return irda_common_decoder_alloc(&protocol_nec);
}
IrdaMessage* irda_decoder_nec_decode(void* decoder, bool level, uint32_t duration) {
return irda_common_decode(decoder, level, duration);
}
void irda_decoder_nec_free(void* decoder) {
irda_common_decoder_free(decoder);
}

View File

@@ -0,0 +1,44 @@
#include <stdint.h>
#include "../irda_i.h"
static const IrdaEncoderTimings encoder_timings = {
.bit1_mark = IRDA_NEC_BIT1_MARK,
.bit1_space = IRDA_NEC_BIT1_SPACE,
.bit0_mark =IRDA_NEC_BIT0_MARK,
.bit0_space = IRDA_NEC_BIT0_SPACE,
.duty_cycle = IRDA_NEC_DUTY_CYCLE,
.carrier_frequency = IRDA_NEC_CARRIER_FREQUENCY,
};
static void irda_encode_nec_preamble(void) {
irda_encode_mark(&encoder_timings, IRDA_NEC_PREAMBULE_MARK);
irda_encode_space(&encoder_timings, IRDA_NEC_PREAMBULE_SPACE);
}
static void irda_encode_nec_repeat(void) {
irda_encode_space(&encoder_timings, IRDA_NEC_REPEAT_PAUSE);
irda_encode_mark(&encoder_timings, IRDA_NEC_REPEAT_MARK);
irda_encode_space(&encoder_timings, IRDA_NEC_REPEAT_SPACE);
irda_encode_bit(&encoder_timings, 1);
}
void irda_encoder_nec_encode(uint32_t addr, uint32_t cmd, bool repeat) {
uint8_t address = addr & 0xFF;
uint8_t command = cmd & 0xFF;
uint8_t address_inverse = (uint8_t) ~address;
uint8_t command_inverse = (uint8_t) ~command;
if (!repeat) {
irda_encode_nec_preamble();
irda_encode_byte(&encoder_timings, address);
irda_encode_byte(&encoder_timings, address_inverse);
irda_encode_byte(&encoder_timings, command);
irda_encode_byte(&encoder_timings, command_inverse);
irda_encode_bit(&encoder_timings, 1);
} else {
irda_encode_nec_repeat();
}
}

View File

@@ -0,0 +1,87 @@
#include <stdbool.h>
#include <stdint.h>
#include <furi.h>
#include "../irda_i.h"
static bool interpret_samsung32(IrdaCommonDecoder* decoder);
static DecodeStatus decode_repeat_samsung32(IrdaCommonDecoder* decoder);
static const IrdaCommonProtocolSpec protocol_samsung32 = {
{
IRDA_SAMSUNG_PREAMBULE_MARK,
IRDA_SAMSUNG_PREAMBULE_SPACE,
IRDA_SAMSUNG_BIT1_MARK,
IRDA_SAMSUNG_BIT1_SPACE,
IRDA_SAMSUNG_BIT0_MARK,
IRDA_SAMSUNG_BIT0_SPACE,
IRDA_SAMSUNG_PREAMBLE_TOLERANCE,
IRDA_SAMSUNG_BIT_TOLERANCE,
},
32,
irda_common_decode_pdwm,
interpret_samsung32,
decode_repeat_samsung32,
};
static bool interpret_samsung32(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
bool result = false;
uint8_t address1 = decoder->data[0];
uint8_t address2 = decoder->data[1];
uint8_t command = decoder->data[2];
uint8_t command_inverse = decoder->data[3];
if ((address1 == address2) && (command == (uint8_t) ~command_inverse)) {
decoder->message.command = command;
decoder->message.address = address1;
decoder->message.repeat = false;
result = true;
}
return result;
}
// timings start from Space (delay between message and repeat)
static DecodeStatus decode_repeat_samsung32(IrdaCommonDecoder* decoder) {
furi_assert(decoder);
float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
DecodeStatus status = DecodeStatusError;
if (decoder->timings_cnt < 6)
return DecodeStatusOk;
if ((decoder->timings[0] > IRDA_SAMSUNG_REPEAT_PAUSE_MIN)
&& (decoder->timings[0] < IRDA_SAMSUNG_REPEAT_PAUSE_MAX)
&& MATCH_PREAMBLE_TIMING(decoder->timings[1], IRDA_SAMSUNG_REPEAT_MARK, preamble_tolerance)
&& MATCH_PREAMBLE_TIMING(decoder->timings[2], IRDA_SAMSUNG_REPEAT_SPACE, preamble_tolerance)
&& MATCH_BIT_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)
&& MATCH_BIT_TIMING(decoder->timings[4], decoder->protocol->timings.bit1_space, bit_tolerance)
&& MATCH_BIT_TIMING(decoder->timings[5], decoder->protocol->timings.bit1_mark, bit_tolerance)
) {
status = DecodeStatusReady;
decoder->timings_cnt = 0;
} else {
status = DecodeStatusError;
}
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);
}

View File

@@ -0,0 +1,44 @@
#include <stdint.h>
#include "../irda_i.h"
static const IrdaEncoderTimings encoder_timings = {
.bit1_mark = IRDA_SAMSUNG_BIT1_MARK,
.bit1_space = IRDA_SAMSUNG_BIT1_SPACE,
.bit0_mark =IRDA_SAMSUNG_BIT0_MARK,
.bit0_space = IRDA_SAMSUNG_BIT0_SPACE,
.duty_cycle = IRDA_SAMSUNG_DUTY_CYCLE,
.carrier_frequency = IRDA_SAMSUNG_CARRIER_FREQUENCY,
};
static void irda_encode_samsung32_preamble(void) {
irda_encode_mark(&encoder_timings, IRDA_SAMSUNG_PREAMBULE_MARK);
irda_encode_space(&encoder_timings, IRDA_SAMSUNG_PREAMBULE_SPACE);
}
static void irda_encode_samsung32_repeat(void) {
irda_encode_space(&encoder_timings, IRDA_SAMSUNG_REPEAT_PAUSE);
irda_encode_mark(&encoder_timings, IRDA_SAMSUNG_REPEAT_MARK);
irda_encode_space(&encoder_timings, IRDA_SAMSUNG_REPEAT_SPACE);
irda_encode_bit(&encoder_timings, 1);
irda_encode_bit(&encoder_timings, 1);
}
void irda_encoder_samsung32_encode(uint32_t addr, uint32_t cmd, bool repeat) {
uint8_t address = addr & 0xFF;
uint8_t command = cmd & 0xFF;
uint8_t command_inverse = (uint8_t) ~command;
if (!repeat) {
irda_encode_samsung32_preamble();
irda_encode_byte(&encoder_timings, address);
irda_encode_byte(&encoder_timings, address);
irda_encode_byte(&encoder_timings, command);
irda_encode_byte(&encoder_timings, command_inverse);
irda_encode_bit(&encoder_timings, 1);
} else {
irda_encode_samsung32_repeat();
}
}