]>
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 | } | |
92 | printf("- adjusted it to %d \n", clock); | |
93 | return clock; | |
7fe9b0b7 | 94 | } |
95 | ||
96 | /* Get or auto-detect clock rate */ | |
97 | int GetClock(const char *str, int peak, int verbose) | |
98 | { | |
99 | int clock; | |
100 | ||
101 | sscanf(str, "%i", &clock); | |
102 | if (!strcmp(str, "")) | |
103 | clock = 0; | |
104 | ||
105 | /* Auto-detect clock */ | |
106 | if (!clock) | |
107 | { | |
108 | clock = DetectClock(peak); | |
109 | /* Only print this message if we're not looping something */ | |
110 | if (!verbose) | |
111 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
112 | } | |
113 | ||
114 | return clock; | |
115 | } | |
c6be64da | 116 | |
117 | ||
118 | /* A simple test to see if there is any data inside Graphbuffer. | |
119 | */ | |
120 | bool HasGraphData(){ | |
121 | ||
122 | if ( GraphTraceLen <= 0) { | |
123 | PrintAndLog("No data available, try reading something first"); | |
124 | return false; | |
125 | } | |
126 | return true; | |
127 | } |