]>
Commit | Line | Data |
---|---|---|
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 | ||
11 | #include <stdio.h> | |
12 | #include <string.h> | |
13 | #include "ui.h" | |
14 | #include "graph.h" | |
15 | #include "lfdemod.h" | |
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; | |
39 | memset(GraphBuffer, 0x00, GraphTraceLen); | |
40 | ||
41 | GraphTraceLen = 0; | |
42 | ||
43 | if (redraw) | |
44 | RepaintGraphWindow(); | |
45 | ||
46 | return gtl; | |
47 | } | |
48 | ||
49 | /* | |
50 | * Detect clock rate | |
51 | */ | |
52 | //decommissioned - has difficulty detecting rf/32 | |
53 | /* | |
54 | int DetectClockOld(int peak) | |
55 | { | |
56 | int i; | |
57 | int clock = 0xFFFF; | |
58 | int lastpeak = 0; | |
59 | ||
60 | // Detect peak if we don't have one | |
61 | if (!peak) | |
62 | for (i = 0; i < GraphTraceLen; ++i) | |
63 | if (GraphBuffer[i] > peak) | |
64 | peak = GraphBuffer[i]; | |
65 | ||
66 | // peak=(int)(peak*.75); | |
67 | for (i = 1; i < GraphTraceLen; ++i) | |
68 | { | |
69 | // If this is the beginning of a peak | |
70 | if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak) | |
71 | { | |
72 | // Find lowest difference between peaks | |
73 | if (lastpeak && i - lastpeak < clock) | |
74 | clock = i - lastpeak; | |
75 | lastpeak = i; | |
76 | } | |
77 | } | |
78 | ||
79 | return clock; | |
80 | } | |
81 | */ | |
82 | /* | |
83 | NOW IN LFDEMOD.C | |
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? | |
88 | int DetectASKClock(int peak) | |
89 | { | |
90 | int i=0; | |
91 | int low=0; | |
92 | int clk[]={16,32,40,50,64,100,128,256}; | |
93 | int loopCnt = 256; | |
94 | if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen; | |
95 | if (!peak){ | |
96 | for (i=0;i<loopCnt;++i){ | |
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 | } | |
107 | int ii; | |
108 | int clkCnt; | |
109 | int tol = 0; | |
110 | int bestErr=1000; | |
111 | int errCnt[]={0,0,0,0,0,0,0,0}; | |
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)){ | |
121 | errCnt[clkCnt]=0; | |
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 | |
127 | errCnt[clkCnt]++; | |
128 | } | |
129 | } | |
130 | if(errCnt[clkCnt]==0) return clk[clkCnt]; | |
131 | if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt]; | |
132 | } | |
133 | } | |
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 | } | |
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]); | |
143 | return clk[best]; | |
144 | } | |
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; | |
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); | |
164 | } | |
165 | return i; | |
166 | } | |
167 | /* Get or auto-detect clock rate */ | |
168 | int GetClock(const char *str, int peak, int verbose) | |
169 | { | |
170 | int clock; | |
171 | // int clock2; | |
172 | sscanf(str, "%i", &clock); | |
173 | if (!strcmp(str, "")) | |
174 | clock = 0; | |
175 | ||
176 | /* Auto-detect clock */ | |
177 | if (!clock) | |
178 | { | |
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 */ | |
184 | if (!verbose){ | |
185 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
186 | //PrintAndLog("clock2: %d",clock2); | |
187 | } | |
188 | } | |
189 | ||
190 | return clock; | |
191 | } |