]>
Commit | Line | Data |
---|---|---|
a553f267 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
3 | // | |
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 | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // Graph utilities | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
7fe9b0b7 | 11 | #include <stdio.h> |
12 | #include <string.h> | |
13 | #include "ui.h" | |
14 | #include "graph.h" | |
d5a72d2f | 15 | #include "lfdemod.h" |
7fe9b0b7 | 16 | |
17 | int GraphBuffer[MAX_GRAPH_TRACE_LEN]; | |
18 | int GraphTraceLen; | |
19 | ||
20 | /* write a bit to the graph */ | |
21 | void AppendGraph(int redraw, int clock, int bit) | |
22 | { | |
23 | int i; | |
24 | ||
25 | for (i = 0; i < (int)(clock / 2); ++i) | |
26 | GraphBuffer[GraphTraceLen++] = bit ^ 1; | |
27 | ||
28 | for (i = (int)(clock / 2); i < clock; ++i) | |
29 | GraphBuffer[GraphTraceLen++] = bit; | |
30 | ||
31 | if (redraw) | |
32 | RepaintGraphWindow(); | |
33 | } | |
34 | ||
35 | /* clear out our graph window */ | |
36 | int ClearGraph(int redraw) | |
37 | { | |
38 | int gtl = GraphTraceLen; | |
52ab55ab | 39 | memset(GraphBuffer, 0x00, GraphTraceLen); |
40 | ||
7fe9b0b7 | 41 | GraphTraceLen = 0; |
42 | ||
43 | if (redraw) | |
44 | RepaintGraphWindow(); | |
45 | ||
46 | return gtl; | |
47 | } | |
48 | ||
49 | /* | |
50 | * Detect clock rate | |
51 | */ | |
d5a72d2f | 52 | //decommissioned - has difficulty detecting rf/32 |
0e74c023 | 53 | /* |
d5a72d2f | 54 | int DetectClockOld(int peak) |
7fe9b0b7 | 55 | { |
56 | int i; | |
57 | int clock = 0xFFFF; | |
58 | int lastpeak = 0; | |
59 | ||
0e74c023 | 60 | // Detect peak if we don't have one |
7fe9b0b7 | 61 | if (!peak) |
62 | for (i = 0; i < GraphTraceLen; ++i) | |
63 | if (GraphBuffer[i] > peak) | |
64 | peak = GraphBuffer[i]; | |
65 | ||
0e74c023 | 66 | // peak=(int)(peak*.75); |
7fe9b0b7 | 67 | for (i = 1; i < GraphTraceLen; ++i) |
68 | { | |
0e74c023 | 69 | // If this is the beginning of a peak |
70 | if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak) | |
7fe9b0b7 | 71 | { |
0e74c023 | 72 | // Find lowest difference between peaks |
7fe9b0b7 | 73 | if (lastpeak && i - lastpeak < clock) |
74 | clock = i - lastpeak; | |
75 | lastpeak = i; | |
76 | } | |
77 | } | |
78 | ||
79 | return clock; | |
80 | } | |
0e74c023 | 81 | */ |
d5a72d2f | 82 | /* |
83 | NOW IN LFDEMOD.C | |
0e74c023 | 84 | |
85 | // by marshmellow | |
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? | |
d5a72d2f | 88 | int DetectASKClock(int peak) |
0e74c023 | 89 | { |
90 | int i=0; | |
91 | int low=0; | |
92 | int clk[]={16,32,40,50,64,100,128,256}; | |
d5a72d2f | 93 | int loopCnt = 256; |
94 | if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen; | |
0e74c023 | 95 | if (!peak){ |
d5a72d2f | 96 | for (i=0;i<loopCnt;++i){ |
0e74c023 | 97 | if(GraphBuffer[i]>peak){ |
98 | peak = GraphBuffer[i]; | |
99 | } | |
100 | if(GraphBuffer[i]<low){ | |
101 | low = GraphBuffer[i]; | |
102 | } | |
103 | } | |
104 | peak=(int)(peak*.75); | |
105 | low= (int)(low*.75); | |
106 | } | |
0e74c023 | 107 | int ii; |
0e74c023 | 108 | int clkCnt; |
109 | int tol = 0; | |
110 | int bestErr=1000; | |
111 | int errCnt[]={0,0,0,0,0,0,0,0}; | |
0e74c023 | 112 | for(clkCnt=0; clkCnt<6;++clkCnt){ |
113 | if (clk[clkCnt]==32){ | |
114 | tol=1; | |
115 | }else{ | |
116 | tol=0; | |
117 | } | |
118 | bestErr=1000; | |
119 | for (ii=0; ii<loopCnt; ++ii){ | |
120 | if ((GraphBuffer[ii]>=peak) || (GraphBuffer[ii]<=low)){ | |
d5a72d2f | 121 | errCnt[clkCnt]=0; |
0e74c023 | 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){ | |
0e74c023 | 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 | |
0e74c023 | 127 | errCnt[clkCnt]++; |
0e74c023 | 128 | } |
129 | } | |
130 | if(errCnt[clkCnt]==0) return clk[clkCnt]; | |
131 | if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt]; | |
132 | } | |
133 | } | |
0e74c023 | 134 | } |
135 | int iii=0; | |
136 | int best=0; | |
137 | for (iii=0; iii<6;++iii){ | |
138 | if (errCnt[iii]<errCnt[best]){ | |
139 | best = iii; | |
140 | } | |
141 | } | |
d5a72d2f | 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]); |
0e74c023 | 143 | return clk[best]; |
144 | } | |
d5a72d2f | 145 | */ |
146 | void setGraphBuf(uint8_t *buff,int size) | |
147 | { | |
148 | int i=0; | |
149 | ClearGraph(0); | |
150 | for (; i < size; ++i){ | |
151 | GraphBuffer[i]=buff[i]; | |
152 | } | |
153 | GraphTraceLen=size; | |
154 | RepaintGraphWindow(); | |
155 | return; | |
156 | } | |
157 | int getFromGraphBuf(uint8_t *buff) | |
158 | { | |
159 | uint32_t i; | |
f822a063 | 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 | |
d5a72d2f | 163 | buff[i]=(uint8_t)(GraphBuffer[i]+128); |
f822a063 | 164 | } |
d5a72d2f | 165 | return i; |
166 | } | |
7fe9b0b7 | 167 | /* Get or auto-detect clock rate */ |
168 | int GetClock(const char *str, int peak, int verbose) | |
169 | { | |
170 | int clock; | |
0e74c023 | 171 | // int clock2; |
7fe9b0b7 | 172 | sscanf(str, "%i", &clock); |
173 | if (!strcmp(str, "")) | |
174 | clock = 0; | |
175 | ||
176 | /* Auto-detect clock */ | |
177 | if (!clock) | |
178 | { | |
d5a72d2f | 179 | uint8_t grph[MAX_GRAPH_TRACE_LEN]={0}; |
180 | int size = getFromGraphBuf(grph); | |
181 | clock = DetectASKClock(grph,size,0); | |
0e74c023 | 182 | //clock2 = DetectClock2(peak); |
7fe9b0b7 | 183 | /* Only print this message if we're not looping something */ |
0e74c023 | 184 | if (!verbose){ |
7fe9b0b7 | 185 | PrintAndLog("Auto-detected clock rate: %d", clock); |
0e74c023 | 186 | //PrintAndLog("clock2: %d",clock2); |
187 | } | |
7fe9b0b7 | 188 | } |
189 | ||
190 | return clock; | |
191 | } |