#include "app.h" #include "nexa.h" #include "oregon.h" // Constructor must run resetOV3Decoder() static int decode(int width); static void done (); static boolean isDone(); static unsigned char total_bits, bits, flip, state, pos, data[25]; enum { UNKNOWN, T0, T1, T2, T3, OK, DONE }; boolean nextPulseoregonV3(unsigned int width) { if (state != DONE) switch (decode(width)) { case -1: resetOV3Decoder(); break; case 1: done(); break; } return isDone(); } static boolean isDone() { return state == DONE; } const unsigned char* getOV3Data (unsigned char *count) { *count = pos; return data; } void resetOV3Decoder () { total_bits = bits = pos = flip = 0; state = UNKNOWN; } // V3-Version of the gitBit-function void gotBit (char value) { data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00); total_bits++; pos = total_bits >> 3; if (pos >= sizeof data) { resetOV3Decoder(); return; } state = OK; } // store a bit using Manchester encoding void manchester (char value) { flip ^= value; // manchester code, long pulse flips the bit gotBit(flip); } static void done () { while (bits) gotBit(0); // padding state = DONE; } static int decode (int width) { if (200 <= width && width < 1200) { char w = width >= 700; switch (state) { case UNKNOWN: if (w == 0) ++flip; else if (32 <= flip) { flip = 1; manchester(1); } else return -1; break; case OK: if (w == 0) state = T0; else manchester(1); break; case T0: if (w == 0) manchester(0); else return -1; break; } } else { return -1; } return total_bits == 80 ? 1: 0; } /* // move bits to the front so that all the bits are aligned to the end static void alignTail(char max) { // align bits if (bits != 0) { data[pos] >>= 8 - bits; for (char i = 0; i < pos; ++i) data[i] = (data[i] >> bits) | (data[i+1] << (8 - bits)); bits = 0; } // optionally shift bytes down if there are too many of 'em if (max > 0 && pos > max) { char n = pos - max; pos = max; for (char i = 0; i < pos; ++i) data[i] = data[i+n]; } } */ /* static void reverseBits () { for (char i = 0; i < pos; ++i) { char b = data[i]; for (char j = 0; j < 8; ++j) { data[i] = (data[i] << 1) | (b & 1); b >>= 1; } } } */ /* static void reverseNibbles () { for (char i = 0; i < pos; ++i) data[i] = (data[i] << 4) | (data[i] >> 4); } */