ClasOSensor.c 2.8 KB

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