]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
17 int GraphBuffer
[MAX_GRAPH_TRACE_LEN
];
20 /* write a bit to the graph */
21 void AppendGraph(int redraw
, int clock
, int bit
)
25 for (i
= 0; i
< (int)(clock
/ 2); ++i
)
26 GraphBuffer
[GraphTraceLen
++] = bit
^ 1;
28 for (i
= (int)(clock
/ 2); i
< clock
; ++i
)
29 GraphBuffer
[GraphTraceLen
++] = bit
;
35 /* clear out our graph window */
36 int ClearGraph(int redraw
)
38 int gtl
= GraphTraceLen
;
39 memset(GraphBuffer
, 0x00, GraphTraceLen
);
52 //decommissioned - has difficulty detecting rf/32
54 int DetectClockOld(int peak)
60 // Detect peak if we don't have one
62 for (i = 0; i < GraphTraceLen; ++i)
63 if (GraphBuffer[i] > peak)
64 peak = GraphBuffer[i];
66 // peak=(int)(peak*.75);
67 for (i = 1; i < GraphTraceLen; ++i)
69 // If this is the beginning of a peak
70 if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak)
72 // Find lowest difference between peaks
73 if (lastpeak && i - lastpeak < clock)
86 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
87 // maybe somehow adjust peak trimming value based on samples to fix?
88 int DetectASKClock(int peak)
92 int clk[]={16,32,40,50,64,100,128,256};
94 if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen;
96 for (i=0;i<loopCnt;++i){
97 if(GraphBuffer[i]>peak){
98 peak = GraphBuffer[i];
100 if(GraphBuffer[i]<low){
101 low = GraphBuffer[i];
104 peak=(int)(peak*.75);
111 int errCnt[]={0,0,0,0,0,0,0,0};
112 for(clkCnt=0; clkCnt<6;++clkCnt){
113 if (clk[clkCnt]==32){
119 for (ii=0; ii<loopCnt; ++ii){
120 if ((GraphBuffer[ii]>=peak) || (GraphBuffer[ii]<=low)){
122 for (i=0; i<((int)(GraphTraceLen/clk[clkCnt])-1); ++i){
123 if (GraphBuffer[ii+(i*clk[clkCnt])]>=peak || GraphBuffer[ii+(i*clk[clkCnt])]<=low){
124 }else if(GraphBuffer[ii+(i*clk[clkCnt])-tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])-tol]<=low){
125 }else if(GraphBuffer[ii+(i*clk[clkCnt])+tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])+tol]<=low){
126 }else{ //error no peak detected
130 if(errCnt[clkCnt]==0) return clk[clkCnt];
131 if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt];
137 for (iii=0; iii<6;++iii){
138 if (errCnt[iii]<errCnt[best]){
142 // PrintAndLog("DEBUG: clkCnt: %d, ii: %d, i: %d peak: %d, low: %d, errcnt: %d, errCnt64: %d",clkCnt,ii,i,peak,low,errCnt[best],errCnt[4]);
146 void setGraphBuf(uint8_t *buff
,int size
)
150 for (; i
< size
; ++i
){
151 GraphBuffer
[i
]=buff
[i
];
154 RepaintGraphWindow();
157 int getFromGraphBuf(uint8_t *buff
)
160 for (i
=0;i
<GraphTraceLen
;++i
){
161 if (GraphBuffer
[i
]>127) GraphBuffer
[i
]=127; //trim
162 if (GraphBuffer
[i
]<-127) GraphBuffer
[i
]=-127; //trim
163 buff
[i
]=(uint8_t)(GraphBuffer
[i
]+128);
167 /* Get or auto-detect clock rate */
168 int GetClock(const char *str
, int peak
, int verbose
)
172 sscanf(str
, "%i", &clock
);
173 if (!strcmp(str
, ""))
176 /* Auto-detect clock */
179 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
180 int size
= getFromGraphBuf(grph
);
181 clock
= DetectASKClock(grph
,size
,0);
182 //clock2 = DetectClock2(peak);
183 /* Only print this message if we're not looping something */
185 PrintAndLog("Auto-detected clock rate: %d", clock
);
186 //PrintAndLog("clock2: %d",clock2);