2021-08-10 23:09:51 +00:00
|
|
|
#include "emmarin.h"
|
|
|
|
#include "decoder-emmarin.h"
|
2021-05-04 13:21:16 +00:00
|
|
|
#include <furi.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;
|
|
|
|
constexpr uint32_t short_time = 255 * clocks_in_us;
|
|
|
|
constexpr uint32_t long_time = 510 * clocks_in_us;
|
|
|
|
constexpr uint32_t jitter_time = 100 * clocks_in_us;
|
|
|
|
|
|
|
|
constexpr uint32_t short_time_low = short_time - jitter_time;
|
|
|
|
constexpr uint32_t short_time_high = short_time + jitter_time;
|
|
|
|
constexpr uint32_t long_time_low = long_time - jitter_time;
|
|
|
|
constexpr uint32_t long_time_high = long_time + jitter_time;
|
|
|
|
|
2021-08-10 23:09:51 +00:00
|
|
|
void DecoderEMMarin::reset_state() {
|
2021-05-04 13:21:16 +00:00
|
|
|
ready = false;
|
|
|
|
readed_data = 0;
|
|
|
|
manchester_advance(
|
|
|
|
manchester_saved_state, ManchesterEventReset, &manchester_saved_state, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-08-10 23:09:51 +00:00
|
|
|
bool DecoderEMMarin::read(uint8_t* data, uint8_t data_size) {
|
2021-05-04 13:21:16 +00:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if(ready) {
|
|
|
|
result = true;
|
2021-08-10 23:09:51 +00:00
|
|
|
em_marin.decode(
|
2021-05-18 08:37:47 +00:00
|
|
|
reinterpret_cast<const uint8_t*>(&readed_data), sizeof(uint64_t), data, data_size);
|
2021-05-04 13:21:16 +00:00
|
|
|
ready = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-10 23:09:51 +00:00
|
|
|
void DecoderEMMarin::process_front(bool polarity, uint32_t time) {
|
2021-05-04 13:21:16 +00:00
|
|
|
if(ready) return;
|
|
|
|
if(time < short_time_low) return;
|
|
|
|
|
|
|
|
ManchesterEvent event = ManchesterEventReset;
|
|
|
|
|
|
|
|
if(time > short_time_low && time < short_time_high) {
|
|
|
|
if(polarity) {
|
|
|
|
event = ManchesterEventShortHigh;
|
|
|
|
} else {
|
|
|
|
event = ManchesterEventShortLow;
|
|
|
|
}
|
|
|
|
} else if(time > long_time_low && time < long_time_high) {
|
|
|
|
if(polarity) {
|
|
|
|
event = ManchesterEventLongHigh;
|
|
|
|
} else {
|
|
|
|
event = ManchesterEventLongLow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event != ManchesterEventReset) {
|
|
|
|
bool data;
|
|
|
|
bool data_ok =
|
|
|
|
manchester_advance(manchester_saved_state, event, &manchester_saved_state, &data);
|
|
|
|
|
|
|
|
if(data_ok) {
|
|
|
|
readed_data = (readed_data << 1) | data;
|
|
|
|
|
2021-08-10 23:09:51 +00:00
|
|
|
ready = em_marin.can_be_decoded(
|
2021-05-18 08:37:47 +00:00
|
|
|
reinterpret_cast<const uint8_t*>(&readed_data), sizeof(uint64_t));
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 23:09:51 +00:00
|
|
|
DecoderEMMarin::DecoderEMMarin() {
|
2021-05-04 13:21:16 +00:00
|
|
|
reset_state();
|
|
|
|
}
|