forked from alexischiang/SmartFishTank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rasp.py
executable file
·101 lines (82 loc) · 2.54 KB
/
rasp.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
import socket
import time
import sys
from time import sleep
from SX127x.LoRa import *
from SX127x.LoRaArgumentParser import LoRaArgumentParser
from SX127x.board_config import BOARD
#HOST = '172.13.231.110'
#PORT = 10042
BOARD.setup()
parser = LoRaArgumentParser("A simple LoRa beacon")
parser.add_argument('--single', '-S', dest='single', default=False, action="store_true", help="Single transmission")
parser.add_argument('--wait', '-w', dest='wait', default=1, action="store", type=float,
help="Waiting time between transmissions (default is 0s)")
class LoRaBeacon(LoRa):
tx_counter = 0
def __init__(self, verbose=False):
super(LoRaBeacon, self).__init__(verbose)
self.set_mode(MODE.SLEEP)
self.set_dio_mapping([1, 0, 0, 0, 0, 0])
def on_rx_done(self):
print("\nRxDone")
print(self.get_irq_flags())
print(map(hex, self.read_payload(nocheck=True)))
self.set_mode(MODE.SLEEP)
self.reset_ptr_rx()
self.set_mode(MODE.RXCONT)
def on_tx_done(self):
global args
self.set_mode(MODE.STDBY)
self.clear_irq_flags(TxDone=1)
sys.stdout.flush()
self.tx_counter += 1
sys.stdout.write("\rtx #%d" % self.tx_counter)
if args.single:
print
sys.exit(0)
BOARD.led_off()
sleep(args.wait)
self.write_payload([0x0f])
BOARD.led_on()
self.set_mode(MODE.TX)
def on_cad_done(self):
print("\non_CadDone")
print(self.get_irq_flags())
def on_rx_timeout(self):
print("\non_RxTimeout")
print(self.get_irq_flags())
def on_valid_header(self):
print("\non_ValidHeader")
print(self.get_irq_flags())
def on_payload_crc_error(self):
print("\non_PayloadCrcError")
print(self.get_irq_flags())
def on_fhss_change_channel(self):
print("\non_FhssChangeChannel")
print(self.get_irq_flags())
def start(self):
global args
self.set_freq(433)
sys.stdout.write("\rstart")
BOARD.led_on()
while True:
sleep(1)
lora = LoRaBeacon(verbose=False)
args = parser.parse_args(lora)
lora.set_pa_config(pa_select=1)
print(lora)
# assert(lora.get_lna()['lna_gain'] == GAIN.NOT_USED)
assert (lora.get_agc_auto_on() == 1)
try:
lora.start()
except KeyboardInterrupt:
sys.stdout.flush()
print("")
sys.stderr.write("KeyboardInterrupt\n")
finally:
sys.stdout.flush()
print("")
lora.set_mode(MODE.SLEEP)
print(lora)
BOARD.teardown()