123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #include "proovesmartSensor.h"
- #include <stdint.h>
- #include <stdio.h>
- #include "../rxTimer.h"
- #include <string.h>
- #include "esp_log.h"
- #include "config.h"
- #ifdef TRANCEIVER_ENABLED
- 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;
- }
- old_time = now;
- ProovesmartSensor_ResetDecoder();
- }
-
- return retVal;
- }
- #endif
|