#include "app.h" #include "nexa.h" #include "temp2.h" enum { UNKNOWN, T0, T1, T2, T3, OK, DONE }; static unsigned char temp2state = T0; unsigned long long temp2_x_data; static unsigned int temp2x_numBits; void temp2ResetDecoder () { temp2_x_data = 0; temp2x_numBits = 0; temp2state = T0; } static boolean temp2isDone() { return temp2state == DONE; } static void temp2done () { temp2state = DONE; } static void temp2gotBit (char value) { // X temp2x_numBits++; temp2_x_data = (temp2_x_data << 1) + (value & 0x01); temp2state = OK; } #define MARG 50 static int temp2decode (unsigned int width) { switch (temp2state) { case T0: // First half of pulse : HIGH around 910 if( temp2x_numBits == 48 ) { // end of frame temp2state = DONE; return 1; } else if ( (910-MARG) <= width && width <= (910+MARG) ) { temp2state = T1; } else { return -1; // error, reset } break; case T1: if ( (410-MARG) <= width && width <= (410+MARG)) { // 410 temp2gotBit(1); } else if ( (1290-MARG) <= width && width <= (1290+MARG) ) { // 1290 temp2gotBit(0); } else { return -1; // error, reset } temp2state = T0; break; } return 0; } boolean nextPulseTemp2 (unsigned int width) { if( width > 0 ) { if (temp2state != DONE) switch (temp2decode(width)) { case -1: temp2ResetDecoder(); break; case 1: temp2done(); break; } } return temp2isDone(); }