-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
256 lines (210 loc) · 5.67 KB
/
code.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
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
# LoraCWBeacon Copyright 2023 Joeri Van Dooren (ON3URE)
import asyncio
import random
import time
import adafruit_rfm9x
import adafruit_si5351
import board
import busio
import digitalio
from adafruit_datetime import datetime
import config
# User config
WPM = config.WPM
FREQ = config.FREQ
BEACON = config.BEACON
BEACONDELAY = config.BEACONDELAY
# Create the I2C interface.
XTAL_FREQ = 25000000
i2c = busio.I2C(scl=board.GP27, sda=board.GP26)
si5351 = adafruit_si5351.SI5351(i2c)
# leds
pwrLED = digitalio.DigitalInOut(board.GP9)
pwrLED.direction = digitalio.Direction.OUTPUT
pwrLED.value = True
txLED = digitalio.DigitalInOut(board.GP10)
txLED.direction = digitalio.Direction.OUTPUT
txLED.value = False
loraLED = digitalio.DigitalInOut(board.GP11)
loraLED.direction = digitalio.Direction.OUTPUT
loraLED.value = False
def setFrequency(frequency):
xtalFreq = XTAL_FREQ
divider = int(900000000 / frequency)
if divider % 2:
divider -= 1
pllFreq = divider * frequency
mult = int(pllFreq / xtalFreq)
l = int(pllFreq % xtalFreq)
f = l
f *= 1048575
f /= xtalFreq
num = int(f)
denom = 1048575
si5351.pll_a.configure_fractional(mult, num, denom)
si5351.clock_0.configure_integer(si5351.pll_a, divider)
def led(what):
if what == "tx":
txLED.value = True
if what == "txOFF":
txLED.value = False
if what == "lora":
loraLED.value = True
if what == "loraOFF":
loraLED.value = False
# setup encode and decode
encodings = {}
def encode(char):
global encodings
if char in encodings:
return encodings[char]
elif char.lower() in encodings:
return encodings[char.lower()]
else:
return ""
decodings = {}
def decode(char):
global decodings
if char in decodings:
return decodings[char]
else:
# return '('+char+'?)'
return "¿"
def MAP(pattern, letter):
decodings[pattern] = letter
encodings[letter] = pattern
MAP(".-", "a")
MAP("-...", "b")
MAP("-.-.", "c")
MAP("-..", "d")
MAP(".", "e")
MAP("..-.", "f")
MAP("--.", "g")
MAP("....", "h")
MAP("..", "i")
MAP(".---", "j")
MAP("-.-", "k")
MAP(".-..", "l")
MAP("--", "m")
MAP("-.", "n")
MAP("---", "o")
MAP(".--.", "p")
MAP("--.-", "q")
MAP(".-.", "r")
MAP("...", "s")
MAP("-", "t")
MAP("..-", "u")
MAP("...-", "v")
MAP(".--", "w")
MAP("-..-", "x")
MAP("-.--", "y")
MAP("--..", "z")
MAP(".----", "1")
MAP("..---", "2")
MAP("...--", "3")
MAP("....-", "4")
MAP(".....", "5")
MAP("-....", "6")
MAP("--...", "7")
MAP("---..", "8")
MAP("----.", "9")
MAP("-----", "0")
MAP(".-.-.-", ".") # period
MAP("--..--", ",") # comma
MAP("..--..", "?") # question mark
MAP("-...-", "=") # equals, also /BT separator
MAP("-....-", "-") # hyphen
MAP("-..-.", "/") # forward slash
MAP(".--.-.", "@") # at sign
MAP("-.--.", "(") # /KN over to named station
MAP(".-.-.", "+") # /AR stop (end of message)
MAP(".-...", "&") # /AS wait
MAP("...-.-", "|") # /SK end of contact
MAP("...-.", "*") # /SN understood
MAP(".......", "#") # error
# key down and up
def cw(on):
if on:
led("tx")
si5351.outputs_enabled = True
else:
led("txOFF")
si5351.outputs_enabled = False
# timing
def dit_time():
global WPM
PARIS = 50
return 60.0 / WPM / PARIS
# transmit pattern
def play(pattern):
for sound in pattern:
if sound == ".":
cw(True)
time.sleep(dit_time())
cw(False)
time.sleep(dit_time())
elif sound == "-":
cw(True)
time.sleep(3 * dit_time())
cw(False)
time.sleep(dit_time())
elif sound == " ":
time.sleep(4 * dit_time())
time.sleep(2 * dit_time())
# play beacon and pause
def beacon():
global cwBeacon
letter = cwBeacon[:1]
cwBeacon = cwBeacon[1:]
print(letter, end="")
play(encode(letter))
async def loraLoop(loop):
# LoRa APRS frequency
RADIO_FREQ_MHZ = 868.000
CS = digitalio.DigitalInOut(board.GP21)
RESET = digitalio.DigitalInOut(board.GP20)
spi = busio.SPI(board.GP18, MOSI=board.GP19, MISO=board.GP16)
rfm9x = adafruit_rfm9x.RFM9x(
spi, CS, RESET, RADIO_FREQ_MHZ, baudrate=1000000, agc=False, crc=True
)
while True:
await asyncio.sleep(5)
stamp = datetime.now()
timeout = int(config.LORATIMEOUT) + random.randint(1, 9)
print(
f"{stamp}: [{config.CALL}] loraRunner: Waiting for lora APRS packet ...\r",
end="",
)
packet = await rfm9x.areceive(with_header=True, timeout=timeout)
if packet is not None:
if packet[:3] == (b"<\xaa\x01"):
try:
rawdata = bytes(packet[3:]).decode("utf-8")
stamp = datetime.now()
print(
f"\r{stamp}: [{config.CALL}] loraRunner: RSSI:{rfm9x.last_rssi} Data:{rawdata}"
)
except:
print(
f"{stamp}: [{config.CALL}] loraRunner: Lost Packet, unable to decode, skipping"
)
continue
async def beaconLoop():
global cwBeacon
global BEACON
global FREQ
cwBeacon = BEACON
setFrequency(FREQ * 1000)
print("Measured Frequency: {0:0.3f} MHz".format(si5351.clock_0.frequency / 1000000))
while True:
beacon()
await asyncio.sleep(0)
if len(cwBeacon) == 0:
cwBeacon = BEACON
await asyncio.sleep(BEACONDELAY)
async def main():
loop = asyncio.get_event_loop()
loraL = asyncio.create_task(loraLoop(loop))
cwL = asyncio.create_task(beaconLoop())
await asyncio.gather(cwL, loraL)
asyncio.run(main())