Merge branch 'master' of https://github.com/Proxmark/proxmark3
[legacy-proxmark3.git] / client / graph.c
blob6362c8fe242dc438914b5f34135db9874737b91d
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 //-----------------------------------------------------------------------------
11 #include <stdio.h>
12 #include <string.h>
13 #include "ui.h"
14 #include "graph.h"
15 #include "lfdemod.h"
17 int GraphBuffer[MAX_GRAPH_TRACE_LEN];
18 int GraphTraceLen;
20 /* write a bit to the graph */
21 void AppendGraph(int redraw, int clock, int bit)
23 int i;
25 for (i = 0; i < (int)(clock / 2); ++i)
26 GraphBuffer[GraphTraceLen++] = bit ^ 1;
28 for (i = (int)(clock / 2); i < clock; ++i)
29 GraphBuffer[GraphTraceLen++] = bit;
31 if (redraw)
32 RepaintGraphWindow();
35 // clear out our graph window
36 int ClearGraph(int redraw)
38 int gtl = GraphTraceLen;
39 memset(GraphBuffer, 0x00, GraphTraceLen);
41 GraphTraceLen = 0;
43 if (redraw)
44 RepaintGraphWindow();
46 return gtl;
49 // DETECT CLOCK NOW IN LFDEMOD.C
51 void setGraphBuf(uint8_t *buff, size_t size)
53 int i=0;
54 ClearGraph(0);
55 for (; i < size; ++i){
56 GraphBuffer[i]=buff[i]-128;
58 GraphTraceLen=size;
59 RepaintGraphWindow();
60 return;
62 size_t getFromGraphBuf(uint8_t *buff)
64 uint32_t i;
65 for (i=0;i<GraphTraceLen;++i){
66 if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim
67 if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim
68 buff[i]=(uint8_t)(GraphBuffer[i]+128);
70 return i;
72 // Get or auto-detect clock rate
73 int GetClock(const char *str, int peak, int verbose)
75 int clock;
76 sscanf(str, "%i", &clock);
77 if (!strcmp(str, ""))
78 clock = 0;
80 // Auto-detect clock
81 if (!clock)
83 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
84 size_t size = getFromGraphBuf(grph);
85 clock = DetectASKClock(grph,size,0);
86 // Only print this message if we're not looping something
87 if (!verbose){
88 PrintAndLog("Auto-detected clock rate: %d", clock);
92 return clock;
95 int GetNRZpskClock(const char *str, int peak, int verbose)
97 int clock;
98 sscanf(str, "%i", &clock);
99 if (!strcmp(str, ""))
100 clock = 0;
102 // Auto-detect clock
103 if (!clock)
105 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
106 size_t size = getFromGraphBuf(grph);
107 clock = DetectpskNRZClock(grph,size,0);
108 // Only print this message if we're not looping something
109 if (!verbose){
110 PrintAndLog("Auto-detected clock rate: %d", clock);
113 return clock;