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