-//by marshmellow
-void askAmp(uint8_t *BitStream, size_t size)
-{
- int shift = 127;
- int shiftedVal=0;
- for(size_t i = 1; i<size; i++){
- if (BitStream[i]-BitStream[i-1]>=30) //large jump up
- shift=127;
- else if(BitStream[i]-BitStream[i-1]<=-20) //large jump down
- shift=-127;
-
- shiftedVal=BitStream[i]+shift;
-
- if (shiftedVal>255)
- shiftedVal=255;
- else if (shiftedVal<0)
- shiftedVal=0;
- BitStream[i-1] = shiftedVal;
- }
- return;
-}
-
-//by marshmellow
-//takes 3 arguments - clock, invert and maxErr as integers
-//attempts to demodulate ask only
-int askrawdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp)
-{
- if (*size==0) return -1;
- int start = DetectASKClock(BinStream, *size, clk, 20); //clock default
- if (*clk==0 || start < 0) return -1;
- if (*invert != 1) *invert = 0;
- if (amp==1) askAmp(BinStream, *size);
-
- uint8_t initLoopMax = 255;
- if (initLoopMax > *size) initLoopMax=*size;
- // Detect high and lows
- //25% clip in case highs and lows aren't clipped [marshmellow]
- int high, low;
- if (getHiLo(BinStream, initLoopMax, &high, &low, 75, 75) < 1)
- return -1; //just noise
-
- // if clean clipped waves detected run alternate demod
- if (DetectCleanAskWave(BinStream, *size, high, low))
- return cleanAskRawDemod(BinStream, size, *clk, *invert, high, low);
-
- int lastBit = 0; //set first clock check - can go negative
- size_t i, iii = 0;
- size_t errCnt = 0, bitnum = 0; //output counter
- uint8_t midBit = 0;
- size_t bestStart = start, bestErrCnt = 0; //(*size/1000);
- 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*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 - *clk; ++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 (i-lastBit > *clk && (BinStream[i] >= high || BinStream[i] <= low)){
- lastBit += *clk;
- midBit = 0;
- } else if (i-lastBit > (*clk/2) && midBit == 0) {
- midBit = 1;
- } else if ((i-lastBit) > *clk) {
- //should have hit a high or low based on clock!!
- //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;
- }
- if ((i-iii)>(MaxBits * *clk)) break; //got enough bits
- }
- //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;
- bestErrCnt=errCnt;
- break; //great read - finish
- }
- if (errCnt<bestErrCnt){ //set this as new best run
- bestErrCnt=errCnt;
- bestStart = iii;
- }
- }
- errCnt=0;
- }
- }
- }
- if (bestErrCnt > maxErr){
- *invert = bestStart;
- *clk = iii;
- return -1;
- }
- //best run is good enough - set to best run and overwrite BinStream
- lastBit = bestStart - *clk - 1;
- errCnt = 0;
-
- for (i = bestStart; i < *size; ++i) {
- if (i - lastBit > *clk){
- if (BinStream[i] >= high) {
- BinStream[bitnum++] = *invert;
- } else if (BinStream[i] <= low) {
- BinStream[bitnum++] = *invert ^ 1;
- } else {
- if (bitnum > 0) {
- BinStream[bitnum++]=77;
- errCnt++;
- }
- }
- midBit = 0;
- lastBit += *clk;
- } else if (i-lastBit > (*clk/2) && midBit == 0){
- if (BinStream[i] >= high) {
- BinStream[bitnum++] = *invert;
- } else if (BinStream[i] <= low) {
- BinStream[bitnum++] = *invert ^ 1;
- } else {
-
- BinStream[bitnum] = BinStream[bitnum-1];
- bitnum++;
- }
- midBit = 1;
- }
- if (bitnum >= MaxBits) break;
- }
- *size = bitnum;
- return errCnt;
-}
-