proovesmartSensor.c 3.3 KB

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