Skip to content

Commit a6d96e8

Browse files
authored
Merge pull request #30 from zachariahpifer/add-type-annotations
Add type annotations
2 parents e981244 + 78126af commit a6d96e8

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

adafruit_rockblock.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@
2424
https://github.com/adafruit/circuitpython/releases
2525
2626
"""
27+
from __future__ import annotations
2728

29+
try:
30+
from typing import Tuple, Union, Optional
31+
from busio import UART
32+
from serial import Serial
33+
except ImportError:
34+
pass
2835

2936
import time
3037
import struct
@@ -36,13 +43,13 @@
3643
class RockBlock:
3744
"""Driver for RockBLOCK Iridium satellite modem."""
3845

39-
def __init__(self, uart, baudrate=19200):
46+
def __init__(self, uart: Union[UART, Serial], baudrate: int = 19200) -> None:
4047
self._uart = uart
4148
self._uart.baudrate = baudrate
4249
self._buf_out = None
4350
self.reset()
4451

45-
def _uart_xfer(self, cmd):
52+
def _uart_xfer(self, cmd: str) -> Tuple[bytes, ...]:
4653
"""Send AT command and return response as tuple of lines read."""
4754
self._uart.reset_input_buffer()
4855
self._uart.write(str.encode("AT" + cmd + "\r"))
@@ -58,22 +65,22 @@ def _uart_xfer(self, cmd):
5865

5966
return tuple(resp)
6067

61-
def reset(self):
68+
def reset(self) -> None:
6269
"""Perform a software reset."""
6370
self._uart_xfer("&F0") # factory defaults
6471
self._uart_xfer("&K0") # flow control off
6572

66-
def _transfer_buffer(self):
73+
def _transfer_buffer(self) -> None:
6774
"""Copy out buffer to in buffer to simulate receiving a message."""
6875
self._uart_xfer("+SBDTC")
6976

7077
@property
71-
def data_out(self):
78+
def data_out(self) -> Optional[bytes]:
7279
"""The binary data in the outbound buffer."""
7380
return self._buf_out
7481

7582
@data_out.setter
76-
def data_out(self, buf):
83+
def data_out(self, buf: bytes) -> None:
7784
if buf is None:
7885
# clear the buffer
7986
resp = self._uart_xfer("+SBDD0")
@@ -100,7 +107,7 @@ def data_out(self, buf):
100107
self._buf_out = buf
101108

102109
@property
103-
def text_out(self):
110+
def text_out(self) -> Optional[str]:
104111
"""The text in the outbound buffer."""
105112
text = None
106113
# TODO: add better check for non-text in buffer
@@ -112,15 +119,15 @@ def text_out(self):
112119
return text
113120

114121
@text_out.setter
115-
def text_out(self, text):
122+
def text_out(self, text: str) -> None:
116123
if not isinstance(text, str):
117124
raise ValueError("Only strings allowed.")
118125
if len(text) > 120:
119126
raise ValueError("Text size limited to 120 bytes.")
120127
self.data_out = str.encode(text)
121128

122129
@property
123-
def data_in(self):
130+
def data_in(self) -> Optional[bytes]:
124131
"""The binary data in the inbound buffer."""
125132
data = None
126133
if self.status[2] == 1:
@@ -130,7 +137,7 @@ def data_in(self):
130137
return data
131138

132139
@data_in.setter
133-
def data_in(self, buf):
140+
def data_in(self, buf: bytes) -> None:
134141
if buf is not None:
135142
raise ValueError("Can only set in buffer to None to clear.")
136143
resp = self._uart_xfer("+SBDD1")
@@ -139,7 +146,7 @@ def data_in(self, buf):
139146
raise RuntimeError("Error clearing buffer.")
140147

141148
@property
142-
def text_in(self):
149+
def text_in(self) -> Optional[str]:
143150
"""The text in the inbound buffer."""
144151
text = None
145152
if self.status[2] == 1:
@@ -151,10 +158,10 @@ def text_in(self):
151158
return text
152159

153160
@text_in.setter
154-
def text_in(self, text):
161+
def text_in(self, text: bytes) -> None:
155162
self.data_in = text
156163

157-
def satellite_transfer(self, location=None):
164+
def satellite_transfer(self, location: str = None) -> Tuple[Optional[int], ...]:
158165
"""Initiate a Short Burst Data transfer with satellites."""
159166
status = (None,) * 6
160167
if location:
@@ -170,7 +177,7 @@ def satellite_transfer(self, location=None):
170177
return tuple(status)
171178

172179
@property
173-
def status(self):
180+
def status(self) -> Tuple[Optional[int], ...]:
174181
"""Return tuple of Short Burst Data status."""
175182
resp = self._uart_xfer("+SBDSX")
176183
if resp[-1].strip().decode() == "OK":
@@ -179,27 +186,27 @@ def status(self):
179186
return (None,) * 6
180187

181188
@property
182-
def model(self):
189+
def model(self) -> Optional[str]:
183190
"""Return modem model."""
184191
resp = self._uart_xfer("+GMM")
185192
if resp[-1].strip().decode() == "OK":
186193
return resp[1].strip().decode()
187194
return None
188195

189196
@property
190-
def serial_number(self):
197+
def serial_number(self) -> Optional[str]:
191198
"""Modem's serial number, also known as the modem's IMEI.
192199
193200
Returns
194-
string
201+
str | None
195202
"""
196203
resp = self._uart_xfer("+CGSN")
197204
if resp[-1].strip().decode() == "OK":
198205
return resp[1].strip().decode()
199206
return None
200207

201208
@property
202-
def signal_quality(self):
209+
def signal_quality(self) -> Optional[int]:
203210
"""Signal Quality also known as the Received Signal Strength Indicator (RSSI).
204211
205212
Values returned are 0 to 5, where 0 is no signal (0 bars) and 5 is strong signal (5 bars).
@@ -217,7 +224,7 @@ def signal_quality(self):
217224
return None
218225

219226
@property
220-
def revision(self):
227+
def revision(self) -> Tuple[Optional[str], ...]:
221228
"""Modem's internal component firmware revisions.
222229
223230
For example: Call Processor Version, Modem DSP Version, DBB Version (ASIC),
@@ -237,7 +244,7 @@ def revision(self):
237244
return (None,) * 7
238245

239246
@property
240-
def ring_alert(self):
247+
def ring_alert(self) -> Optional[bool]:
241248
"""The current ring indication mode.
242249
243250
False means Ring Alerts are disabled, and True means Ring Alerts are enabled.
@@ -255,7 +262,7 @@ def ring_alert(self):
255262
return None
256263

257264
@ring_alert.setter
258-
def ring_alert(self, value):
265+
def ring_alert(self, value: Union[int, bool]) -> Optional[bool]:
259266
if value in (True, False):
260267
resp = self._uart_xfer("+SBDMTA=" + str(int(value)))
261268
if resp[-1].strip().decode() == "OK":
@@ -266,7 +273,7 @@ def ring_alert(self, value):
266273
)
267274

268275
@property
269-
def ring_indication(self):
276+
def ring_indication(self) -> Tuple[Optional[str], ...]:
270277
"""The ring indication status.
271278
272279
Returns the reason for the most recent assertion of the Ring Indicate signal.
@@ -294,7 +301,9 @@ def ring_indication(self):
294301
return (None,) * 2
295302

296303
@property
297-
def geolocation(self):
304+
def geolocation(
305+
self,
306+
) -> Union[Tuple[int, int, int, time.struct_time], Tuple[None, None, None, None]]:
298307
"""Most recent geolocation of the modem as measured by the Iridium constellation
299308
including a timestamp of when geolocation measurement was made.
300309
@@ -313,20 +322,20 @@ def geolocation(self):
313322
This geolocation coordinate system is known as ECEF (acronym earth-centered, earth-fixed),
314323
also known as ECR (initialism for earth-centered rotational)
315324
316-
<timestamp> is a time_struct
325+
<timestamp> is a time.struct_time
317326
The timestamp is assigned by the modem when the geolocation grid code received from
318327
the network is stored to the modem's internal memory.
319328
320329
The timestamp used by the modem is Iridium system time, which is a running count of
321330
90 millisecond intervals, since Sunday May 11, 2014, at 14:23:55 UTC (the most recent
322331
Iridium epoch).
323332
The timestamp returned by the modem is a 32-bit integer displayed in hexadecimal form.
324-
We convert the modem's timestamp and return it as a time_struct.
333+
We convert the modem's timestamp and return it as a time.struct_time.
325334
326335
The system time value is always expressed in UTC time.
327336
328337
Returns a tuple:
329-
(int, int, int, time_struct)
338+
(int, int, int, time.struct_time)
330339
"""
331340
resp = self._uart_xfer("-MSGEO")
332341
if resp[-1].strip().decode() == "OK":
@@ -358,7 +367,7 @@ def geolocation(self):
358367
return (None,) * 4
359368

360369
@property
361-
def system_time(self):
370+
def system_time(self) -> Optional[time.struct_time]:
362371
"""Current date and time as given by the Iridium network.
363372
364373
The system time is available and valid only after the ISU has registered with
@@ -371,12 +380,12 @@ def system_time(self):
371380
90 millisecond intervals, since Sunday May 11, 2014, at 14:23:55 UTC (the most recent
372381
Iridium epoch).
373382
The timestamp returned by the modem is a 32-bit integer displayed in hexadecimal form.
374-
We convert the modem's timestamp and return it as a time_struct.
383+
We convert the modem's timestamp and return it as a time.struct_time.
375384
376385
The system time value is always expressed in UTC time.
377386
378387
Returns:
379-
time_struct
388+
time.struct_time
380389
"""
381390
resp = self._uart_xfer("-MSSTM")
382391
if resp[-1].strip().decode() == "OK":
@@ -405,7 +414,7 @@ def system_time(self):
405414
return None
406415

407416
@property
408-
def energy_monitor(self):
417+
def energy_monitor(self) -> Optional[int]:
409418
"""The current accumulated energy usage estimate in microamp hours.
410419
411420
Returns an estimate of the charge taken from the +5V supply to the modem,
@@ -436,7 +445,7 @@ def energy_monitor(self):
436445
return None
437446

438447
@energy_monitor.setter
439-
def energy_monitor(self, value):
448+
def energy_monitor(self, value: int) -> Optional[int]:
440449
if 0 <= value <= 67108863: # 0 to 2^26 - 1
441450
resp = self._uart_xfer("+GEMON=" + str(value))
442451
if resp[-1].strip().decode() == "OK":

optional_requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# SPDX-FileCopyrightText: 2022 Alec Delaney, for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
4+
5+
pyserial

0 commit comments

Comments
 (0)