// Temperature sensor/hydrometer ProoveSmart TSS320 (Kjells) #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; } // Pulse timings #define START_PULSE 860 #define LONG_PULSE 1380 #define SHORT_PULSE 480 #define MARG 100 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 ( (START_PULSE-MARG) <= width && width <= (START_PULSE+MARG) ) { temp2state = T1; } else { return -1; // error, reset } break; case T1: if ( (SHORT_PULSE-MARG) <= width && width <= (SHORT_PULSE+MARG)) { // 410 temp2gotBit(1); } else if ( (LONG_PULSE-MARG) <= width && width <= (LONG_PULSE+MARG) ) { // 1290 temp2gotBit(0); } else { return -1; // error, reset } temp2state = T0; break; } return 0; } #pragma optimize=none boolean nextPulseTemp2 (unsigned int width) { static int result; if( width > 0 ) { if( temp2state != DONE ) { result = temp2decode(width); switch( result ) { case -1: temp2ResetDecoder(); break; case 1: temp2done(); break; default: case 0: break; } } } return temp2isDone(); }