int16_t CTRLState1; int16_t CTRLState2; int16_t CTRLBitBuf_Pos; int16_t CTRL_H, CTRL_T, CTRL_MAX; int8_t CTRLBitBuffer[4000]; // not sure how long this buffer really is int8_t CTRL_BUFFER[0x1F4]; // 500-byte ring buffer uint8_t getCTS(); void readCTRL() { int i; if (CTRLState1 == 0) { // look for a high bit (start bit?) if (getCTS() == 0) return; // return if there is a low bit CTRLState1 += 1; CTRLState2 = 4; CTRLBitBuf_Pos = 0; } else { CTRLState1 += 1; if (CTRLState2 > CTRLState1) return; if (CTRLState1 > 4) { if (CTRLState1 >= 0x5E) { if (getCTS() == 0xFF) { // look for 00 (stop bit?) CTRLState1 = CTRLState2 = CTRLBitBuf_Pos = 0; } else { int32_t temp; i = CTRLBitBuf_Pos-1; for (i=CTRLBitBuf_Pos-1; i>0; i--) { if (CTRLBitBuffer[i] < 0) { // CTS was high/1 temp &= ~(1 << i); // clear bit i of temp } else { temp |= 1 << i; // set bit i of temp } } CTRL_BUFFER[CTRL_H] = temp; CTRL_H++; if (CTRL_H == 0x1F4) CTRL_H = 0; int c = CTRL_H-CTRL_T; if (CTRL_H < CTRL_T) c += 0x1F4; (HEAP_3B897+$25CB) = c; // not sure what this variable is used for if (c >= CTRL_MAX) CTRL_MAX = c; CTRLState1 = CTRLState2 = CTRLBitBuf_Pos = 0; return; } } else { CTRLBitBuffer[CTRLBitBuf_Pos] = getCTS(); CTRLBitBuf_Pos += 1; CTRLState2 += 0xA; } } else { if (getCTS() == 0) { CTRLState1 = CTRLState2 = CTRLBitBuf_Pos = 0; } else { CTRLState2 = 0xE; for (i=0;i<8;i++) { // set the first 8 bits of the bit buffer to 0s? CTRLBitBuffer[i] = 0; } return; } } } }