- uint32_t i;
- // int invert=0; //invert default
- int clk2 = *clk;
- *clk=DetectASKClock(BinStream, *size, *clk); //clock default
- //uint8_t BitStream[502] = {0};
-
- //HACK: if clock not detected correctly - default
- if (clk2==0 && *clk<8) *clk =64;
- if (clk2==0 && *clk<32 && clk2==0) *clk=32;
- if (*invert != 0 && *invert != 1) *invert =0;
- uint32_t initLoopMax = 200;
- if (initLoopMax > *size) initLoopMax=*size;
- // Detect high and lows
- //25% fuzz in case highs and lows aren't clipped [marshmellow]
- int high, low, ans;
- ans = getHiLo(BinStream, initLoopMax, &high, &low, 75, 75);
- if (ans<1) return -2; //just noise
-
- //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
- int lastBit = 0; //set first clock check
- uint32_t bitnum = 0; //output counter
- uint8_t tol = 0; //clock tolerance adjust - waves will be accepted as within the clock
- // if they fall + or - this value + clock from last valid wave
- if (*clk == 32) tol=1; //clock tolerance may not be needed anymore currently set to
- // + or - 1 but could be increased for poor waves or removed entirely
- uint32_t iii = 0;
- uint32_t gLen = *size;
- if (gLen > 500) gLen=500;
- uint8_t errCnt =0;
- uint32_t bestStart = *size;
- uint32_t bestErrCnt = (*size/1000);
- uint32_t maxErr = bestErrCnt;
- uint8_t midBit=0;
- //PrintAndLog("DEBUG - lastbit - %d",lastBit);
- //loop to find first wave that works
- for (iii=0; iii < gLen; ++iii){
- if ((BinStream[iii]>=high) || (BinStream[iii]<=low)){
- lastBit=iii-*clk;
- //loop through to see if this start location works
- for (i = iii; i < *size; ++i) {
- if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
- lastBit+=*clk;
- midBit=0;
- } else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
- //low found and we are expecting a bar
- lastBit+=*clk;
- midBit=0;
- } else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
- //mid bar?
- midBit=1;
- } else if ((BinStream[i]>=high) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
- //mid bar?
- midBit=1;
- } else if ((i-lastBit)>((*clk/2)+tol) && (midBit==0)){
- //no mid bar found
- midBit=1;
- } else {
- //mid value found or no bar supposed to be here
-
- if ((i-lastBit)>(*clk+tol)){
- //should have hit a high or low based on clock!!
- //debug
- //PrintAndLog("DEBUG - no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(tol)))),(lastBit+(clk+((int)(tol)))),lastBit);
-
- errCnt++;
- lastBit+=*clk;//skip over until hit too many errors
- if (errCnt > ((*size/1000))){ //allow 1 error for every 1000 samples else start over
- errCnt=0;
- break;
- }
- }
- }
- if ((i-iii)>(500 * *clk)) break; //got enough bits
- }
- //we got more than 64 good bits and not all errors
- if ((((i-iii)/ *clk) > (64+errCnt)) && (errCnt<(*size/1000))) {
- //possible good read
- if (errCnt==0){
- bestStart=iii;
- bestErrCnt=errCnt;
- break; //great read - finish
- }
- if (errCnt<bestErrCnt){ //set this as new best run
- bestErrCnt=errCnt;
- bestStart = iii;
- }
- }
- }
- }
- if (bestErrCnt<maxErr){
- //best run is good enough - set to best run and overwrite BinStream
- iii=bestStart;
- lastBit = bestStart - *clk;
- bitnum=0;
- for (i = iii; i < *size; ++i) {
- if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){
- lastBit += *clk;
- BinStream[bitnum] = *invert;
- bitnum++;
- midBit=0;
- } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
- //low found and we are expecting a bar
- lastBit+=*clk;
- BinStream[bitnum] = 1-*invert;
- bitnum++;
- midBit=0;
- } else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
- //mid bar?
- midBit=1;
- BinStream[bitnum] = 1 - *invert;
- bitnum++;
- } else if ((BinStream[i]>=high) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
- //mid bar?
- midBit=1;
- BinStream[bitnum] = *invert;
- bitnum++;
- } else if ((i-lastBit)>((*clk/2)+tol) && (midBit==0)){
- //no mid bar found
- midBit=1;
- if (bitnum!=0) BinStream[bitnum] = BinStream[bitnum-1];
- bitnum++;
-
- } else {
- //mid value found or no bar supposed to be here
- if ((i-lastBit)>(*clk+tol)){
- //should have hit a high or low based on clock!!
-
- //debug
- //PrintAndLog("DEBUG - no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(tol)))),(lastBit+(clk+((int)(tol)))),lastBit);
- if (bitnum > 0){
- BinStream[bitnum]=77;
- bitnum++;
- }
-
- lastBit+=*clk;//skip over error
- }
- }
- if (bitnum >=400) break;
- }
- *size=bitnum;
- } else{
- *invert=bestStart;
- *clk=iii;
- return -1;