-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHASduck.py
executable file
·483 lines (442 loc) · 22.1 KB
/
HASduck.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
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
#!/usr/bin/python3
#
#
# HASduck2
#
#
# Usage: HASduck.py
#
# DESCRIPTION
#
# Runs as a service (systemd) or standalone
#
# OPTIONS
#
# RELEASE: 20210303-130000
#
#
##
# IMPORT LIBRARIES
#
import argparse
import json
import signal
import sys
import time
import subprocess
from HASvioletRF import HASrf
from HASvioletHID import HAShid
#
# VARIABLES
#
hasDUCKver="HASduck v1.0"
hasVIOLETcfg = "cfg/hasVIOLET.json"
hasDUCKcfg = "cfg/hasDUCK.json"
hvdnLOGO = "cfg/hvdn-logo.xbm"
#
# OBJECTS
#
class Duckwalk:
def __init__(self):
self.cfgjson = hasDUCKcfg
with open(self.cfgjson) as configFileJson:
jsonConfig = json.load(configFileJson)
self.message = jsonConfig["0"]["message"]
self.interval = jsonConfig["0"]["interval"]
self.interval_start = time.time()
self.interval_stop = int(self.interval_start + self.interval)
self.trigger = jsonConfig["0"]["trigger"] # values include bluetooth, custo, gps, none, wifi
self.trigger_value = jsonConfig["0"]["trigger_value"]
self.action = jsonConfig["0"]["action"] # values include custom, next, reset, quit
self.duration = jsonConfig["0"]["duration"]
self.duration_start = time.time()
self.duration_stop = int(self.duration_start + self.duration)
return
def trigger(self, ttype, tvalue):
return
def action(self, taction):
return
HASit = HASrf()
HAShat = HAShid()
Ducky = Duckwalk()
#
# FUNCTIONS
#
def sigs_handler(signal_received, frame):
# Handle any cleanup here
print('Exiting gracefully')
HASit.cleanup()
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.OLED.text('Goodbye...', 0, 10, 1)
HAShat.OLED.show()
time.sleep(2)
HAShat.OLED.fill(0)
HAShat.OLED.show()
exit(0)
def main_menu():
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.OLED.text(" ",0, HAShat.prevpos,1)
HAShat.OLED.text("*",0, HAShat.currpos,1)
HAShat.OLED.text(" TRANSMIT",1,0,1)
HAShat.OLED.text(" RECEIVE",1,8,1)
HAShat.OLED.text(" DUCKHUNT",1,16,1)
HAShat.OLED.text(" OPTIONS",1,24,1)
HAShat.OLED.show()
def tx_menu():
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.OLED.text(" ",0, HAShat.prevpos,1)
HAShat.OLED.text("*",0, HAShat.currpos,1)
HAShat.OLED.text(" TX QSL",1,0,1)
HAShat.OLED.text(" TX QRZ",1,8,1)
HAShat.OLED.text(" TX BEACON",1,16,1)
HAShat.OLED.text(" RETURN",1,24,1)
HAShat.OLED.show()
def rx_menu():
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.OLED.text(" ",0, HAShat.prevpos,1)
HAShat.OLED.text("*",0, HAShat.currpos,1)
HAShat.OLED.text(" RX ALL",1,0,1)
HAShat.OLED.text(" RX BEACONS",1,8,1)
HAShat.OLED.text(" RX ME",1,16,1)
HAShat.OLED.text(" RETURN",1,24,1)
HAShat.OLED.show()
def game_menu():
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.OLED.text(" ",0, HAShat.prevpos,1)
HAShat.OLED.text("*",0, HAShat.currpos,1)
HAShat.OLED.text(" SHOW DUCK",1,0,1)
HAShat.OLED.text(" RUN DUCK",1,8,1)
HAShat.OLED.text(" RETURN",1,16,1)
HAShat.OLED.text(" *********",1,24,1)
HAShat.OLED.show()
def options_menu():
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.OLED.text(" ",0, HAShat.prevpos,1)
HAShat.OLED.text("*",0, HAShat.currpos,1)
HAShat.OLED.text(" ABOUT",1,0,1)
HAShat.OLED.text(" RESTART",1,8,1)
HAShat.OLED.text(" QUIT",1,16,1)
HAShat.OLED.text(" RETURN",1,24,1)
HAShat.OLED.show()
def menu_update():
HAShat.OLED.text(" ",0, HAShat.prevpos,1)
HAShat.OLED.text("*",0, HAShat.currpos,1)
HAShat.OLED.show()
def restart_svc():
HAShat.prevpos = 0
HAShat.currpos = 0
HAShat.menulvl="main_menu"
main_menu()
menu_update()
print("restarted")
def tx_msg(message):
HASheader = HASit.mystation + ">" + "BEACON-99"
HASpayload = HASheader + " | " + message
HASit.transmit(HASpayload)
print(HASpayload)
HAShat.OLED.fill(0)
HAShat.OLED.text(HASheader, 0, 0, 1)
HAShat.OLED.text(message, 0, 10, 1)
HAShat.OLED.show()
time.sleep(5)
HAShat.OLED.fill(0)
HAShat.OLED.show()
def tx_beacon():
HASheader = HASit.mystation + ">" + "BEACON-99"
HASpayload = HASheader + " | " + HASit.mybeacon
while HAShat.btnRight.value:
HASit.transmit(HASpayload)
print(HASpayload)
HAShat.OLED.fill(0)
HAShat.OLED.text(HASheader, 0, 0, 1)
HAShat.OLED.text(HASit.mybeacon , 0, 10, 1)
HAShat.OLED.show()
time.sleep(5)
HAShat.OLED.fill(0)
HAShat.OLED.show()
time.sleep(0.25)
def rx_msg_rfm9x(whom):
breakbad = 0
while True:
while not HASit.rfm.available():
if breakbad == 1:
break
if not HAShat.btnRight.value:
breakbad = 1
break
if breakbad == 1:
break
HASit.receive = HASit.rfm.recv()
HASit.receive_rssi = str(int(HASit.rfm.last_rssi))
HASit.receive_string = str(HASit.receive)
HASit.receive_ascii=""
for i in HASit.receive:
HASit.receive_ascii=HASit.receive_ascii+chr(i)
(HASit.header, HASit.payload) = HASit.receive_ascii.split("|")
(HASit.source, HASit.destination) = HASit.header.split(">")
if whom == 'all':
print (HASit.receive_ascii,':RSSI:',HASit.receive_rssi)
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.OLED.text(HASit.header, 0, 0, 1)
HAShat.OLED.text(HASit.payload, 0, 10, 1)
HAShat.OLED.text("RSSI: " + HASit.receive_rssi, 0, 20, 1)
HAShat.OLED.show()
if (whom == 'BEACON-99') and (HASit.destination == 'BEACON-99'):
print (HASit.receive_ascii,':RSSI:',HASit.receive_rssi)
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.OLED.text(HASit.header, 0, 0, 1)
HAShat.OLED.text(HASit.payload, 0, 10, 1)
HAShat.OLED.text("RSSI: " + HASit.receive_rssi, 0, 20, 1)
HAShat.OLED.show()
if (whom == HASit.destination) and (HASit.destination == HASit.mystation):
print (HASit.receive_ascii,':RSSI:',HASit.receive_rssi)
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.OLED.text(HASit.header, 0, 0, 1)
HAShat.OLED.text(HASit.payload, 0, 10, 1)
HAShat.OLED.text("RSSI: " + HASit.receive_rssi, 0, 20, 1)
HAShat.OLED.show()
time.sleep(0.25)
def rx_oled_scroll():
HAShat.OLED.fill(0)
HAShat.OLED.show()
HASit.ppayload = HASit.receive_ascii.split("|")
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.OLED.text(HASit.ppayload[0], 0, 1, 1)
HAShat.OLED.text("RSSI: " + HASit.receive_rssi, 0, 9, 1)
HAShat.OLED.text(HASit.ppayload[1], 0, 17, 1)
HAShat.OLED.show()
def duckhunt(subpop):
if subpop == "show":
print("Duckhunt Show")
print("message", Ducky.message)
print("interval", Ducky.interval)
print("trigger " + Ducky.trigger + " " + Ducky.trigger_value)
print("action " + Ducky.action)
print("duration", Ducky.duration)
HAShat.OLED.fill(0)
HAShat.OLED.text("message " + str(Ducky.message), 0, 0, 1)
HAShat.OLED.text("interval " + str(Ducky.interval), 0, 8, 1)
HAShat.OLED.text("trigger " + Ducky.trigger + " " + Ducky.trigger_value, 0, 16, 1)
HAShat.OLED.text("action " + Ducky.action, 0, 24, 1)
HAShat.OLED.text("duration " + str(Ducky.duration), 0, 32, 1)
HAShat.OLED.show()
time.sleep(5)
HAShat.OLED.fill(0)
HAShat.OLED.show()
return
HASheader = HASit.mystation + ">" + "BEACON-99"
HASpayload = HASheader + " | " + Ducky.message
while (time.time() < Ducky.duration_stop):
if not HAShat.btnRight.value:
break
if Ducky.trigger != "none": # If there is a TRIGGER
Ducky.trigger(Duck.trigger, Duck.trigger_value) # Call DUCKY.TRIGGER FUNCTION
Ducky.action() # then call ACTION
HASit.transmit(HASpayload)
print(HASpayload)
HAShat.OLED.fill(0)
HAShat.OLED.text(HASheader, 0, 0, 1)
HAShat.OLED.text(Ducky.message, 0, 10, 1)
HAShat.OLED.show()
time.sleep(Ducky.interval)
HAShat.OLED.fill(0)
HAShat.OLED.show()
restart_svc()
time.sleep(0.25)
#
# MAIN
#
HAShat.logo(hvdnLOGO)
# CTRL-C is SIGINT and closes program gracefully
signal.signal(signal.SIGINT, sigs_handler)
signal.signal(signal.SIGTERM, sigs_handler)
# RF and LoRa Config init
HASit.startradio() # rf95 library
HAShat.prevpos = 0 # Tracking OLED cursor Y position
HAShat.currpos = 0 # Tracking OLED cursor Y position
HAShat.menulvl="main_menu" # Current Menu
main_menu()
menu_update()
firstoledline = 0 # First OLED Line position (Y)
lastoledline = 24 # Last OLED Line position (Y)
while True:
#print(HAShat.menulvl, HAShat.currpos)
if not HAShat.btnLeft.value: # Move cursor with left button
time.sleep(0.025) # Inserted to debounce button press
if not HAShat.btnLeft.value: # Left button definitely pressed
if HAShat.currpos >= lastoledline: # If cursor on last OLED line
HAShat.currpos = firstoledline # - then move cursor to first OLED line
HAShat.prevpos = lastoledline # - and then remember the cursor position as last oled line
else:
HAShat.prevpos = HAShat.currpos # Else we first remember what the current cursor position is
HAShat.currpos = HAShat.currpos + 8 # - and advance the cursor position to the next OLED line
if HAShat.menulvl == "main_menu": # Given MENULEVEL, display that MENU
main_menu()
elif HAShat.menulvl == "tx_menu":
tx_menu()
elif HAShat.menulvl == "rx_menu":
rx_menu()
elif HAShat.menulvl == "options_menu":
options_menu()
elif HAShat.menulvl == "game_menu":
game_menu()
else:
pass
if not HAShat.btnMid.value: # Selection within a menu using middle button
time.sleep(.025) # Inserted to debounce button press
if not HAShat.btnMid.value: # Middle button definitely pressed
if HAShat.menulvl == "main_menu" and HAShat.currpos == 0: # If the current menu is MAIN and our cursor is next to TRANSMIT
HAShat.menulvl = "tx_menu" # - change MENULVL to TRANSMIT
HAShat.prevpos = 0
HAShat.currpos == 0 # - reset cursor position to first OLED line
tx_menu() # - Display TRANSMIT menu
elif HAShat.menulvl == "main_menu" and HAShat.currpos == 8: # Else If the current menu is MAIN and our cursor is next to RECEIVE
HAShat.menulvl = "rx_menu" # - change MENULVL to RECEIVE
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
rx_menu() # - Display RECEIVE menu
elif HAShat.menulvl == "main_menu" and HAShat.currpos == 16: # Else If the current menu is MAIN and our cursor is next to DUCKHUNT
HAShat.menulvl = "game_menu" # - change MENULVL to DUCKHUNT
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
game_menu() # - Display DUCKHUNT menu
elif HAShat.menulvl == "main_menu" and HAShat.currpos == 24: # Else If the current menu is MAIN and our cursor is next to OPTIONS
HAShat.menulvl = "options_menu" # - change MENULVL to OPTIONS
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
options_menu() # - Display OPTIONS menu
elif HAShat.menulvl == "tx_menu" and HAShat.currpos == 0: # If the current menu is TRANSMIT and our cursor is next to TX QSL
tx_msg(' QSL ') # - Transmit the message QSL
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
HAShat.menulvl = "tx_menu"
tx_menu()
elif HAShat.menulvl == "tx_menu" and HAShat.currpos == 8: # If the current menu is TRANSMIT and our cursor is next to TX QRZ
tx_msg(' QRZ ') # - Transmit the message QRZ
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
HAShat.menulvl = "tx_menu"
tx_menu()
elif HAShat.menulvl == "tx_menu" and HAShat.currpos == 16: # If the current menu is TRANSMIT and our cursor is next to TX BEACON
tx_beacon() # - Transmit the BEACON message HASit.mybeacon loaded from HASviolet.json
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
HAShat.menulvl = "tx_menu"
tx_menu()
elif HAShat.menulvl == "tx_menu" and HAShat.currpos == 24: # If the current menu is TRANSMIT and our cursor is next to RETURN
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
HAShat.menulvl = "main_menu"
main_menu()
elif HAShat.menulvl == "rx_menu" and HAShat.currpos == 0: # If the current menu is RECEIVE and our cursor is next to RX ALL
HAShat.OLED.fill(0) # - Clear OLED display
HAShat.OLED.show()
HAShat.OLED.text('RX ALL...', 1, 0, 1) # - Say what mode we are in
HAShat.OLED.show()
time.sleep(1)
rx_msg_rfm9x('all') # - Run RECEIVE with no filtering of output. Function returns on right button press
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
HAShat.menulvl = "rx_menu"
rx_menu() # - refresh OLED to put in known menu state
elif HAShat.menulvl == "rx_menu" and HAShat.currpos == 8: # If the current menu is RECEIVE and our cursor is next to RX BEACONS
HAShat.OLED.fill(0) # - Clear OLED display
HAShat.OLED.show()
HAShat.OLED.text('RX BEACONS...', 1, 0, 1) # - Say what mode we are in
HAShat.OLED.show()
time.sleep(1)
rx_msg_rfm9x('BEACON-99') # - Run RECEIVE with filtering on BEACON-99. Function returns on right button press
HAShat.prevpos = 0
HAShat.currpos == 0 # - reset cursor position to first OLED line
HAShat.menulvl == "rx_menu"
rx_menu() # - refresh OLED to put in known menu state
elif HAShat.menulvl == "rx_menu" and HAShat.currpos == 16: # If the current menu is RECEIVE and our cursor is next to RX ME
HAShat.OLED.fill(0) # - Clear OLED display
HAShat.OLED.show()
HAShat.OLED.text('RX ME...', 1, 0, 1) # - Say what mode we are in
HAShat.OLED.show()
time.sleep(1)
rx_msg_rfm9x(HASit.mystation) # - Run RECEIVE with filtering on your call HAS.mystation. Function returns on right button press
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
HAShat.menulvl = "rx_menu"
rx_menu() # - refresh OLED to put in known menu state
elif HAShat.menulvl == "rx_menu" and HAShat.currpos == 24: # If the current menu is RECEIVE and our cursor is next to RETURN
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
HAShat.menulvl = "main_menu"
main_menu()
elif HAShat.menulvl == "game_menu" and HAShat.currpos == 0: # If the current menu is DUCKHUNT and our cursor is next to SHOW DUCK
HAShat.OLED.fill(0) # - Clear OLED display
HAShat.OLED.show()
duckhunt("show")
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
HAShat.menulvl = "game_menu"
game_menu() # - refresh OLED to put in known menu state
elif HAShat.menulvl == "game_menu" and HAShat.currpos == 8: # If the current menu is DUCKHUNT and our cursor is next to RUN DUCK
HAShat.OLED.fill(0) # - Clear OLED display
HAShat.OLED.show()
duckhunt("run")
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
HAShat.menulvl = "game_menu"
game_menu() # - refresh OLED to put in known menu state # - refresh OLED to put in known menu state
elif HAShat.menulvl == "game_menu" and HAShat.currpos == 16: # If the current menu is DUCKHUNT and our cursor is next to RETURN
HAShat.prevpos = 0
HAShat.currpos = 0
HAShat.menulvl = "main_menu" # - Go up one menu level which will be MAIN MENU
main_menu()
elif HAShat.menulvl == "game_menu" and HAShat.currpos == 24: # If the current menu is DUCKHUNT and our cursor is next to ******
HAShat.prevpos = 0
HAShat.currpos = 0
HAShat.menulvl = "main_menu" # - Go up one menu level which will be MAIN MENU
main_menu()
elif HAShat.menulvl == "options_menu" and HAShat.currpos == 0: # If the current menu is OPTIONS and our cursor is next to ABOUT
print(hasDUCKver)
HAShat.OLED.fill(0)
HAShat.OLED.text(hasDUCKver, 0, 10, 1)
HAShat.OLED.show()
time.sleep(3)
HAShat.OLED.fill(0)
HAShat.OLED.show()
HAShat.prevpos = 0
HAShat.currpos == 0 # - reset cursor position to first OLED line
HAShat.menulvl = "options_menu"
options_menu()
elif HAShat.menulvl == "options_menu" and HAShat.currpos == 8: # If the current menu is OPTIONS and our cursor is next to RESTART
HAShat.OLED.fill(0) # - Clear OLED display for shaneless logo display
HAShat.OLED.show()
HAShat.logo(hvdnLOGO) # - Display HVDN logo
restart_svc() # - refresh OLED to put in known menu state
elif HAShat.menulvl == "options_menu" and HAShat.currpos == 16: # If the current menu is OPTIONS and our cursor is next to QUIT
HASit.cleanup() # - Shutdown RF module properly
HAShat.OLED.fill(0) # - Clear OLED display for Goodby message
HAShat.OLED.show()
HAShat.OLED.text('Goodbye...', 0, 10, 1) # - Say Goodbye
HAShat.OLED.show()
time.sleep(2)
HAShat.OLED.fill(0) # - Clear OLED display
HAShat.OLED.show()
exit(0)
elif HAShat.menulvl == "options_menu" and HAShat.currpos == 24: # If the current menu is OPTIONS and our cursor is next to RETURN
HAShat.prevpos = 0
HAShat.currpos = 0 # - reset cursor position to first OLED line
HAShat.menulvl = "main_menu"
main_menu()
else:
pass
if not HAShat.btnRight.value:
time.sleep(0.025)
if not HAShat.btnRight.value:
restart_svc()