-size_t fsk_demod(uint8_t * dest, size_t size)
-{
- uint32_t last_transition = 0;
- uint32_t idx = 1;
-
- // we don't care about actual value, only if it's more or less than a
- // threshold essentially we capture zero crossings for later analysis
- uint8_t threshold_value = 127;
-
- // sync to first lo-hi transition, and threshold
-
- //Need to threshold first sample
- dest[0] = (dest[0] < threshold_value) ? 0 : 1;
-
- size_t numBits = 0;
- // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
- // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
- // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
- for(idx = 1; idx < size; idx++) {
- // threshold current value
- dest[idx] = (dest[idx] < threshold_value) ? 0 : 1;
-
- // Check for 0->1 transition
- if (dest[idx-1] < dest[idx]) { // 0 -> 1 transition
-
- dest[numBits] = (idx-last_transition < 9) ? 1 : 0;
- last_transition = idx;
- numBits++;
- }
- }
- return numBits; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
-}
-
-
-size_t aggregate_bits(uint8_t *dest,size_t size, uint8_t h2l_crossing_value,uint8_t l2h_crossing_value, uint8_t maxConsequtiveBits, uint8_t invert )
-{
- uint8_t lastval=dest[0];
- uint32_t idx=0;
- size_t numBits=0;
- uint32_t n=1;
-
- for( idx=1; idx < size; idx++) {
-
- if (dest[idx]==lastval) {
- n++;
- continue;
- }
- //if lastval was 1, we have a 1->0 crossing
- if ( dest[idx-1] ) {
- n=(n+1) / h2l_crossing_value;
- } else {// 0->1 crossing
- n=(n+1) / l2h_crossing_value;
- }
- if (n == 0) n = 1;
-
- if(n < maxConsequtiveBits)
- {
- if ( invert==0)
- memset(dest+numBits, dest[idx-1] , n);
- else
- memset(dest+numBits, dest[idx-1]^1 , n);
-
- numBits += n;
- }
- n=0;
- lastval=dest[idx];
- }//end for
-
- return numBits;
-
-}
-// loop to capture raw HID waveform then FSK demodulate the TAG ID from it