- uint32_t i;
- // int invert=0; //invert default
- int high = 0, low = 128;
- *clk=DetectASKClock(BinStream,*bitLen,*clk); //clock default
- uint8_t BitStream[502] = {0};
-
- 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 plenty 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
- 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 = *bitLen;
- if (gLen > 500) gLen=500;
- uint8_t errCnt =0;
- uint32_t bestStart = *bitLen;
- uint32_t bestErrCnt = (*bitLen/1000);
- 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 < *bitLen; ++i) {
- if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
- lastBit+=*clk;
- BitStream[bitnum] = *invert;
- bitnum++;
- midBit=0;
- } else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
- //low found and we are expecting a bar
- lastBit+=*clk;
- BitStream[bitnum] = 1-*invert;
- bitnum++;
- midBit=0;
- } else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
- //mid bar?
- midBit=1;
- BitStream[bitnum]= 1-*invert;
- bitnum++;
- } else if ((BinStream[i]>=high)&&(midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
- //mid bar?
- midBit=1;
- BitStream[bitnum]= *invert;
- bitnum++;
- } else if ((i-lastBit)>((*clk/2)+tol)&&(midBit==0)){
- //no mid bar found
- midBit=1;
- BitStream[bitnum]= BitStream[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){
- BitStream[bitnum]=77;
- bitnum++;
+ uint32_t i;
+ // int invert=0; //invert default
+ int high = 0, low = 128;
+ *clk=DetectASKClock(BinStream,*bitLen,*clk); //clock default
+ uint8_t BitStream[502] = {0};
+
+ 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 plenty 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
+ 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 = *bitLen;
+ if (gLen > 500) gLen=500;
+ uint8_t errCnt =0;
+ uint32_t bestStart = *bitLen;
+ uint32_t bestErrCnt = (*bitLen/1000);
+ 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 < *bitLen; ++i) {
+ if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
+ lastBit+=*clk;
+ BitStream[bitnum] = *invert;
+ bitnum++;
+ midBit=0;
+ } else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
+ //low found and we are expecting a bar
+ lastBit+=*clk;
+ BitStream[bitnum] = 1-*invert;
+ bitnum++;
+ midBit=0;
+ } else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
+ //mid bar?
+ midBit=1;
+ BitStream[bitnum]= 1-*invert;
+ bitnum++;
+ } else if ((BinStream[i]>=high)&&(midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
+ //mid bar?
+ midBit=1;
+ BitStream[bitnum]= *invert;
+ bitnum++;
+ } else if ((i-lastBit)>((*clk/2)+tol)&&(midBit==0)){
+ //no mid bar found
+ midBit=1;
+ BitStream[bitnum]= BitStream[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){
+ BitStream[bitnum]=77;
+ bitnum++;
+ }
+
+
+ errCnt++;
+ lastBit+=*clk;//skip over until hit too many errors
+ if (errCnt>((*bitLen/1000))){ //allow 1 error for every 1000 samples else start over
+ errCnt=0;
+ bitnum=0;//start over
+ break;
+ }
+ }
+ }
+ if (bitnum>500) break;