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