-//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)
-{
- int i;
- //int clk2=*clk;
- int start = DetectASKClock(BinStream, *size, clk, 20); //clock default
- if (*clk==0) return -3;
- if (start < 0) return -3;
- // if autodetected too low then adjust //MAY NEED ADJUSTMENT
- //if (clk2==0 && *clk<8) *clk =64;
- //if (clk2==0 && *clk<32) *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
- int 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
- int iii = 0;
- uint32_t gLen = *size;
- if (gLen > 3000) gLen=3000;
- //if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
- if (!maxErr) gLen=*clk*2;
- uint8_t errCnt =0;
- uint16_t MaxBits = 500;
- uint32_t bestStart = *size;
- int bestErrCnt = maxErr+1;
- // 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;
- errCnt=0;
- // 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;
- } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
- //low found and we are expecting a bar
- lastBit+=*clk;
- } 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>(maxErr)) break; //allow 1 error for every 1000 samples else start over
- }
- }
- if ((i-iii) >(MaxBits * *clk)) break; //got plenty of bits
- }
- //we got more than 64 good bits and not all errors
- if ((((i-iii)/ *clk) > (64)) && (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;
- }
- }
- }
- }
- if (bestErrCnt<=maxErr){
- //best run is good enough set to best run and set 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++;
- } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
- //low found and we are expecting a bar
- lastBit+=*clk;
- BinStream[bitnum] = 1-*invert;
- 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 >=MaxBits) break;
- }
- *size=bitnum;
- } else{
- *invert=bestStart;
- *clk=iii;
- return -1;
- }
- 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){
- i=0;
- for (i=i+ii;i<*size-2;i+=2){
- if(BitStream[i]==1 && (BitStream[i+1]==0)){
- } else if((BitStream[i]==0)&& BitStream[i+1]==1){
- } else {
- errCnt++;
- }
- if(bitnum>MaxBits) break;
- }
- if (bestErr>errCnt){
- bestErr=errCnt;
- bestRun=ii;
- }
- errCnt=0;
- }
- errCnt=bestErr;
- if (errCnt<20){
- ii=bestRun;
- i=0;
- for (i=i+ii; 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;
- //errCnt++;
- }
- if(bitnum>MaxBits) break;
- }
- *size=bitnum;
- }
- return errCnt;
-}
-
-//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;
- uint32_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(int 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;
-}
-