24
24
from micropython import const
25
25
from .adafruit_fona import FONA , REPLY_OK
26
26
27
+ try :
28
+ from typing import Optional , Tuple , Union , Literal
29
+ from busio import UART
30
+ from digitalio import DigitalInOut
31
+ except ImportError :
32
+ pass
33
+
27
34
__version__ = "0.0.0-auto.0"
28
35
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FONA.git"
29
36
32
39
33
40
class FONA3G (FONA ):
34
41
"""FONA 3G module interface.
35
- :param ~busio.uart UART: FONA UART connection.
36
- :param ~digialio RST: FONA RST pin.
37
- :param ~digialio RI: Optional FONA Ring Interrupt (RI) pin.
38
- :param bool debug: Enable debugging output.
39
42
43
+ :param ~busio.uart uart: FONA UART connection.
44
+ :param ~DigitalInOut rst: FONA RST pin.
45
+ :param ~DigitalInOut ri: Optional FONA Ring Interrupt (RI) pin.
46
+ :param bool debug: Enable debugging output.
40
47
"""
41
48
42
- def __init__ (self , uart , rst , ri = None , debug = False ):
49
+ def __init__ (
50
+ self ,
51
+ uart : UART ,
52
+ rst : DigitalInOut ,
53
+ ri : Optional [DigitalInOut ] = None ,
54
+ debug : bool = False ,
55
+ ) -> None :
43
56
uart .baudrate = 4800
44
57
super ().__init__ (uart , rst , ri , debug )
45
58
46
- def set_baudrate (self , baudrate ) :
59
+ def set_baudrate (self , baudrate : int ) -> bool :
47
60
"""Sets the FONA's UART baudrate."""
48
61
if not self ._send_check_reply (
49
62
b"AT+IPREX=" + str (baudrate ).encode (), reply = REPLY_OK
@@ -52,14 +65,14 @@ def set_baudrate(self, baudrate):
52
65
return True
53
66
54
67
@property
55
- def gps (self ):
68
+ def gps (self ) -> bool :
56
69
"""Module's GPS status."""
57
70
if not self ._send_check_reply (b"AT+CGPS?" , reply = b"+CGPS: 1,1" ):
58
71
return False
59
72
return True
60
73
61
74
@gps .setter
62
- def gps (self , gps_on = False ):
75
+ def gps (self , gps_on : bool = False ) -> bool :
63
76
# check if GPS is already enabled
64
77
if not self ._send_parse_reply (b"AT+CGPS?" , b"+CGPS: " ):
65
78
return False
@@ -77,25 +90,30 @@ def gps(self, gps_on=False):
77
90
return True
78
91
79
92
@property
80
- def ue_system_info (self ):
93
+ def ue_system_info (self ) -> bool :
81
94
"""UE System status."""
82
95
self ._send_parse_reply (b"AT+CPSI?\r \n " , b"+CPSI: " )
83
96
if not self ._buf == "GSM" or self ._buf == "WCDMA" : # 5.15
84
97
return False
85
98
return True
86
99
87
100
@property
88
- def local_ip (self ):
101
+ def local_ip (self ) -> Optional [ str ] :
89
102
"""Module's local IP address, None if not set."""
90
103
if not self ._send_parse_reply (b"AT+IPADDR" , b"+IPADDR:" ):
91
104
return None
92
105
return self ._buf
93
106
94
107
# pylint: disable=too-many-return-statements
95
- def set_gprs (self , apn = None , enable = True ):
108
+ def set_gprs (
109
+ self ,
110
+ apn : Optional [Tuple [str , Optional [str ], Optional [str ]]] = None ,
111
+ enable : bool = True ,
112
+ ) -> bool :
96
113
"""Configures and brings up GPRS.
97
- :param bool enable: Enables or disables GPRS.
98
114
115
+ :param tuple apn: APN configuration settings
116
+ :param bool enable: Enables or disables GPRS.
99
117
"""
100
118
if enable :
101
119
if not self ._send_check_reply (b"AT+CGATT=1" , reply = REPLY_OK , timeout = 10000 ):
@@ -142,24 +160,25 @@ def set_gprs(self, apn=None, enable=True):
142
160
### Socket API (TCP, UDP) ###
143
161
144
162
@property
145
- def tx_timeout (self ):
163
+ def tx_timeout (self ) -> bool :
146
164
"""CIPSEND timeout, in milliseconds."""
147
165
self ._read_line ()
148
166
if not self ._send_parse_reply (b"AT+CIPTIMEOUT?" , b"+CIPTIMEOUT:" , idx = 2 ):
149
167
return False
150
168
return True
151
169
152
170
@tx_timeout .setter
153
- def tx_timeout (self , timeout ) :
171
+ def tx_timeout (self , timeout : int ) -> bool :
154
172
self ._read_line ()
155
173
if not self ._send_check_reply (
156
174
b"AT+CIPTIMEOUT=" + str (timeout ).encode (), reply = REPLY_OK
157
175
):
158
176
return False
159
177
return True
160
178
161
- def get_host_by_name (self , hostname ) :
179
+ def get_host_by_name (self , hostname : str ) -> Union [ str , Literal [ False ]] :
162
180
"""Converts a hostname to a 4-byte IP address.
181
+
163
182
:param str hostname: Domain name.
164
183
"""
165
184
self ._read_line ()
@@ -175,7 +194,7 @@ def get_host_by_name(self, hostname):
175
194
return False
176
195
return self ._buf
177
196
178
- def get_socket (self ):
197
+ def get_socket (self ) -> int :
179
198
"""Returns an unused socket."""
180
199
if self ._debug :
181
200
print ("*** Get socket" )
@@ -198,14 +217,16 @@ def get_socket(self):
198
217
print ("Allocated socket #%d" % socket )
199
218
return socket
200
219
201
- def socket_connect (self , sock_num , dest , port , conn_mode = 0 ):
220
+ def socket_connect (
221
+ self , sock_num : int , dest : str , port : int , conn_mode : int = 0
222
+ ) -> bool :
202
223
"""Connects to a destination IP address or hostname.
203
224
By default, we use conn_mode TCP_MODE but we may also use UDP_MODE.
225
+
204
226
:param int sock_num: Desired socket number
205
227
:param str dest: Destination dest address.
206
228
:param int port: Destination dest port.
207
229
:param int conn_mode: Connection mode (TCP/UDP)
208
-
209
230
"""
210
231
if self ._debug :
211
232
print (
@@ -236,8 +257,11 @@ def socket_connect(self, sock_num, dest, port, conn_mode=0):
236
257
return False
237
258
return True
238
259
239
- def remote_ip (self , sock_num ):
240
- """Returns the IP address of the remote connection."""
260
+ def remote_ip (self , sock_num : int ) -> str :
261
+ """Returns the IP address of the remote connection.
262
+
263
+ :param int sock_num: Desired socket number
264
+ """
241
265
self ._read_line ()
242
266
assert (
243
267
sock_num < FONA_MAX_SOCKETS
@@ -254,12 +278,12 @@ def remote_ip(self, sock_num):
254
278
self ._read_line () # eat the rest of '+CIPOPEN' responses
255
279
return ip_addr
256
280
257
- def socket_write (self , sock_num , buffer , timeout = 120000 ):
281
+ def socket_write (self , sock_num : int , buffer : bytes , timeout : int = 120000 ) -> bool :
258
282
"""Writes len(buffer) bytes to the socket.
283
+
259
284
:param int sock_num: Desired socket number to write to.
260
285
:param bytes buffer: Bytes to write to socket.
261
286
:param int timeout: Socket write timeout, in milliseconds. Defaults to 120000ms.
262
-
263
287
"""
264
288
self ._read_line ()
265
289
assert (
@@ -295,10 +319,10 @@ def socket_write(self, sock_num, buffer, timeout=120000):
295
319
return False
296
320
return True
297
321
298
- def socket_status (self , sock_num ) :
322
+ def socket_status (self , sock_num : int ) -> bool :
299
323
"""Returns socket status, True if connected. False otherwise.
300
- :param int sock_num: Desired socket number.
301
324
325
+ :param int sock_num: Desired socket number.
302
326
"""
303
327
if not self ._send_parse_reply (b"AT+CIPCLOSE?" , b"+CIPCLOSE:" , idx = sock_num ):
304
328
return False
0 commit comments