-//demodulates strong heavily clipped samples
-int cleanAskRawDemod(uint8_t *BinStream, size_t *size, int clk, int invert, int high, int low, int *startIdx)
-{
- *startIdx=0;
- size_t bitCnt=0, smplCnt=1, errCnt=0;
- bool waveHigh = (BinStream[0] >= high);
- for (size_t i=1; 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++;
- if (g_debugMode==2) prnt("DEBUG ASK: Modulation Error at: %u", i);
- BinStream[bitCnt++] = 7;
- } else if (waveHigh) {
- BinStream[bitCnt++] = invert;
- BinStream[bitCnt++] = invert;
- } else if (!waveHigh) {
- BinStream[bitCnt++] = invert ^ 1;
- BinStream[bitCnt++] = invert ^ 1;
- }
- if (*startIdx==0) *startIdx = i-clk;
- waveHigh = !waveHigh;
- smplCnt = 0;
- } else if (smplCnt > (clk/2) - (clk/4)-1) { //half clock
- if (waveHigh) {
- BinStream[bitCnt++] = invert;
- } else if (!waveHigh) {
- BinStream[bitCnt++] = invert ^ 1;
- }
- if (*startIdx==0) *startIdx = i-(clk/2);
- waveHigh = !waveHigh;
- smplCnt = 0;
- } else {
- smplCnt++;
- //transition bit oops
- }
- } else { //haven't hit new high or new low yet
- smplCnt++;
- }
- }
- }
- *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");
+//countFC is to detect the field clock lengths.
+//counts and returns the 2 most common wave lengths
+//mainly used for FSK field clock detection
+uint16_t countFC(uint8_t *BitStream, size_t size, uint8_t fskAdj) {
+ uint8_t fcLens[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+ uint16_t fcCnts[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+ uint8_t fcLensFnd = 0;
+ uint8_t lastFCcnt = 0;
+ uint8_t fcCounter = 0;
+ size_t i;
+ if (size < 180) return 0;