forked from g0orx/pihpsdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeter.c
411 lines (344 loc) · 12.6 KB
/
meter.c
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* Copyright (C)
* 2015 - John Melton, G0ORX/N6LYT
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "meter.h"
#include "wdsp.h"
#include "radio.h"
#include "version.h"
#include "mode.h"
#ifdef FREEDV
#include "freedv.h"
#endif
#ifdef PSK
#include "psk.h"
#endif
#include "new_menu.h"
static GtkWidget *parent_window;
static GtkWidget *meter;
static cairo_surface_t *meter_surface = NULL;
static int meter_width;
static int meter_height;
static int last_meter_type=SMETER;
static int max_level=-200;
static int max_count=0;
static int max_reverse=0;
static void
meter_clear_surface (void)
{
cairo_t *cr;
cr = cairo_create (meter_surface);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_fill (cr);
cairo_destroy (cr);
}
meter_configure_event_cb (GtkWidget *widget,
GdkEventConfigure *event,
gpointer data)
{
if (meter_surface)
cairo_surface_destroy (meter_surface);
meter_surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
CAIRO_CONTENT_COLOR,
gtk_widget_get_allocated_width (widget),
gtk_widget_get_allocated_height (widget));
/* Initialize the surface to black */
cairo_t *cr;
cr = cairo_create (meter_surface);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
return TRUE;
}
/* Redraw the screen from the surface. Note that the ::draw
* signal receives a ready-to-be-used cairo_t that is already
* clipped to only draw the exposed areas of the widget
*/
static gboolean
meter_draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data) {
cairo_set_source_surface (cr, meter_surface, 0, 0);
cairo_paint (cr);
return TRUE;
}
/*
static void
smeter_select_cb (GtkWidget *widget,
gpointer data)
{
smeter=(int)data;
}
static void
alc_meter_select_cb (GtkWidget *widget,
gpointer data)
{
alc=(int)data;
}
*/
static gboolean
meter_press_event_cb (GtkWidget *widget,
GdkEventButton *event,
gpointer data)
{
start_meter();
/*
GtkWidget *dialog=gtk_dialog_new_with_buttons("Meter",GTK_WINDOW(parent_window),GTK_DIALOG_DESTROY_WITH_PARENT,NULL,NULL);
//gtk_window_set_decorated(GTK_WINDOW(dialog),FALSE);
GdkRGBA color;
color.red = 1.0;
color.green = 1.0;
color.blue = 1.0;
color.alpha = 1.0;
gtk_widget_override_background_color(dialog,GTK_STATE_FLAG_NORMAL,&color);
GtkWidget *content=gtk_dialog_get_content_area(GTK_DIALOG(dialog));
GtkWidget *grid=gtk_grid_new();
gtk_grid_set_column_homogeneous(GTK_GRID(grid),TRUE);
gtk_grid_set_row_homogeneous(GTK_GRID(grid),TRUE);
GtkWidget *smeter_peak=gtk_radio_button_new_with_label(NULL,"S Meter Peak");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (smeter_peak), smeter==RXA_S_PK);
gtk_widget_show(smeter_peak);
gtk_grid_attach(GTK_GRID(grid),smeter_peak,0,1,1,1);
g_signal_connect(smeter_peak,"pressed",G_CALLBACK(smeter_select_cb),(gpointer *)RXA_S_PK);
GtkWidget *smeter_average=gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(smeter_peak),"S Meter Average");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (smeter_average), smeter==RXA_S_AV);
gtk_widget_show(smeter_average);
gtk_grid_attach(GTK_GRID(grid),smeter_average,0,2,1,1);
g_signal_connect(smeter_average,"pressed",G_CALLBACK(smeter_select_cb),(gpointer *)RXA_S_AV);
GtkWidget *alc_peak=gtk_radio_button_new_with_label(NULL,"ALC Peak");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (alc_peak), alc==TXA_ALC_PK);
gtk_widget_show(alc_peak);
gtk_grid_attach(GTK_GRID(grid),alc_peak,1,1,1,1);
g_signal_connect(alc_peak,"pressed",G_CALLBACK(alc_meter_select_cb),(gpointer *)TXA_ALC_PK);
GtkWidget *alc_average=gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(alc_peak),"ALC Average");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (alc_average), alc==TXA_ALC_AV);
gtk_widget_show(alc_average);
gtk_grid_attach(GTK_GRID(grid),alc_average,1,2,1,1);
g_signal_connect(alc_average,"pressed",G_CALLBACK(alc_meter_select_cb),(gpointer *)TXA_ALC_AV);
GtkWidget *alc_gain=gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(alc_average),"ALC Gain");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (alc_gain), alc==TXA_ALC_GAIN);
gtk_widget_show(alc_gain);
gtk_grid_attach(GTK_GRID(grid),alc_gain,1,3,1,1);
g_signal_connect(alc_gain,"pressed",G_CALLBACK(alc_meter_select_cb),(gpointer *)TXA_ALC_GAIN);
gtk_container_add(GTK_CONTAINER(content),grid);
GtkWidget *close_button=gtk_dialog_add_button(GTK_DIALOG(dialog),"Close",GTK_RESPONSE_OK);
gtk_widget_override_font(close_button, pango_font_description_from_string("FreeMono 18"));
gtk_widget_show_all(dialog);
g_signal_connect_swapped (dialog,
"response",
G_CALLBACK (gtk_widget_destroy),
dialog);
int result=gtk_dialog_run(GTK_DIALOG(dialog));
*/
return TRUE;
}
GtkWidget* meter_init(int width,int height,GtkWidget *parent) {
fprintf(stderr,"meter_init: width=%d height=%d\n",width,height);
meter_width=width;
meter_height=height;
parent_window=parent;
meter = gtk_drawing_area_new ();
gtk_widget_set_size_request (meter, width, height);
/* Signals used to handle the backing surface */
g_signal_connect (meter, "draw",
G_CALLBACK (meter_draw_cb), NULL);
g_signal_connect (meter,"configure-event",
G_CALLBACK (meter_configure_event_cb), NULL);
/* Event signals */
g_signal_connect (meter, "button-press-event",
G_CALLBACK (meter_press_event_cb), NULL);
gtk_widget_set_events (meter, gtk_widget_get_events (meter)
| GDK_BUTTON_PRESS_MASK);
return meter;
}
void meter_update(int meter_type,double value,double reverse,double exciter,double alc) {
char text[128];
char sf[32];
int text_location;
double offset;
cairo_t *cr;
cr = cairo_create (meter_surface);
// clear the meter
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_paint (cr);
//sprintf(text,"Version: %s %s", build_date, version);
sprintf(text,"Version: %s", version);
cairo_select_font_face(cr, "FreeMono",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
cairo_set_font_size(cr, 12);
cairo_move_to(cr, 5, 15);
cairo_show_text(cr, text);
if(last_meter_type!=meter_type) {
last_meter_type=meter_type;
max_count=0;
if(meter_type==SMETER) {
max_level=-200;
} else {
max_level=0;
max_reverse=0;
}
}
cairo_set_source_rgb(cr, 1, 1, 1);
switch(meter_type) {
case SMETER:
// value is dBm
text_location=10;
offset=5.0;
double level=value+(double)get_attenuation();
if(meter_width>=114) {
//int db=meter_width/114; // S9+60 (9*6)+60
//if(db>2) db=2;
int db=1;
int i;
cairo_set_line_width(cr, 1.0);
cairo_set_source_rgb(cr, 1, 1, 1);
for(i=0;i<54;i++) {
cairo_move_to(cr,offset+(double)(i*db),(double)meter_height-10);
if(i%18==0) {
cairo_line_to(cr,offset+(double)(i*db),(double)(meter_height-20));
} else if(i%6==0) {
cairo_line_to(cr,offset+(double)(i*db),(double)(meter_height-15));
}
}
cairo_stroke(cr);
cairo_set_font_size(cr, 12);
cairo_move_to(cr, offset+(double)(18*db)-3.0, (double)meter_height);
cairo_show_text(cr, "3");
cairo_move_to(cr, offset+(double)(36*db)-3.0, (double)meter_height);
cairo_show_text(cr, "6");
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_move_to(cr,offset+(double)(54*db),(double)meter_height-10);
cairo_line_to(cr,offset+(double)(54*db),(double)(meter_height-20));
cairo_move_to(cr,offset+(double)(74*db),(double)meter_height-10);
cairo_line_to(cr,offset+(double)(74*db),(double)(meter_height-20));
cairo_move_to(cr,offset+(double)(94*db),(double)meter_height-10);
cairo_line_to(cr,offset+(double)(94*db),(double)(meter_height-20));
cairo_move_to(cr,offset+(double)(114*db),(double)meter_height-10);
cairo_line_to(cr,offset+(double)(114*db),(double)(meter_height-20));
cairo_stroke(cr);
cairo_move_to(cr, offset+(double)(54*db)-3.0, (double)meter_height);
cairo_show_text(cr, "9");
cairo_move_to(cr, offset+(double)(74*db)-12.0, (double)meter_height);
cairo_show_text(cr, "+20");
cairo_move_to(cr, offset+(double)(94*db)-9.0, (double)meter_height);
cairo_show_text(cr, "+40");
cairo_move_to(cr, offset+(double)(114*db)-6.0, (double)meter_height);
cairo_show_text(cr, "+60");
cairo_set_source_rgb(cr, 0, 1, 0);
cairo_rectangle(cr, offset+0.0, (double)(meter_height-40), (double)((level+127.0)*db), 20.0);
cairo_fill(cr);
if(level>max_level || max_count==10) {
max_level=level;
max_count=0;
}
if(max_level!=0) {
cairo_set_source_rgb(cr, 1, 1, 0);
cairo_move_to(cr,offset+(double)((max_level+127.0)*db),(double)meter_height-20);
cairo_line_to(cr,offset+(double)((max_level+127.0)*db),(double)(meter_height-40));
}
cairo_stroke(cr);
max_count++;
text_location=offset+(db*114)+5;
}
cairo_set_font_size(cr, 12);
sprintf(sf,"%d dBm",(int)level);
cairo_move_to(cr, text_location, meter_height-30);
cairo_show_text(cr, sf);
#ifdef FREEDV
if(active_receiver->mode==modeFREEDV) {
if(freedv_sync) {
cairo_set_source_rgb(cr, 0, 1, 0);
} else {
cairo_set_source_rgb(cr, 1, 0, 0);
}
cairo_set_font_size(cr, 12);
sprintf(sf,"SNR: %3.2f",freedv_snr);
cairo_move_to(cr, text_location, 30);
cairo_show_text(cr, sf);
}
#endif
break;
#ifdef PSK
case PSKMETER:
{
int i;
offset=5.0;
cairo_set_line_width(cr, 1.0);
cairo_set_source_rgb(cr, 1, 1, 1);
for(i=0;i<11;i++) {
cairo_move_to(cr,offset+(double)(i*20),(double)meter_height-10);
if((i%2)==0) {
cairo_line_to(cr,offset+(double)(i*20),(double)(meter_height-20));
cairo_move_to(cr,offset+(double)(i*20),(double)meter_height);
sprintf(sf,"%d",i*10);
cairo_show_text(cr, sf);
} else {
cairo_line_to(cr,offset+(double)(i*20),(double)(meter_height-15));
}
}
cairo_stroke(cr);
cairo_set_source_rgb(cr, 0, 1, 0);
cairo_rectangle(cr, offset+0.0, (double)(meter_height-40), value*2, 20.0);
cairo_fill(cr);
cairo_set_source_rgb(cr, 0, 1, 0);
cairo_set_font_size(cr, 12);
sprintf(sf,"Level: %d",(int)value);
cairo_move_to(cr, 210, 45);
cairo_show_text(cr, sf);
}
break;
#endif
case POWER:
// value is Watts
cairo_select_font_face(cr, "FreeMono",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 12);
if((int)value>max_level || max_count==10) {
max_level=(int)value;
max_count=0;
}
max_count++;
sprintf(sf,"FWD: %d W",(int)max_level);
cairo_move_to(cr, 10, 35);
cairo_show_text(cr, sf);
double swr=(max_level+reverse)/(max_level-reverse);
cairo_select_font_face(cr, "FreeMono",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 12);
sprintf(sf,"SWR: %1.1f:1",swr);
cairo_move_to(cr, 10, 55);
cairo_show_text(cr, sf);
sprintf(sf,"ALC: %2.1f dB",alc);
cairo_move_to(cr, meter_width/2, 35);
cairo_show_text(cr, sf);
/*
sprintf(sf,"REV: %3.2f W",reverse);
cairo_move_to(cr, 10, 45);
cairo_show_text(cr, sf);
*/
break;
}
cairo_destroy(cr);
gtk_widget_queue_draw (meter);
}