]>
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 <stdbool.h> | |
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; | |
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 | GraphTraceLen = 0; | |
40 | ||
41 | if (redraw) | |
42 | RepaintGraphWindow(); | |
43 | ||
44 | return gtl; | |
45 | } | |
46 | ||
47 | /* | |
48 | * Detect clock rate | |
49 | */ | |
50 | int DetectClock(int peak) | |
51 | { | |
52 | int i; | |
53 | int clock = 0xFFFF; | |
54 | int lastpeak = 0; | |
55 | ||
56 | /* Detect peak if we don't have one */ | |
57 | if (!peak) | |
58 | for (i = 0; i < GraphTraceLen; ++i) | |
59 | if (GraphBuffer[i] > peak) | |
60 | peak = GraphBuffer[i]; | |
61 | ||
62 | for (i = 1; i < GraphTraceLen; ++i) | |
63 | { | |
64 | /* If this is the beginning of a peak */ | |
65 | if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] == peak) | |
66 | { | |
67 | /* Find lowest difference between peaks */ | |
68 | if (lastpeak && i - lastpeak < clock) | |
69 | clock = i - lastpeak; | |
70 | lastpeak = i; | |
71 | } | |
72 | } | |
73 | ||
74 | return clock; | |
75 | } | |
76 | ||
77 | /* Get or auto-detect clock rate */ | |
78 | int GetClock(const char *str, int peak, int verbose) | |
79 | { | |
80 | int clock; | |
81 | ||
82 | sscanf(str, "%i", &clock); | |
83 | if (!strcmp(str, "")) | |
84 | clock = 0; | |
85 | ||
86 | /* Auto-detect clock */ | |
87 | if (!clock) | |
88 | { | |
89 | clock = DetectClock(peak); | |
90 | /* Only print this message if we're not looping something */ | |
91 | if (!verbose) | |
92 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
93 | } | |
94 | ||
95 | return clock; | |
96 | } | |
97 | ||
98 | ||
99 | /* A simple test to see if there is any data inside Graphbuffer. | |
100 | */ | |
101 | bool HasGraphData(){ | |
102 | ||
103 | if ( GraphTraceLen <= 0) { | |
104 | PrintAndLog("No data available, try reading something first"); | |
105 | return false; | |
106 | } | |
107 | return true; | |
108 | } |