decoder_emmarin.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "emmarin.h"
  2. #include "decoder_emmarin.h"
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. constexpr uint32_t clocks_in_us = 64;
  6. constexpr uint32_t short_time = 255 * clocks_in_us;
  7. constexpr uint32_t long_time = 510 * clocks_in_us;
  8. constexpr uint32_t jitter_time = 100 * clocks_in_us;
  9. constexpr uint32_t short_time_low = short_time - jitter_time;
  10. constexpr uint32_t short_time_high = short_time + jitter_time;
  11. constexpr uint32_t long_time_low = long_time - jitter_time;
  12. constexpr uint32_t long_time_high = long_time + jitter_time;
  13. void DecoderEMMarin::reset_state() {
  14. ready = false;
  15. read_data = 0;
  16. manchester_advance(
  17. manchester_saved_state, ManchesterEventReset, &manchester_saved_state, nullptr);
  18. }
  19. bool DecoderEMMarin::read(uint8_t* data, uint8_t data_size) {
  20. bool result = false;
  21. if(ready) {
  22. result = true;
  23. em_marin.decode(
  24. reinterpret_cast<const uint8_t*>(&read_data), sizeof(uint64_t), data, data_size);
  25. ready = false;
  26. }
  27. return result;
  28. }
  29. void DecoderEMMarin::process_front(bool polarity, uint32_t time) {
  30. if(ready) return;
  31. if(time < short_time_low) return;
  32. ManchesterEvent event = ManchesterEventReset;
  33. if(time > short_time_low && time < short_time_high) {
  34. if(polarity) {
  35. event = ManchesterEventShortHigh;
  36. } else {
  37. event = ManchesterEventShortLow;
  38. }
  39. } else if(time > long_time_low && time < long_time_high) {
  40. if(polarity) {
  41. event = ManchesterEventLongHigh;
  42. } else {
  43. event = ManchesterEventLongLow;
  44. }
  45. }
  46. if(event != ManchesterEventReset) {
  47. bool data;
  48. bool data_ok =
  49. manchester_advance(manchester_saved_state, event, &manchester_saved_state, &data);
  50. if(data_ok) {
  51. read_data = (read_data << 1) | data;
  52. ready = em_marin.can_be_decoded(
  53. reinterpret_cast<const uint8_t*>(&read_data), sizeof(uint64_t));
  54. }
  55. }
  56. }
  57. DecoderEMMarin::DecoderEMMarin() {
  58. reset_state();
  59. }