ClasOSensor.c 3.1 KB

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