ClasOSensor.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "ClasOSensor.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "esp_log.h"
  5. #include "../rxTimer.h"
  6. #include "config.h"
  7. #ifdef TRANCEIVER_ENABLED
  8. enum
  9. {
  10. UNKNOWN,
  11. T0,
  12. T1,
  13. T2,
  14. T3,
  15. TEMP1_OK,
  16. DONE
  17. };
  18. static uint8_t rx_state = UNKNOWN;
  19. static uint64_t sensor_data;
  20. static uint32_t rx_numBits;
  21. void ClasO_ResetDecoder()
  22. {
  23. sensor_data = 0;
  24. rx_numBits = 0;
  25. rx_state = UNKNOWN;
  26. }
  27. static void addBit(uint8_t value)
  28. {
  29. rx_numBits++;
  30. sensor_data = (sensor_data << 1) + (value & 0x01);
  31. rx_state = TEMP1_OK;
  32. }
  33. #define START_PULSE_MIN (3880-200)
  34. #define START_PULSE_MAX (4010+200)
  35. #define T0_PULSE_MIN (380-100)
  36. #define T0_PULSE_MAX (520+100)
  37. #define SHORT_PULSE_MIN (890-100)
  38. #define SHORT_PULSE_MAX (990+100)
  39. #define LONG_PULSE_MIN (1880-150)
  40. #define LONG_PULSE_MAX (1980+150)
  41. static int32_t rx_decode(uint32_t width)
  42. {
  43. switch (rx_state) {
  44. case UNKNOWN: // Start of frame
  45. if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX )
  46. {
  47. rx_state = T0;
  48. }
  49. else
  50. {
  51. return -1; // error, reset
  52. }
  53. break;
  54. case T0: // First half of pulse : HIGH around 230us
  55. if (rx_numBits == 32)
  56. { // end of frame
  57. rx_state = DONE;
  58. sensor_data = (sensor_data >> 8); // Mask away some bits at the end
  59. return 1;
  60. }
  61. else if( T0_PULSE_MIN <= width && width <= T0_PULSE_MAX )
  62. {
  63. rx_state = T1;
  64. }
  65. else
  66. {
  67. if (rx_numBits == 0 && START_PULSE_MIN <= width && width <= START_PULSE_MAX )
  68. {
  69. rx_state = T0;
  70. }
  71. else
  72. {
  73. return -1; // error, reset
  74. }
  75. }
  76. break;
  77. case T1:
  78. if( SHORT_PULSE_MIN <= width && width <= SHORT_PULSE_MAX )
  79. {
  80. addBit(0);
  81. }
  82. else if( LONG_PULSE_MIN <= width && width <= LONG_PULSE_MAX )
  83. {
  84. addBit(1);
  85. }
  86. else
  87. {
  88. return -1; // error, reset
  89. }
  90. rx_state = T0;
  91. break;
  92. }
  93. return 0;
  94. }
  95. int64_t nextPulseClasOSensor(uint32_t width)
  96. {
  97. static int64_t previous_data = 0;
  98. static uint32_t old_time=0;
  99. static uint32_t now;
  100. int64_t retVal = -1;
  101. if (width > 0)
  102. {
  103. if (rx_state != DONE)
  104. {
  105. switch (rx_decode(width))
  106. {
  107. case -1:
  108. ClasO_ResetDecoder();
  109. break;
  110. case 1:
  111. rx_state = DONE;
  112. break;
  113. }
  114. }
  115. }
  116. if (rx_state == DONE) {
  117. now = millis();
  118. if( sensor_data != previous_data || (now > (old_time+1000)) ) {
  119. previous_data = sensor_data;
  120. retVal = sensor_data;
  121. }
  122. old_time = now;
  123. ClasO_ResetDecoder();
  124. }
  125. return retVal;
  126. }
  127. #endif