- if(Demod.posCount==0) {
- Demod.posCount = 1;
- if(modulation) {
- Demod.sub = SUB_FIRST_HALF;
- }
- else {
- Demod.sub = SUB_NONE;
- }
- }
- else {
- Demod.posCount = 0;
- /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
- if(Demod.state!=DEMOD_ERROR_WAIT) {
- Demod.state = DEMOD_ERROR_WAIT;
- Demod.output[Demod.len] = 0xaa;
- error = 0x01;
- }
- }*/
- //else if(modulation) {
- if(modulation) {
- if(Demod.sub == SUB_FIRST_HALF) {
- Demod.sub = SUB_BOTH;
- }
- else {
- Demod.sub = SUB_SECOND_HALF;
- }
- }
- else if(Demod.sub == SUB_NONE) {
- if(Demod.state == DEMOD_SOF_COMPLETE) {
- Demod.output[Demod.len] = 0x0f;
- Demod.len++;
- Demod.parityBits <<= 1;
- Demod.parityBits ^= OddByteParity[0x0f];
- Demod.state = DEMOD_UNSYNCD;
-// error = 0x0f;
- return TRUE;
- }
- else {
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0x33;
- }
- /*if(Demod.state!=DEMOD_ERROR_WAIT) {
- Demod.state = DEMOD_ERROR_WAIT;
- Demod.output[Demod.len] = 0xaa;
- error = 0x01;
- }*/
- }
-
- switch(Demod.state) {
- case DEMOD_START_OF_COMMUNICATION:
- if(Demod.sub == SUB_BOTH) {
- //Demod.state = DEMOD_MANCHESTER_D;
- Demod.state = DEMOD_START_OF_COMMUNICATION2;
- Demod.posCount = 1;
- Demod.sub = SUB_NONE;
- }
- else {
- Demod.output[Demod.len] = 0xab;
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0xd2;
- }
- break;
- case DEMOD_START_OF_COMMUNICATION2:
- if(Demod.sub == SUB_SECOND_HALF) {
- Demod.state = DEMOD_START_OF_COMMUNICATION3;
- }
- else {
- Demod.output[Demod.len] = 0xab;
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0xd3;
- }
- break;
- case DEMOD_START_OF_COMMUNICATION3:
- if(Demod.sub == SUB_SECOND_HALF) {
-// Demod.state = DEMOD_MANCHESTER_D;
- Demod.state = DEMOD_SOF_COMPLETE;
- //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
- //Demod.len++;
- }
- else {
- Demod.output[Demod.len] = 0xab;
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0xd4;
- }
- break;
- case DEMOD_SOF_COMPLETE:
- case DEMOD_MANCHESTER_D:
- case DEMOD_MANCHESTER_E:
- // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
- // 00001111 = 1 (0 in 14443)
- if(Demod.sub == SUB_SECOND_HALF) { // SUB_FIRST_HALF
- Demod.bitCount++;
- Demod.shiftReg = (Demod.shiftReg >> 1) ^ 0x100;
- Demod.state = DEMOD_MANCHESTER_D;
- }
- else if(Demod.sub == SUB_FIRST_HALF) { // SUB_SECOND_HALF
- Demod.bitCount++;
- Demod.shiftReg >>= 1;
- Demod.state = DEMOD_MANCHESTER_E;
- }
- else if(Demod.sub == SUB_BOTH) {
- Demod.state = DEMOD_MANCHESTER_F;
- }
- else {
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0x55;
- }
- break;
-
- case DEMOD_MANCHESTER_F:
- // Tag response does not need to be a complete byte!
- if(Demod.len > 0 || Demod.bitCount > 0) {
- if(Demod.bitCount > 1) { // was > 0, do not interpret last closing bit, is part of EOF
- Demod.shiftReg >>= (9 - Demod.bitCount);
- Demod.output[Demod.len] = Demod.shiftReg & 0xff;
- Demod.len++;
- // No parity bit, so just shift a 0
- Demod.parityBits <<= 1;
- }