]>
Commit | Line | Data |
---|---|---|
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 | 13 | double CursorScaleFactor; |
7ddb9900 | 14 | int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64; |
7fe9b0b7 | 15 | int offline; |
8d0a3e87 | 16 | int flushAfterWrite = 0; |
9492e0b0 | 17 | extern pthread_mutex_t print_lock; |
18 | ||
7fe9b0b7 | 19 | static char *logfilename = "proxmark3.log"; |
20 | ||
21 | void 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; |
2d3f8e5f | 28 | // time_t current_time; |
29 | // struct tm* tm_info; | |
30 | // char buffer[26] = {0}; | |
31 | ||
9492e0b0 | 32 | // lock this section to avoid interlacing prints from different threats |
33 | pthread_mutex_lock(&print_lock); | |
34 | ||
35 | if (logging && !logfile) { | |
8d0a3e87 | 36 | logfile = fopen(logfilename, "a"); |
9492e0b0 | 37 | if (!logfile) { |
38 | fprintf(stderr, "Can't open logfile, logging disabled!\n"); | |
39 | logging=0; | |
40 | } | |
41 | } | |
51969283 M |
42 | |
43 | int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0; | |
7fe9b0b7 | 44 | |
51969283 M |
45 | if (need_hack) { |
46 | saved_point = rl_point; | |
47 | saved_line = rl_copy_text(0, rl_end); | |
48 | rl_save_prompt(); | |
49 | rl_replace_line("", 0); | |
50 | rl_redisplay(); | |
51 | } | |
52 | ||
9492e0b0 | 53 | va_start(argptr, fmt); |
54 | va_copy(argptr2, argptr); | |
55 | vprintf(fmt, argptr); | |
56 | printf(" "); // cleaning prompt | |
57 | va_end(argptr); | |
58 | printf("\n"); | |
51969283 M |
59 | |
60 | if (need_hack) { | |
61 | rl_restore_prompt(); | |
62 | rl_replace_line(saved_line, 0); | |
63 | rl_point = saved_point; | |
64 | rl_redisplay(); | |
65 | free(saved_line); | |
66 | } | |
67 | ||
9492e0b0 | 68 | if (logging && logfile) { |
2d3f8e5f | 69 | |
70 | /* | |
71 | // Obtain current time. | |
72 | current_time = time(NULL); | |
73 | // Convert to local time format. | |
74 | tm_info = localtime(¤t_time); | |
75 | strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info); | |
76 | fprintf(logfile, "%s ", buffer); | |
77 | */ | |
78 | ||
9492e0b0 | 79 | vfprintf(logfile, fmt, argptr2); |
80 | fprintf(logfile,"\n"); | |
81 | fflush(logfile); | |
82 | } | |
83 | va_end(argptr2); | |
84 | ||
8d0a3e87 | 85 | if (flushAfterWrite == 1) { |
ed77aabe | 86 | fflush(NULL); |
87 | } | |
9492e0b0 | 88 | //release lock |
89 | pthread_mutex_unlock(&print_lock); | |
7fe9b0b7 | 90 | } |
91 | ||
d16b33fe | 92 | void SetLogFilename(char *fn) { |
2dcf60f3 | 93 | logfilename = fn; |
7fe9b0b7 | 94 | } |
b44e5233 | 95 | |
9e43f09a | 96 | void iceIIR_Butterworth(int *data, const size_t len){ |
7bd30f12 | 97 | |
98 | int i,j; | |
149aeada | 99 | |
100 | int * output = (int* ) malloc(sizeof(int) * len); | |
4c543dbd | 101 | if ( !output ) return; |
102 | ||
103 | // clear mem | |
149aeada | 104 | memset(output, 0x00, len); |
081151ea | 105 | |
4c543dbd | 106 | size_t adjustedLen = len; |
107 | float fc = 0.1125f; // center frequency | |
108 | ||
7bd30f12 | 109 | // create very simple low-pass filter to remove images (2nd-order Butterworth) |
110 | float complex iir_buf[3] = {0,0,0}; | |
111 | float b[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929}; | |
112 | float a[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023}; | |
113 | ||
9e43f09a | 114 | float sample = 0; // input sample read from array |
081151ea | 115 | float complex x_prime = 1.0f; // save sample for estimating frequency |
7bd30f12 | 116 | float complex x; |
117 | ||
4c543dbd | 118 | for (i = 0; i < adjustedLen; ++i) { |
7bd30f12 | 119 | |
9e43f09a | 120 | sample = data[i]; |
7bd30f12 | 121 | |
122 | // remove DC offset and mix to complex baseband | |
123 | x = (sample - 127.5f) * cexpf( _Complex_I * 2 * M_PI * fc * i ); | |
124 | ||
125 | // apply low-pass filter, removing spectral image (IIR using direct-form II) | |
126 | iir_buf[2] = iir_buf[1]; | |
127 | iir_buf[1] = iir_buf[0]; | |
128 | iir_buf[0] = x - a[1]*iir_buf[1] - a[2]*iir_buf[2]; | |
129 | x = b[0]*iir_buf[0] + | |
130 | b[1]*iir_buf[1] + | |
131 | b[2]*iir_buf[2]; | |
132 | ||
133 | // compute instantaneous frequency by looking at phase difference | |
134 | // between adjacent samples | |
135 | float freq = cargf(x*conjf(x_prime)); | |
136 | x_prime = x; // retain this sample for next iteration | |
137 | ||
4c543dbd | 138 | output[i] =(freq > 0) ? 127 : -127; |
7bd30f12 | 139 | } |
140 | ||
141 | // show data | |
4c543dbd | 142 | //memcpy(data, output, adjustedLen); |
081151ea | 143 | for (j=0; j<adjustedLen; ++j) |
7bd30f12 | 144 | data[j] = output[j]; |
4c543dbd | 145 | |
149aeada | 146 | free(output); |
7bd30f12 | 147 | } |
148 | ||
4c543dbd | 149 | void iceSimple_Filter(int *data, const size_t len, uint8_t k){ |
150 | // ref: http://www.edn.com/design/systems-design/4320010/A-simple-software-lowpass-filter-suits-embedded-system-applications | |
151 | // parameter K | |
152 | #define FILTER_SHIFT 4 | |
153 | ||
154 | int32_t filter_reg = 0; | |
155 | int16_t input, output; | |
156 | int8_t shift = (k <=8 ) ? k : FILTER_SHIFT; | |
157 | ||
158 | for (int i = 0; i < len; ++i){ | |
159 | ||
160 | input = data[i]; | |
161 | // Update filter with current sample | |
162 | filter_reg = filter_reg - (filter_reg >> shift) + input; | |
163 | ||
164 | // Scale output for unity gain | |
165 | output = filter_reg >> shift; | |
166 | data[i] = output; | |
167 | } | |
168 | } | |
169 | ||
7bd30f12 | 170 | float complex cexpf (float complex Z) |
171 | { | |
172 | float complex Res; | |
173 | double rho = exp (__real__ Z); | |
174 | __real__ Res = rho * cosf(__imag__ Z); | |
175 | __imag__ Res = rho * sinf(__imag__ Z); | |
176 | return Res; | |
177 | } |