-
Notifications
You must be signed in to change notification settings - Fork 5
/
siggen.py
executable file
·221 lines (170 loc) · 6.56 KB
/
siggen.py
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
#!/usr/bin/env python3
#
# This code is licenced under the GPL version 2, a copy of which is attached
# in the files called 'LICENSE'
#
#
# Copyright Matt Nottingham, 2015
#
#
import os
import os.path as osp
from guidata.qt import QtGui
from guidata.qt import QtCore
from guidata.qt.QtGui import QMainWindow, QMessageBox, QSplitter, QListWidget, QSpinBox
from guidata.qt.QtGui import QFont, QDesktopWidget, QFileDialog, QProgressBar
from guidata.qt.QtCore import QSettings, QThread, QTimer, QObject
from guiqwt.plot import CurveDialog, CurveWidget, BasePlot
from guiqwt.builder import make
from guiqwt.image import ImageItem
from guiqwt.styles import ImageParam
from guiqwt.annotations import AnnotatedPoint
from guiqwt.shapes import PointShape, Marker
from guiqwt.styles import AnnotationParam, ShapeParam, SymbolParam
import guidata
import guiqwt.curve
from guidata.configtools import get_icon
from guidata.qthelpers import create_action, add_actions, get_std_icon
from guidata.utils import update_dataset
from guidata.qt.QtCore import QSize, QT_VERSION_STR, PYQT_VERSION_STR, Qt
from guiqwt.config import _
from guiqwt.plot import ImageWidget
from guiqwt.plot import ImageDialog
from guiqwt.builder import make
import numpy as np
import sys
import platform
import serial
import struct
import datetime
import time
APP_NAME = _("Signal Generator")
VERS = '0.0.1'
class BG7(QThread):
def __init__(self, freq, sport='/dev/ttyUSB0'):
QThread.__init__(self)
self.freq = freq
self.timeout_timer = QTimer()
self.timeout_timer.setInterval(3000)
#self.connect(self.timeout_timer, QtCore.SIGNAL('timeout()'), self.timeout_serial)
self.fp = None
self.restart = False
self.do_debug = False
self.sport = sport
try:
self.reconnect()
except Exception as e:
print(e)
self.empty_buffer()
def empty_buffer(self):
pass
def timeout_serial(self):
print('Timeout serial')
self.timeout_timer.stop()
self.reconnect()
self.run()
def reconnect(self):
if self.fp is not None:
try:
self.fp.close()
except Exception as e:
print(e)
try:
self.fp = serial.Serial(self.sport, 57600, timeout=4)
except Exception as e:
print(e)
raise e
def __del__(self):
self.wait()
def run(self):
if self.fp is not None:
if self.restart:
self.restart = False
#self.freq = self.freq
while self.fp.inWaiting() > 0:
pants = self.fp.read(self.fp.inWaiting())
time.sleep(1.5)
print('BG7: trying to empty buff', self.fp.inWaiting())
print('BG7: Finished empty_buffer')
print('Sending command', self.freq)
self.fp.write('\x8f' + 'f' + format(int(self.freq/10.0), '09'))
class MainWindow(QMainWindow):
def __init__(self, reset=False, start_freq=None,
bandwidth=None, numpts=None, max_hold=None):
QMainWindow.__init__(self)
self.settings = QSettings("Darkstar007", "signal_generator")
if reset:
self.settings.clear()
self.setup(start_freq, bandwidth, numpts, max_hold)
def setup(self, start_freq, bandwidth, numpts, max_hold):
"""Setup window parameters"""
self.setWindowIcon(get_icon('python.png'))
self.setWindowTitle(APP_NAME)
#dt = QDesktopWidget()
#print dt.numScreens(), dt.screenGeometry()
#sz = dt.screenGeometry()
#self.resize(QSize(sz.width()*9/10, sz.height()*9/10))
# Welcome message in statusbar:
status = self.statusBar()
status.showMessage(_("Welcome to the Signal Generator application!"), 5000)
# File menu
file_menu = self.menuBar().addMenu(_("File"))
open_action = create_action(self, _("Save"),
shortcut="Ctrl+S",
icon=get_std_icon("FileIcon"),
tip=_("Save a File"),
triggered=self.saveFileDialog)
quit_action = create_action(self, _("Quit"),
shortcut="Ctrl+Q",
icon=get_std_icon("DialogCloseButton"),
tip=_("Quit application"),
triggered=self.close)
add_actions(file_menu, (open_action, None, quit_action))
# Help menu - prolly should just say "you're on your own..."!!
help_menu = self.menuBar().addMenu("Help")
about_action = create_action(self, _("About..."),
icon=get_std_icon('MessageBoxInformation'),
triggered=self.about)
add_actions(help_menu, (about_action,))
main_toolbar = self.addToolBar("Main")
# Calibration action?
add_actions(main_toolbar, (open_action, ))
# Set central widget:
toolbar = self.addToolBar("Image")
self.setCentralWidget(self.mainwidget)
if max_hold:
self.do_max_hold()
def about(self):
QMessageBox.about( self, _("About ")+APP_NAME,
"""<b>%s</b> v%s<p>%s Matt Nottingham
<br>Copyright © 2015 Matt Nottingham
<p>Python %s, Qt %s, PyQt %s %s %s""" % \
(APP_NAME, VERS, _("Developped by"), platform.python_version(),
QT_VERSION_STR, PYQT_VERSION_STR, _("on"), platform.system()) )
import getopt
def usage():
print('siggen.py [options]')
print('-r/--reset Reset the defaults')
print('-f/--freq <freq> Set the frequency')
return
if __name__ == '__main__':
from guidata import qapplication
try:
optlist,args = getopt.getopt(sys.argv[1:], 'rf:',
['reset', 'freq='])
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
reset = False
freq = 1.1e9
for o,a in optlist:
if o in ('-f', '--freq'):
freq = float(a)
#app = qapplication()
#window = MainWindow(reset=reset, start_freq=start_freq,
# bandwidth=bandwidth, numpts=numpts)
#window.show()
#app.exec_()
d = BG7(freq)
d.run()