ClasOSensor.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. static uint32_t debug_width;
  21. void ClasO_ResetDecoder()
  22. {
  23. sensor_data = 0;
  24. rx_numBits = 0;
  25. rx_state = UNKNOWN;
  26. debug_width = 0;
  27. }
  28. static void addBit(uint8_t value)
  29. {
  30. rx_numBits++;
  31. sensor_data = (sensor_data << 1) + (value & 0x01);
  32. rx_state = TEMP1_OK;
  33. }
  34. static int32_t rx_decode(uint32_t width)
  35. {
  36. switch (rx_state) {
  37. case UNKNOWN: // Start of frame
  38. if (3700 <= width && width <= 4150)
  39. {
  40. rx_state = T0;
  41. debug_width = width;
  42. }
  43. else
  44. {
  45. return -1; // error, reset
  46. }
  47. break;
  48. case T0: // First half of pulse : HIGH around 230us
  49. if (rx_numBits == 32)
  50. { // end of frame
  51. rx_state = DONE;
  52. //printf("c");
  53. sensor_data = (sensor_data >> 8); // Mask away some bits at the end
  54. return 1;
  55. }
  56. else if (420 <= width && width <= 560)
  57. {
  58. rx_state = T1;
  59. }
  60. else
  61. {
  62. if (rx_numBits == 0 && 3700 <= width && width <= 4150 )
  63. {
  64. rx_state = T0;
  65. }
  66. else
  67. {
  68. return -1; // error, reset
  69. }
  70. }
  71. break;
  72. case T1:
  73. if (880 <= width && width <= 1080)
  74. {
  75. addBit(0);
  76. }
  77. else if (1800 <= width && width <= 2100)
  78. {
  79. //debug_width = width;
  80. addBit(1);
  81. }
  82. else
  83. {
  84. return -1; // error, reset
  85. }
  86. rx_state = T0;
  87. break;
  88. }
  89. return 0;
  90. }
  91. int64_t nextPulseClasOSensor(uint32_t width)
  92. {
  93. static int64_t previous_data = 0;
  94. static uint32_t old_time=0;
  95. static uint32_t now;
  96. int64_t retVal = -1;
  97. if (width > 0)
  98. {
  99. if (rx_state != DONE)
  100. {
  101. switch (rx_decode(width))
  102. {
  103. case -1:
  104. ClasO_ResetDecoder();
  105. break;
  106. case 1:
  107. rx_state = DONE;
  108. //printf("%d\n",debug_width);
  109. break;
  110. }
  111. }
  112. }
  113. if (rx_state == DONE) {
  114. now = millis();
  115. // For debug only
  116. if( ((sensor_data>>12) & 0x007) == 1 ) {
  117. ESP_LOGE("CLAS","Width:%u",debug_width);
  118. }
  119. if( sensor_data != previous_data || (now > (old_time+1000)) ) {
  120. previous_data = sensor_data;
  121. retVal = sensor_data;
  122. blinkTheLED();
  123. }
  124. old_time = now;
  125. ClasO_ResetDecoder();
  126. }
  127. return retVal;
  128. }