#include "nexa.h" #include #include #include "../rxTimer.h" #include "../led.h" #define FACTOR 9 #define CONV(a) ((((a)*FACTOR) / 100) + (a)) struct nexaData_t { unsigned char bitNo; unsigned short width; } ; enum { UNKNOWN, T0, T1, T2, T3, OK_Sensor, DONE }; static uint8_t rx_state = UNKNOWN; static uint64_t sensor_data; static uint32_t rx_numBits; static uint8_t x_2ndbit; void NEXA_resetDecoder () { sensor_data = 0; rx_numBits = 0; x_2ndbit = false; rx_state = UNKNOWN; } static void addBit (uint8_t value) { // X rx_numBits++; sensor_data = (sensor_data << 1) + (value & 0x01); if( x_2ndbit == false ) { x_2ndbit = true; } else { x_2ndbit = false; unsigned int bits = (sensor_data & 0x03); if( bits == 2 ) { // Bit is 1 sensor_data = (sensor_data >> 1) | 0x00000001; } else { // Bit is 0 sensor_data = (sensor_data >> 1) & 0xFFFFFFFE; } } rx_state = OK_Sensor; } static int rx_decode(uint32_t width) { if( rx_numBits == 3 ) { // end of frame //state = DONE; //return 1; } switch (rx_state) { case UNKNOWN: // Start of frame if ( 2240 <= width && width <= 3540 ) { rx_state = T0; } else { return -1; // error, reset } break; case T0: // First half of pulse if ( 155 <= width && width <= 305 ) { rx_state = T1; } else { if( rx_numBits == 0 && 2240 <= width && width <= 3540 ) { rx_state = T0; } else { return -1; // error, reset } } break; case T1: if ( 240 <= width && width <= 440 ) { addBit(0); } else if ( (1410-300) <= width && width <= (1410+300) ) { //debug_width = width; addBit(1); } else if( rx_numBits == 64 ) { // end of frame rx_state = DONE; return 1; } else { return -1; // error, reset } rx_state = T0; break; } return 0; } #define TOO_SHORT 100 int64_t nextPulseNEXA(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: NEXA_resetDecoder(); break; case 1: rx_state = DONE; //printf("%d\n",debug_width); break; } } } if (rx_state == DONE) { now = millis(); if( sensor_data != previous_data || (now > (old_time+1000)) ) { previous_data = sensor_data; retVal = sensor_data; blinkTheLED(); } old_time = now; NEXA_resetDecoder(); } return retVal; }