if (BitStream[i] < *low) *low = BitStream[i];
}
if (*high < 123) return -1; // just noise
- *high = (int)(((*high-128)*(((float)fuzzHi)/100))+128);
- *low = (int)(((*low-128)*(((float)fuzzLo)/100))+128);
+ *high = ((*high-128)*fuzzHi + 12800)/100;
+ *low = ((*low-128)*fuzzLo + 12800)/100;
return 1;
}
return 0;
}
+// demodulates strong heavily clipped samples
+int cleanAskRawDemod(uint8_t *BinStream, size_t *size, int clk, int invert, int high, int low)
+{
+ size_t bitCnt=0, smplCnt=0, errCnt=0;
+ uint8_t waveHigh = 0;
+ //PrintAndLog("clk: %d", clk);
+ for (size_t i=0; i < *size; i++){
+ if (BinStream[i] >= high && waveHigh){
+ smplCnt++;
+ } else if (BinStream[i] <= low && !waveHigh){
+ smplCnt++;
+ } else { //transition
+ if ((BinStream[i] >= high && !waveHigh) || (BinStream[i] <= low && waveHigh)){
+ if (smplCnt > clk-(clk/4)-1) { //full clock
+ if (smplCnt > clk + (clk/4)+1) { //too many samples
+ errCnt++;
+ BinStream[bitCnt++]=77;
+ } else if (waveHigh) {
+ BinStream[bitCnt++] = invert;
+ BinStream[bitCnt++] = invert;
+ } else if (!waveHigh) {
+ BinStream[bitCnt++] = invert ^ 1;
+ BinStream[bitCnt++] = invert ^ 1;
+ }
+ waveHigh ^= 1;
+ smplCnt = 0;
+ } else if (smplCnt > (clk/2) - (clk/4)-1) {
+ if (waveHigh) {
+ BinStream[bitCnt++] = invert;
+ } else if (!waveHigh) {
+ BinStream[bitCnt++] = invert ^ 1;
+ }
+ waveHigh ^= 1;
+ smplCnt = 0;
+ } else if (!bitCnt) {
+ //first bit
+ waveHigh = (BinStream[i] >= high);
+ smplCnt = 1;
+ } else {
+ smplCnt++;
+ //transition bit oops
+ }
+ } else { //haven't hit new high or new low yet
+ smplCnt++;
+ }
+ }
+ }
+ *size = bitCnt;
+ return errCnt;
+}
+
//by marshmellow
//takes 3 arguments - clock, invert, maxErr as integers
//attempts to demodulate ask while decoding manchester
int high, low;
if (getHiLo(BinStream, initLoopMax, &high, &low, 75, 75) < 1) return -2; //just noise
+ // if clean clipped waves detected run alternate demod
+ if (DetectCleanAskWave(BinStream, *size, high, low)) {
+ cleanAskRawDemod(BinStream, size, *clk, *invert, high, low);
+ return manrawdecode(BinStream, size);
+ }
+
+
// PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
int lastBit = 0; //set first clock check
uint16_t bitnum = 0; //output counter
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
size_t iii = 0;
//if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
- if (!maxErr) initLoopMax = *clk * 2;
+ if (!maxErr && initLoopMax > *clk*3) initLoopMax = *clk * 3;
uint16_t errCnt = 0, MaxBits = 512;
uint16_t bestStart = start;
uint16_t bestErrCnt = 0;
if (start <= 0 || start > initLoopMax){
bestErrCnt = maxErr+1;
// loop to find first wave that works
- for (iii=0; iii < initLoopMax; ++iii){
+ for (iii=0; iii < initLoopMax-tol-*clk; ++iii){
// if no peak skip
if (BinStream[iii] < high && BinStream[iii] > low) continue;
if ((i-iii) > (MaxBits * *clk) || errCnt > maxErr) break; //got plenty of bits or too many errors
}
//we got more than 64 good bits and not all errors
- if ((((i-iii)/ *clk) > (64)) && (errCnt<=maxErr)) {
+ if ((((i-iii)/ *clk) > (32)) && (errCnt<=maxErr)) {
//possible good read
if (!errCnt || errCnt < bestErrCnt){
bestStart = iii; //set this as new best run
}
errCnt=0;
}
- if (bestErr<20){
- for (i=bestRun; i < *size-2; i+=2){
- if(BitStream[i] == 1 && (BitStream[i+1] == 0)){
- BitStream[bitnum++]=0;
- } else if((BitStream[i] == 0) && BitStream[i+1] == 1){
- BitStream[bitnum++]=1;
- } else {
- BitStream[bitnum++]=77;
- }
- if(bitnum>MaxBits) break;
+ for (i=bestRun; i < *size-2; i+=2){
+ if(BitStream[i] == 1 && (BitStream[i+1] == 0)){
+ BitStream[bitnum++]=0;
+ } else if((BitStream[i] == 0) && BitStream[i+1] == 1){
+ BitStream[bitnum++]=1;
+ } else {
+ BitStream[bitnum++]=77;
}
- *size=bitnum;
+ if(bitnum>MaxBits) break;
}
+ *size=bitnum;
return bestErr;
}
return;
}
-// demodulates strong heavily clipped samples
-int cleanAskRawDemod(uint8_t *BinStream, size_t *size, int clk, int invert, int high, int low)
-{
- size_t bitCnt=0, smplCnt=0, errCnt=0;
- uint8_t waveHigh = 0;
- //PrintAndLog("clk: %d", clk);
- for (size_t i=0; i < *size; i++){
- if (BinStream[i] >= high && waveHigh){
- smplCnt++;
- } else if (BinStream[i] <= low && !waveHigh){
- smplCnt++;
- } else { //transition
- if ((BinStream[i] >= high && !waveHigh) || (BinStream[i] <= low && waveHigh)){
- if (smplCnt > clk-(clk/4)-1) { //full clock
- if (smplCnt > clk + (clk/4)+1) { //too many samples
- errCnt++;
- BinStream[bitCnt++]=77;
- } else if (waveHigh) {
- BinStream[bitCnt++] = invert;
- BinStream[bitCnt++] = invert;
- } else if (!waveHigh) {
- BinStream[bitCnt++] = invert ^ 1;
- BinStream[bitCnt++] = invert ^ 1;
- }
- waveHigh ^= 1;
- smplCnt = 0;
- } else if (smplCnt > (clk/2) - (clk/4)-1) {
- if (waveHigh) {
- BinStream[bitCnt++] = invert;
- } else if (!waveHigh) {
- BinStream[bitCnt++] = invert ^ 1;
- }
- waveHigh ^= 1;
- smplCnt = 0;
- } else if (!bitCnt) {
- //first bit
- waveHigh = (BinStream[i] >= high);
- smplCnt = 1;
- } else {
- smplCnt++;
- //transition bit oops
- }
- } else { //haven't hit new high or new low yet
- smplCnt++;
- }
- }
- }
- *size = bitCnt;
- return errCnt;
-}
-
//by marshmellow
//takes 3 arguments - clock, invert and maxErr as integers
//attempts to demodulate ask only
size_t MaxBits = 1024;
//if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
- if (!maxErr) initLoopMax = *clk * 2;
+ if (!maxErr && initLoopMax > *clk*3) initLoopMax = *clk * 3;
//if best start not already found by detectclock
if (start <= 0 || start > initLoopMax){
bestErrCnt = maxErr+1;
//PrintAndLog("DEBUG - lastbit - %d",lastBit);
//loop to find first wave that works
- for (iii=0; iii < initLoopMax; ++iii){
+ for (iii=0; iii < initLoopMax - *clk; ++iii){
if ((BinStream[iii] >= high) || (BinStream[iii] <= low)){
lastBit = iii - *clk;
//loop through to see if this start location works
}
if ((i-iii)>(MaxBits * *clk)) break; //got enough bits
}
- //we got more than 64 good bits and not all errors
- if ((((i-iii)/ *clk) > 64) && (errCnt<=maxErr)) {
+ //we got more than 32 good bits and not all errors
+ if ((((i-iii)/ *clk) > 32) && (errCnt<=maxErr)) {
//possible good read
if (errCnt==0){
bestStart=iii;
size_t idx=0;
size_t numBits=0;
uint32_t n=1;
- uint16_t lowWaves = ((rfLen*100/fclow)); // (((float)(rfLen))/((float)fclow));
- uint16_t highWaves = ((rfLen*100/fchigh)); // (((float)(rfLen))/((float)fchigh));
for( idx=1; idx < size; idx++) {
n++;
if (dest[idx]==lastval) continue;
//if lastval was 1, we have a 1->0 crossing
if (dest[idx-1]==1) {
- if (!numBits && n < lowWaves/100) {
+ if (!numBits && n < rfLen/fclow) {
n=0;
lastval = dest[idx];
continue;
}
- n = (size_t)((((n*1000)/lowWaves)+5)/10);
+ n = (n * fclow + rfLen/2) / rfLen;
} else {// 0->1 crossing
//test first bitsample too small
- if (!numBits && n < highWaves/100) {
+ if (!numBits && n < rfLen/fchigh) {
n=0;
lastval = dest[idx];
continue;
}
- n = (((n*1000)/highWaves)+5)/10;
+ n = (n * fchigh + rfLen/2) / rfLen;
}
if (n == 0) n = 1;
lastval=dest[idx];
}//end for
// if valid extra bits at the end were all the same frequency - add them in
- if (n > highWaves/100) {
+ if (n > rfLen/fchigh) {
if (dest[idx-2]==1) {
- n=(((n*1000)/lowWaves)+5)/10;
+ n = (n * fclow + rfLen/2) / rfLen;
} else {
- n=(((n*1000)/highWaves)+5)/10;
+ n = (n * fchigh + rfLen/2) / rfLen;
}
memset(dest+numBits, dest[idx-1]^invert , n);
numBits += n;
size_t i=0;
uint8_t clk[]={8,16,32,40,50,64,100,128,255};
uint8_t loopCnt = 255; //don't need to loop through entire array...
- if (size <= loopCnt) return -1; //not enough samples
+ if (size==0) return -1;
+ if (size <= loopCnt) loopCnt = size-1; //not enough samples
//if we already have a valid clock quit
for (;i<8;++i)
}else{
tol=0;
}
- if (!maxErr) loopCnt=clk[clkCnt]*2;
+ if (!maxErr && loopCnt>clk[clkCnt]*2) loopCnt=clk[clkCnt]*2;
bestErr[clkCnt]=1000;
//try lining up the peaks by moving starting point (try first 256)
- for (ii=0; ii < loopCnt; ii++){
+ for (ii=0; ii < loopCnt-tol-clk[clkCnt]; ii++){
if (dest[ii] < peak && dest[ii] > low) continue;
errCnt=0;