123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include "proovesmartSensor.h"
- #include <stdint.h>
- #include <stdio.h>
- #include "../rxTimer.h"
- #include "../led.h"
- #include <string.h>
- #include "esp_log.h"
- enum { UNKNOWN, T0, T1, T2, T3, OK_Sensor, DONE };
- static unsigned char rx_state = T0;
- static uint64_t sensor_data;
- static uint32_t rx_numBits;
- static unsigned char isTrigged = 0;
- static uint32_t trigDataBuff[4] = {0,0,0,0};
- void ProovesmartSensor_ResetDecoder () {
- sensor_data = 0;
- rx_numBits = 0;
- rx_state = T0;
- isTrigged = 0;
- memset(trigDataBuff,0,sizeof(trigDataBuff));
- }
- static void addBit (uint8_t value) {
- rx_numBits++;
- sensor_data = (sensor_data << 1) + (value & 0x01);
- rx_state = OK_Sensor;
- }
- #define MARG 55
- static int rx_decode (uint32_t width) {
-
- switch (rx_state) {
- uint32_t bitData;
- case T0: // First ON-half of pulse : HIGH around 910
- if ( (1010-MARG) <= width && width <= (1080) ) {
- rx_state = T1;
- }
- else {
- if( isTrigged ) {
- //ESP_LOGE("PROOVE", "T0 Un-Trig B:%u W:%u",rx_numBits,width);
- }
- return -1; // error, reset
- }
- break;
- case T1:
- if ( (445-MARG) <= width && width <= (445+MARG)) {
- bitData = 1;
- } else if ( (1420-MARG) <= width && width <= (1420+MARG) ) {
- bitData = 0;
- } else {
- if( isTrigged ) {
- //ESP_LOGE("PROOVE", "T1 Un-Trig B:%u W:%u",rx_numBits,width);
- }
- return -1; // error, reset
- }
- if( isTrigged == 0 ) {
- trigDataBuff[3] = trigDataBuff[2];
- trigDataBuff[2] = trigDataBuff[1];
- trigDataBuff[1] = trigDataBuff[0];
- trigDataBuff[0] = bitData;
- if( trigDataBuff[3] == 0 &&
- trigDataBuff[2] == 1 &&
- trigDataBuff[1] == 0 &&
- trigDataBuff[0] == 0) {
- isTrigged = 1;
- // ESP_LOGI("PROOVE", "Trig %u",width);
- }
- }
- else {
- addBit(bitData);
- if( rx_numBits == 36-8 ) { // end of frame (We skip the last 8 bits)
- rx_state = DONE;
- return 1;
- }
- }
-
- rx_state = T0;
- break;
- }
- return 0;
- }
- int64_t nextPulseProovesmartSensor(uint32_t width)
- {
- static int64_t previous_data = 0;
- static uint32_t old_time=0;
- static uint32_t now;
- int64_t retVal = -1;
- if (width > 0)
- {
- if (rx_state != DONE)
- {
- switch (rx_decode(width))
- {
- case -1:
- ProovesmartSensor_ResetDecoder();
- break;
- case 1:
- rx_state = DONE;
- break;
- }
- }
- }
- if (rx_state == DONE) {
- now = millis();
- sensor_data <<= 8;
- if( sensor_data != previous_data || (now > (old_time+1000)) ) {
- previous_data = sensor_data;
- retVal = sensor_data;
- blinkTheLED();
- //printf("%d\n",debug_width);
- }
- old_time = now;
- ProovesmartSensor_ResetDecoder();
- }
-
- return retVal;
- }
|