1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #include "app.h"
- #include "nexa.h"
- enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
- static unsigned char temp1state = UNKNOWN;
- unsigned long temp1_x_data;
- static unsigned int temp1x_numBits;
- void temp1ResetDecoder () {
- temp1_x_data = 0;
- temp1x_numBits = 0;
- temp1state = UNKNOWN;
- }
- static boolean temp1isDone() { return temp1state == DONE; }
- static void temp1done () { temp1state = DONE; }
- static void temp1gotBit (char value) {
- // X
- temp1x_numBits++;
- temp1_x_data = (temp1_x_data << 1) + (value & 0x01);
- temp1state = OK;
- }
- // Pulse timings
- #define INIT_PULSE 3600
- #define START_PULSE 440
- #define SHORT_PULSE 850
- #define LONG_PULSE 1720
- #define MARG 200
- static int temp1decode (unsigned int width) {
-
-
- switch (temp1state) {
- case UNKNOWN: // Start of frame
- if ( (INIT_PULSE-MARG) <= width && width <= (INIT_PULSE+MARG) ) {
- temp1state = T0;
- }
- else {
- return -1; // error, reset
- }
- break;
- case T0: // First half of pulse : HIGH around 230us
- if( temp1x_numBits == 32 ) { // end of frame
- temp1state = DONE;
- temp1_x_data = (temp1_x_data>>8); // Mask away some bits at the end
- return 1;
- }
- else if ( (START_PULSE-MARG) <= width && width <= (START_PULSE+MARG) ) {
- temp1state = T1;
- }
- else {
- if( temp1x_numBits == 0 && 3400 <= width && width <= 3800 ) {
- temp1state = T0;
- }
- else {
- return -1; // error, reset
- }
- }
- break;
- case T1:
- if ( (SHORT_PULSE-MARG) <= width && width <= (SHORT_PULSE+MARG)) {
- temp1gotBit(0);
- } else if ( (LONG_PULSE-MARG) <= width && width <= (LONG_PULSE+MARG) ) {
- temp1gotBit(1);
- } else {
- return -1; // error, reset
- }
- temp1state = T0;
- break;
- }
- return 0;
- }
- boolean nextPulseTemp1 (unsigned int width) {
-
- if( width > 0 ) {
- if (temp1state != DONE)
- switch (temp1decode(width)) {
- case -1:
- temp1ResetDecoder();
- break;
- case 1: temp1done(); break;
- }
- }
- return temp1isDone();
- }
|