2023-01-13 16:50:19 +03:00
|
|
|
#include "infrared_protocol_nec_i.h"
|
|
|
|
|
|
|
|
#include <core/core_defines.h>
|
2022-07-20 13:56:33 +03:00
|
|
|
#include <core/check.h>
|
2021-05-18 13:51:00 +03:00
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
static const uint32_t repeat_timings[] = {
|
2022-02-25 18:22:58 +03:00
|
|
|
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,
|
2021-05-18 13:51:00 +03:00
|
|
|
};
|
|
|
|
|
2022-02-25 18:22:58 +03:00
|
|
|
void infrared_encoder_nec_reset(void* encoder_ptr, const InfraredMessage* message) {
|
2021-07-08 21:20:13 +03:00
|
|
|
furi_assert(encoder_ptr);
|
2021-08-20 23:51:15 +03:00
|
|
|
furi_assert(message);
|
2021-07-08 21:20:13 +03:00
|
|
|
|
2022-02-25 18:22:58 +03:00
|
|
|
InfraredCommonEncoder* encoder = encoder_ptr;
|
|
|
|
infrared_common_encoder_reset(encoder);
|
2021-07-08 21:20:13 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
uint32_t* data1 = (void*)encoder->data;
|
2021-09-15 20:22:58 +03:00
|
|
|
uint32_t* data2 = data1 + 1;
|
2022-02-25 18:22:58 +03:00
|
|
|
if(message->protocol == InfraredProtocolNEC) {
|
2021-09-15 20:22:58 +03:00
|
|
|
uint8_t address = message->address;
|
|
|
|
uint8_t address_inverse = ~address;
|
|
|
|
uint8_t command = message->command;
|
|
|
|
uint8_t command_inverse = ~command;
|
|
|
|
*data1 = address;
|
|
|
|
*data1 |= address_inverse << 8;
|
|
|
|
*data1 |= command << 16;
|
|
|
|
*data1 |= command_inverse << 24;
|
|
|
|
encoder->bits_to_encode = 32;
|
2022-02-25 18:22:58 +03:00
|
|
|
} else if(message->protocol == InfraredProtocolNECext) {
|
2022-01-05 19:10:18 +03:00
|
|
|
*data1 = (uint16_t)message->address;
|
2021-09-15 20:22:58 +03:00
|
|
|
*data1 |= (message->command & 0xFFFF) << 16;
|
|
|
|
encoder->bits_to_encode = 32;
|
2022-02-25 18:22:58 +03:00
|
|
|
} else if(message->protocol == InfraredProtocolNEC42) {
|
2021-09-15 20:22:58 +03:00
|
|
|
/* 13 address + 13 inverse address + 8 command + 8 inv command */
|
|
|
|
*data1 = message->address & 0x1FFFUL;
|
|
|
|
*data1 |= (~message->address & 0x1FFFUL) << 13;
|
|
|
|
*data1 |= ((message->command & 0x3FUL) << 26);
|
|
|
|
*data2 = (message->command & 0xC0UL) >> 6;
|
|
|
|
*data2 |= (~message->command & 0xFFUL) << 2;
|
|
|
|
encoder->bits_to_encode = 42;
|
2022-02-25 18:22:58 +03:00
|
|
|
} else if(message->protocol == InfraredProtocolNEC42ext) {
|
2021-09-15 20:22:58 +03:00
|
|
|
*data1 = message->address & 0x3FFFFFF;
|
|
|
|
*data1 |= ((message->command & 0x3F) << 26);
|
|
|
|
*data2 = (message->command & 0xFFC0) >> 6;
|
|
|
|
encoder->bits_to_encode = 42;
|
|
|
|
} else {
|
2023-10-31 13:40:32 +03:00
|
|
|
furi_crash();
|
2021-08-20 23:51:15 +03:00
|
|
|
}
|
2021-07-08 21:20:13 +03:00
|
|
|
}
|
|
|
|
|
2022-02-25 18:22:58 +03:00
|
|
|
InfraredStatus infrared_encoder_nec_encode_repeat(
|
|
|
|
InfraredCommonEncoder* encoder,
|
|
|
|
uint32_t* duration,
|
|
|
|
bool* level) {
|
2021-07-08 21:20:13 +03:00
|
|
|
furi_assert(encoder);
|
|
|
|
|
|
|
|
/* space + 2 timings preambule + payload + stop bit */
|
2021-09-15 20:22:58 +03:00
|
|
|
uint32_t timings_encoded_up_to_repeat = 1 + 2 + encoder->bits_to_encode * 2 + 1;
|
2021-07-08 21:20:13 +03:00
|
|
|
uint32_t repeat_cnt = encoder->timings_encoded - timings_encoded_up_to_repeat;
|
|
|
|
|
|
|
|
furi_assert(encoder->timings_encoded >= timings_encoded_up_to_repeat);
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
if(repeat_cnt > 0) {
|
2021-07-08 21:20:13 +03:00
|
|
|
*duration = repeat_timings[repeat_cnt % COUNT_OF(repeat_timings)];
|
2021-09-10 00:37:32 +03:00
|
|
|
} else {
|
2022-02-25 18:22:58 +03:00
|
|
|
*duration = INFRARED_NEC_REPEAT_PERIOD - encoder->timings_sum;
|
2021-09-10 00:37:32 +03:00
|
|
|
}
|
2021-07-08 21:20:13 +03:00
|
|
|
|
|
|
|
*level = repeat_cnt % 2;
|
|
|
|
++encoder->timings_encoded;
|
|
|
|
bool done = (!((repeat_cnt + 1) % COUNT_OF(repeat_timings)));
|
|
|
|
|
2022-02-25 18:22:58 +03:00
|
|
|
return done ? InfraredStatusDone : InfraredStatusOk;
|
2021-07-08 21:20:13 +03:00
|
|
|
}
|
2021-05-18 13:51:00 +03:00
|
|
|
|
2022-02-25 18:22:58 +03:00
|
|
|
void* infrared_encoder_nec_alloc(void) {
|
2023-01-13 16:50:19 +03:00
|
|
|
return infrared_common_encoder_alloc(&infrared_protocol_nec);
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
|
|
|
|
2022-02-25 18:22:58 +03:00
|
|
|
void infrared_encoder_nec_free(void* encoder_ptr) {
|
|
|
|
infrared_common_encoder_free(encoder_ptr);
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
|
|
|
|
2022-02-25 18:22:58 +03:00
|
|
|
InfraredStatus infrared_encoder_nec_encode(void* encoder_ptr, uint32_t* duration, bool* level) {
|
|
|
|
return infrared_common_encode(encoder_ptr, duration, level);
|
2021-06-02 18:16:05 +03:00
|
|
|
}
|