]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/graph.c
problems creating pull request with the new traces
[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"
d5a72d2f 15#include "lfdemod.h"
7fe9b0b7 16
17int GraphBuffer[MAX_GRAPH_TRACE_LEN];
18int GraphTraceLen;
19
20/* write a bit to the graph */
21void 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 */
36int 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 */
d5a72d2f 50 //decommissioned - has difficulty detecting rf/32
0e74c023 51/*
d5a72d2f 52int DetectClockOld(int peak)
7fe9b0b7 53{
54 int i;
55 int clock = 0xFFFF;
56 int lastpeak = 0;
57
0e74c023 58 // Detect peak if we don't have one
7fe9b0b7 59 if (!peak)
60 for (i = 0; i < GraphTraceLen; ++i)
61 if (GraphBuffer[i] > peak)
62 peak = GraphBuffer[i];
63
0e74c023 64 // peak=(int)(peak*.75);
7fe9b0b7 65 for (i = 1; i < GraphTraceLen; ++i)
66 {
0e74c023 67 // If this is the beginning of a peak
68 if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak)
7fe9b0b7 69 {
0e74c023 70 // Find lowest difference between peaks
7fe9b0b7 71 if (lastpeak && i - lastpeak < clock)
72 clock = i - lastpeak;
73 lastpeak = i;
74 }
75 }
76
77 return clock;
78}
0e74c023 79*/
d5a72d2f 80/*
81NOW IN LFDEMOD.C
0e74c023 82
83// by marshmellow
84// not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
85// maybe somehow adjust peak trimming value based on samples to fix?
d5a72d2f 86int DetectASKClock(int peak)
0e74c023 87{
88 int i=0;
89 int low=0;
90 int clk[]={16,32,40,50,64,100,128,256};
d5a72d2f 91 int loopCnt = 256;
92 if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen;
0e74c023 93 if (!peak){
d5a72d2f 94 for (i=0;i<loopCnt;++i){
0e74c023 95 if(GraphBuffer[i]>peak){
96 peak = GraphBuffer[i];
97 }
98 if(GraphBuffer[i]<low){
99 low = GraphBuffer[i];
100 }
101 }
102 peak=(int)(peak*.75);
103 low= (int)(low*.75);
104 }
0e74c023 105 int ii;
0e74c023 106 int clkCnt;
107 int tol = 0;
108 int bestErr=1000;
109 int errCnt[]={0,0,0,0,0,0,0,0};
0e74c023 110 for(clkCnt=0; clkCnt<6;++clkCnt){
111 if (clk[clkCnt]==32){
112 tol=1;
113 }else{
114 tol=0;
115 }
116 bestErr=1000;
117 for (ii=0; ii<loopCnt; ++ii){
118 if ((GraphBuffer[ii]>=peak) || (GraphBuffer[ii]<=low)){
d5a72d2f 119 errCnt[clkCnt]=0;
0e74c023 120 for (i=0; i<((int)(GraphTraceLen/clk[clkCnt])-1); ++i){
121 if (GraphBuffer[ii+(i*clk[clkCnt])]>=peak || GraphBuffer[ii+(i*clk[clkCnt])]<=low){
0e74c023 122 }else if(GraphBuffer[ii+(i*clk[clkCnt])-tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])-tol]<=low){
123 }else if(GraphBuffer[ii+(i*clk[clkCnt])+tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])+tol]<=low){
124 }else{ //error no peak detected
0e74c023 125 errCnt[clkCnt]++;
0e74c023 126 }
127 }
128 if(errCnt[clkCnt]==0) return clk[clkCnt];
129 if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt];
130 }
131 }
0e74c023 132 }
133 int iii=0;
134 int best=0;
135 for (iii=0; iii<6;++iii){
136 if (errCnt[iii]<errCnt[best]){
137 best = iii;
138 }
139 }
d5a72d2f 140 // PrintAndLog("DEBUG: clkCnt: %d, ii: %d, i: %d peak: %d, low: %d, errcnt: %d, errCnt64: %d",clkCnt,ii,i,peak,low,errCnt[best],errCnt[4]);
0e74c023 141 return clk[best];
142}
d5a72d2f 143*/
144void setGraphBuf(uint8_t *buff,int size)
145{
146 int i=0;
147 ClearGraph(0);
148 for (; i < size; ++i){
149 GraphBuffer[i]=buff[i];
150 }
151 GraphTraceLen=size;
152 RepaintGraphWindow();
153 return;
154}
155int getFromGraphBuf(uint8_t *buff)
156{
157 uint32_t i;
f822a063 158 for (i=0;i<GraphTraceLen;++i){
159 if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim
160 if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim
d5a72d2f 161 buff[i]=(uint8_t)(GraphBuffer[i]+128);
f822a063 162 }
d5a72d2f 163 return i;
164}
7fe9b0b7 165/* Get or auto-detect clock rate */
166int GetClock(const char *str, int peak, int verbose)
167{
168 int clock;
0e74c023 169// int clock2;
7fe9b0b7 170 sscanf(str, "%i", &clock);
171 if (!strcmp(str, ""))
172 clock = 0;
173
174 /* Auto-detect clock */
175 if (!clock)
176 {
d5a72d2f 177 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
178 int size = getFromGraphBuf(grph);
179 clock = DetectASKClock(grph,size,0);
0e74c023 180 //clock2 = DetectClock2(peak);
7fe9b0b7 181 /* Only print this message if we're not looping something */
0e74c023 182 if (!verbose){
7fe9b0b7 183 PrintAndLog("Auto-detected clock rate: %d", clock);
0e74c023 184 //PrintAndLog("clock2: %d",clock2);
185 }
7fe9b0b7 186 }
187
188 return clock;
189}
Impressum, Datenschutz