- 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;
- }
- //we got more than 64 good bits and not all errors
- if ((bitnum > (64+errCnt)) && (errCnt<(*bitLen/1000))) {
- //possible good read
- if (errCnt==0) break; //great read - finish
- if (bestStart == iii) break; //if current run == bestErrCnt run (after exhausted testing) then finish
- if (errCnt<bestErrCnt){ //set this as new best run
- bestErrCnt=errCnt;
- bestStart = iii;
- }
- }
- }
- if (iii>=gLen){ //exhausted test
- //if there was a ok test go back to that one and re-run the best run (then dump after that run)
- if (bestErrCnt < (*bitLen/1000)) iii=bestStart;
- }
- }
- if (bitnum>16){
-
- // PrintAndLog("Data start pos:%d, lastBit:%d, stop pos:%d, numBits:%d",iii,lastBit,i,bitnum);
- //move BitStream back to BinStream
- // ClearGraph(0);
- for (i=0; i < bitnum; ++i){
- BinStream[i]=BitStream[i];
- }
- *bitLen=bitnum;
- // RepaintGraphWindow();
- //output
- // if (errCnt>0){
- // PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
- // }
- // PrintAndLog("ASK decoded bitstream:");
- // Now output the bitstream to the scrollback by line of 16 bits
- // printBitStream2(BitStream,bitnum);
- //int errCnt=0;
- //errCnt=manrawdemod(BitStream,bitnum);
-
- // Em410xDecode(Cmd);
- } else return -1;
- return errCnt;
+ //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;
+ }
+ return bestErrCnt;