#include "ClasOSensor.h" #include #include #include "esp_log.h" #include "../rxTimer.h" #include "../led.h" enum { UNKNOWN, T0, T1, T2, T3, TEMP1_OK, DONE }; static uint8_t rx_state = UNKNOWN; static uint64_t sensor_data; static uint32_t rx_numBits; static uint32_t debug_width; void ClasO_ResetDecoder() { sensor_data = 0; rx_numBits = 0; rx_state = UNKNOWN; debug_width = 0; } static void addBit(uint8_t value) { rx_numBits++; sensor_data = (sensor_data << 1) + (value & 0x01); rx_state = TEMP1_OK; } static int32_t rx_decode(uint32_t width) { switch (rx_state) { case UNKNOWN: // Start of frame if (3700 <= width && width <= 4150) { rx_state = T0; debug_width = width; } else { return -1; // error, reset } break; case T0: // First half of pulse : HIGH around 230us if (rx_numBits == 32) { // end of frame rx_state = DONE; //printf("c"); sensor_data = (sensor_data >> 8); // Mask away some bits at the end return 1; } else if (420 <= width && width <= 560) { rx_state = T1; } else { if (rx_numBits == 0 && 3700 <= width && width <= 4150 ) { rx_state = T0; } else { return -1; // error, reset } } break; case T1: if (880 <= width && width <= 1080) { addBit(0); } else if (1800 <= width && width <= 2100) { //debug_width = width; addBit(1); } else { return -1; // error, reset } rx_state = T0; break; } return 0; } int64_t nextPulseClasOSensor(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: ClasO_ResetDecoder(); break; case 1: rx_state = DONE; //printf("%d\n",debug_width); break; } } } if (rx_state == DONE) { now = millis(); // For debug only if( ((sensor_data>>12) & 0x007) == 1 ) { ESP_LOGE("CLAS","Width:%u",debug_width); } if( sensor_data != previous_data || (now > (old_time+1000)) ) { previous_data = sensor_data; retVal = sensor_data; blinkTheLED(); } old_time = now; ClasO_ResetDecoder(); } return retVal; }