proovesmartSensor.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "proovesmartSensor.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "../rxTimer.h"
  5. #include "../led.h"
  6. #include <string.h>
  7. #include "esp_log.h"
  8. typedef struct {
  9. uint32_t max;
  10. uint32_t min;
  11. uint32_t cnt;
  12. uint32_t total;
  13. } debug_data_t;
  14. debug_data_t debug_data = { 0,99999,0,0 };
  15. debug_data_t debug_data_tot = { 0,99999,0,0 };
  16. #define DEBUG_WIDTH_FUNC() { \
  17. if( width > debug_data.max ) debug_data.max = width; \
  18. if( width < debug_data.min ) debug_data.min = width; \
  19. debug_data.total += width; \
  20. debug_data.cnt++; \
  21. }
  22. enum { UNKNOWN, T0, T1, T2, T3, OK_Sensor, DONE };
  23. static unsigned char rx_state = T0;
  24. static uint64_t sensor_data;
  25. static uint32_t rx_numBits;
  26. static unsigned char isTrigged = 0;
  27. static uint32_t trigDataBuff[4] = {0,0,0,0};
  28. void ProovesmartSensor_ResetDecoder () {
  29. sensor_data = 0;
  30. rx_numBits = 0;
  31. rx_state = T0;
  32. isTrigged = 0;
  33. memset(trigDataBuff,0,sizeof(trigDataBuff));
  34. }
  35. static void addBit (uint8_t value) {
  36. rx_numBits++;
  37. sensor_data = (sensor_data << 1) + (value & 0x01);
  38. rx_state = OK_Sensor;
  39. }
  40. static int rx_decode (uint32_t width) {
  41. switch (rx_state) {
  42. uint32_t bitData;
  43. case T0: // First ON-half of pulse : HIGH around 910
  44. if ( (960-100) <= width && width <= (1080+100) ) {
  45. rx_state = T1;
  46. }
  47. else {
  48. return -1; // error, reset
  49. }
  50. break;
  51. case T1:
  52. if ( 200 <= width && width <= 2000 ) {
  53. if( width > 1000 ) {
  54. bitData = 0;
  55. }
  56. else {
  57. //DEBUG_WIDTH_FUNC();
  58. bitData = 1;
  59. }
  60. } else {
  61. return -1; // error, reset
  62. }
  63. if( isTrigged == 0 ) {
  64. trigDataBuff[3] = trigDataBuff[2];
  65. trigDataBuff[2] = trigDataBuff[1];
  66. trigDataBuff[1] = trigDataBuff[0];
  67. trigDataBuff[0] = bitData;
  68. if( trigDataBuff[3] == 0 &&
  69. trigDataBuff[2] == 1 &&
  70. trigDataBuff[1] == 0 &&
  71. trigDataBuff[0] == 0) {
  72. isTrigged = 1;
  73. // ESP_LOGI("PROOVE", "Trig %u",width);
  74. }
  75. }
  76. else {
  77. addBit(bitData);
  78. if( rx_numBits == 36-8 ) { // end of frame (We skip the last 8 bits)
  79. rx_state = DONE;
  80. return 1;
  81. }
  82. }
  83. rx_state = T0;
  84. break;
  85. }
  86. return 0;
  87. }
  88. int64_t nextPulseProovesmartSensor(uint32_t width)
  89. {
  90. static int64_t previous_data = 0;
  91. static uint32_t old_time=0;
  92. static uint32_t now;
  93. int64_t retVal = -1;
  94. if (width > 0)
  95. {
  96. if (rx_state != DONE)
  97. {
  98. switch (rx_decode(width))
  99. {
  100. case -1:
  101. ProovesmartSensor_ResetDecoder();
  102. break;
  103. case 1:
  104. rx_state = DONE;
  105. break;
  106. }
  107. }
  108. }
  109. if (rx_state == DONE) {
  110. now = millis();
  111. sensor_data <<= 8;
  112. /*
  113. if( debug_data.max > debug_data_tot.max ) debug_data_tot.max = debug_data.max;
  114. if( debug_data.min < debug_data_tot.min ) debug_data_tot.min = debug_data.min;
  115. printf("OREGON DEbug min,max:%u %u\n",debug_data_tot.min,debug_data_tot.max);
  116. */
  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. ProovesmartSensor_ResetDecoder();
  124. }
  125. return retVal;
  126. }