- *size = bitCnt;
- return errCnt;
-}
-
-//by marshmellow
-//attempts to demodulate ask modulations, askType == 0 for ask/raw, askType==1 for ask/manchester
-int askdemod_ext(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType, int *startIdx) {
- if (*size==0) return -1;
- int start = DetectASKClock(BinStream, *size, clk, maxErr); //clock default
- if (*clk==0 || start < 0) return -3;
- if (*invert != 1) *invert = 0;
- if (amp==1) askAmp(BinStream, *size);
- if (g_debugMode==2) prnt("DEBUG ASK: clk %d, beststart %d, amp %d", *clk, start, amp);
-
- //start pos from detect ask clock is 1/2 clock offset
- // NOTE: can be negative (demod assumes rest of wave was there)
- *startIdx = start - (*clk/2);
- 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 -2; //just noise
-
- size_t errCnt = 0;
- // if clean clipped waves detected run alternate demod
- if (DetectCleanAskWave(BinStream, *size, high, low)) {
- if (g_debugMode==2) prnt("DEBUG ASK: Clean Wave Detected - using clean wave demod");
- errCnt = cleanAskRawDemod(BinStream, size, *clk, *invert, high, low, startIdx);
- if (askType) { //askman
- uint8_t alignPos = 0;
- errCnt = manrawdecode(BinStream, size, 0, &alignPos);
- *startIdx += *clk/2 * alignPos;
- if (g_debugMode) prnt("DEBUG ASK CLEAN: startIdx %i, alignPos %u", *startIdx, alignPos);
- return errCnt;
- } else { //askraw
- return errCnt;
- }
- }
- if (g_debugMode) prnt("DEBUG ASK WEAK: startIdx %i", *startIdx);
- if (g_debugMode==2) prnt("DEBUG ASK: Weak Wave Detected - using weak wave demod");
-
- int lastBit; //set first clock check - can go negative
- size_t i, bitnum = 0; //output counter
- uint8_t midBit = 0;
- 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 MaxBits = 3072; //max bits to collect
- lastBit = start - *clk;
-
- for (i = start; i < *size; ++i) {
- if (i-lastBit >= *clk-tol){
- if (BinStream[i] >= high) {
- BinStream[bitnum++] = *invert;
- } else if (BinStream[i] <= low) {
- BinStream[bitnum++] = *invert ^ 1;
- } else if (i-lastBit >= *clk+tol) {
- if (bitnum > 0) {
- if (g_debugMode==2) prnt("DEBUG ASK: Modulation Error at: %u", i);
- BinStream[bitnum++]=7;
- errCnt++;
- }
- } else { //in tolerance - looking for peak
- continue;
- }
- midBit = 0;
- lastBit += *clk;
- } else if (i-lastBit >= (*clk/2-tol) && !midBit && !askType){
- if (BinStream[i] >= high) {
- BinStream[bitnum++] = *invert;
- } else if (BinStream[i] <= low) {
- BinStream[bitnum++] = *invert ^ 1;
- } else if (i-lastBit >= *clk/2+tol) {
- BinStream[bitnum] = BinStream[bitnum-1];
- bitnum++;
- } else { //in tolerance - looking for peak
- continue;
+ bool errBitHigh = 0;
+ bool bitHigh = 0;
+ uint8_t ignoreCnt = 0;
+ uint8_t ignoreWindow = 4;
+ bool lastPeakHigh = 0;
+ int lastBit = 0;
+ size_t bestStart[]={0,0,0,0,0,0,0,0,0};
+ peakcnt=0;
+ //test each valid clock from smallest to greatest to see which lines up
+ for(clkCnt=0; clkCnt < 8; ++clkCnt){
+ //ignore clocks smaller than smallest peak
+ if (clk[clkCnt] < maxPeak - (clk[clkCnt]/4)) continue;
+ //try lining up the peaks by moving starting point (try first 256)
+ for (ii=20; ii < loopCnt; ++ii){
+ if ((dest[ii] >= peak) || (dest[ii] <= low)){
+ peakcnt = 0;
+ bitHigh = false;
+ ignoreCnt = 0;
+ lastBit = ii-clk[clkCnt];
+ //loop through to see if this start location works
+ for (i = ii; i < size-20; ++i) {
+ //if we are at a clock bit
+ if ((i >= lastBit + clk[clkCnt] - tol) && (i <= lastBit + clk[clkCnt] + tol)) {
+ //test high/low
+ if (dest[i] >= peak || dest[i] <= low) {
+ //if same peak don't count it
+ if ((dest[i] >= peak && !lastPeakHigh) || (dest[i] <= low && lastPeakHigh)) {
+ peakcnt++;
+ }
+ lastPeakHigh = (dest[i] >= peak);
+ bitHigh = true;
+ errBitHigh = false;
+ ignoreCnt = ignoreWindow;
+ lastBit += clk[clkCnt];
+ } else if (i == lastBit + clk[clkCnt] + tol) {
+ lastBit += clk[clkCnt];
+ }
+ //else if not a clock bit and no peaks
+ } else if (dest[i] < peak && dest[i] > low){
+ if (ignoreCnt==0){
+ bitHigh=false;
+ if (errBitHigh==true) peakcnt--;
+ errBitHigh=false;
+ } else {
+ ignoreCnt--;
+ }
+ // else if not a clock bit but we have a peak
+ } else if ((dest[i]>=peak || dest[i]<=low) && (!bitHigh)) {
+ //error bar found no clock...
+ errBitHigh=true;
+ }
+ }
+ if(peakcnt>peaksdet[clkCnt]) {
+ bestStart[clkCnt]=ii;
+ peaksdet[clkCnt]=peakcnt;
+ }