decoder_indala.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "decoder_indala.h"
  2. #include <furi_hal.h>
  3. constexpr uint32_t clocks_in_us = 64;
  4. constexpr uint32_t us_per_bit = 255;
  5. bool DecoderIndala::read(uint8_t* data, uint8_t data_size) {
  6. bool result = false;
  7. if(ready) {
  8. result = true;
  9. if(cursed_data_valid) {
  10. indala.decode(
  11. reinterpret_cast<const uint8_t*>(&cursed_raw_data),
  12. sizeof(uint64_t),
  13. data,
  14. data_size);
  15. } else {
  16. indala.decode(
  17. reinterpret_cast<const uint8_t*>(&raw_data), sizeof(uint64_t), data, data_size);
  18. }
  19. reset_state();
  20. }
  21. return result;
  22. }
  23. void DecoderIndala::process_front(bool polarity, uint32_t time) {
  24. if(ready) return;
  25. process_internal(polarity, time, &raw_data);
  26. if(ready) return;
  27. if(polarity) {
  28. time = time + 110;
  29. } else {
  30. time = time - 110;
  31. }
  32. process_internal(!polarity, time, &cursed_raw_data);
  33. if(ready) {
  34. cursed_data_valid = true;
  35. }
  36. }
  37. void DecoderIndala::process_internal(bool polarity, uint32_t time, uint64_t* data) {
  38. time /= clocks_in_us;
  39. time += (us_per_bit / 2);
  40. uint32_t bit_count = (time / us_per_bit);
  41. if(bit_count < 64) {
  42. for(uint32_t i = 0; i < bit_count; i++) {
  43. *data = (*data << 1) | polarity;
  44. if((*data >> 32) == 0xa0000000ULL) {
  45. if(indala.can_be_decoded(
  46. reinterpret_cast<const uint8_t*>(data), sizeof(uint64_t))) {
  47. ready = true;
  48. break;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. DecoderIndala::DecoderIndala() {
  55. reset_state();
  56. }
  57. void DecoderIndala::reset_state() {
  58. raw_data = 0;
  59. cursed_raw_data = 0;
  60. ready = false;
  61. cursed_data_valid = false;
  62. }