aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/display_iq.c
blob: 9df3725d4b63cc5b309bd72bdd5306734080d44c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* display IQ data form functions
 *
 * (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include "sample.h"
#include "sender.h"

#define DISPLAY_INTERVAL 0.04

/* must be odd value! */
#define SIZE	23

static char screen[SIZE][MAX_DISPLAY_WIDTH];
static char overdrive[SIZE][MAX_DISPLAY_WIDTH];
static int iq_on = 0;
static double db = 80;

static dispiq_t disp;

void display_iq_init(int samplerate)
{
	memset(&disp, 0, sizeof(disp));
	disp.interval_max = (double)samplerate * DISPLAY_INTERVAL + 0.5;
	/* should not happen due to low interval */
	if (disp.interval_max < MAX_DISPLAY_IQ - 1)
		disp.interval_max = MAX_DISPLAY_IQ - 1;
}

void display_iq_on(int on)
{
	int j;
	int w, h;

	get_win_size(&w, &h);

	if (iq_on) {
		memset(&screen, ' ', sizeof(screen));
		printf("\0337\033[H");
		for (j = 0; j < SIZE; j++) {
			screen[j][w] = '\0';
			puts(screen[j]);
		}
		printf("\0338"); fflush(stdout);
	}

	if (on < 0) {
		if (++iq_on == 3)
			iq_on = 0;
	} else
		iq_on = on;
}

void display_iq_limit_scroll(int on)
{
	int w, h;

	if (!iq_on)
		return;

	get_win_size(&w, &h);

	printf("\0337");
	printf("\033[%d;%dr", (on) ? SIZE + 1 : 1, h);
	printf("\0338");
}

/*
 * plot IQ data:
 *
 * theoretical example: SIZE = 3 allows 6 steps plotted as dots
 *
 * Line 0:   :
 * Line 1:   :
 * Line 2:   :
 *
 * The level of -1.0 .. 1.0 is scaled to -3 and 3.
 *
 * The lowest of the upper 3 dots ranges from 0.0 .. <1.5.
 * The upper most dot ranges from 2.5 .. <3.5.
 * The highest of the lower 3 dots ranges from <0.0 .. >-1.5;
 * The lower most dot ranges from -2.5 .. >-3.5.
 *
 * The center column ranges from -0.5 .. <0.5.
 * The columns about the center from -1.5 .. <1.5.
 */
void display_iq(float *samples, int length)
{
	int pos, max;
	float *buffer;
	int i, j, k;
	int color = 9; /* default color */
	int x_center, y_center;
	double I, Q, L, l, s;
	int x, y;
	int width, h;

	if (!iq_on)
		return;

	get_win_size(&width, &h);

	/* at what line we draw our zero-line and what character we use */
	x_center = width >> 1;
	y_center = (SIZE - 1) >> 1;

	pos = disp.interval_pos;
	max = disp.interval_max;
	buffer = disp.buffer;

	for (i = 0; i < length; i++) {
		if (pos >= MAX_DISPLAY_IQ) {
			if (++pos == max)
				pos = 0;
			continue;
		}
		buffer[pos * 2] = samples[i * 2];
		buffer[pos * 2 + 1] = samples[i * 2 + 1];
		pos++;
		if (pos == MAX_DISPLAY_IQ) {
			memset(&screen, ' ', sizeof(screen));
			memset(&overdrive, 0, sizeof(overdrive));
			for (j = 0; j < MAX_DISPLAY_IQ; j++) {
				I = buffer[j * 2];
				Q = buffer[j * 2 + 1];
				L = I*I + Q*Q;
				if (iq_on > 1) {
					/* logarithmic scale */
					l = sqrt(L);
					s = log10(l) * 20 + db;
					if (s < 0)
						s = 0;
					I = (I / l) * (s / db);
					Q = (Q / l) * (s / db);
				}
				x = x_center + (int)(I * (double)SIZE + (double)width + 0.5) - width;
				if (x < 0)
					continue;
				if (x > width - 1)
					continue;
				if (Q >= 0)
					y = SIZE - 1 - (int)(Q * (double)SIZE - 0.5);
				else
					y = SIZE - (int)(Q * (double)SIZE + 0.5);
				if (y < 0)
					continue;
				if (y > SIZE * 2 - 1)
					continue;
				if (screen[y/2][x] == ':')
					continue;
				if (screen[y/2][x] == '.') {
					if ((y & 1) == 0)
						screen[y/2][x] = ':';
					continue;
				}
				if (screen[y/2][x] == '\'') {
					if ((y & 1))
						screen[y/2][x] = ':';
					continue;
				}
				if ((y & 1) == 0)
					screen[y/2][x] = '\'';
				else
					screen[y/2][x] = '.';
				/* overdrive:
				 * 2 = close to -1..1 or above
				 * 1 = close to -0.5..0.5 or above
				 */
				if (L > 0.9)
					overdrive[y/2][x] = 2;
				else if (L > 0.45 && overdrive[y/2][x] < 1)
					overdrive[y/2][x] = 1;
			}
			if (iq_on == 1)
				sprintf(screen[0], "(IQ linear");
			else
				sprintf(screen[0], "(IQ log %.0f dB", db);
			*strchr(screen[0], '\0') = ')';
			printf("\0337\033[H");
			for (j = 0; j < SIZE; j++) {
				for (k = 0; k < width; k++) {
					if ((j == y_center || k == x_center) && screen[j][k] == ' ') {
						/* blue cross */
						if (color != 4) {
							color = 4;
							printf("\033[0;34m");
						}
						if (j == y_center) {
							if (k == x_center)
								putchar('o');
							else if (k == x_center - SIZE)
								putchar('+');
							else if (k == x_center + SIZE)
								putchar('+');
							else
								putchar('-');
						} else {
							if (j == 0 || j == SIZE - 1)
								putchar('+');
							else
								putchar('|');
						}
					} else if (screen[j][k] == ':' || screen[j][k] == '.' || screen[j][k] == '\'') {
						/* red / yellow / green plot */
						switch (overdrive[j][k]) {
						case 2:
							/* red  */
							if (color != 1) {
								color = 1;
								printf("\033[1;31m");
							}
							break;
						case 1:
							/* yellow */
							if (color != 3) {
								color = 3;
								printf("\033[1;33m");
							}
							break;
						default:
							/* green */
							if (color != 2) {
								color = 2;
								printf("\033[1;32m");
							}
						}
						putchar(screen[j][k]);
					} else if (screen[j][k] != ' ') {
						/* white other characters */
						if (color != 7) {
							color = 7;
							printf("\033[1;37m");
						}
						putchar(screen[j][k]);
					} else
						putchar(screen[j][k]);
				}
				printf("\n");
			}
			/* reset color and position */
			printf("\033[0;39m\0338"); fflush(stdout);
		}
	}

	disp.interval_pos = pos;
}