2021-07-08 18:20:13 +00:00
|
|
|
#include "furi/check.h"
|
|
|
|
#include "irda.h"
|
|
|
|
#include "irda_common_i.h"
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <furi.h>
|
|
|
|
#include "irda_i.h"
|
2021-09-15 17:22:58 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
static IrdaStatus
|
|
|
|
irda_common_encode_bits(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
|
2021-09-15 17:22:58 +00:00
|
|
|
IrdaStatus status = encoder->protocol->encode(encoder, duration, level);
|
|
|
|
furi_assert(status == IrdaStatusOk);
|
|
|
|
++encoder->timings_encoded;
|
|
|
|
encoder->timings_sum += *duration;
|
2022-01-05 16:10:18 +00:00
|
|
|
if((encoder->bits_encoded == encoder->bits_to_encode) && *level) {
|
2021-09-15 17:22:58 +00:00
|
|
|
status = IrdaStatusDone;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2021-07-08 18:20:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* 3:
|
|
|
|
* even_timing = 0
|
|
|
|
* level = 0 ^ 1 = 1
|
|
|
|
* 4:
|
|
|
|
* even_timing = 1
|
|
|
|
* level = 1 ^ 1 = 0
|
|
|
|
* ++timing;
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 0 1 2 | 3 4 |
|
|
|
|
* _____-------_____---___
|
|
|
|
*/
|
2022-01-05 16:10:18 +00:00
|
|
|
IrdaStatus
|
|
|
|
irda_common_encode_manchester(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
|
2021-07-08 18:20:13 +00:00
|
|
|
furi_assert(encoder);
|
|
|
|
furi_assert(duration);
|
|
|
|
furi_assert(level);
|
|
|
|
|
|
|
|
const IrdaTimings* timings = &encoder->protocol->timings;
|
|
|
|
uint8_t index = encoder->bits_encoded / 8;
|
2022-01-05 16:10:18 +00:00
|
|
|
uint8_t shift = encoder->bits_encoded % 8; // LSB first
|
2021-07-08 18:20:13 +00:00
|
|
|
bool logic_value = !!(encoder->data[index] & (0x01 << shift));
|
|
|
|
bool even_timing = !(encoder->timings_encoded % 2);
|
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
*level = even_timing ^ logic_value;
|
2021-07-08 18:20:13 +00:00
|
|
|
*duration = timings->bit1_mark;
|
2022-01-05 16:10:18 +00:00
|
|
|
if(even_timing)
|
2021-07-08 18:20:13 +00:00
|
|
|
++encoder->bits_encoded;
|
2022-01-05 16:10:18 +00:00
|
|
|
else if(*level && (encoder->bits_encoded + 1 == encoder->bits_to_encode))
|
|
|
|
++encoder->bits_encoded; /* don't encode last space */
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2021-09-15 17:22:58 +00:00
|
|
|
return IrdaStatusOk;
|
2021-07-08 18:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IrdaStatus irda_common_encode_pdwm(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
|
|
|
|
furi_assert(encoder);
|
|
|
|
furi_assert(duration);
|
|
|
|
furi_assert(level);
|
|
|
|
|
|
|
|
const IrdaTimings* timings = &encoder->protocol->timings;
|
|
|
|
uint8_t index = encoder->bits_encoded / 8;
|
2022-01-05 16:10:18 +00:00
|
|
|
uint8_t shift = encoder->bits_encoded % 8; // LSB first
|
2021-07-08 18:20:13 +00:00
|
|
|
bool logic_value = !!(encoder->data[index] & (0x01 << shift));
|
2021-09-15 17:22:58 +00:00
|
|
|
bool pwm = timings->bit1_space == timings->bit0_space;
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(encoder->timings_encoded % 2) { /* start encoding from space */
|
2021-07-08 18:20:13 +00:00
|
|
|
*duration = logic_value ? timings->bit1_mark : timings->bit0_mark;
|
|
|
|
*level = true;
|
2022-01-05 16:10:18 +00:00
|
|
|
if(pwm) ++encoder->bits_encoded;
|
2021-07-08 18:20:13 +00:00
|
|
|
} else {
|
|
|
|
*duration = logic_value ? timings->bit1_space : timings->bit0_space;
|
|
|
|
*level = false;
|
2022-01-05 16:10:18 +00:00
|
|
|
if(!pwm) ++encoder->bits_encoded;
|
2021-09-09 21:37:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 17:22:58 +00:00
|
|
|
return IrdaStatusOk;
|
2021-07-08 18:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IrdaStatus irda_common_encode(IrdaCommonEncoder* encoder, uint32_t* duration, bool* level) {
|
|
|
|
furi_assert(encoder);
|
|
|
|
furi_assert(duration);
|
|
|
|
furi_assert(level);
|
|
|
|
IrdaStatus status = IrdaStatusOk;
|
|
|
|
const IrdaTimings* timings = &encoder->protocol->timings;
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
switch(encoder->state) {
|
2021-09-09 21:37:32 +00:00
|
|
|
case IrdaCommonEncoderStateSilence:
|
2021-07-08 18:20:13 +00:00
|
|
|
*duration = encoder->protocol->timings.silence_time;
|
|
|
|
*level = false;
|
|
|
|
status = IrdaStatusOk;
|
|
|
|
encoder->state = IrdaCommonEncoderStatePreamble;
|
|
|
|
++encoder->timings_encoded;
|
2021-09-09 21:37:32 +00:00
|
|
|
encoder->timings_sum = 0;
|
2021-07-08 18:20:13 +00:00
|
|
|
break;
|
|
|
|
case IrdaCommonEncoderStatePreamble:
|
2022-01-05 16:10:18 +00:00
|
|
|
if(timings->preamble_mark) {
|
|
|
|
if(encoder->timings_encoded == 1) {
|
2021-07-08 18:20:13 +00:00
|
|
|
*duration = timings->preamble_mark;
|
|
|
|
*level = true;
|
|
|
|
} else {
|
|
|
|
*duration = timings->preamble_space;
|
|
|
|
*level = false;
|
|
|
|
encoder->state = IrdaCommonEncoderStateEncode;
|
|
|
|
}
|
|
|
|
++encoder->timings_encoded;
|
2021-09-09 21:37:32 +00:00
|
|
|
encoder->timings_sum += *duration;
|
2021-07-08 18:20:13 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
encoder->state = IrdaCommonEncoderStateEncode;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case IrdaCommonEncoderStateEncode:
|
2021-09-15 17:22:58 +00:00
|
|
|
status = irda_common_encode_bits(encoder, duration, level);
|
2022-01-05 16:10:18 +00:00
|
|
|
if(status == IrdaStatusDone) {
|
|
|
|
if(encoder->protocol->encode_repeat) {
|
2021-07-08 18:20:13 +00:00
|
|
|
encoder->state = IrdaCommonEncoderStateEncodeRepeat;
|
|
|
|
} else {
|
|
|
|
encoder->timings_encoded = 0;
|
2021-09-09 21:37:32 +00:00
|
|
|
encoder->timings_sum = 0;
|
2021-07-08 18:20:13 +00:00
|
|
|
encoder->bits_encoded = 0;
|
|
|
|
encoder->switch_detect = 0;
|
2021-09-09 21:37:32 +00:00
|
|
|
encoder->state = IrdaCommonEncoderStateSilence;
|
2021-07-08 18:20:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IrdaCommonEncoderStateEncodeRepeat:
|
|
|
|
status = encoder->protocol->encode_repeat(encoder, duration, level);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* irda_common_encoder_alloc(const IrdaCommonProtocolSpec* protocol) {
|
|
|
|
furi_assert(protocol);
|
2022-01-05 16:10:18 +00:00
|
|
|
if(protocol->decode == irda_common_decode_pdwm) {
|
|
|
|
furi_assert(
|
|
|
|
(protocol->timings.bit1_mark == protocol->timings.bit0_mark) ^
|
|
|
|
(protocol->timings.bit1_space == protocol->timings.bit0_space));
|
2021-09-15 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
|
2022-01-05 16:10:18 +00:00
|
|
|
for(int i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
|
2021-09-15 17:22:58 +00:00
|
|
|
furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
|
|
|
|
}
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
uint32_t alloc_size = sizeof(IrdaCommonDecoder) + protocol->databit_len[0] / 8 +
|
|
|
|
!!(protocol->databit_len[0] % 8);
|
2022-02-18 19:53:46 +00:00
|
|
|
IrdaCommonEncoder* encoder = malloc(alloc_size);
|
2021-07-08 18:20:13 +00:00
|
|
|
memset(encoder, 0, alloc_size);
|
|
|
|
encoder->protocol = protocol;
|
|
|
|
|
|
|
|
return encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void irda_common_encoder_free(IrdaCommonEncoder* encoder) {
|
|
|
|
furi_assert(encoder);
|
|
|
|
free(encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void irda_common_encoder_reset(IrdaCommonEncoder* encoder) {
|
|
|
|
furi_assert(encoder);
|
|
|
|
encoder->timings_encoded = 0;
|
2021-09-09 21:37:32 +00:00
|
|
|
encoder->timings_sum = 0;
|
2021-07-08 18:20:13 +00:00
|
|
|
encoder->bits_encoded = 0;
|
2021-09-09 21:37:32 +00:00
|
|
|
encoder->state = IrdaCommonEncoderStateSilence;
|
2021-07-08 18:20:13 +00:00
|
|
|
encoder->switch_detect = 0;
|
|
|
|
|
2021-09-15 17:22:58 +00:00
|
|
|
uint8_t max_databit_len = 0;
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
for(int i = 0; i < COUNT_OF(encoder->protocol->databit_len); ++i) {
|
2021-09-15 17:22:58 +00:00
|
|
|
max_databit_len = MAX(max_databit_len, encoder->protocol->databit_len[i]);
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
uint8_t bytes_to_clear = max_databit_len / 8 + !!(max_databit_len % 8);
|
2021-07-08 18:20:13 +00:00
|
|
|
memset(encoder->data, 0, bytes_to_clear);
|
|
|
|
}
|