]>
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> |
c6be64da | 12 | #include <stdbool.h> |
7fe9b0b7 | 13 | #include <string.h> |
14 | #include "ui.h" | |
15 | #include "graph.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; | |
a61b4976 | 24 | int half = (int)(clock/2); |
25 | int firstbit = bit ^ 1; | |
26 | ||
27 | for (i = 0; i < half; ++i) | |
28 | GraphBuffer[GraphTraceLen++] = firstbit; | |
7fe9b0b7 | 29 | |
a61b4976 | 30 | for (i = 0; i <= half; ++i) |
7fe9b0b7 | 31 | GraphBuffer[GraphTraceLen++] = bit; |
32 | ||
33 | if (redraw) | |
34 | RepaintGraphWindow(); | |
35 | } | |
36 | ||
37 | /* clear out our graph window */ | |
38 | int ClearGraph(int redraw) | |
39 | { | |
40 | int gtl = GraphTraceLen; | |
2ae8a312 | 41 | memset(GraphBuffer, 0x00, GraphTraceLen); |
7fe9b0b7 | 42 | |
2ae8a312 | 43 | GraphTraceLen = 0; |
44 | ||
7fe9b0b7 | 45 | if (redraw) |
46 | RepaintGraphWindow(); | |
47 | ||
48 | return gtl; | |
49 | } | |
50 | ||
51 | /* | |
52 | * Detect clock rate | |
53 | */ | |
54 | int DetectClock(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 | for (i = 1; i < GraphTraceLen; ++i) | |
67 | { | |
68 | /* If this is the beginning of a peak */ | |
69 | if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] == peak) | |
70 | { | |
71 | /* Find lowest difference between peaks */ | |
72 | if (lastpeak && i - lastpeak < clock) | |
73 | clock = i - lastpeak; | |
74 | lastpeak = i; | |
75 | } | |
76 | } | |
a61b4976 | 77 | |
78 | int clockmod = clock%8; | |
79 | if ( clockmod == 0) | |
80 | return clock; | |
81 | ||
82 | // When detected clock is 31 or 33 then return 32 | |
83 | ||
84 | printf("Found clock at %d ", clock); | |
85 | switch( clockmod ) | |
86 | { | |
87 | case 7: clock++; break; | |
88 | case 6: clock += 2 ; break; | |
89 | case 1: clock--; break; | |
90 | case 2: clock -= 2; break; | |
91 | } | |
c15d2bdc | 92 | if ( clock < 32) |
93 | clock = 32; | |
94 | ||
a61b4976 | 95 | printf("- adjusted it to %d \n", clock); |
96 | return clock; | |
7fe9b0b7 | 97 | } |
98 | ||
99 | /* Get or auto-detect clock rate */ | |
100 | int GetClock(const char *str, int peak, int verbose) | |
101 | { | |
102 | int clock; | |
103 | ||
104 | sscanf(str, "%i", &clock); | |
105 | if (!strcmp(str, "")) | |
106 | clock = 0; | |
107 | ||
108 | /* Auto-detect clock */ | |
109 | if (!clock) | |
110 | { | |
111 | clock = DetectClock(peak); | |
112 | /* Only print this message if we're not looping something */ | |
113 | if (!verbose) | |
114 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
115 | } | |
116 | ||
117 | return clock; | |
118 | } | |
c6be64da | 119 | |
120 | ||
121 | /* A simple test to see if there is any data inside Graphbuffer. | |
122 | */ | |
123 | bool HasGraphData(){ | |
124 | ||
125 | if ( GraphTraceLen <= 0) { | |
126 | PrintAndLog("No data available, try reading something first"); | |
127 | return false; | |
128 | } | |
129 | return true; | |
130 | } |