24
24
https://github.com/adafruit/circuitpython/releases
25
25
26
26
"""
27
-
27
+ try :
28
+ from typing import Tuple , List , Union , Optional
29
+ from board import UART
30
+ from serial import Serial
31
+ from time import time_struct
32
+ except ImportError :
33
+ pass
28
34
29
35
import time
30
36
import struct
36
42
class RockBlock :
37
43
"""Driver for RockBLOCK Iridium satellite modem."""
38
44
39
- def __init__ (self , uart , baudrate = 19200 ):
45
+ def __init__ (self , uart : Union [ UART , Serial ], baudrate : int = 19200 ) -> None :
40
46
self ._uart = uart
41
47
self ._uart .baudrate = baudrate
42
48
self ._buf_out = None
43
49
self .reset ()
44
50
45
- def _uart_xfer (self , cmd ) :
51
+ def _uart_xfer (self , cmd : str ) -> Tuple [ List [ bytes ]] :
46
52
"""Send AT command and return response as tuple of lines read."""
47
53
self ._uart .reset_input_buffer ()
48
54
self ._uart .write (str .encode ("AT" + cmd + "\r " ))
@@ -58,22 +64,22 @@ def _uart_xfer(self, cmd):
58
64
59
65
return tuple (resp )
60
66
61
- def reset (self ):
67
+ def reset (self ) -> None :
62
68
"""Perform a software reset."""
63
69
self ._uart_xfer ("&F0" ) # factory defaults
64
70
self ._uart_xfer ("&K0" ) # flow control off
65
71
66
- def _transfer_buffer (self ):
72
+ def _transfer_buffer (self ) -> None :
67
73
"""Copy out buffer to in buffer to simulate receiving a message."""
68
74
self ._uart_xfer ("+SBDTC" )
69
75
70
76
@property
71
- def data_out (self ):
77
+ def data_out (self ) -> None :
72
78
"""The binary data in the outbound buffer."""
73
79
return self ._buf_out
74
80
75
81
@data_out .setter
76
- def data_out (self , buf ) :
82
+ def data_out (self , buf : bytes ) -> None :
77
83
if buf is None :
78
84
# clear the buffer
79
85
resp = self ._uart_xfer ("+SBDD0" )
@@ -100,7 +106,7 @@ def data_out(self, buf):
100
106
self ._buf_out = buf
101
107
102
108
@property
103
- def text_out (self ):
109
+ def text_out (self ) -> Optional [ str ] :
104
110
"""The text in the outbound buffer."""
105
111
text = None
106
112
# TODO: add better check for non-text in buffer
@@ -112,15 +118,15 @@ def text_out(self):
112
118
return text
113
119
114
120
@text_out .setter
115
- def text_out (self , text ) :
121
+ def text_out (self , text : str ) -> None :
116
122
if not isinstance (text , str ):
117
123
raise ValueError ("Only strings allowed." )
118
124
if len (text ) > 120 :
119
125
raise ValueError ("Text size limited to 120 bytes." )
120
126
self .data_out = str .encode (text )
121
127
122
128
@property
123
- def data_in (self ):
129
+ def data_in (self ) -> Optional [ List [ bytes ]] :
124
130
"""The binary data in the inbound buffer."""
125
131
data = None
126
132
if self .status [2 ] == 1 :
@@ -130,7 +136,7 @@ def data_in(self):
130
136
return data
131
137
132
138
@data_in .setter
133
- def data_in (self , buf ) :
139
+ def data_in (self , buf : bytes ) -> None :
134
140
if buf is not None :
135
141
raise ValueError ("Can only set in buffer to None to clear." )
136
142
resp = self ._uart_xfer ("+SBDD1" )
@@ -139,7 +145,7 @@ def data_in(self, buf):
139
145
raise RuntimeError ("Error clearing buffer." )
140
146
141
147
@property
142
- def text_in (self ):
148
+ def text_in (self ) -> Optional [ str ] :
143
149
"""The text in the inbound buffer."""
144
150
text = None
145
151
if self .status [2 ] == 1 :
@@ -151,10 +157,10 @@ def text_in(self):
151
157
return text
152
158
153
159
@text_in .setter
154
- def text_in (self , text ) :
160
+ def text_in (self , text : str ) -> None :
155
161
self .data_in = text
156
162
157
- def satellite_transfer (self , location = None ):
163
+ def satellite_transfer (self , location : str = None ) -> Tuple [ Optional [ str ], ...] :
158
164
"""Initiate a Short Burst Data transfer with satellites."""
159
165
status = (None ,) * 6
160
166
if location :
@@ -170,7 +176,7 @@ def satellite_transfer(self, location=None):
170
176
return tuple (status )
171
177
172
178
@property
173
- def status (self ):
179
+ def status (self ) -> Tuple [ Optional [ str ], ...] :
174
180
"""Return tuple of Short Burst Data status."""
175
181
resp = self ._uart_xfer ("+SBDSX" )
176
182
if resp [- 1 ].strip ().decode () == "OK" :
@@ -179,27 +185,27 @@ def status(self):
179
185
return (None ,) * 6
180
186
181
187
@property
182
- def model (self ):
188
+ def model (self ) -> Optional [ str ] :
183
189
"""Return modem model."""
184
190
resp = self ._uart_xfer ("+GMM" )
185
191
if resp [- 1 ].strip ().decode () == "OK" :
186
192
return resp [1 ].strip ().decode ()
187
193
return None
188
194
189
195
@property
190
- def serial_number (self ):
196
+ def serial_number (self ) -> Optional [ str ] :
191
197
"""Modem's serial number, also known as the modem's IMEI.
192
198
193
199
Returns
194
- string
200
+ str | None
195
201
"""
196
202
resp = self ._uart_xfer ("+CGSN" )
197
203
if resp [- 1 ].strip ().decode () == "OK" :
198
204
return resp [1 ].strip ().decode ()
199
205
return None
200
206
201
207
@property
202
- def signal_quality (self ):
208
+ def signal_quality (self ) -> int :
203
209
"""Signal Quality also known as the Received Signal Strength Indicator (RSSI).
204
210
205
211
Values returned are 0 to 5, where 0 is no signal (0 bars) and 5 is strong signal (5 bars).
@@ -217,7 +223,7 @@ def signal_quality(self):
217
223
return None
218
224
219
225
@property
220
- def revision (self ):
226
+ def revision (self ) -> Tuple [ Optional [ str ], ...] :
221
227
"""Modem's internal component firmware revisions.
222
228
223
229
For example: Call Processor Version, Modem DSP Version, DBB Version (ASIC),
@@ -237,7 +243,7 @@ def revision(self):
237
243
return (None ,) * 7
238
244
239
245
@property
240
- def ring_alert (self ):
246
+ def ring_alert (self ) -> Optional [ bool ] :
241
247
"""The current ring indication mode.
242
248
243
249
False means Ring Alerts are disabled, and True means Ring Alerts are enabled.
@@ -255,7 +261,7 @@ def ring_alert(self):
255
261
return None
256
262
257
263
@ring_alert .setter
258
- def ring_alert (self , value ) :
264
+ def ring_alert (self , value : Union [ int , bool ]) -> Optional [ bool ] :
259
265
if value in (True , False ):
260
266
resp = self ._uart_xfer ("+SBDMTA=" + str (int (value )))
261
267
if resp [- 1 ].strip ().decode () == "OK" :
@@ -266,7 +272,7 @@ def ring_alert(self, value):
266
272
)
267
273
268
274
@property
269
- def ring_indication (self ):
275
+ def ring_indication (self ) -> Tuple [ Optional [ str ], ...] :
270
276
"""The ring indication status.
271
277
272
278
Returns the reason for the most recent assertion of the Ring Indicate signal.
@@ -294,7 +300,7 @@ def ring_indication(self):
294
300
return (None ,) * 2
295
301
296
302
@property
297
- def geolocation (self ):
303
+ def geolocation (self ) -> Union [ Tuple [ int , int , int , time_struct ], Tuple [ None , None , None , None ]] :
298
304
"""Most recent geolocation of the modem as measured by the Iridium constellation
299
305
including a timestamp of when geolocation measurement was made.
300
306
@@ -358,7 +364,7 @@ def geolocation(self):
358
364
return (None ,) * 4
359
365
360
366
@property
361
- def system_time (self ):
367
+ def system_time (self ) -> Optional [ time_struct ] :
362
368
"""Current date and time as given by the Iridium network.
363
369
364
370
The system time is available and valid only after the ISU has registered with
@@ -405,7 +411,7 @@ def system_time(self):
405
411
return None
406
412
407
413
@property
408
- def energy_monitor (self ):
414
+ def energy_monitor (self ) -> Optional [ int ] :
409
415
"""The current accumulated energy usage estimate in microamp hours.
410
416
411
417
Returns an estimate of the charge taken from the +5V supply to the modem,
@@ -436,7 +442,7 @@ def energy_monitor(self):
436
442
return None
437
443
438
444
@energy_monitor .setter
439
- def energy_monitor (self , value ) :
445
+ def energy_monitor (self , value : int ) -> Optional [ int ] :
440
446
if 0 <= value <= 67108863 : # 0 to 2^26 - 1
441
447
resp = self ._uart_xfer ("+GEMON=" + str (value ))
442
448
if resp [- 1 ].strip ().decode () == "OK" :
0 commit comments