]>
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" | |
6ff6ade2 | 16 | #include "lfdemod.h" |
7fe9b0b7 | 17 | |
18 | int GraphBuffer[MAX_GRAPH_TRACE_LEN]; | |
19 | int GraphTraceLen; | |
20 | ||
21 | /* write a bit to the graph */ | |
22 | void AppendGraph(int redraw, int clock, int bit) | |
23 | { | |
24 | int i; | |
a61b4976 | 25 | int half = (int)(clock/2); |
26 | int firstbit = bit ^ 1; | |
27 | ||
28 | for (i = 0; i < half; ++i) | |
29 | GraphBuffer[GraphTraceLen++] = firstbit; | |
7fe9b0b7 | 30 | |
a61b4976 | 31 | for (i = 0; i <= half; ++i) |
7fe9b0b7 | 32 | GraphBuffer[GraphTraceLen++] = bit; |
33 | ||
34 | if (redraw) | |
35 | RepaintGraphWindow(); | |
36 | } | |
37 | ||
38 | /* clear out our graph window */ | |
39 | int ClearGraph(int redraw) | |
40 | { | |
41 | int gtl = GraphTraceLen; | |
2ae8a312 | 42 | memset(GraphBuffer, 0x00, GraphTraceLen); |
7fe9b0b7 | 43 | |
2ae8a312 | 44 | GraphTraceLen = 0; |
45 | ||
7fe9b0b7 | 46 | if (redraw) |
47 | RepaintGraphWindow(); | |
48 | ||
49 | return gtl; | |
50 | } | |
51 | ||
8d0a3e87 | 52 | void SetGraphBuf(uint8_t *buff, int size) |
7fe9b0b7 | 53 | { |
8d0a3e87 | 54 | if ( buff == NULL ) return; |
a61b4976 | 55 | |
8d0a3e87 | 56 | uint16_t i = 0; |
57 | if ( size > MAX_GRAPH_TRACE_LEN ) | |
58 | size = MAX_GRAPH_TRACE_LEN; | |
59 | ClearGraph(0); | |
60 | for (; i < size; ++i){ | |
61 | GraphBuffer[i] = buff[i]; | |
62 | } | |
63 | GraphTraceLen = size; | |
64 | RepaintGraphWindow(); | |
65 | return; | |
6ff6ade2 | 66 | } |
8d0a3e87 | 67 | |
68 | // Copies grahpbuff to buff. | |
69 | // while triming values to the range -127 -- 127. | |
70 | int GetFromGraphBuf(uint8_t *buff) | |
6ff6ade2 | 71 | { |
8d0a3e87 | 72 | if ( buff == NULL ) return -1; |
73 | uint32_t i = 0; | |
74 | ||
75 | for (; i < GraphTraceLen; ++i){ | |
76 | ||
77 | // trim upper and lower values. | |
78 | if (GraphBuffer[i] > 127) | |
79 | GraphBuffer[i] = 127; | |
80 | else if (GraphBuffer[i] < -127) | |
81 | GraphBuffer[i] = -127; | |
82 | ||
83 | buff[i] = (uint8_t)(GraphBuffer[i] + 128); | |
84 | } | |
85 | return i; | |
6ff6ade2 | 86 | } |
7fe9b0b7 | 87 | /* Get or auto-detect clock rate */ |
8d0a3e87 | 88 | int GetClock(const char *str, int verbose) |
7fe9b0b7 | 89 | { |
8d0a3e87 | 90 | int clock; |
91 | ||
92 | sscanf(str, "%i", &clock); | |
93 | if (!strcmp(str, "")) | |
94 | clock = 0; | |
95 | ||
96 | /* Auto-detect clock */ | |
97 | if (!clock) { | |
98 | ||
99 | uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0x00}; | |
100 | int size = GetFromGraphBuf(grph); | |
101 | if ( size < 0 ) { | |
102 | PrintAndLog("Failed to copy from graphbuffer"); | |
103 | return -1; | |
104 | } | |
105 | clock = DetectASKClock(grph, size, 0); | |
106 | ||
107 | /* Only print this message if we're not looping something */ | |
108 | if (verbose) | |
109 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
110 | } | |
111 | return clock; | |
7fe9b0b7 | 112 | } |
c6be64da | 113 | |
8d0a3e87 | 114 | // A simple test to see if there is any data inside Graphbuffer. |
c6be64da | 115 | bool HasGraphData(){ |
116 | ||
117 | if ( GraphTraceLen <= 0) { | |
118 | PrintAndLog("No data available, try reading something first"); | |
119 | return false; | |
120 | } | |
121 | return true; | |
8d0a3e87 | 122 | } |
123 | ||
124 | // Detect high and lows in Grapbuffer. | |
125 | // Only loops the first 256 values. | |
126 | void DetectHighLowInGraph(int *high, int *low, bool addFuzz) { | |
127 | ||
128 | uint8_t loopMax = 255; | |
129 | if ( loopMax > GraphTraceLen) | |
130 | loopMax = GraphTraceLen; | |
131 | ||
132 | for (uint8_t i = 0; i < loopMax; ++i) { | |
133 | if (GraphBuffer[i] > *high) | |
134 | *high = GraphBuffer[i]; | |
135 | else if (GraphBuffer[i] < *low) | |
136 | *low = GraphBuffer[i]; | |
137 | } | |
138 | ||
139 | //12% fuzz in case highs and lows aren't clipped | |
140 | if (addFuzz) { | |
141 | *high = (int)(*high * .88); | |
142 | *low = (int)(*low * .88); | |
143 | } | |
c6be64da | 144 | } |