#include "proovesmartSensor.h" #include #include #include "../rxTimer.h" #include "../led.h" #include #include "esp_log.h" typedef struct { uint32_t max; uint32_t min; uint32_t cnt; uint32_t total; } debug_data_t; static debug_data_t debug_data = { 0,99999,0,0 }; static debug_data_t debug_data_tot = { 0,99999,0,0 }; #define DEBUG_WIDTH_FUNC() { \ if( width > debug_data.max ) debug_data.max = width; \ if( width < debug_data.min ) debug_data.min = width; \ debug_data.total += width; \ debug_data.cnt++; \ } 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; } static int rx_decode (uint32_t width) { switch (rx_state) { uint32_t bitData; case T0: // First ON-half of pulse : HIGH around 910 if ( (960-100) <= width && width <= (1080+100) ) { rx_state = T1; } else { return -1; // error, reset } break; case T1: if ( 200 <= width && width <= 2000 ) { if( width > 1000 ) { bitData = 0; } else { //DEBUG_WIDTH_FUNC(); bitData = 1; } } else { 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( debug_data.max > debug_data_tot.max ) debug_data_tot.max = debug_data.max; if( debug_data.min < debug_data_tot.min ) debug_data_tot.min = debug_data.min; printf("OREGON DEbug min,max:%u %u\n",debug_data_tot.min,debug_data_tot.max); */ if( sensor_data != previous_data || (now > (old_time+1000)) ) { previous_data = sensor_data; retVal = sensor_data; blinkTheLED(); } old_time = now; ProovesmartSensor_ResetDecoder(); } return retVal; }