-
Notifications
You must be signed in to change notification settings - Fork 2
/
transceiver_control.py
198 lines (175 loc) · 5.34 KB
/
transceiver_control.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
import datetime
import time
import serial
import struct
import os
import yaml
from consolemenu import *
from consolemenu.items import *
import sys
from ft8 import FT8Send
from ft4 import FT4Send
# Import weakmon to use encoders
sys.path.append(os.path.expandvars('$WEAKMON'))
# Read configuration file
configs_file = open('transceiver_config.yml', 'r')
configs = yaml.safe_load(configs_file)
# Serial port for arduino
serial_port = configs['serial_port']
baudrate = configs['baudrate']
try:
puerto = serial.Serial(serial_port, baudrate, timeout=0.5)
except serial.serialutil.SerialException as e:
print("\nCannot connect to the serial port: " + serial_port + "\n")
exit(1)
# Global variables
callsign = configs['callsign']
grid = configs['grid']
current_msg = ''
rx_callsign = ''
mode = 'FT8'
# FT8 encoder
ft8_encoder = FT8Send()
ft4_encoder = FT4Send()
def encode_ft8(msg):
try:
a77 = ft8_encoder.pack(msg, 1)
symbols = ft8_encoder.make_symbols(a77)
except Exception as e:
print("FT8 encoder error:", e)
symbols = None
time.sleep(3)
return symbols
def encode_ft4(msg):
try:
a77 = ft4_encoder.pack(msg, 1)
symbols = ft4_encoder.make_symbols(a77)
except:
print("FT4 encoder error, check message!")
symbols = None
time.sleep(3)
return symbols
def load_symbols(symbols):
print("Load symbols into transmitter..")
puerto.write(b'm')
for symbol in symbols:
puerto.write(struct.pack('>B', symbol))
puerto.write(b'\0')
time.sleep(1)
def change_freq():
no_freq = True
while(no_freq):
new_freq = int(input("Enter new offset frequency (0 - 2000Hz): "))
if (new_freq > 0 and new_freq < 2000):
no_freq = False
puerto.write(b'o')
for kk in range(2):
puerto.write(struct.pack('>B', (new_freq >> 8*kk) & 0xFF))
time.sleep(1)
def change_mode():
global mode
global current_msg
if 'FT8' in mode:
print("Switch mode to FT4!")
menu.remove_item(chft4_item)
menu.append_item(chft8_item)
mode = 'FT4'
else:
print("Switch mode to FT8!")
menu.remove_item(chft8_item)
menu.append_item(chft4_item)
mode = 'FT8'
puerto.write(b's')
current_msg = ''
time.sleep(1)
menu.show()
def change_udp():
print("Switch to UDP mode!")
puerto.write(b'u')
time.sleep(1)
menu.show()
def new_msg(msg):
global current_msg
global mode
print(msg)
if msg != current_msg:
if 'FT8' in mode:
symbols = encode_ft8(msg)
else:
symbols = encode_ft4(msg)
if symbols is not None:
load_symbols(symbols)
current_msg = msg
else:
return
transmit()
def transmit():
if not current_msg:
print("No previous message!")
time.sleep(1)
else:
print("Waiting for slot..")
while True:
utc_time = datetime.datetime.now(datetime.timezone.utc)
if 'FT8' in mode:
if (utc_time.second % 15 == 14):
print("TX!")
puerto.write(b't')
time.sleep(1)
break
else:
mscount = utc_time.second + utc_time.microsecond/1e6
if (abs(mscount%7.5 - 6.5) < 0.05):
print("TX!")
puerto.write(b't')
time.sleep(1)
break
def call_cq():
msg = 'CQ ' + callsign + ' ' + grid
new_msg(msg)
def resp_new():
global rx_callsign
rx_callsign = input("Respond to callsign: ")
resp_submenu.show()
def resp_last():
if not rx_callsign:
print("No previous callsign!")
time.sleep(1)
else:
resp_submenu.show()
def respond(response):
if 'grid' in response:
msg = rx_callsign + ' ' + callsign + ' ' + grid
elif 'signal' in response:
snr = input("Signal strength: ")
msg = rx_callsign + ' ' + callsign + ' ' + snr
elif 'RR73' in response:
msg = rx_callsign + ' ' + callsign + ' RR73'
new_msg(msg)
# Create the menus
menu = ConsoleMenu("WJSTX Transceiver Control ", "FT8 and FT4")
resp_submenu = ConsoleMenu("Respond with: ", "", exit_option_text="Go back")
# Main menu
ver_item = FunctionItem("Use UART, not UDP", change_udp, [])
cq_item = FunctionItem("Call CQ", call_cq, [])
retransmit_item = FunctionItem("Retransmit last", transmit, [])
resp_new_item = FunctionItem("Respond to new callsign", resp_new, [])
submenu_item = FunctionItem("Respond to last callsign", resp_last, [])
chfreq_item = FunctionItem("Change TX frequency", change_freq, [])
chft4_item = FunctionItem("Change mode to FT4", change_mode, [])
chft8_item = FunctionItem("Change mode to FT8", change_mode, [])
# Resp submenu
resp_grid_item = FunctionItem("Respond with grid", respond, ['grid'])
resp_signal_item = FunctionItem("Respond with R + signal strength", respond, ['signal'])
resp_73_item = FunctionItem("Respond with RR73", respond, ['RR73'])
resp_submenu.append_item(resp_grid_item)
resp_submenu.append_item(resp_signal_item)
resp_submenu.append_item(resp_73_item)
menu.append_item(ver_item)
menu.append_item(cq_item)
menu.append_item(resp_new_item)
menu.append_item(submenu_item)
menu.append_item(retransmit_item)
menu.append_item(chfreq_item)
menu.append_item(chft4_item)
menu.show()