1
+ # -*- encoding: utf-8 -*-
2
+ '''
3
+ @File : _imu_pro.py
4
+ @Time : 2023/11/06 xx:xx:xx
5
+ @Author : TONG YIHAN
6
+
7
+ @License : (C)Copyright 2015-2023, M5STACK
8
+ @Desc : The LoRaE220 Unit is a LoRa communication module, designed for the 920MHz frequency band.
9
+ '''
10
+ # UART: ['any', 'read', 'readinto', 'readline', 'write', 'INV_CTS', 'INV_RTS', 'INV_RX', 'INV_TX', 'deinit', 'init', 'sendbreak']
11
+ import machine
12
+ import time
13
+ import _thread
14
+
15
+ class Def :
16
+ # Baud Rate Values
17
+ BAUD_1200 = 0b000
18
+ BAUD_2400 = 0b001
19
+ BAUD_4800 = 0b010
20
+ BAUD_9600 = 0b011
21
+ BAUD_19200 = 0b100
22
+ BAUD_38400 = 0b101
23
+ BAUD_57600 = 0b110
24
+ BAUD_115200 = 0b111
25
+
26
+ # Data Rate Values
27
+ BW125K_SF5 = 0b00000
28
+ BW125K_SF6 = 0b00100
29
+ BW125K_SF7 = 0b01000
30
+ BW125K_SF8 = 0b01100
31
+ BW125K_SF9 = 0b10000
32
+ BW250K_SF5 = 0b00001
33
+ BW250K_SF6 = 0b00101
34
+ BW250K_SF7 = 0b01001
35
+ BW250K_SF8 = 0b01101
36
+ BW250K_SF9 = 0b10001
37
+ BW250K_SF10 = 0b10101
38
+ BW500K_SF5 = 0b00010
39
+ BW500K_SF6 = 0b00110
40
+ BW500K_SF7 = 0b01010
41
+ BW500K_SF8 = 0b01110
42
+ BW500K_SF9 = 0b10010
43
+ BW500K_SF10 = 0b10110
44
+ BW500K_SF11 = 0b11010
45
+
46
+ # Subpacket Size Values
47
+ SUBPACKET_200_BYTE = 0b00
48
+ SUBPACKET_128_BYTE = 0b01
49
+ SUBPACKET_64_BYTE = 0b10
50
+ SUBPACKET_32_BYTE = 0b11
51
+
52
+ # RSSI Ambient Noise Flag Values
53
+ RSSI_AMBIENT_NOISE_ENABLE = 0b1
54
+ RSSI_AMBIENT_NOISE_DISABLE = 0b0
55
+
56
+ # Transmitting Power Values
57
+ TX_POWER_13dBm = 0b01
58
+ TX_POWER_7dBm = 0b10
59
+ TX_POWER_0dBm = 0b11
60
+
61
+ # RSSI Byte Flag Values
62
+ RSSI_BYTE_ENABLE = 0b1
63
+ RSSI_BYTE_DISABLE = 0b0
64
+
65
+ # Transmission Method Type Values
66
+ UART_TT_MODE = 0b0
67
+ UART_P2P_MODE = 0b1
68
+
69
+ # LBT Flag Values
70
+ LBT_ENABLE = 0b1
71
+ LBT_DISABLE = 0b0
72
+
73
+ # WOR Cycle Values
74
+ WOR_500MS = 0b000
75
+ WOR_1000MS = 0b001
76
+ WOR_1500MS = 0b010
77
+ WOR_2000MS = 0b011
78
+
79
+ class LoRaE220JPUnit :
80
+ def __init__ (self , port , port_id = 1 ) -> None :
81
+ # print('Port: ', port)
82
+ self .uart = machine .UART (port_id , tx = port [1 ], rx = port [0 ])
83
+ self .uart .init (9600 , bits = 0 , parity = None , stop = 1 , rxbuf = 1024 )
84
+ self .recv_running = False
85
+ self .receive_callback = None
86
+
87
+ def _conf_range (self , target , min , max ):
88
+ return min <= target <= max
89
+
90
+ def setup (self , own_address = 0 , own_channel = 0 , encryption_key = 0x2333 , air_data_rate = Def .BW125K_SF9 , subpacket_size = Def .SUBPACKET_200_BYTE , rssi_ambient_noise_flag = Def .RSSI_AMBIENT_NOISE_ENABLE , transmitting_power = Def .TX_POWER_13dBm ,rssi_byte_flag = Def .RSSI_BYTE_ENABLE , transmission_method_type = Def .UART_P2P_MODE , lbt_flag = Def .LBT_DISABLE , wor_cycle = Def .WOR_2000MS ):
91
+ if not self ._conf_range (own_channel , 0 , 30 ):
92
+ return False
93
+
94
+ self .subpacket_size = subpacket_size
95
+ self .max_len = 0
96
+ if self .subpacket_size == Def .SUBPACKET_128_BYTE :
97
+ self .max_len = 128
98
+ elif self .subpacket_size == Def .SUBPACKET_64_BYTE :
99
+ self .max_len = 64
100
+ elif self .subpacket_size == Def .SUBPACKET_32_BYTE :
101
+ self .max_len = 32
102
+ else :
103
+ self .max_len = 200
104
+
105
+ command = [0xc0 , 0x00 , 0x08 ]
106
+ ADDH = own_address >> 8
107
+ ADDL = own_address & 0xff
108
+ command .extend ([ADDH , ADDL ])
109
+
110
+ REG0 = (Def .BAUD_9600 << 5 ) | air_data_rate
111
+ command .append (REG0 )
112
+
113
+ REG1 = (subpacket_size << 6 ) | (rssi_ambient_noise_flag << 5 ) | transmitting_power
114
+ command .append (REG1 )
115
+
116
+ command .append (own_channel )
117
+
118
+ REG3 = (rssi_byte_flag << 7 ) | (transmission_method_type << 6 ) | (lbt_flag << 4 ) | wor_cycle
119
+ command .append (REG3 )
120
+
121
+ CRYPT_H = encryption_key >> 8
122
+ CRYPT_L = encryption_key & 0xff
123
+ command .extend ([CRYPT_H , CRYPT_L ])
124
+
125
+ self .uart .write (bytes (command ))
126
+
127
+ time .sleep_ms (100 ) # wait for response
128
+
129
+ response = []
130
+ if self .uart .any ():
131
+ response = self .uart .read ()
132
+
133
+ if len (response ) != len (command ):
134
+ print ("[WARN] Setup LoRa unit failed, please check connection and wether unit in configuration mode." )
135
+ return False
136
+ return True
137
+
138
+ def _recvCallback (self ):
139
+ response = bytes ()
140
+ rssi = 0
141
+ _ = self .uart .read ()
142
+ while self .recv_running :
143
+ if self .uart .any ():
144
+ response += self .uart .read ()
145
+ elif len (response ) > 0 :
146
+ time .sleep_ms (10 )
147
+ if not self .uart .any ():
148
+ rssi = int (response [- 1 ]) - 256
149
+ self .receive_callback (response [:- 1 ], rssi )
150
+ response = bytes ()
151
+ time .sleep_ms (10 )
152
+
153
+ def receiveNoneBlock (self , receive_callback ):
154
+ if not self .recv_running :
155
+ self .recv_running = True
156
+ self .receive_callback = receive_callback
157
+ _thread .start_new_thread (self ._recvCallback , ())
158
+
159
+ def stopReceive (self ):
160
+ self .recv_running = False
161
+ time .sleep_ms (50 )
162
+ self .receive_callback = None
163
+
164
+ def receive (self , timeout = 1000 ):
165
+ start = time .ticks_ms ()
166
+ response = bytes ()
167
+ rssi = 0
168
+ while True :
169
+ if time .ticks_ms () - start > timeout :
170
+ break
171
+
172
+ if self .uart .any ():
173
+ response += self .uart .read ()
174
+
175
+ elif len (response ) > 0 :
176
+ time .sleep_ms (10 )
177
+ if not self .uart .any ():
178
+ rssi = int (response [- 1 ]) - 256
179
+ break
180
+ time .sleep_ms (10 )
181
+ return response [:- 1 ], rssi
182
+
183
+ def send (self , target_address , target_channel , send_data ):
184
+ if type (send_data ) is str :
185
+ send_data = send_data .encode ('utf-8' )
186
+
187
+ if type (send_data ) is list :
188
+ send_data = bytearray (send_data )
189
+
190
+ if len (send_data ) > self .max_len : # Adjust based on allowed packet size
191
+ print ('ERROR: Length of send_data over %d bytes.' % self .max_len )
192
+ return False
193
+
194
+ target_address_H = target_address >> 8
195
+ target_address_L = target_address & 0xff
196
+
197
+ frame = bytearray ([target_address_H , target_address_L , target_channel ])
198
+ frame .extend (send_data )
199
+
200
+ self .uart .write (frame )
201
+
202
+ return True
0 commit comments