]>
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 | { | |
ba1a299c | 23 | int i; |
7fe9b0b7 | 24 | |
ba1a299c | 25 | for (i = 0; i < (int)(clock / 2); ++i) |
26 | GraphBuffer[GraphTraceLen++] = bit ^ 1; | |
7fe9b0b7 | 27 | |
ba1a299c | 28 | for (i = (int)(clock / 2); i < clock; ++i) |
29 | GraphBuffer[GraphTraceLen++] = bit; | |
30 | ||
31 | if (redraw) | |
32 | RepaintGraphWindow(); | |
7fe9b0b7 | 33 | } |
34 | ||
35 | /* clear out our graph window */ | |
36 | int ClearGraph(int redraw) | |
37 | { | |
ba1a299c | 38 | int gtl = GraphTraceLen; |
39 | GraphTraceLen = 0; | |
7fe9b0b7 | 40 | |
ba1a299c | 41 | if (redraw) |
42 | RepaintGraphWindow(); | |
7fe9b0b7 | 43 | |
ba1a299c | 44 | return gtl; |
7fe9b0b7 | 45 | } |
46 | ||
47 | /* | |
48 | * Detect clock rate | |
49 | */ | |
ba1a299c | 50 | //decommissioned - has difficulty detecting rf/32 |
0e74c023 | 51 | /* |
d5a72d2f | 52 | int DetectClockOld(int peak) |
7fe9b0b7 | 53 | { |
ba1a299c | 54 | int i; |
55 | int clock = 0xFFFF; | |
56 | int lastpeak = 0; | |
7fe9b0b7 | 57 | |
ba1a299c | 58 | // Detect peak if we don't have one |
59 | if (!peak) | |
60 | for (i = 0; i < GraphTraceLen; ++i) | |
61 | if (GraphBuffer[i] > peak) | |
62 | peak = GraphBuffer[i]; | |
7fe9b0b7 | 63 | |
0e74c023 | 64 | // peak=(int)(peak*.75); |
ba1a299c | 65 | for (i = 1; i < GraphTraceLen; ++i) |
66 | { | |
67 | // If this is the beginning of a peak | |
68 | if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak) | |
69 | { | |
70 | // Find lowest difference between peaks | |
71 | if (lastpeak && i - lastpeak < clock) | |
72 | clock = i - lastpeak; | |
73 | lastpeak = i; | |
74 | } | |
75 | } | |
76 | ||
77 | return clock; | |
7fe9b0b7 | 78 | } |
0e74c023 | 79 | */ |
d5a72d2f | 80 | /* |
81 | NOW IN LFDEMOD.C | |
0e74c023 | 82 | |
83 | // by marshmellow | |
84 | // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping) | |
85 | // maybe somehow adjust peak trimming value based on samples to fix? | |
d5a72d2f | 86 | int DetectASKClock(int peak) |
0e74c023 | 87 | { |
ba1a299c | 88 | int i=0; |
89 | int low=0; | |
90 | int clk[]={16,32,40,50,64,100,128,256}; | |
91 | int loopCnt = 256; | |
92 | if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen; | |
93 | if (!peak){ | |
94 | for (i=0;i<loopCnt;++i){ | |
95 | if(GraphBuffer[i]>peak){ | |
96 | peak = GraphBuffer[i]; | |
97 | } | |
98 | if(GraphBuffer[i]<low){ | |
99 | low = GraphBuffer[i]; | |
100 | } | |
101 | } | |
102 | peak=(int)(peak*.75); | |
103 | low= (int)(low*.75); | |
104 | } | |
105 | int ii; | |
106 | int clkCnt; | |
107 | int tol = 0; | |
108 | int bestErr=1000; | |
109 | int errCnt[]={0,0,0,0,0,0,0,0}; | |
110 | for(clkCnt=0; clkCnt<6;++clkCnt){ | |
111 | if (clk[clkCnt]==32){ | |
112 | tol=1; | |
113 | }else{ | |
114 | tol=0; | |
115 | } | |
116 | bestErr=1000; | |
117 | for (ii=0; ii<loopCnt; ++ii){ | |
118 | if ((GraphBuffer[ii]>=peak) || (GraphBuffer[ii]<=low)){ | |
119 | errCnt[clkCnt]=0; | |
120 | for (i=0; i<((int)(GraphTraceLen/clk[clkCnt])-1); ++i){ | |
121 | if (GraphBuffer[ii+(i*clk[clkCnt])]>=peak || GraphBuffer[ii+(i*clk[clkCnt])]<=low){ | |
122 | }else if(GraphBuffer[ii+(i*clk[clkCnt])-tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])-tol]<=low){ | |
123 | }else if(GraphBuffer[ii+(i*clk[clkCnt])+tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])+tol]<=low){ | |
124 | }else{ //error no peak detected | |
125 | errCnt[clkCnt]++; | |
126 | } | |
127 | } | |
128 | if(errCnt[clkCnt]==0) return clk[clkCnt]; | |
129 | if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt]; | |
130 | } | |
131 | } | |
132 | } | |
133 | int iii=0; | |
134 | int best=0; | |
135 | for (iii=0; iii<6;++iii){ | |
136 | if (errCnt[iii]<errCnt[best]){ | |
137 | best = iii; | |
138 | } | |
139 | } | |
140 | // 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]); | |
141 | return clk[best]; | |
0e74c023 | 142 | } |
d5a72d2f | 143 | */ |
ba1a299c | 144 | void setGraphBuf(uint8_t *buff, size_t size) |
d5a72d2f | 145 | { |
ba1a299c | 146 | int i=0; |
147 | ClearGraph(0); | |
148 | for (; i < size; ++i){ | |
149 | GraphBuffer[i]=buff[i]-128; | |
150 | } | |
151 | GraphTraceLen=size; | |
152 | RepaintGraphWindow(); | |
153 | return; | |
d5a72d2f | 154 | } |
ba1a299c | 155 | size_t getFromGraphBuf(uint8_t *buff) |
d5a72d2f | 156 | { |
ba1a299c | 157 | uint32_t i; |
158 | for (i=0;i<GraphTraceLen;++i){ | |
159 | if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim | |
160 | if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim | |
161 | buff[i]=(uint8_t)(GraphBuffer[i]+128); | |
162 | } | |
163 | return i; | |
d5a72d2f | 164 | } |
7fe9b0b7 | 165 | /* Get or auto-detect clock rate */ |
166 | int GetClock(const char *str, int peak, int verbose) | |
167 | { | |
ba1a299c | 168 | int clock; |
ba1a299c | 169 | sscanf(str, "%i", &clock); |
170 | if (!strcmp(str, "")) | |
171 | clock = 0; | |
172 | ||
173 | /* Auto-detect clock */ | |
174 | if (!clock) | |
175 | { | |
176 | uint8_t grph[MAX_GRAPH_TRACE_LEN]={0}; | |
177 | size_t size = getFromGraphBuf(grph); | |
178 | clock = DetectASKClock(grph,size,0); | |
ba1a299c | 179 | /* Only print this message if we're not looping something */ |
180 | if (!verbose){ | |
181 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
ba1a299c | 182 | } |
183 | } | |
184 | ||
185 | return clock; | |
7fe9b0b7 | 186 | } |
ba1a299c | 187 | |
4118b74d | 188 | int GetNRZpskClock(const char *str, int peak, int verbose) |
189 | { | |
ba1a299c | 190 | int clock; |
ba1a299c | 191 | sscanf(str, "%i", &clock); |
192 | if (!strcmp(str, "")) | |
193 | clock = 0; | |
194 | ||
195 | // Auto-detect clock | |
196 | if (!clock) | |
197 | { | |
198 | uint8_t grph[MAX_GRAPH_TRACE_LEN]={0}; | |
d6d20c54 | 199 | size_t size = getFromGraphBuf(grph); |
200 | clock = DetectpskNRZClock(grph,size,0); | |
ba1a299c | 201 | // Only print this message if we're not looping something |
202 | if (!verbose){ | |
203 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
ba1a299c | 204 | } |
205 | } | |
206 | return clock; | |
4118b74d | 207 | } |