-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx_qt.py
More file actions
235 lines (190 loc) · 7.91 KB
/
tx_qt.py
File metadata and controls
235 lines (190 loc) · 7.91 KB
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
import sys
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtCore import Qt, QTime, QTimer
from multiprocessing import shared_memory
from binascii import hexlify
from utils import LOG_FOLDER, DEFAULT_BG_COLOR, DEFAULT_BRUSH_SIZE, DEFAULT_PEN_COLOR, my_memcpy, pack_draw, pack_color, pack_size, pack_clear
from bitstruct import *
import time
from datetime import datetime
import os
import json
from os import getenv
from dotenv import load_dotenv
load_dotenv()
sim = getenv("SIM") == "True"
LOG_DATA = getenv("LOG_DATA") == "True"
EXPERIMENT_COUNTER = getenv("EXPERIMENT_COUNTER") == "True"
if not sim:
'''RASPBERRY PI: use below'''
import board
import digitalio
import adafruit_si4713
FREQUENCY_KHZ = int(getenv("FREQ") + "00")
i2c = board.I2C()
si_reset = digitalio.DigitalInOut(board.D5)
print("initializing si4713 instance")
si4713 = adafruit_si4713.SI4713(i2c, reset=si_reset, timeout_s=0.5)
print("done")
noise = si4713.received_noise_level(FREQUENCY_KHZ)
print("Noise at {0:0.3f} mhz: {1} dBuV".format(FREQUENCY_KHZ / 1000.0, noise))
si4713.tx_frequency_khz = FREQUENCY_KHZ
si4713.tx_power = 115
print("Transmitting at {0:0.3f} mhz".format(si4713.tx_frequency_khz / 1000.0))
print("Transmitter power: {0} dBuV".format(si4713.tx_power))
print(
"Transmitter antenna capacitance: {0:0.2} pF".format(si4713.tx_antenna_capacitance)
)
si4713.gpio_control(gpio1=True, gpio2=True)
print("Broadcasting...")
si4713.configure_rds(0xADAF, station=bytes('(1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (10,10)', 'utf-8'), rds_buffer=bytes('(1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (10,10)', 'utf-8'))
'''RASPBERRY PI: use above'''
DELAY = 50
class SliderProxyStyle(QtWidgets.QProxyStyle):
def pixelMetric(self, metric, option, widget):
if metric == QtWidgets.QStyle.PM_SliderThickness:
return 40
elif metric == QtWidgets.QStyle.PM_SliderLength:
return 40
return super().pixelMetric(metric, option, widget)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, shared_input_buffer_name: str):
super().__init__()
self.experiment_counter = 0
self.shared_input_buffer_name = shared_input_buffer_name
self.shared_memory = None
if self.shared_input_buffer_name is not None:
self.shared_memory = shared_memory.SharedMemory(name=self.shared_input_buffer_name)
self.log_filename = None
self.log_file = None
if LOG_DATA:
self.log_filename = os.path.join(LOG_FOLDER, str(datetime.now()) + '-tx.log')
self.log_file = open(self.log_filename, 'w+')
menubar = self.addToolBar("toolbar")
font = self.font()
font.setPointSize(16)
menubar.setFont(font)
menubar.addAction("Clear").triggered.connect(self.clearEvent)
menubar.addAction("Color").triggered.connect(self.colorChangeEvent)
self.brushSize = DEFAULT_BRUSH_SIZE
self.mySlider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal, self)
self.mySlider.setRange(1, 100)
self.mySlider.setStyle(SliderProxyStyle(self.mySlider.style()))
self.mySlider.sliderReleased.connect(self.brushSizeChangeEvent)
menubar = self.addToolBar("toolbar")
menubar.addWidget(self.mySlider)
self.label = QtWidgets.QLabel()
canvas = QtGui.QPixmap(800, 600)
canvas.fill(DEFAULT_BG_COLOR)
self.label.setPixmap(canvas)
self.label.mouseMoveEvent = self._mouseMoveEvent
self.label.mouseReleaseEvent = self._mouseReleaseEvent
self.setCentralWidget(self.label)
self.last_x, self.last_y, self.line_code = None, None, 0
self.color = QtGui.QColor(DEFAULT_PEN_COLOR)
self.coords = []
# send initial color message
self.coords.append(self._getColorMessage())
self.time = QTime(0, 0, 0, 0)
timer = QTimer(self)
timer.timeout.connect(self.send_coords)
timer.start(DELAY)
def _getColorMessage(self):
(r, g, b, a) = self.color.getRgb()
a = a // 16
print('COLOR', r, g, b, a)
return pack_color(r, g, b, a)
def _mouseMoveEvent(self, e):
x = int(e.localPos().x())
y = int(e.localPos().y())
if self.last_x is None: # First event.
self.last_x = x
self.last_y = y
# self.last_draw_time = time.time()
self.coords.append(pack_draw(x, y, self.line_code))
return # Ignore the first time.
# if (x - self.last_x)**2 + (y - self.last_y)**2 > 200 or time.time() - self.last_draw_time > 0.3:
canvas = self.label.pixmap()
painter = QtGui.QPainter(canvas)
pen = QtGui.QPen(self.color, self.brushSize)
pen.setCapStyle(QtCore.Qt.RoundCap)
painter.setPen(pen)
painter.drawLine(self.last_x, self.last_y, x, y)
painter.end()
self.label.setPixmap(canvas)
# Update the origin for next time.
self.last_x = x
self.last_y = y
# self.last_draw_time = time.time()
self.coords.append(pack_draw(x, y, self.line_code))
def _mouseReleaseEvent(self, e):
self.last_x = None
self.last_y = None
self.line_code = 0 if self.line_code == 15 else 15
def colorChangeEvent(self):
self.color = QtWidgets.QColorDialog.getColor(options=QtWidgets.QColorDialog.ShowAlphaChannel)
self.coords.append(self._getColorMessage())
def brushSizeChangeEvent(self):
sliderPosition = self.mySlider.sliderPosition()
self.brushSize = sliderPosition
entry = pack_size(self.brushSize)
for i in range(3):
self.coords.append(entry)
def clearEvent(self):
canvas = self.label.pixmap()
canvas.fill(Qt.white)
self.label.setPixmap(canvas)
clear_msg = pack_clear()
for i in range(3):
self.coords.append(clear_msg)
def send_coords(self):
self.time = self.time.addMSecs(DELAY)
if (not len(self.coords)) and not EXPERIMENT_COUNTER:
return
if not EXPERIMENT_COUNTER:
coords = self.coords.pop(0)
if EXPERIMENT_COUNTER:
coords = self.experiment_counter.to_bytes(4,byteorder='big')
self.experiment_counter += 1
'''PI: use below'''
if not sim:
si4713._set_rds_buffer(coords)
'''PI: use above'''
'''SIMULATOR: use below'''
if sim:
my_memcpy(self.shared_memory, coords)
'''SIMULATOR: use above'''
print_dictionary = {
'time': self.time.toString("hh:mm:ss.zzz"),
'datetime': str(datetime.now()),
'binary': str(hexlify(coords)),
# 'msg': msg,
'last_x': self.last_x,
'last_y': self.last_y,
'line_code': self.line_code,
'color': str(self.color.red()) + "," + str(self.color.green()) + "," + str(self.color.blue()) + "," + str(self.color.alpha()),
'brushSize': self.brushSize
}
# logging
if LOG_DATA:
self.log_file.write(json.dumps(print_dictionary) + ',\n')
print(print_dictionary)
def tx_qt_main_func(shared_input_buffer_name: str):
app = QtWidgets.QApplication(sys.argv)
window = MainWindow(shared_input_buffer_name)
window.show()
app.exec()
if __name__ == '__main__':
shared_memory_name = None
if sim:
try:
s1 = shared_memory.SharedMemory(name='s1', create=True, size=6)
except FileExistsError:
# yoink the one that was hopefully already created correctly
s1 = shared_memory.SharedMemory(name='s1', create=False, size=6)
pack_data = pack_draw(255, 255, 15)
my_memcpy(s1, pack_data)
print(s1.buf)
shared_memory_name = s1.name
# main function
tx_qt_main_func(shared_memory_name)