#include "nexa.h" #include #include #include "../rxTimer.h" #include "../led.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++; \ } #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; debug_data.max = 0; debug_data.min = 99999; } 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 ) { // Hallway-button: 2760 //DEBUG_WIDTH_FUNC(); rx_state = T0; } else { return -1; // error, reset } break; case T0: // First half of pulse if ( 155 <= width && width <= 305 ) { // Hallway-button: 230-250 rx_state = T1; } else { if( rx_numBits == 0 && 2240 <= width && width <= 3540 ) { rx_state = T0; } else { return -1; // error, reset } } break; case T1: if ( 200 <= width && width <= 440 ) { // Hallway-button: 250-270 addBit(0); } else if ( 950 <= width && width <= 1700 ) { // Hallway-button: 1250-1270 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( 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("NEXA 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; NEXA_resetDecoder(); } return retVal; }