cyfral_decoder.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <stdint.h>
  3. #include <atomic>
  4. class CyfralDecoder {
  5. public:
  6. bool read(uint8_t* data, uint8_t data_size);
  7. void process_front(bool polarity, uint32_t time);
  8. CyfralDecoder();
  9. private:
  10. enum class BitState : uint8_t {
  11. WAIT_FRONT_HIGH,
  12. WAIT_FRONT_LOW,
  13. };
  14. enum class State : uint8_t {
  15. WAIT_START_NIBBLE,
  16. READ_NIBBLE,
  17. READ_STOP_NIBBLE,
  18. };
  19. State state;
  20. BitState bit_state;
  21. bool process_bit(bool polarity, uint32_t time, bool* readed, bool* readed_value);
  22. void reset_state();
  23. bool nibble_valid(uint8_t data);
  24. // high + low period time
  25. uint32_t period_time;
  26. // ready flag, key is readed and valid
  27. std::atomic<bool> ready;
  28. // key data storage
  29. uint16_t key_data;
  30. // temporary nibble storage
  31. uint8_t readed_nibble;
  32. // data valid flag
  33. // MUST be checked only in READ_STOP_NIBBLE state
  34. bool data_valid;
  35. // nibble index, we expect 8 nibbles
  36. uint8_t index;
  37. // bit index in nibble, 4 bit per nibble
  38. uint8_t bit_index;
  39. // max period, 230us x clock per us
  40. uint32_t max_period;
  41. };