summaryrefslogtreecommitdiff
path: root/gnuradio-examples/python/apps/hf_explorer/hfx2.py
blob: d780223b8fe86cecd336470af3f9f220903e4969 (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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
#!/usr/bin/env python
# -*- coding: ANSI_X3.4-1968 -*-
# generated by wxGlade 0.4 on Tue Mar 14 10:16:06 2006
#
# Copyright 2006 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio 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, or (at your option)
# any later version.
#
# GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
#-----------------------------------------------------------------
#
#                 +-->(fft)
#                 |
# (src)->(xlate)--+->(audio filter)--+-->(sel_am)-+--------------+
#                                    |            |              |
#                                    |          (pll)            |
#                                    |            |              |
#                                    |   (pll_carrier_scale)     |
#                                    |            |              |
#                                    |   (pll_carrier_filter)    |
#                                    |            |              |
#                                    |            +--(fft2)      |
#                                    |            |              |
#                                    |       +--(c2f3)--+        |
#                                    |       |          |        |
#                                    |   (phaser1)  (phaser2)    |
#                                    |       |          |        |
#                                    |       +--(f2c)---+        |
#                                    |            |              V
#                                    V            +---------->(am_det)
#                                  (c2f)                         |
#                                    |                         (c2f2)
#                                    |                           |
#                                    +-->(sel_sb)------------>(combine)
#                                                                |
#                                                                V
#                                  +--------------------------(scale)
#                                  |                             |
#                                  |                             |
#                                  |                            +++
#                                  V                            | |
#                                (agc)<--(offset)<--(intr)<---(sqr1)
#                                  |
#                                  V
#                                (dst)
#                                                       
#----------------------------------------------------------------------
#
# Versions 2.2.1 adds loop antenna automatic tuner
#
# 2.3.1 adds web control, made AM Sync display optional,
# added more comments.
#
# 2.4.1 updates usrp interface to support auto subdev
# 
# 2.8.1 changed saved file format from 8-byte complex to
# 4-byte short for obvious storage space savings.

# Web server control disabled by default. Do not enable
# until directory structure and scripts are in place.
WEB_CONTROL = False

# Controls display of AM Sync Carrier - turn off for smaller
# window if not needed
AM_SYNC_DISPLAY = False

import os, wx, sys, math
import wx.lib.evtmgr as em
from gnuradio.wxgui import powermate, fftsink
from gnuradio import gr, audio, eng_notation, usrp, gru
from gnuradio.eng_option import eng_option
from optparse import OptionParser

ID_BUTTON_1 = wx.NewId()	# LSB button
ID_BUTTON_2 = wx.NewId()	# USB
ID_BUTTON_3 = wx.NewId()	# AM
ID_BUTTON_4 = wx.NewId()	# CW
ID_BUTTON_5 = wx.NewId()	# Powermate controls: Upper audio freq cutoff
ID_BUTTON_6 = wx.NewId()	#  "                  Lower audio freq cutoff
ID_BUTTON_7 = wx.NewId()	#  "                  Frequency 
ID_BUTTON_8 = wx.NewId()	#  "                  Volume
ID_BUTTON_9 = wx.NewId()	#  "                  Time
ID_BUTTON_10 = wx.NewId()	# Time Seek Forwards
ID_BUTTON_11 = wx.NewId()	# Time Seek Backwards
ID_BUTTON_12 = wx.NewId()	# Automatic Antenna Tune (AT) enable
ID_BUTTON_13 = wx.NewId()	# AT Calibrate point
ID_BUTTON_14 = wx.NewId()	# AT Reset
ID_TEXT_1 = wx.NewId()		# Band Center, USRP ddc Freq
ID_SPIN_1 = wx.NewId()		# Frequency display and control
ID_SLIDER_1 = wx.NewId()	# Upper audio freq cutoff
ID_SLIDER_2 = wx.NewId()	# Lower audio freq cutoff
ID_SLIDER_3 = wx.NewId()	# Frequency 
ID_SLIDER_4 = wx.NewId()	# Volume
ID_SLIDER_5 = wx.NewId()	# Programmable Gain Amp, PGA, RF gain
ID_SLIDER_6 = wx.NewId()	# AM Sync carrier level
ID_SLIDER_7 = wx.NewId()	# AT control voltage output
ID_EXIT	= wx.NewId()		# Menu Exit


def pick_subdevice(u):
    """
    The user didn't specify a subdevice on the command line.
    If there's a daughterboard on A, select A.
    If there's a daughterboard on B, select B.
    Otherwise, select A.
    """
    if u.db[0][0].dbid() >= 0:       # dbid is < 0 if there's no d'board or a problem
        return (0, 0)
    if u.db[1][0].dbid() >= 0:
        return (1, 0)
    return (0, 0)


class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        
        # Menu Bar
        self.frame_1_menubar = wx.MenuBar()
        self.SetMenuBar(self.frame_1_menubar)
        wxglade_tmp_menu = wx.Menu()
        self.Exit = wx.MenuItem(wxglade_tmp_menu, ID_EXIT, "Exit", "Exit", wx.ITEM_NORMAL)
        wxglade_tmp_menu.AppendItem(self.Exit)
        self.frame_1_menubar.Append(wxglade_tmp_menu, "File")
        # Menu Bar end
        self.panel_1 = wx.Panel(self, -1)
        self.button_1 = wx.Button(self, ID_BUTTON_1, "LSB")
        self.button_2 = wx.Button(self, ID_BUTTON_2, "USB")
        self.button_3 = wx.Button(self, ID_BUTTON_3, "AM")
        self.button_4 = wx.Button(self, ID_BUTTON_4, "CW")
        self.button_5 = wx.ToggleButton(self, ID_BUTTON_5, "Upper")
        self.slider_1 = wx.Slider(self, ID_SLIDER_1, 0, -15799, 15799, style=wx.SL_HORIZONTAL|wx.SL_LABELS)
        self.button_6 = wx.ToggleButton(self, ID_BUTTON_6, "Lower")
        self.slider_2 = wx.Slider(self, ID_SLIDER_2, 0, -15799, 15799, style=wx.SL_HORIZONTAL|wx.SL_LABELS)
        self.panel_5 = wx.Panel(self, -1)
        self.label_1 = wx.StaticText(self, -1, " Band\nCenter")
        self.text_ctrl_1 = wx.TextCtrl(self, ID_TEXT_1, "")
        self.panel_6 = wx.Panel(self, -1)
        self.panel_7 = wx.Panel(self, -1)
        self.panel_2 = wx.Panel(self, -1)
        self.button_7 = wx.ToggleButton(self, ID_BUTTON_7, "Freq")
        self.slider_3 = wx.Slider(self, ID_SLIDER_3, 3000, 0, 6000)
        self.spin_ctrl_1 = wx.SpinCtrl(self, ID_SPIN_1, "", min=0, max=100)
        self.button_8 = wx.ToggleButton(self, ID_BUTTON_8, "Vol")
        self.slider_4 = wx.Slider(self, ID_SLIDER_4, 0, 0, 500)
        self.slider_5 = wx.Slider(self, ID_SLIDER_5, 0, 0, 20)
        self.button_9 = wx.ToggleButton(self, ID_BUTTON_9, "Time")
        self.button_11 = wx.Button(self, ID_BUTTON_11, "Rew")
        self.button_10 = wx.Button(self, ID_BUTTON_10, "Fwd")
        self.panel_3 = wx.Panel(self, -1)
        self.label_2 = wx.StaticText(self, -1, "PGA               ")
        self.panel_4 = wx.Panel(self, -1)
        self.panel_8 = wx.Panel(self, -1)
        self.panel_9 = wx.Panel(self, -1)
        self.label_3 = wx.StaticText(self, -1, "AM Sync\nCarrier")
        self.slider_6 = wx.Slider(self, ID_SLIDER_6, 50, 0, 200, style=wx.SL_HORIZONTAL|wx.SL_LABELS)
        self.label_4 = wx.StaticText(self, -1, "Antenna Tune")
        self.slider_7 = wx.Slider(self, ID_SLIDER_7, 1575, 950, 2200, style=wx.SL_HORIZONTAL|wx.SL_LABELS)
        self.panel_10 = wx.Panel(self, -1)
        self.button_12 = wx.ToggleButton(self, ID_BUTTON_12, "Auto Tune")
        self.button_13 = wx.Button(self, ID_BUTTON_13, "Calibrate")
        self.button_14 = wx.Button(self, ID_BUTTON_14, "Reset")
        self.panel_11 = wx.Panel(self, -1)
        self.panel_12 = wx.Panel(self, -1)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

        parser = OptionParser (option_class=eng_option)
        parser.add_option ("-c", "--ddc-freq", type="eng_float", default=3.9e6,
                           help="set Rx DDC frequency to FREQ", metavar="FREQ")
        parser.add_option ("-a", "--audio_file", default="",
                           help="audio output file", metavar="FILE")
        parser.add_option ("-r", "--radio_file", default="",
                           help="radio output file", metavar="FILE")
        parser.add_option ("-i", "--input_file", default="",
                           help="radio input file", metavar="FILE")
        parser.add_option ("-d", "--decim", type="int", default=250,
                           help="USRP decimation")
        parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,                          help="select USRP Rx side A or B (default=first one with a daughterboard)")
        (options, args) = parser.parse_args ()

        self.usrp_center = options.ddc_freq
        usb_rate = 64e6 / options.decim
        self.slider_range = usb_rate * 0.9375
        self.f_lo = self.usrp_center - (self.slider_range/2)
        self.f_hi = self.usrp_center + (self.slider_range/2)
	self.af_sample_rate = 32000
	fir_decim = long (usb_rate / self.af_sample_rate)

        # data point arrays for antenna tuner
        self.xdata = []
        self.ydata = []

	self.fg = gr.flow_graph()

        # radio variables, initial conditions
        self.frequency = self.usrp_center
        # these map the frequency slider (0-6000) to the actual range
        self.f_slider_offset = self.f_lo
        self.f_slider_scale = 10000/options.decim
        self.spin_ctrl_1.SetRange(self.f_lo,self.f_hi)
        self.text_ctrl_1.SetValue(str(int(self.usrp_center)))
        self.slider_5.SetValue(0) 
	self.AM_mode = False

        self.slider_3.SetValue((self.frequency-self.f_slider_offset)/self.f_slider_scale)
        self.spin_ctrl_1.SetValue(int(self.frequency))

        POWERMATE = True
        try:
            self.pm = powermate.powermate(self)
        except:
            sys.stderr.write("Unable to find PowerMate or Contour Shuttle\n")
            POWERMATE = False

        if POWERMATE:
          powermate.EVT_POWERMATE_ROTATE(self, self.on_rotate)
          powermate.EVT_POWERMATE_BUTTON(self, self.on_pmButton)
	self.active_button = 7

        # command line options
        if options.audio_file == "": SAVE_AUDIO_TO_FILE = False
        else: SAVE_AUDIO_TO_FILE = True
        if options.radio_file == "": SAVE_RADIO_TO_FILE = False
        else: SAVE_RADIO_TO_FILE = True
        if options.input_file == "": self.PLAY_FROM_USRP = True
        else: self.PLAY_FROM_USRP = False

        if self.PLAY_FROM_USRP:
           self.src = usrp.source_s(decim_rate=options.decim)
	   if options.rx_subdev_spec is None:
              options.rx_subdev_spec = pick_subdevice(self.src)
           self.src.set_mux(usrp.determine_rx_mux_value(self.src, options.rx_subdev_spec))
	   self.subdev = usrp.selected_subdev(self.src, options.rx_subdev_spec)
           self.src.tune(0, self.subdev, self.usrp_center)
           self.tune_offset = 0 # -self.usrp_center - self.src.rx_freq(0)

        else:
           self.src = gr.file_source (gr.sizeof_short,options.input_file)
           self.tune_offset = 2200 # 2200 works for 3.5-4Mhz band

        # save radio data to a file
        if SAVE_RADIO_TO_FILE:
           file = gr.file_sink(gr.sizeof_short, options.radio_file)
           self.fg.connect (self.src, file)

	# 2nd DDC
        xlate_taps = gr.firdes.low_pass ( \
           1.0, usb_rate, 16e3, 4e3, gr.firdes.WIN_HAMMING )
        self.xlate = gr.freq_xlating_fir_filter_ccf ( \
           fir_decim, xlate_taps, self.tune_offset, usb_rate )

        # convert rf data in interleaved short int form to complex
        s2ss = gr.stream_to_streams(gr.sizeof_short,2)
        s2f1 = gr.short_to_float()
        s2f2 = gr.short_to_float()
        src_f2c = gr.float_to_complex()
        self.fg.connect(self.src,s2ss)
        self.fg.connect((s2ss,0),s2f1)
        self.fg.connect((s2ss,1),s2f2)
        self.fg.connect(s2f1,(src_f2c,0))
        self.fg.connect(s2f2,(src_f2c,1))


	# Complex Audio filter
        audio_coeffs = gr.firdes.complex_band_pass (
                1.0,    # gain
                self.af_sample_rate, # sample rate
                -3000,    # low cutoff
                0,   # high cutoff
                100,    # transition
                gr.firdes.WIN_HAMMING)  # window
	self.slider_1.SetValue(0)
	self.slider_2.SetValue(-3000)

        self.audio_filter = gr.fir_filter_ccc ( 1, audio_coeffs)

	# Main +/- 16Khz spectrum display
        self.fft = fftsink.fft_sink_c (self.fg, self.panel_2, fft_size=512, sample_rate=self.af_sample_rate, average=True, size=(640,240))

	# AM Sync carrier 
	if AM_SYNC_DISPLAY:
	   self.fft2 = fftsink.fft_sink_c (self.fg, self.panel_9, y_per_div=20, fft_size=512, sample_rate=self.af_sample_rate, average=True, size=(640,240))

        c2f = gr.complex_to_float()

	# AM branch
	self.sel_am = gr.multiply_const_cc(0)
	# the following frequencies turn out to be in radians/sample
	# gr.pll_refout_cc(alpha,beta,min_freq,max_freq)
	# suggested alpha = X, beta = .25 * X * X
	pll = gr.pll_refout_cc(.5,.0625,(2.*math.pi*7.5e3/self.af_sample_rate),(2.*math.pi*6.5e3/self.af_sample_rate))
	self.pll_carrier_scale = gr.multiply_const_cc(complex(10,0))
	am_det = gr.multiply_cc()
	# these are for converting +7.5kHz to -7.5kHz
	# for some reason gr.conjugate_cc() adds noise ??
	c2f2 = gr.complex_to_float()
	c2f3 = gr.complex_to_float()
	f2c = gr.float_to_complex()
	phaser1 = gr.multiply_const_ff(1)
	phaser2 = gr.multiply_const_ff(-1)

	# filter for pll generated carrier
        pll_carrier_coeffs = gr.firdes.complex_band_pass (
                2.0,    # gain
                self.af_sample_rate, # sample rate
                7400,    # low cutoff
                7600,   # high cutoff
                100,    # transition
                gr.firdes.WIN_HAMMING)  # window

        self.pll_carrier_filter = gr.fir_filter_ccc ( 1, pll_carrier_coeffs)

	self.sel_sb = gr.multiply_const_ff(1)
	combine = gr.add_ff()

	#AGC
	sqr1 = gr.multiply_ff()
	intr = gr.iir_filter_ffd ( [.004, 0], [0, .999] )
	offset = gr.add_const_ff(1)
	agc = gr.divide_ff()


        self.scale = gr.multiply_const_ff(0.00001)
        dst = audio.sink(long(self.af_sample_rate))

        self.fg.connect(src_f2c,self.xlate,self.fft)
        self.fg.connect(self.xlate,self.audio_filter,self.sel_am,(am_det,0))
	self.fg.connect(self.sel_am,pll,self.pll_carrier_scale,self.pll_carrier_filter,c2f3)
	self.fg.connect((c2f3,0),phaser1,(f2c,0))
	self.fg.connect((c2f3,1),phaser2,(f2c,1))
	self.fg.connect(f2c,(am_det,1))
	self.fg.connect(am_det,c2f2,(combine,0))
	self.fg.connect(self.audio_filter,c2f,self.sel_sb,(combine,1))
	if AM_SYNC_DISPLAY:
	  self.fg.connect(self.pll_carrier_filter,self.fft2)
	self.fg.connect(combine,self.scale)
	self.fg.connect(self.scale,(sqr1,0))
	self.fg.connect(self.scale,(sqr1,1))
	self.fg.connect(sqr1, intr, offset, (agc, 1))
	self.fg.connect(self.scale,(agc, 0))
	self.fg.connect(agc,dst)

	if SAVE_AUDIO_TO_FILE:
	  f_out = gr.file_sink(gr.sizeof_short,options.audio_file)
	  sc1 = gr.multiply_const_ff(64000)
	  f2s1 = gr.float_to_short()
	  self.fg.connect(agc,sc1,f2s1,f_out)

        self.fg.start()

        # for mouse position reporting on fft display
        em.eventManager.Register(self.Mouse, wx.EVT_MOTION, self.fft.win)
        # and left click to re-tune
        em.eventManager.Register(self.Click, wx.EVT_LEFT_DOWN, self.fft.win)

        # start a timer to check for web commands
        if WEB_CONTROL:
	   self.timer = UpdateTimer(self, 1000) # every 1000 mSec, 1 Sec


	wx.EVT_BUTTON(self,ID_BUTTON_1,self.set_lsb)
	wx.EVT_BUTTON(self,ID_BUTTON_2,self.set_usb)
	wx.EVT_BUTTON(self,ID_BUTTON_3,self.set_am)
	wx.EVT_BUTTON(self,ID_BUTTON_4,self.set_cw)
	wx.EVT_BUTTON(self,ID_BUTTON_10,self.fwd)
	wx.EVT_BUTTON(self,ID_BUTTON_11,self.rew)
        wx.EVT_BUTTON(self, ID_BUTTON_13, self.AT_calibrate)
        wx.EVT_BUTTON(self, ID_BUTTON_14, self.AT_reset)
	wx.EVT_TOGGLEBUTTON(self,ID_BUTTON_5,self.on_button)
	wx.EVT_TOGGLEBUTTON(self,ID_BUTTON_6,self.on_button)
	wx.EVT_TOGGLEBUTTON(self,ID_BUTTON_7,self.on_button)
	wx.EVT_TOGGLEBUTTON(self,ID_BUTTON_8,self.on_button)
	wx.EVT_TOGGLEBUTTON(self,ID_BUTTON_9,self.on_button)
        wx.EVT_SLIDER(self,ID_SLIDER_1,self.set_filter)
        wx.EVT_SLIDER(self,ID_SLIDER_2,self.set_filter)
        wx.EVT_SLIDER(self,ID_SLIDER_3,self.slide_tune)
        wx.EVT_SLIDER(self,ID_SLIDER_4,self.set_volume)
	wx.EVT_SLIDER(self,ID_SLIDER_5,self.set_pga)
	wx.EVT_SLIDER(self,ID_SLIDER_6,self.am_carrier)
        wx.EVT_SLIDER(self,ID_SLIDER_7,self.antenna_tune)
        wx.EVT_SPINCTRL(self,ID_SPIN_1,self.spin_tune)

        wx.EVT_MENU(self, ID_EXIT,  self.TimeToQuit)

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("HF Explorer 2")
        self.slider_1.SetMinSize((450, 38))
        self.slider_2.SetMinSize((450, 38))
        self.panel_2.SetMinSize((640, 240))
        self.button_7.SetValue(1)
        self.slider_3.SetMinSize((450, 19))
        self.slider_4.SetMinSize((275, 19))
        self.slider_5.SetMinSize((275, 19))
	if AM_SYNC_DISPLAY:
           self.panel_9.SetMinSize((640, 240))
        self.slider_6.SetMinSize((300, 38))
        self.slider_7.SetMinSize((400, 38))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1 = wx.FlexGridSizer(11, 2, 0, 0)
        sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_6 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        grid_sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
        sizer_2.Add(self.button_1, 0, wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(self.button_2, 0, wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(self.button_3, 0, wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(self.button_4, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.button_5, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.slider_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.button_6, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.slider_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        sizer_6.Add(self.panel_5, 1, wx.EXPAND, 0)
        sizer_6.Add(self.label_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        sizer_6.Add(self.text_ctrl_1, 0, wx.ADJUST_MINSIZE, 0)
        sizer_6.Add(self.panel_6, 1, wx.EXPAND, 0)
        sizer_6.Add(self.panel_7, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(sizer_6, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.panel_2, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.button_7, 0, wx.ADJUST_MINSIZE, 0)
        sizer_3.Add(self.slider_3, 0, wx.ADJUST_MINSIZE, 0)
        sizer_3.Add(self.spin_ctrl_1, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(sizer_3, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.button_8, 0, wx.ADJUST_MINSIZE, 0)
        sizer_4.Add(self.slider_4, 0, wx.ADJUST_MINSIZE, 0)
        sizer_4.Add(self.slider_5, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(sizer_4, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.button_9, 0, wx.ADJUST_MINSIZE, 0)
        sizer_5.Add(self.button_11, 0, wx.ADJUST_MINSIZE, 0)
        sizer_5.Add(self.button_10, 0, wx.ADJUST_MINSIZE, 0)
        sizer_5.Add(self.panel_3, 1, wx.EXPAND, 0)
        sizer_5.Add(self.label_2, 0, wx.ADJUST_MINSIZE, 0)
        sizer_5.Add(self.panel_4, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(sizer_5, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.panel_8, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.panel_9, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.label_3, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.slider_6, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.label_4, 0, wx.ALIGN_BOTTOM|wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.slider_7, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.panel_10, 1, wx.EXPAND, 0)
        sizer_7.Add(self.button_12, 0, wx.ADJUST_MINSIZE, 0)
        sizer_7.Add(self.button_13, 0, wx.ADJUST_MINSIZE, 0)
        sizer_7.Add(self.button_14, 0, wx.ADJUST_MINSIZE, 0)
        sizer_7.Add(self.panel_11, 1, wx.EXPAND, 0)
        sizer_7.Add(self.panel_12, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(sizer_7, 1, wx.EXPAND, 0)
        sizer_1.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        sizer_1.SetSizeHints(self)
        self.Layout()
        # end wxGlade

    # Menu exit
    def TimeToQuit(self, event):
        self.fg.stop()
        self.Close(True)

    # Powermate being turned
    def on_rotate(self, event):
	if self.active_button == 5:
	   self.slider_1.SetValue(self.slider_1.GetValue()+event.delta)
           if self.slider_2.GetValue() > (self.slider_1.GetValue() - 200) :
              self.slider_2.SetValue(self.slider_1.GetValue() - 200)
	   self.filter()
	if self.active_button == 6:
	   self.slider_2.SetValue(self.slider_2.GetValue()+event.delta)
	   if self.slider_1.GetValue() < (self.slider_2.GetValue() + 200) :
	      self.slider_1.SetValue(self.slider_2.GetValue() + 200)
	   self.filter()
	if self.active_button == 7:
           new = max(0, min(6000, self.slider_3.GetValue() + event.delta))
           self.slider_3.SetValue(new)
           self.frequency = (self.f_slider_scale * new) + self.f_slider_offset
           self.spin_ctrl_1.SetValue(self.frequency)
	   if self.AM_mode == False:
             self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset))
	   else:
	     self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset - 7.5e3))
	   if self.button_12.GetValue():
	      self.auto_antenna_tune()
	if self.active_button == 8:
           new = max(0, min(500, self.slider_4.GetValue() + event.delta))
           self.slider_4.SetValue(new)
           self.scale.set_k(math.pow(10.,((self.slider_4.GetValue()-500.)/100.)))
	if self.active_button == 9:
           if self.PLAY_FROM_USRP == False:
              if event.delta == -1:
                 self.src.seek(-1000000,gr.SEEK_CUR)
              elif event.delta == 1:
                 self.src.seek(1000000,gr.SEEK_CUR)


    # Powermate pressed to switch controlled function
    def on_pmButton(self, event):
	if event.value == 0:
	   if self.active_button == 5:
	      self.active_button = 6
	      self.button_5.SetValue(False)
	      self.button_6.SetValue(True)
	   elif self.active_button == 6:
	      self.active_button = 7
	      self.button_6.SetValue(False)
	      self.button_7.SetValue(True)
	   elif self.active_button == 7:
	      self.active_button = 8
	      self.button_7.SetValue(False)
	      self.button_8.SetValue(True)
	   elif self.active_button == 8:
	      self.active_button = 9
	      self.button_8.SetValue(False)
	      self.button_9.SetValue(True)
	   elif self.active_button == 9:
	      self.active_button = 5
	      self.button_9.SetValue(False)
	      self.button_5.SetValue(True)

    # Clicking one PM control button turns the rest off
    def on_button(self, event):
	id = event.GetId()
	if id == ID_BUTTON_5:
	   self.active_button = 5
	   self.button_6.SetValue(False)
	   self.button_7.SetValue(False)
	   self.button_8.SetValue(False)
	   self.button_9.SetValue(False)
	if id == ID_BUTTON_6:
	   self.active_button = 6
           self.button_5.SetValue(False)
           self.button_7.SetValue(False)
           self.button_8.SetValue(False)
           self.button_9.SetValue(False)
        if id == ID_BUTTON_7:
           self.active_button = 7
           self.button_5.SetValue(False)
           self.button_6.SetValue(False)
           self.button_8.SetValue(False)
           self.button_9.SetValue(False)
        if id == ID_BUTTON_8:
           self.active_button = 8
           self.button_5.SetValue(False)
           self.button_6.SetValue(False)
           self.button_7.SetValue(False)
           self.button_9.SetValue(False)
        if id == ID_BUTTON_9:
           self.active_button = 9
           self.button_5.SetValue(False)
           self.button_6.SetValue(False)
           self.button_7.SetValue(False)
           self.button_8.SetValue(False)

    # Make sure filter settings are legal
    def set_filter(self, event):
        slider = event.GetId()
        slider1 = self.slider_1.GetValue()
        slider2 = self.slider_2.GetValue()
        if slider == ID_SLIDER_1:
           if slider2 > (self.slider_1.GetValue() - 200) :
              self.slider_2.SetValue(slider1 - 200)
        elif slider == ID_SLIDER_2:
           if slider1 < (self.slider_2.GetValue() + 200) :
              self.slider_1.SetValue(slider2 + 200)
	self.filter()

    # Calculate taps and apply
    def filter(self):
        audio_coeffs = gr.firdes.complex_band_pass (
                1.0,    # gain
                self.af_sample_rate, # sample rate
                self.slider_2.GetValue(),    # low cutoff
                self.slider_1.GetValue(),   # high cutoff
                100,    # transition
                gr.firdes.WIN_HAMMING)  # window
        self.audio_filter.set_taps(audio_coeffs)

    def set_lsb(self, event):
	self.AM_mode = False
	self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset))
	self.sel_sb.set_k(1)
	self.sel_am.set_k(0)
	self.slider_1.SetValue(0)
	self.slider_2.SetValue(-3000)
	self.filter()

    def set_usb(self, event):
	self.AM_mode = False
	self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset))
	self.sel_sb.set_k(1)
	self.sel_am.set_k(0)
	self.slider_1.SetValue(3000)
	self.slider_2.SetValue(0)
	self.filter()

    def set_am(self, event):
	self.AM_mode = True
	self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset - 7.5e3))
	self.sel_sb.set_k(0)
	self.sel_am.set_k(1)
	self.slider_1.SetValue(12500)
	self.slider_2.SetValue(2500)
	self.filter()

    def set_cw(self, event):
	self.AM_mode = False
	self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset))
	self.AM_mode = False
	self.sel_sb.set_k(1)
	self.sel_am.set_k(0)
	self.slider_1.SetValue(-400)
	self.slider_2.SetValue(-800)
	self.filter()

    def set_volume(self, event):
        self.scale.set_k(math.pow(10.,((self.slider_4.GetValue()-500.)/100.)))

    def set_pga(self,event):
	if self.PLAY_FROM_USRP:
	   self.subdev.set_gain(self.slider_5.GetValue())

    def slide_tune(self, event):
        self.frequency = (self.f_slider_scale * self.slider_3.GetValue()) + self.f_slider_offset
	if self.AM_mode == False:
          self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset))
	else:
	  self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset - 7.5e3))
        self.spin_ctrl_1.SetValue(self.frequency)
	if self.button_12.GetValue():
	   self.auto_antenna_tune()

    def spin_tune(self, event):
	self.frequency = self.spin_ctrl_1.GetValue()
	if self.AM_mode == False:
           self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset))
	else:
	   self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset - 7.5e3))
        self.slider_3.SetValue(int((self.frequency-self.f_slider_offset)/self.f_slider_scale))
	if self.button_12.GetValue():
	   self.auto_antenna_tune()

    # Seek forwards in file
    def fwd(self, event):
	if self.PLAY_FROM_USRP == False:
	   self.src.seek(10000000,gr.SEEK_CUR)

    # Seek backwards in file
    def rew(self, event):
	if self.PLAY_FROM_USRP == False:
	   self.src.seek(-10000000,gr.SEEK_CUR)

    # Mouse over fft display - show frequency in tooltip
    def Mouse(self,event):
	if self.AM_mode:
	   fRel = ( event.GetX() - 330. ) / 14.266666 - 7.5
	else:
	   fRel = ( event.GetX() - 330. ) / 14.266666 
        self.fft.win.SetToolTip(wx.ToolTip(eng_notation.num_to_str(self.frequency + (fRel*1e3))))

    # Mouse clicked on fft display - change frequency 
    def Click(self,event):
        fRel = ( event.GetX() - 330. ) / 14.266666
	if self.AM_mode == False:
           self.frequency = self.frequency + (fRel*1e3)
	else:
	   self.frequency = self.frequency + (fRel*1e3) - 7.5e3
        self.spin_ctrl_1.SetValue(int(self.frequency))
        self.slider_3.SetValue(int((self.frequency-self.f_slider_offset)/self.f_slider_scale))
        if self.AM_mode == False:
	   self.xlate.set_center_freq ( self.usrp_center - ( self.frequency - self.tune_offset ))
	else:
	   self.xlate.set_center_freq( self.usrp_center - (self.frequency - self.tune_offset - 7.5e3))

    # Set power of AM sync carrier
    def am_carrier(self,event):
	scale = math.pow(10,(self.slider_6.GetValue())/50.)
	self.pll_carrier_scale.set_k(complex(scale,0))

    # Reset AT data and start calibrate over
    def AT_reset(self, event):
        self.xdata = []
        self.ydata = []

    # Save AT setting for a particular frequency
    def AT_calibrate(self, event):
        self.xdata.append(float(self.frequency))
        self.ydata.append(self.slider_7.GetValue())
        if len(self.xdata) > 1:
          self.m = []
          self.b = []
          for i in range(0,len(self.xdata)-1):
            self.m.append( (self.ydata[i+1] - self.ydata[i]) / (self.xdata[i+1] - self.xdata[i]) )
            self.b.append( self.ydata[i] - self.m[i] * self.xdata[i] )

    # Lookup calibrated points and calculate interpolated antenna tune voltage.
    # This is to automatically tune a narrowband loop antenna when the freq
    # is changed, to keep signals peaked.
    def auto_antenna_tune(self):
        for i in range(0,len(self.xdata)-1):
          if (self.frequency > self.xdata[i]) & (self.frequency < self.xdata[i+1]):
            self.slider_7.SetValue(self.m[i]*self.frequency + self.b[i])
        self.antenna_tune(0)

    # Slider to set loop antenna capacitance
    def antenna_tune(self, evt):
        if self.PLAY_FROM_USRP:
           self.src.write_aux_dac(0,3,self.slider_7.GetValue())

    # Timer events - check for web commands
    def OnUpdate(self):
      cmds = os.listdir("/var/www/cgi-bin/commands/")
      if cmds!=[]:
        if cmds[0]=='chfreq':
          fd=open("/var/www/cgi-bin/commands/chfreq","r")
          new=fd.readline()
          fd.close()
          if new!='':
            os.unlink("/var/www/cgi-bin/commands/chfreq")
            if ( int(new) >= self.f_lo ) & ( int(new) <= self.f_hi ):
               self.frequency = int(new)
               self.slider_3.SetValue(( self.frequency - self.f_slider_offset) / self.f_slider_scale )
               self.spin_ctrl_1.SetValue(self.frequency)
               if self.button_12.GetValue():
                 self.auto_antenna_tune()
               if self.AM_mode:
                 self.xlate.set_center_freq ( self.usrp_center - ( self.frequency - self.tune_offset - 7.5e3 ))
               else:
                 self.xlate.set_center_freq ( self.usrp_center - ( self.frequency - self.tune_offset ))

        if cmds[0]=='chvolume':
          fd=open("/var/www/cgi-bin/commands/chvolume","r")
          new=fd.readline()
          fd.close()
          if new!='':
            os.unlink("/var/www/cgi-bin/commands/chvolume")
            if ( int(new) >= 0 ) & ( int(new) <= 500 ):
               self.volume = int(new)
               self.slider_4.SetValue(self.volume)
               self.scale.set_k(math.pow(10.,((self.slider_4.GetValue()-500.)/100.)))

      else: # no new web commands, update state
        fh = open("/var/www/cgi-bin/state/freq","w")
        fh.write(str(int(self.frequency))+'\n')
        fh.close()
        fh = open("/var/www/cgi-bin/state/volume","w")
        fh.write(str(self.slider_4.GetValue())+'\n')
        fh.close()


# end of class MyFrame

# wx.Timer to check for web updates
class UpdateTimer(wx.Timer):
    def __init__(self, target, dur=1000):
        wx.Timer.__init__(self)
        self.target = target
        self.Start(dur)

    def Notify(self):
        """Called every timer interval"""
        if self.target:
            self.target.OnUpdate()


class MyApp(wx.App):
  def OnInit(self):
    frame = MyFrame(None, -1, "HF Explorer 2")
    frame.Show(True)
    self.SetTopWindow(frame)
    return True

app = MyApp(0)
app.MainLoop()