//by marshmellow
//takes 1s and 0s and searches for EM410x format - output EM ID
-uint64_t Em410xDecode(uint8_t BitStream[],uint32_t BitLen)
+uint64_t Em410xDecode(uint8_t *BitStream,uint32_t BitLen)
{
//no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
// otherwise could be a void with no arguments
uint64_t lo=0; //hi=0,
uint32_t i = 0;
- uint32_t initLoopMax = 1000;
+ uint32_t initLoopMax = 65;
if (initLoopMax>BitLen) initLoopMax=BitLen;
- for (;i < initLoopMax; ++i) //1000 samples should be plenty to find high and low values
+ for (;i < initLoopMax; ++i) //65 samples should be plenty to find high and low values
{
if (BitStream[i] > high)
high = BitStream[i];
uint32_t ii=0;
uint8_t resetCnt = 0;
while( (idx + 64) < BitLen) {
-restart:
+ restart:
// search for a start of frame marker
if ( memcmp(BitStream+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0)
{ // frame marker found
//int invert=0; //invert default
int high = 0, low = 0;
*clk=DetectClock2(BinStream,(size_t)*BitLen,*clk); //clock default
- uint8_t BitStream[MAX_BitStream_LEN] = {0};
+ uint8_t BitStream[252] = {0};
//sscanf(Cmd, "%i %i", &clk, &invert);
if (*clk<8) *clk =64;
if (*clk<32) *clk=32;
if (*invert != 0 && *invert != 1) *invert=0;
- uint32_t initLoopMax = 1000;
+ uint32_t initLoopMax = 200;
if (initLoopMax>*BitLen) initLoopMax=*BitLen;
// Detect high and lows
//PrintAndLog("Using Clock: %d and invert=%d",clk,invert);
- for (i = 0; i < initLoopMax; ++i) //1000 samples should be plenty to find high and low values
+ for (i = 0; i < initLoopMax; ++i) //200 samples should be enough to find high and low values
{
if (BinStream[i] > high)
high = BinStream[i];
for (iii=0; iii < gLen; ++iii){
if ((BinStream[iii]>=high)||(BinStream[iii]<=low)){
lastBit=iii-*clk;
+ bitnum=0;
//loop through to see if this start location works
for (i = iii; i < *BitLen; ++i) {
if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
}
}
}
+ if (bitnum >250) break;
}
//we got more than 64 good bits and not all errors
if ((bitnum > (64+errCnt)) && (errCnt<(*BitLen/1000))) {
//run through 2 times and take least errCnt
int manrawdemod(uint8_t * BitStream, int *bitLen)
{
- uint8_t BitStream2[MAX_BitStream_LEN]={0};
+ uint8_t BitStream2[252]={0};
int bitnum=0;
int errCnt =0;
int i=1;
BitStream2[bitnum++]=77;
errCnt++;
}
+ if(bitnum>250) break;
}
if (bestErr>errCnt){
bestErr=errCnt;
bitnum=0;
}
errCnt=bestErr;
- if (errCnt<10){
+ if (errCnt<20){
for (i=0; i<bitnum;++i){
BitStream[i]=BitStream2[i];
}
// int invert=0; //invert default
int high = 0, low = 0;
*clk=DetectClock2(BinStream,*bitLen,*clk); //clock default
- uint8_t BitStream[MAX_BitStream_LEN] = {0};
+ uint8_t BitStream[502] = {0};
if (*clk<8) *clk =64;
if (*clk<32) *clk=32;
if (*invert != 0 && *invert != 1) *invert =0;
- uint32_t initLoopMax = 1000;
+ uint32_t initLoopMax = 200;
if (initLoopMax>*bitLen) initLoopMax=*bitLen;
// Detect high and lows
- for (i = 0; i < initLoopMax; ++i) //1000 samples should be plenty to find high and low values
+ for (i = 0; i < initLoopMax; ++i) //200 samples should be plenty to find high and low values
{
if (BinStream[i] > high)
high = BinStream[i];
// PrintAndLog("no data found");
return -1;
}
- //13% fuzz in case highs and lows aren't clipped [marshmellow]
+ //25% fuzz in case highs and lows aren't clipped [marshmellow]
high=(int)(0.75*high);
low=(int)(0.75*low);
bitnum=0;//start over
break;
}
- }
+ }
}
+ if (bitnum>500) break;
}
//we got more than 64 good bits and not all errors
if ((bitnum > (64+errCnt)) && (errCnt<(*bitLen/1000))) {
return errCnt;
}
//translate wave to 11111100000 (1 for each short wave 0 for each long wave)
-size_t fsk_wave_demod2(uint8_t * dest, size_t size)
+size_t fsk_wave_demod(uint8_t * dest, size_t size)
{
uint32_t last_transition = 0;
uint32_t idx = 1;
uint32_t maxVal=0;
- // // 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
-
- // we do care about the actual value as sometimes near the center of the
+
+ // we do care about the actual theshold value as sometimes near the center of the
// wave we may get static that changes direction of wave for one value
// if our value is too low it might affect the read. and if our tag or
// antenna is weak a setting too high might not see anything. [marshmellow]
// set close to the top of the wave threshold with 13% margin for error
// less likely to get a false transition up there.
// (but have to be careful not to go too high and miss some short waves)
- uint32_t threshold_value = (uint32_t)(maxVal*.87); idx=1;
- //uint8_t threshold_value = 127;
+ uint8_t threshold_value = (uint8_t)(maxVal*.87); idx=1;
+ //uint8_t threshold_value = 127;
// sync to first lo-hi transition, and threshold
// Check for 0->1 transition
if (dest[idx-1] < dest[idx]) { // 0 -> 1 transition
- if (idx-last_transition<6){
+ if (idx-last_transition<6){ //0-5 = garbage noise
//do nothing with extra garbage
- } else if (idx-last_transition < 9) {
+ } else if (idx-last_transition < 9) { //6-8 = 8 waves
dest[numBits]=1;
- } else {
+ } else { //9+ = 10 waves
dest[numBits]=0;
}
last_transition = idx;
}
//translate 11111100000 to 10
-size_t aggregate_bits2(uint8_t *dest,size_t size, uint8_t rfLen, uint8_t maxConsequtiveBits, uint8_t invert )// uint8_t h2l_crossing_value,uint8_t l2h_crossing_value,
+size_t aggregate_bits(uint8_t *dest,size_t size, uint8_t rfLen, uint8_t maxConsequtiveBits, uint8_t invert )// uint8_t h2l_crossing_value,uint8_t l2h_crossing_value,
{
uint8_t lastval=dest[0];
uint32_t idx=0;
n=myround2((float)(n+1)/((float)(rfLen)/(float)8));
//n=(n+1) / h2l_crossing_value;
} else {// 0->1 crossing
- n=myround2((float)(n+1)/((float)(rfLen-2)/(float)10));
+ n=myround2((float)(n+1)/((float)(rfLen-2)/(float)10)); //-2 for fudge factor
//n=(n+1) / l2h_crossing_value;
}
if (n == 0) n = 1;
// }
// size_t size = GraphTraceLen;
// FSK demodulator
- size = fsk_wave_demod2(dest, size);
- size = aggregate_bits2(dest, size,rfLen,192,invert);
+ size = fsk_wave_demod(dest, size);
+ size = aggregate_bits(dest, size,rfLen,192,invert);
// size = aggregate_bits(size, h2l_crossing_value, l2h_crossing_value,192, invert); //192=no limit to same values
//done messing with GraphBuffer - repaint
//RepaintGraphWindow();
int IOdemodFSK(uint8_t *dest, size_t size)
{
- size_t idx=0;
+ uint32_t idx=0;
//make sure buffer has data
if (size < 64) return -1;
//test samples are not just noise
//
//XSF(version)facility:codeone+codetwo
//Handle the data
- uint8_t mask[] = {0,0,0,0,0,0,0,0,0,1};
+ uint8_t mask[] = {0,0,0,0,0,0,0,0,0,1};
for( idx=0; idx < (size - 74); idx++) {
if ( memcmp(dest + idx, mask, sizeof(mask))==0) {
//frame marker found
if (!dest[idx+8] && dest[idx+17]==1 && dest[idx+26]==1 && dest[idx+35]==1 && dest[idx+44]==1 && dest[idx+53]==1){
//confirmed proper separator bits found
//return start position
- return idx;
+ return (int) idx;
}
}
}