- }
- if(mask != 0x01) { // this happens, when we miss the EOF
- // TODO: for some reason this happens quite often
- if (DEBUG) Dbprintf("error, uneven octet! (extra bits!) mask=%02x", mask);
- if (mask<0x08) k--; // discard the last uneven octet;
- // 0x08 is an assumption - but works quite often
- }
- // uint8_t str1 [8];
- // itoa(k,str1);
- // strncat(str1," octets read",8);
-
- // DbpString( str1); // DbpString("%d octets", k);
-
- // for(i = 0; i < k; i+=3) {
- // //DbpString("# %2d: %02x ", i, outBuf[i]);
- // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
- // }
-
- for(i = 0; i < k; i++) {
- receivedResponse[i] = outBuf[i];
- }
- } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
- return k; // return the number of bytes demodulated
+ if (DecodeReader->posCount == 8) {
+ DecodeReader->posCount = 0;
+ int corr10 = DecodeReader->sum1 - DecodeReader->sum2;
+ int corr01 = DecodeReader->sum2 - DecodeReader->sum1;
+ int corr11 = (DecodeReader->sum1 + DecodeReader->sum2) / 2;
+ if (corr01 > corr11 && corr01 > corr10) { // EOF
+ LED_B_OFF(); // Finished receiving
+ DecodeReader->state = STATE_READER_UNSYNCD;
+ if (DecodeReader->byteCount != 0) {
+ return true;
+ }
+ }
+ if (corr10 > corr11) { // detected a 2bit position
+ DecodeReader->shiftReg >>= 2;
+ DecodeReader->shiftReg |= (DecodeReader->bitCount << 6);
+ }
+ if (DecodeReader->bitCount == 15) { // we have a full byte
+ DecodeReader->output[DecodeReader->byteCount++] = DecodeReader->shiftReg;
+ if (DecodeReader->byteCount > DecodeReader->byteCountMax) {
+ // buffer overflow, give up
+ LED_B_OFF();
+ DecodeReader->state = STATE_READER_UNSYNCD;
+ }
+ DecodeReader->bitCount = 0;
+ } else {
+ DecodeReader->bitCount++;
+ }
+ }
+ break;
+
+ case STATE_READER_RECEIVE_DATA_1_OUT_OF_256:
+ DecodeReader->posCount++;
+ if (DecodeReader->posCount == 1) {
+ DecodeReader->sum1 = bit;
+ } else if (DecodeReader->posCount <= 4) {
+ DecodeReader->sum1 += bit;
+ } else if (DecodeReader->posCount == 5) {
+ DecodeReader->sum2 = bit;
+ } else {
+ DecodeReader->sum2 += bit;
+ }
+ if (DecodeReader->posCount == 8) {
+ DecodeReader->posCount = 0;
+ int corr10 = DecodeReader->sum1 - DecodeReader->sum2;
+ int corr01 = DecodeReader->sum2 - DecodeReader->sum1;
+ int corr11 = (DecodeReader->sum1 + DecodeReader->sum2) / 2;
+ if (corr01 > corr11 && corr01 > corr10) { // EOF
+ LED_B_OFF(); // Finished receiving
+ DecodeReader->state = STATE_READER_UNSYNCD;
+ if (DecodeReader->byteCount != 0) {
+ return true;
+ }
+ }
+ if (corr10 > corr11) { // detected the bit position
+ DecodeReader->shiftReg = DecodeReader->bitCount;
+ }
+ if (DecodeReader->bitCount == 255) { // we have a full byte
+ DecodeReader->output[DecodeReader->byteCount++] = DecodeReader->shiftReg;
+ if (DecodeReader->byteCount > DecodeReader->byteCountMax) {
+ // buffer overflow, give up
+ LED_B_OFF();
+ DecodeReader->state = STATE_READER_UNSYNCD;
+ }
+ }
+ DecodeReader->bitCount++;
+ }
+ break;