]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/graph.c
LF Demod bug fixes and add lf em em410xdemod
[proxmark3-svn] / client / graph.c
CommitLineData
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>
12#include <string.h>
13#include "ui.h"
14#include "graph.h"
15
16int GraphBuffer[MAX_GRAPH_TRACE_LEN];
17int GraphTraceLen;
18
19/* write a bit to the graph */
20void AppendGraph(int redraw, int clock, int bit)
21{
22 int i;
23
24 for (i = 0; i < (int)(clock / 2); ++i)
25 GraphBuffer[GraphTraceLen++] = bit ^ 1;
26
27 for (i = (int)(clock / 2); i < clock; ++i)
28 GraphBuffer[GraphTraceLen++] = bit;
29
30 if (redraw)
31 RepaintGraphWindow();
32}
33
34/* clear out our graph window */
35int ClearGraph(int redraw)
36{
37 int gtl = GraphTraceLen;
38 GraphTraceLen = 0;
39
40 if (redraw)
41 RepaintGraphWindow();
42
43 return gtl;
44}
45
46/*
47 * Detect clock rate
48 */
0e74c023 49 //decommissioned - has difficulty detecting rf/32 and only works if data is manchester encoded
50/*
51int DetectClock2(int peak)
7fe9b0b7 52{
53 int i;
54 int clock = 0xFFFF;
55 int lastpeak = 0;
56
0e74c023 57 // Detect peak if we don't have one
7fe9b0b7 58 if (!peak)
59 for (i = 0; i < GraphTraceLen; ++i)
60 if (GraphBuffer[i] > peak)
61 peak = GraphBuffer[i];
62
0e74c023 63 // peak=(int)(peak*.75);
7fe9b0b7 64 for (i = 1; i < GraphTraceLen; ++i)
65 {
0e74c023 66 // If this is the beginning of a peak
67 if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak)
7fe9b0b7 68 {
0e74c023 69 // Find lowest difference between peaks
7fe9b0b7 70 if (lastpeak && i - lastpeak < clock)
71 clock = i - lastpeak;
72 lastpeak = i;
73 }
74 }
75
76 return clock;
77}
0e74c023 78*/
79
80// by marshmellow
81// not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
82// maybe somehow adjust peak trimming value based on samples to fix?
83int DetectClock(int peak)
84{
85 int i=0;
86 int low=0;
87 int clk[]={16,32,40,50,64,100,128,256};
88 if (!peak){
89 for (i=0;i<GraphTraceLen;++i){
90 if(GraphBuffer[i]>peak){
91 peak = GraphBuffer[i];
92 }
93 if(GraphBuffer[i]<low){
94 low = GraphBuffer[i];
95 }
96 }
97 peak=(int)(peak*.75);
98 low= (int)(low*.75);
99 }
100 //int numbits;
101 int ii;
102 int loopCnt = 256;
103 if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen;
104 int clkCnt;
105 int tol = 0;
106 int bestErr=1000;
107 int errCnt[]={0,0,0,0,0,0,0,0};
108 // int good;
109 for(clkCnt=0; clkCnt<6;++clkCnt){
110 if (clk[clkCnt]==32){
111 tol=1;
112 }else{
113 tol=0;
114 }
115 bestErr=1000;
116 for (ii=0; ii<loopCnt; ++ii){
117 if ((GraphBuffer[ii]>=peak) || (GraphBuffer[ii]<=low)){
118 //numbits=0;
119 //good=1;
120 errCnt[clkCnt]=0;
121 for (i=0; i<((int)(GraphTraceLen/clk[clkCnt])-1); ++i){
122 if (GraphBuffer[ii+(i*clk[clkCnt])]>=peak || GraphBuffer[ii+(i*clk[clkCnt])]<=low){
123 //numbits++;
124 }else if(GraphBuffer[ii+(i*clk[clkCnt])-tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])-tol]<=low){
125 }else if(GraphBuffer[ii+(i*clk[clkCnt])+tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])+tol]<=low){
126 }else{ //error no peak detected
127 //numbits=0;
128 //good=0;
129 errCnt[clkCnt]++;
130 //break;
131 }
132 }
133 if(errCnt[clkCnt]==0) return clk[clkCnt];
134 if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt];
135 }
136 }
137 errCnt[clkCnt]=bestErr;
138 }
139 int iii=0;
140 int best=0;
141 for (iii=0; iii<6;++iii){
142 if (errCnt[iii]<errCnt[best]){
143 best = iii;
144 }
145 }
146 PrintAndLog("clkCnt: %d, ii: %d, i: %d peak: %d, low: %d, errcnt: %d, errCnt64: %d",clkCnt,ii,i,peak,low,errCnt[best],errCnt[4]);
147 return clk[best];
148}
149
7fe9b0b7 150
151/* Get or auto-detect clock rate */
152int GetClock(const char *str, int peak, int verbose)
153{
154 int clock;
0e74c023 155// int clock2;
7fe9b0b7 156 sscanf(str, "%i", &clock);
157 if (!strcmp(str, ""))
158 clock = 0;
159
160 /* Auto-detect clock */
161 if (!clock)
162 {
163 clock = DetectClock(peak);
0e74c023 164 //clock2 = DetectClock2(peak);
7fe9b0b7 165 /* Only print this message if we're not looping something */
0e74c023 166 if (!verbose){
7fe9b0b7 167 PrintAndLog("Auto-detected clock rate: %d", clock);
0e74c023 168 //PrintAndLog("clock2: %d",clock2);
169 }
7fe9b0b7 170 }
171
172 return clock;
173}
Impressum, Datenschutz