+ if (justNoise(dest, size)) return -1;
+ //make sure buffer has data
+ if (size < 66*64) return -2;
+ // FSK demodulator
+ size = fskdemod(dest, size, 64, 1, 10, 8); // FSK2a RF/64
+ if (size < 65) return -3; //did we get a good demod?
+ //Index map
+ //0 10 20 30 40 50 60
+ //| | | | | | |
+ //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
+ //-----------------------------------------------------------------------------
+ //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
+ //
+ //XSF(version)facility:codeone+codetwo
+ //Handle the data
+ size_t startIdx = 0;
+ uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,1};
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), &size, &startIdx);
+ if (errChk == 0) return -4; //preamble not found
+
+ if (!dest[startIdx+8] && dest[startIdx+17]==1 && dest[startIdx+26]==1 && dest[startIdx+35]==1 && dest[startIdx+44]==1 && dest[startIdx+53]==1){
+ //confirmed proper separator bits found
+ //return start position
+ return (int) startIdx;
+ }
+ return -5;
+}
+
+// by marshmellow
+// takes a array of binary values, start position, length of bits per parity (includes parity bit),
+// Parity Type (1 for odd 0 for even), and binary Length (length to run)
+size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen)
+{
+ uint32_t parityWd = 0;
+ size_t j = 0, bitCnt = 0;
+ for (int word = 0; word < (bLen); word+=pLen){
+ for (int bit=0; bit < pLen; bit++){
+ parityWd = (parityWd << 1) | BitStream[startIdx+word+bit];
+ BitStream[j++] = (BitStream[startIdx+word+bit]);
+ }
+ j--;
+ // if parity fails then return 0
+ if (parityTest(parityWd, pLen, pType) == 0) return -1;
+ bitCnt+=(pLen-1);
+ parityWd = 0;
+ }
+ // if we got here then all the parities passed
+ //return ID start index and size
+ return bitCnt;
+}
+
+// by marshmellow
+// FSK Demod then try to locate an AWID ID
+int AWIDdemodFSK(uint8_t *dest, size_t *size)
+{
+ //make sure buffer has enough data
+ if (*size < 96*50) return -1;
+
+ if (justNoise(dest, *size)) return -2;
+
+ // FSK demodulator
+ *size = fskdemod(dest, *size, 50, 1, 10, 8); // fsk2a RF/50
+ if (*size < 96) return -3; //did we get a good demod?
+
+ uint8_t preamble[] = {0,0,0,0,0,0,0,1};
+ size_t startIdx = 0;
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -4; //preamble not found
+ if (*size != 96) return -5;
+ return (int)startIdx;
+}
+
+// by marshmellow
+// FSK Demod then try to locate an Farpointe Data (pyramid) ID
+int PyramiddemodFSK(uint8_t *dest, size_t *size)
+{
+ //make sure buffer has data
+ if (*size < 128*50) return -5;
+
+ //test samples are not just noise
+ if (justNoise(dest, *size)) return -1;
+
+ // FSK demodulator
+ *size = fskdemod(dest, *size, 50, 1, 10, 8); // fsk2a RF/50
+ if (*size < 128) return -2; //did we get a good demod?
+
+ uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
+ size_t startIdx = 0;
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -4; //preamble not found
+ if (*size != 128) return -3;
+ return (int)startIdx;
+}
+
+
+uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, int high, int low)
+{
+ uint8_t allPeaks=1;
+ uint16_t cntPeaks=0;
+ for (size_t i=20; i<255; i++){
+ if (dest[i]>low && dest[i]<high)
+ allPeaks=0;
+ else
+ cntPeaks++;
+ }
+ if (allPeaks==0){
+ if (cntPeaks>190) return 1;
+ }
+ return allPeaks;
+}
+
+// by marshmellow
+// not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
+// maybe somehow adjust peak trimming value based on samples to fix?
+// return start index of best starting position for that clock and return clock (by reference)
+int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr)
+{
+ int i=0;
+ int clk[]={8,16,32,40,50,64,100,128,256};
+ int loopCnt = 256; //don't need to loop through entire array...
+ if (size == 0) return -1;
+ if (size<loopCnt) loopCnt = size;
+ //if we already have a valid clock quit
+
+ for (;i<8;++i)
+ if (clk[i] == *clock) return 0;
+
+ //get high and low peak
+ int peak, low;
+ getHiLo(dest, loopCnt, &peak, &low, 75, 75);
+
+ //test for large clean peaks
+ if (DetectCleanAskWave(dest, size, peak, low)==1){
+ uint16_t fcTest=0;
+ uint8_t mostFC=0;
+ fcTest=countFC(dest, size, &mostFC);
+ uint8_t fc1 = fcTest >> 8;
+ uint8_t fc2 = fcTest & 0xFF;
+
+ for (i=0; i<8; i++){
+ if (clk[i] == fc1) {
+ *clock=fc1;
+ return 0;
+ }
+ if (clk[i] == fc2) {
+ *clock=fc2;
+ return 0;
+ }
+ }
+ }
+
+ int ii;
+ int clkCnt;
+ int tol = 0;
+ int bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
+ int bestStart[]={0,0,0,0,0,0,0,0,0};
+ int errCnt=0;
+ //test each valid clock from smallest to greatest to see which lines up
+ for(clkCnt=0; clkCnt < 8; clkCnt++){
+ if (clk[clkCnt] == 32){
+ tol=1;
+ }else{
+ tol=0;