+//search for given preamble in given BitStream and return success=1 or fail=0 and startIndex and length
+uint8_t preambleSearch(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx) {
+ return (preambleSearchEx(BitStream, preamble, pLen, size, startIdx, false)) ? 1 : 0;
+}
+
+// find start of modulating data (for fsk and psk) in case of beginning noise or slow chip startup.
+size_t findModStart(uint8_t dest[], size_t size, uint8_t expWaveSize) {
+ size_t i = 0;
+ size_t waveSizeCnt = 0;
+ uint8_t thresholdCnt = 0;
+ bool isAboveThreshold = dest[i++] >= FSK_PSK_THRESHOLD;
+ for (; i < size-20; i++ ) {
+ if(dest[i] < FSK_PSK_THRESHOLD && isAboveThreshold) {
+ thresholdCnt++;
+ if (thresholdCnt > 2 && waveSizeCnt < expWaveSize+1) break;
+ isAboveThreshold = false;
+ waveSizeCnt = 0;
+ } else if (dest[i] >= FSK_PSK_THRESHOLD && !isAboveThreshold) {
+ thresholdCnt++;
+ if (thresholdCnt > 2 && waveSizeCnt < expWaveSize+1) break;
+ isAboveThreshold = true;
+ waveSizeCnt = 0;
+ } else {
+ waveSizeCnt++;
+ }
+ if (thresholdCnt > 10) break;
+ }
+ if (g_debugMode == 2) prnt("DEBUG: threshold Count reached at %u, count: %u",i, thresholdCnt);
+ return i;
+}
+
+int getClosestClock(int testclk) {
+ uint8_t fndClk[] = {8,16,32,40,50,64,128};
+
+ for (uint8_t clkCnt = 0; clkCnt<7; clkCnt++)
+ if (testclk >= fndClk[clkCnt]-(fndClk[clkCnt]/8) && testclk <= fndClk[clkCnt]+1)
+ return fndClk[clkCnt];
+
+ return 0;
+}
+
+void getNextLow(uint8_t samples[], size_t size, int low, size_t *i) {
+ while ((samples[*i] > low) && (*i < size))
+ *i+=1;
+}
+
+void getNextHigh(uint8_t samples[], size_t size, int high, size_t *i) {
+ while ((samples[*i] < high) && (*i < size))
+ *i+=1;
+}
+
+// load wave counters
+bool loadWaveCounters(uint8_t samples[], size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low) {
+ size_t i=0, firstLow, firstHigh;
+ size_t testsize = (size < 512) ? size : 512;
+
+ if ( getHiLo(samples, testsize, high, low, 80, 80) == -1 ) {
+ if (g_debugMode==2) prnt("DEBUG STT: just noise detected - quitting");
+ return false; //just noise
+ }
+
+ // get to first full low to prime loop and skip incomplete first pulse
+ getNextHigh(samples, size, *high, &i);
+ getNextLow(samples, size, *low, &i);
+ *skip = i;
+
+ // populate tmpbuff buffer with pulse lengths
+ while (i < size) {
+ // measure from low to low
+ firstLow = i;
+ //find first high point for this wave
+ getNextHigh(samples, size, *high, &i);
+ firstHigh = i;
+
+ getNextLow(samples, size, *low, &i);
+
+ if (*waveCnt >= (size/LOWEST_DEFAULT_CLOCK))
+ break;
+
+ highToLowWaveLen[*waveCnt] = i - firstHigh; //first high to first low
+ lowToLowWaveLen[*waveCnt] = i - firstLow;
+ *waveCnt += 1;
+ if (i-firstLow < *minClk && i < size) {
+ *minClk = i - firstLow;
+ }
+ }
+ return true;
+}
+
+size_t pskFindFirstPhaseShift(uint8_t samples[], size_t size, uint8_t *curPhase, size_t waveStart, uint16_t fc, uint16_t *fullWaveLen) {
+ uint16_t loopCnt = (size+3 < 4096) ? size : 4096; //don't need to loop through entire array...
+
+ uint16_t avgWaveVal=0, lastAvgWaveVal=0;
+ size_t i = waveStart, waveEnd, waveLenCnt, firstFullWave;
+ for (; i<loopCnt; i++) {
+ // find peak // was "samples[i] + fc" but why? must have been used to weed out some wave error... removed..
+ if (samples[i] < samples[i+1] && samples[i+1] >= samples[i+2]){
+ waveEnd = i+1;
+ if (g_debugMode == 2) prnt("DEBUG PSK: waveEnd: %u, waveStart: %u", waveEnd, waveStart);
+ waveLenCnt = waveEnd-waveStart;
+ if (waveLenCnt > fc && waveStart > fc && !(waveLenCnt > fc+8)){ //not first peak and is a large wave but not out of whack
+ lastAvgWaveVal = avgWaveVal/(waveLenCnt);
+ firstFullWave = waveStart;
+ *fullWaveLen = waveLenCnt;
+ //if average wave value is > graph 0 then it is an up wave or a 1 (could cause inverting)
+ if (lastAvgWaveVal > FSK_PSK_THRESHOLD) *curPhase ^= 1;
+ return firstFullWave;
+ }
+ waveStart = i+1;
+ avgWaveVal = 0;
+ }
+ avgWaveVal += samples[i+2];
+ }
+ return 0;
+}
+
+//by marshmellow
+//amplify based on ask edge detection - not accurate enough to use all the time
+void askAmp(uint8_t *BitStream, size_t size) {
+ uint8_t Last = 128;
+ for(size_t i = 1; i<size; i++){
+ if (BitStream[i]-BitStream[i-1]>=30) //large jump up
+ Last = 255;
+ else if(BitStream[i-1]-BitStream[i]>=20) //large jump down
+ Last = 0;
+
+ BitStream[i-1] = Last;
+ }
+ return;
+}
+
+uint32_t manchesterEncode2Bytes(uint16_t datain) {
+ uint32_t output = 0;
+ uint8_t curBit = 0;
+ for (uint8_t i=0; i<16; i++) {
+ curBit = (datain >> (15-i) & 1);
+ output |= (1<<(((15-i)*2)+curBit));
+ }
+ return output;
+}
+
+//by marshmellow
+//encode binary data into binary manchester
+//NOTE: BitStream must have triple the size of "size" available in memory to do the swap
+int ManchesterEncode(uint8_t *BitStream, size_t size) {
+ //allow up to 4K out (means BitStream must be at least 2048+4096 to handle the swap)
+ size = (size>2048) ? 2048 : size;
+ size_t modIdx = size;
+ size_t i;
+ for (size_t idx=0; idx < size; idx++){
+ BitStream[idx+modIdx++] = BitStream[idx];
+ BitStream[idx+modIdx++] = BitStream[idx]^1;
+ }
+ for (i=0; i<(size*2); i++){
+ BitStream[i] = BitStream[i+size];
+ }
+ return i;
+}
+
+// by marshmellow
+// to detect a wave that has heavily clipped (clean) samples
+uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, uint8_t high, uint8_t low) {
+ bool allArePeaks = true;
+ uint16_t cntPeaks=0;
+ size_t loopEnd = 512+160;
+ if (loopEnd > size) loopEnd = size;
+ for (size_t i=160; i<loopEnd; i++){
+ if (dest[i]>low && dest[i]<high)
+ allArePeaks = false;
+ else
+ cntPeaks++;
+ }
+ if (!allArePeaks){
+ if (cntPeaks > 300) return true;
+ }
+ return allArePeaks;
+}
+
+//**********************************************************************************************
+//-------------------Clock / Bitrate Detection Section------------------------------------------
+//**********************************************************************************************
+
+// by marshmellow
+// to help detect clocks on heavily clipped samples
+// based on count of low to low
+int DetectStrongAskClock(uint8_t dest[], size_t size, int high, int low, int *clock) {
+ size_t startwave;
+ size_t i = 100;
+ size_t minClk = 255;
+ int shortestWaveIdx = 0;
+ // get to first full low to prime loop and skip incomplete first pulse
+ getNextHigh(dest, size, high, &i);
+ getNextLow(dest, size, low, &i);
+
+ // loop through all samples
+ while (i < size) {
+ // measure from low to low
+ startwave = i;
+
+ getNextHigh(dest, size, high, &i);
+ getNextLow(dest, size, low, &i);
+ //get minimum measured distance
+ if (i-startwave < minClk && i < size) {
+ minClk = i - startwave;
+ shortestWaveIdx = startwave;
+ }
+ }
+ // set clock
+ if (g_debugMode==2) prnt("DEBUG ASK: DetectStrongAskClock smallest wave: %d",minClk);
+ *clock = getClosestClock(minClk);
+ if (*clock == 0)