- if (*clk<8) *clk =64;
- if (*clk<32) *clk=32;
- if (*invert != 0 && *invert != 1) *invert=0;
- uint32_t initLoopMax = 200;
- if (initLoopMax>*BitLen) initLoopMax=*BitLen;
- // Detect high and lows
- for (i = 0; i < initLoopMax; ++i) //200 samples should be enough to find high and low values
- {
- if (BinStream[i] > high)
- high = BinStream[i];
- else if (BinStream[i] < low)
- low = BinStream[i];
- }
- if ((high < 158) ){ //throw away static
- //PrintAndLog("no data found");
- return -2;
- }
- //25% fuzz in case highs and lows aren't clipped [marshmellow]
- high=(int)((high-128)*.75)+128;
- low= (int)((low-128)*.75)+128;
-
- //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
- int lastBit = 0; //set first clock check
- uint32_t bitnum = 0; //output counter
- int 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
- int iii = 0;
- uint32_t gLen = *BitLen;
- if (gLen > 3000) gLen=3000;
- uint8_t errCnt =0;
- uint32_t bestStart = *BitLen;
- uint32_t bestErrCnt = (*BitLen/1000);
- uint32_t maxErr = (*BitLen/1000);
- //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;
- errCnt=0;
- //loop through to see if this start location works
- for (i = iii; i < *BitLen; ++i) {
- if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
- lastBit+=*clk;
- } else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
- //low found and we are expecting a bar
- lastBit+=*clk;
- } 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>(maxErr)) break; //allow 1 error for every 1000 samples else start over
- }
- }
- if ((i-iii) >(400 * *clk)) break; //got plenty of bits
- }
- //we got more than 64 good bits and not all errors
- if ((((i-iii)/ *clk) > (64+errCnt)) && (errCnt<maxErr)) {
- //possible good read
- if (errCnt==0){
+ //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
+ int lastBit = 0; //set first clock check
+ uint32_t bitnum = 0; //output counter
+ int 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
+ int iii = 0;
+ uint32_t gLen = *size;
+ if (gLen > 3000) gLen=3000;
+ uint8_t errCnt =0;
+ uint32_t bestStart = *size;
+ uint32_t bestErrCnt = (*size/1000);
+ uint32_t maxErr = (*size/1000);
+ //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;
+ errCnt=0;
+ //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;
+ } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
+ //low found and we are expecting a bar
+ lastBit+=*clk;
+ } 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>(maxErr)) break; //allow 1 error for every 1000 samples else start over
+ }
+ }
+ if ((i-iii) >(400 * *clk)) break; //got plenty of bits
+ }
+ //we got more than 64 good bits and not all errors
+ if ((((i-iii)/ *clk) > (64+errCnt)) && (errCnt<maxErr)) {
+ //possible good read
+ if (errCnt==0){