+ if (DecodeTag->posCount <= 4) {
+ DecodeTag->sum1 += amplitude;
+ } else {
+ DecodeTag->sum2 += amplitude;
+ }
+ if (DecodeTag->posCount == 8) {
+ if (DecodeTag->sum1 > DecodeTag->threshold_half && DecodeTag->sum2 > DecodeTag->threshold_half) { // modulation in both halves
+ if (DecodeTag->lastBit == LOGIC0) { // this was already part of EOF
+ DecodeTag->state = STATE_TAG_EOF;
+ } else {
+ DecodeTag->posCount = 0;
+ DecodeTag->previous_amplitude = amplitude;
+ DecodeTag->state = STATE_TAG_SOF_LOW;
+ LED_C_OFF();
+ }
+ } else if (DecodeTag->sum1 < DecodeTag->threshold_half && DecodeTag->sum2 > DecodeTag->threshold_half) { // modulation in second half
+ // logic 1
+ if (DecodeTag->lastBit == SOF_PART1) { // still part of SOF
+ DecodeTag->lastBit = SOF_PART2; // SOF completed
+ } else {
+ DecodeTag->lastBit = LOGIC1;
+ DecodeTag->shiftReg >>= 1;
+ DecodeTag->shiftReg |= 0x80;
+ DecodeTag->bitCount++;
+ if (DecodeTag->bitCount == 8) {
+ DecodeTag->output[DecodeTag->len] = DecodeTag->shiftReg;
+ DecodeTag->len++;
+ // if (DecodeTag->shiftReg == 0x12 && DecodeTag->len == 1) FpgaDisableTracing(); // DEBUGGING
+ if (DecodeTag->len > DecodeTag->max_len) {
+ // buffer overflow, give up
+ LED_C_OFF();
+ return true;
+ }
+ DecodeTag->bitCount = 0;
+ DecodeTag->shiftReg = 0;
+ }
+ }
+ } else if (DecodeTag->sum1 > DecodeTag->threshold_half && DecodeTag->sum2 < DecodeTag->threshold_half) { // modulation in first half
+ // logic 0
+ if (DecodeTag->lastBit == SOF_PART1) { // incomplete SOF
+ DecodeTag->posCount = 0;
+ DecodeTag->previous_amplitude = amplitude;
+ DecodeTag->state = STATE_TAG_SOF_LOW;
+ LED_C_OFF();
+ } else {
+ DecodeTag->lastBit = LOGIC0;
+ DecodeTag->shiftReg >>= 1;
+ DecodeTag->bitCount++;
+ if (DecodeTag->bitCount == 8) {
+ DecodeTag->output[DecodeTag->len] = DecodeTag->shiftReg;
+ DecodeTag->len++;
+ // if (DecodeTag->shiftReg == 0x12 && DecodeTag->len == 1) FpgaDisableTracing(); // DEBUGGING
+ if (DecodeTag->len > DecodeTag->max_len) {
+ // buffer overflow, give up
+ DecodeTag->posCount = 0;
+ DecodeTag->previous_amplitude = amplitude;
+ DecodeTag->state = STATE_TAG_SOF_LOW;
+ LED_C_OFF();
+ }
+ DecodeTag->bitCount = 0;
+ DecodeTag->shiftReg = 0;
+ }
+ }
+ } else { // no modulation
+ if (DecodeTag->lastBit == SOF_PART2) { // only SOF (this is OK for iClass)
+ LED_C_OFF();
+ return true;
+ } else {
+ DecodeTag->posCount = 0;
+ DecodeTag->state = STATE_TAG_SOF_LOW;
+ LED_C_OFF();
+ }
+ }
+ DecodeTag->posCount = 0;
+ }
+ DecodeTag->posCount++;
+ break;
+
+ case STATE_TAG_EOF:
+ if (DecodeTag->posCount == 1) {
+ DecodeTag->sum1 = 0;
+ DecodeTag->sum2 = 0;
+ }
+ if (DecodeTag->posCount <= 4) {
+ DecodeTag->sum1 += amplitude;
+ } else {
+ DecodeTag->sum2 += amplitude;
+ }
+ if (DecodeTag->posCount == 8) {
+ if (DecodeTag->sum1 > DecodeTag->threshold_half && DecodeTag->sum2 < DecodeTag->threshold_half) { // modulation in first half
+ DecodeTag->posCount = 0;
+ DecodeTag->state = STATE_TAG_EOF_TAIL;
+ } else {
+ DecodeTag->posCount = 0;
+ DecodeTag->previous_amplitude = amplitude;
+ DecodeTag->state = STATE_TAG_SOF_LOW;
+ LED_C_OFF();
+ }
+ }
+ DecodeTag->posCount++;
+ break;
+
+ case STATE_TAG_EOF_TAIL:
+ if (DecodeTag->posCount == 1) {
+ DecodeTag->sum1 = 0;
+ DecodeTag->sum2 = 0;
+ }
+ if (DecodeTag->posCount <= 4) {
+ DecodeTag->sum1 += amplitude;