#include "ClasOSensor.h" #include #include #include "esp_log.h" #include "../rxTimer.h" #include "config.h" #ifdef TRANCEIVER_ENABLED 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; void ClasO_ResetDecoder() { sensor_data = 0; rx_numBits = 0; rx_state = UNKNOWN; } static void addBit(uint8_t value) { rx_numBits++; sensor_data = (sensor_data << 1) + (value & 0x01); rx_state = TEMP1_OK; } #define START_PULSE_MIN (3880-200) #define START_PULSE_MAX (4010+200) #define T0_PULSE_MIN (380-100) #define T0_PULSE_MAX (520+100) #define SHORT_PULSE_MIN (890-100) #define SHORT_PULSE_MAX (990+100) #define LONG_PULSE_MIN (1880-150) #define LONG_PULSE_MAX (1980+150) static int32_t rx_decode(uint32_t width) { switch (rx_state) { case UNKNOWN: // Start of frame if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX ) { rx_state = T0; } 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; sensor_data = (sensor_data >> 8); // Mask away some bits at the end return 1; } else if( T0_PULSE_MIN <= width && width <= T0_PULSE_MAX ) { rx_state = T1; } else { if (rx_numBits == 0 && START_PULSE_MIN <= width && width <= START_PULSE_MAX ) { rx_state = T0; } else { return -1; // error, reset } } break; case T1: if( SHORT_PULSE_MIN <= width && width <= SHORT_PULSE_MAX ) { addBit(0); } else if( LONG_PULSE_MIN <= width && width <= LONG_PULSE_MAX ) { 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; break; } } } if (rx_state == DONE) { now = millis(); if( sensor_data != previous_data || (now > (old_time+1000)) ) { previous_data = sensor_data; retVal = sensor_data; } old_time = now; ClasO_ResetDecoder(); } return retVal; } #endif