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