2021-05-04 13:21:16 +00:00
|
|
|
#include "decoder-indala.h"
|
2021-08-08 18:03:25 +00:00
|
|
|
#include <furi-hal.h>
|
2021-05-04 13:21:16 +00:00
|
|
|
|
|
|
|
constexpr uint32_t clocks_in_us = 64;
|
2021-08-01 22:11:18 +00:00
|
|
|
constexpr uint32_t us_per_bit = 255;
|
2021-05-04 13:21:16 +00:00
|
|
|
|
|
|
|
bool DecoderIndala::read(uint8_t* data, uint8_t data_size) {
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if(ready) {
|
|
|
|
result = true;
|
2021-08-01 22:11:18 +00:00
|
|
|
if(cursed_data_valid) {
|
|
|
|
indala.decode(
|
|
|
|
reinterpret_cast<const uint8_t*>(&cursed_raw_data),
|
|
|
|
sizeof(uint64_t),
|
|
|
|
data,
|
|
|
|
data_size);
|
|
|
|
} else {
|
|
|
|
indala.decode(
|
|
|
|
reinterpret_cast<const uint8_t*>(&raw_data), sizeof(uint64_t), data, data_size);
|
|
|
|
}
|
|
|
|
reset_state();
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecoderIndala::process_front(bool polarity, uint32_t time) {
|
|
|
|
if(ready) return;
|
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
process_internal(polarity, time, &raw_data);
|
|
|
|
if(ready) return;
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
if(polarity) {
|
|
|
|
time = time + 110;
|
2021-05-04 13:21:16 +00:00
|
|
|
} else {
|
2021-08-01 22:11:18 +00:00
|
|
|
time = time - 110;
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
process_internal(!polarity, time, &cursed_raw_data);
|
|
|
|
if(ready) {
|
|
|
|
cursed_data_valid = true;
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
2021-08-01 22:11:18 +00:00
|
|
|
}
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
void DecoderIndala::process_internal(bool polarity, uint32_t time, uint64_t* data) {
|
|
|
|
time /= clocks_in_us;
|
|
|
|
time += (us_per_bit / 2);
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
uint32_t bit_count = (time / us_per_bit);
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
if(bit_count < 64) {
|
|
|
|
for(uint32_t i = 0; i < bit_count; i++) {
|
|
|
|
*data = (*data << 1) | polarity;
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
if((*data >> 32) == 0xa0000000ULL) {
|
|
|
|
if(indala.can_be_decoded(
|
|
|
|
reinterpret_cast<const uint8_t*>(data), sizeof(uint64_t))) {
|
|
|
|
ready = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-01 22:11:18 +00:00
|
|
|
}
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
DecoderIndala::DecoderIndala() {
|
|
|
|
reset_state();
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
2021-08-01 22:11:18 +00:00
|
|
|
|
|
|
|
void DecoderIndala::reset_state() {
|
|
|
|
raw_data = 0;
|
|
|
|
cursed_raw_data = 0;
|
|
|
|
ready = false;
|
|
|
|
cursed_data_valid = false;
|
|
|
|
}
|