]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/ui.c
CHG: according to measureement by @wilrn the actual timing for receiving tag data...
[proxmark3-svn] / client / ui.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
212ef3a0 2// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
a553f267 3// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4//
5// This code is licensed to you under the terms of the GNU GPL, version 2 or,
6// at your option, any later version. See the LICENSE.txt file for the text of
7// the license.
8//-----------------------------------------------------------------------------
9// UI utilities
10//-----------------------------------------------------------------------------
11
7bd30f12 12#include "ui.h"
7fe9b0b7 13double CursorScaleFactor;
7ddb9900 14int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64;
7fe9b0b7 15int offline;
8d0a3e87 16int flushAfterWrite = 0;
9492e0b0 17extern pthread_mutex_t print_lock;
18
7fe9b0b7 19static char *logfilename = "proxmark3.log";
20
21void PrintAndLog(char *fmt, ...)
22{
51969283
M
23 char *saved_line;
24 int saved_point;
9492e0b0 25 va_list argptr, argptr2;
26 static FILE *logfile = NULL;
8d0a3e87 27 static int logging = 1;
7fe9b0b7 28
9492e0b0 29 // lock this section to avoid interlacing prints from different threats
30 pthread_mutex_lock(&print_lock);
31
32 if (logging && !logfile) {
8d0a3e87 33 logfile = fopen(logfilename, "a");
9492e0b0 34 if (!logfile) {
35 fprintf(stderr, "Can't open logfile, logging disabled!\n");
36 logging=0;
37 }
38 }
51969283
M
39
40 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
7fe9b0b7 41
51969283
M
42 if (need_hack) {
43 saved_point = rl_point;
44 saved_line = rl_copy_text(0, rl_end);
45 rl_save_prompt();
46 rl_replace_line("", 0);
47 rl_redisplay();
48 }
49
9492e0b0 50 va_start(argptr, fmt);
51 va_copy(argptr2, argptr);
52 vprintf(fmt, argptr);
53 printf(" "); // cleaning prompt
54 va_end(argptr);
55 printf("\n");
51969283
M
56
57 if (need_hack) {
58 rl_restore_prompt();
59 rl_replace_line(saved_line, 0);
60 rl_point = saved_point;
61 rl_redisplay();
62 free(saved_line);
63 }
64
9492e0b0 65 if (logging && logfile) {
66 vfprintf(logfile, fmt, argptr2);
67 fprintf(logfile,"\n");
68 fflush(logfile);
69 }
70 va_end(argptr2);
71
8d0a3e87 72 if (flushAfterWrite == 1) {
ed77aabe 73 fflush(NULL);
74 }
9492e0b0 75 //release lock
76 pthread_mutex_unlock(&print_lock);
7fe9b0b7 77}
78
d16b33fe 79void SetLogFilename(char *fn) {
7fe9b0b7 80 logfilename = fn;
81}
b44e5233 82
9e43f09a 83void iceIIR_Butterworth(int *data, const size_t len){
7bd30f12 84
85 int i,j;
149aeada 86
87 int * output = (int* ) malloc(sizeof(int) * len);
4c543dbd 88 if ( !output ) return;
89
90 // clear mem
149aeada 91 memset(output, 0x00, len);
081151ea 92
4c543dbd 93 size_t adjustedLen = len;
94 float fc = 0.1125f; // center frequency
95
7bd30f12 96 // create very simple low-pass filter to remove images (2nd-order Butterworth)
97 float complex iir_buf[3] = {0,0,0};
98 float b[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929};
99 float a[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023};
100
9e43f09a 101 float sample = 0; // input sample read from array
081151ea 102 float complex x_prime = 1.0f; // save sample for estimating frequency
7bd30f12 103 float complex x;
104
4c543dbd 105 for (i = 0; i < adjustedLen; ++i) {
7bd30f12 106
9e43f09a 107 sample = data[i];
7bd30f12 108
109 // remove DC offset and mix to complex baseband
110 x = (sample - 127.5f) * cexpf( _Complex_I * 2 * M_PI * fc * i );
111
112 // apply low-pass filter, removing spectral image (IIR using direct-form II)
113 iir_buf[2] = iir_buf[1];
114 iir_buf[1] = iir_buf[0];
115 iir_buf[0] = x - a[1]*iir_buf[1] - a[2]*iir_buf[2];
116 x = b[0]*iir_buf[0] +
117 b[1]*iir_buf[1] +
118 b[2]*iir_buf[2];
119
120 // compute instantaneous frequency by looking at phase difference
121 // between adjacent samples
122 float freq = cargf(x*conjf(x_prime));
123 x_prime = x; // retain this sample for next iteration
124
4c543dbd 125 output[i] =(freq > 0) ? 127 : -127;
7bd30f12 126 }
127
128 // show data
4c543dbd 129 //memcpy(data, output, adjustedLen);
081151ea 130 for (j=0; j<adjustedLen; ++j)
7bd30f12 131 data[j] = output[j];
4c543dbd 132
149aeada 133 free(output);
7bd30f12 134}
135
4c543dbd 136void iceSimple_Filter(int *data, const size_t len, uint8_t k){
137// ref: http://www.edn.com/design/systems-design/4320010/A-simple-software-lowpass-filter-suits-embedded-system-applications
138// parameter K
139#define FILTER_SHIFT 4
140
141 int32_t filter_reg = 0;
142 int16_t input, output;
143 int8_t shift = (k <=8 ) ? k : FILTER_SHIFT;
144
145 for (int i = 0; i < len; ++i){
146
147 input = data[i];
148 // Update filter with current sample
149 filter_reg = filter_reg - (filter_reg >> shift) + input;
150
151 // Scale output for unity gain
152 output = filter_reg >> shift;
153 data[i] = output;
154 }
155}
156
7bd30f12 157float complex cexpf (float complex Z)
158{
159 float complex Res;
160 double rho = exp (__real__ Z);
161 __real__ Res = rho * cosf(__imag__ Z);
162 __imag__ Res = rho * sinf(__imag__ Z);
163 return Res;
164}
Impressum, Datenschutz