-//takes 3 arguments - clock, invert, maxErr as integers
-//attempts to demodulate ask while decoding manchester
-//prints binary found and saves in graphbuffer for further commands
-int askmandemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr)
-{
- size_t i;
- int start = DetectASKClock(BinStream, *size, clk, 20); //clock default
- if (*clk==0 || start < 0) return -3;
- if (*invert != 1) *invert=0;
- uint8_t initLoopMax = 255;
- if (initLoopMax > *size) initLoopMax = *size;
- // Detect high and lows
- // 25% fuzz in case highs and lows aren't clipped [marshmellow]
- int high, low;
- if (getHiLo(BinStream, initLoopMax, &high, &low, 75, 75) < 1) return -2; //just noise
-
- // PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
- int lastBit = 0; //set first clock check
- uint16_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
- 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;
- uint16_t errCnt = 0, MaxBits = 512;
- uint16_t bestStart = start;
- uint16_t bestErrCnt = 0;
- // PrintAndLog("DEBUG - lastbit - %d",lastBit);
- // if best start position not already found by detect clock then
- if (start <= 0 || start > initLoopMax){
- bestErrCnt = maxErr+1;
- // loop to find first wave that works
- for (iii=0; iii < initLoopMax; ++iii){
- // if no peak skip
- if (BinStream[iii] < high && BinStream[iii] > low) continue;
-
- lastBit = iii - *clk;
- // loop through to see if this start location works
- for (i = iii; i < *size; ++i) {
- if ((i-lastBit) > (*clk-tol) && (BinStream[i] >= high || BinStream[i] <= low)) {
- lastBit += *clk;
- } else if ((i-lastBit) > (*clk+tol)) {
- errCnt++;
- lastBit += *clk;
- }
- 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)) {
- //possible good read
- if (!errCnt || errCnt < bestErrCnt){
- bestStart = iii; //set this as new best run
- bestErrCnt = errCnt;
- if (!errCnt) break; //great read - finish
- }
- }
- errCnt = 0;
- }
- }
- if (bestErrCnt > maxErr){
- *invert = bestStart;
- *clk = iii;
- return -1;
- }
- //best run is good enough set to best run and set overwrite BinStream
- lastBit = bestStart - *clk;
- errCnt = 0;
- for (i = bestStart; i < *size; ++i) {
- if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){
- //high found and we are expecting a bar
- lastBit += *clk;
- BinStream[bitnum++] = *invert;
- } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
- //low found and we are expecting a bar
- lastBit += *clk;
- BinStream[bitnum++] = *invert ^ 1;
- } else if ((i-lastBit)>(*clk+tol)){
- //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);
- if (bitnum > 0) {
- BinStream[bitnum++] = 77;
- errCnt++;
- }
- lastBit += *clk;//skip over error
- }
- if (bitnum >= MaxBits) break;
- }
- *size = bitnum;
- return bestErrCnt;
-}
-
-//by marshmellow
-//encode binary data into binary manchester
-int ManchesterEncode(uint8_t *BitStream, size_t size)
-{
- size_t modIdx=20000, i=0;
- if (size>modIdx) return -1;
- for (size_t idx=0; idx < size; idx++){
- BitStream[idx+modIdx++] = BitStream[idx];
- BitStream[idx+modIdx++] = BitStream[idx]^1;
- }
- for (; i<(size*2); i++){
- BitStream[i] = BitStream[i+20000];
- }
- return i;
-}
-
-//by marshmellow
-//take 10 and 01 and manchester decode
-//run through 2 times and take least errCnt
-int manrawdecode(uint8_t * BitStream, size_t *size)
-{
- uint16_t bitnum=0, MaxBits = 512, errCnt = 0;
- size_t i, ii;
- uint16_t bestErr = 1000, bestRun = 0;
- if (size == 0) return -1;
- for (ii=0;ii<2;++ii){
- for (i=ii; i<*size-2; i+=2)
- if (BitStream[i]==BitStream[i+1])
- errCnt++;
-
- if (bestErr>errCnt){
- bestErr=errCnt;
- bestRun=ii;
- }
- 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;
- }
- *size=bitnum;
- }
- return bestErr;
-}
-
-//by marshmellow
-//take 01 or 10 = 1 and 11 or 00 = 0
-//check for phase errors - should never have 111 or 000 should be 01001011 or 10110100 for 1010
-//decodes biphase or if inverted it is AKA conditional dephase encoding AKA differential manchester encoding
-int BiphaseRawDecode(uint8_t *BitStream, size_t *size, int offset, int invert)
-{
- uint16_t bitnum = 0;
- uint16_t errCnt = 0;
- size_t i = offset;
- uint16_t MaxBits=512;
- //if not enough samples - error
- if (*size < 51) return -1;
- //check for phase change faults - skip one sample if faulty
- uint8_t offsetA = 1, offsetB = 1;
- for (; i<48; i+=2){
- if (BitStream[i+1]==BitStream[i+2]) offsetA=0;
- if (BitStream[i+2]==BitStream[i+3]) offsetB=0;
- }
- if (!offsetA && offsetB) offset++;
- for (i=offset; i<*size-3; i+=2){
- //check for phase error
- if (BitStream[i+1]==BitStream[i+2]) {
- BitStream[bitnum++]=77;
- errCnt++;
- }
- if((BitStream[i]==1 && BitStream[i+1]==0) || (BitStream[i]==0 && BitStream[i+1]==1)){
- BitStream[bitnum++]=1^invert;
- } else if((BitStream[i]==0 && BitStream[i+1]==0) || (BitStream[i]==1 && BitStream[i+1]==1)){
- BitStream[bitnum++]=invert;
- } else {
- BitStream[bitnum++]=77;
- errCnt++;
- }
- if(bitnum>MaxBits) break;
- }
- *size=bitnum;
- return errCnt;
-}
-
-//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;
-}
-
-// demodulates strong heavily clipped samples