- if (g_debugMode==2) prnt("DEBUG FSK: RF %d, cnts %d",rfLens[i], rfCnts[i]);
- }
- // set allowed clock remainder tolerance to be 1 large field clock length+1
- // we could have mistakenly made a 9 a 10 instead of an 8 or visa versa so rfLens could be 1 FC off
- uint8_t tol1 = fcHigh+1;
-
- if (g_debugMode==2) prnt("DEBUG FSK: most counted rf values: 1 %d, 2 %d, 3 %d",rfLens[rfHighest],rfLens[rfHighest2],rfLens[rfHighest3]);
-
- // loop to find the highest clock that has a remainder less than the tolerance
- // compare samples counted divided by
- // test 128 down to 32 (shouldn't be possible to have fc/10 & fc/8 and rf/16 or less)
- int ii=7;
- for (; ii>=2; ii--){
- if (rfLens[rfHighest] % clk[ii] < tol1 || rfLens[rfHighest] % clk[ii] > clk[ii]-tol1){
- if (rfLens[rfHighest2] % clk[ii] < tol1 || rfLens[rfHighest2] % clk[ii] > clk[ii]-tol1){
- if (rfLens[rfHighest3] % clk[ii] < tol1 || rfLens[rfHighest3] % clk[ii] > clk[ii]-tol1){
- if (g_debugMode==2) prnt("DEBUG FSK: clk %d divides into the 3 most rf values within tolerance",clk[ii]);
- break;
+ avgWaveVal += dest[i+2];
+ }
+ if (firstFullWave == 0) {
+ // no phase shift detected - could be all 1's or 0's - doesn't matter where we start
+ // so skip a little to ensure we are past any Start Signal
+ firstFullWave = 160;
+ memset(dest, curPhase, firstFullWave / *clock);
+ } else {
+ memset(dest, curPhase^1, firstFullWave / *clock);
+ }
+ //advance bits
+ numBits += (firstFullWave / *clock);
+ *startIdx = firstFullWave - (*clock * numBits)+2;
+ //set start of wave as clock align
+ lastClkBit = firstFullWave;
+ if (g_debugMode==2) prnt("DEBUG PSK: firstFullWave: %u, waveLen: %u, startIdx %i",firstFullWave,fullWaveLen, *startIdx);
+ if (g_debugMode==2) prnt("DEBUG PSK: clk: %d, lastClkBit: %u, fc: %u", *clock, lastClkBit,(unsigned int) fc);
+ waveStart = 0;
+ dest[numBits++] = curPhase; //set first read bit
+ for (i = firstFullWave + fullWaveLen - 1; i < *size-3; i++){
+ //top edge of wave = start of new wave
+ if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
+ if (waveStart == 0) {
+ waveStart = i+1;
+ waveLenCnt = 0;
+ avgWaveVal = dest[i+1];
+ } else { //waveEnd
+ waveEnd = i+1;
+ waveLenCnt = waveEnd-waveStart;
+ lastAvgWaveVal = avgWaveVal/waveLenCnt;
+ if (waveLenCnt > fc){
+ //PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
+ //this wave is a phase shift
+ //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
+ if (i+1 >= lastClkBit + *clock - tol){ //should be a clock bit
+ curPhase ^= 1;
+ dest[numBits++] = curPhase;
+ lastClkBit += *clock;
+ } else if (i < lastClkBit+10+fc){
+ //noise after a phase shift - ignore
+ } else { //phase shift before supposed to based on clock
+ errCnt++;
+ dest[numBits++] = 7;
+ }
+ } else if (i+1 > lastClkBit + *clock + tol + fc){
+ lastClkBit += *clock; //no phase shift but clock bit
+ dest[numBits++] = curPhase;
+ } else if (waveLenCnt < fc - 1) { //wave is smaller than field clock (shouldn't happen often)
+ errCnt2++;
+ if(errCnt2 > 101) return errCnt2;