irda_common_decoder_i.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include <stdint.h>
  3. #include "irda.h"
  4. #define MATCH_BIT_TIMING(x, v, delta) ( ((x) < (v + delta)) \
  5. && ((x) > (v - delta)))
  6. #define MATCH_PREAMBLE_TIMING(x, v, delta) ( ((x) < ((v) * (1 + (delta)))) \
  7. && ((x) > ((v) * (1 - (delta)))))
  8. typedef enum {
  9. DecodeStatusError,
  10. DecodeStatusOk,
  11. DecodeStatusReady,
  12. } DecodeStatus;
  13. typedef struct IrdaCommonDecoder IrdaCommonDecoder;
  14. typedef DecodeStatus (*IrdaCommonDecode)(IrdaCommonDecoder*);
  15. typedef bool (*IrdaCommonInterpret)(IrdaCommonDecoder*);
  16. typedef DecodeStatus (*IrdaCommonDecodeRepeat)(IrdaCommonDecoder*);
  17. typedef enum IrdaCommonState {
  18. IrdaCommonStateWaitPreamble,
  19. IrdaCommonStateDecode,
  20. IrdaCommonStateProcessRepeat,
  21. } IrdaCommonState;
  22. typedef struct {
  23. uint16_t preamble_mark;
  24. uint16_t preamble_space;
  25. uint16_t bit1_mark;
  26. uint16_t bit1_space;
  27. uint16_t bit0_mark;
  28. uint16_t bit0_space;
  29. float preamble_tolerance;
  30. uint32_t bit_tolerance;
  31. } IrdaCommonDecoderTimings;
  32. typedef struct {
  33. IrdaCommonDecoderTimings timings;
  34. uint32_t databit_len;
  35. IrdaCommonDecode decode;
  36. IrdaCommonInterpret interpret;
  37. IrdaCommonDecodeRepeat decode_repeat;
  38. } IrdaCommonProtocolSpec;
  39. struct IrdaCommonDecoder {
  40. const IrdaCommonProtocolSpec* protocol;
  41. IrdaCommonState state;
  42. IrdaMessage message;
  43. uint32_t timings[6];
  44. uint8_t timings_cnt;
  45. uint32_t level;
  46. uint16_t databit_cnt;
  47. uint8_t data[];
  48. };
  49. static inline void shift_left_array(uint32_t *array, uint32_t len, uint32_t shift) {
  50. for (int i = 0; i < len; ++i)
  51. array[i] = array[i + shift];
  52. }
  53. IrdaMessage* irda_common_decode(IrdaCommonDecoder *decoder, bool level, uint32_t duration);
  54. void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec *protocol);
  55. void irda_common_decoder_free(void* decoder);
  56. void irda_common_decoder_reset(void* decoder);
  57. DecodeStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder);