proovesmartSensor.c 3.8 KB

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