32
32
33
33
34
34
class UARTService :
35
- """Provide UART-like functionality via the Nordic NUS service.
35
+ """
36
+ Provide UART-like functionality via the Nordic NUS service.
37
+
38
+ :param int timeout: the timeout in seconds to wait
39
+ for the first character and between subsequent characters.
40
+ :param int buffer_size: buffer up to this many bytes.
41
+ If more bytes are received, older bytes will be discarded.
36
42
37
43
Example::
38
44
@@ -44,7 +50,7 @@ class UARTService:
44
50
NUS_RX_CHAR_UUID = UUID ("6E400002-B5A3-F393-E0A9-E50E24DCCA9E" )
45
51
NUS_TX_CHAR_UUID = UUID ("6E400003-B5A3-F393-E0A9-E50E24DCCA9E" )
46
52
47
- def __init__ (self ):
53
+ def __init__ (self , timeout = 1.0 , buffer_size = 64 ):
48
54
"""Define the NUS service and start advertising it."""
49
55
self ._nus_tx_char = Characteristic (self .NUS_TX_CHAR_UUID , notify = True )
50
56
self ._nus_rx_char = Characteristic (self .NUS_RX_CHAR_UUID ,
@@ -53,21 +59,65 @@ def __init__(self):
53
59
nus_uart_service = Service (self .NUS_SERVICE_UUID , (self ._nus_tx_char , self ._nus_rx_char ))
54
60
55
61
self ._periph = Peripheral ((nus_uart_service ,))
56
- self ._rx_buffer = CharacteristicBuffer (self ._nus_rx_char , 64 )
62
+ self ._rx_buffer = CharacteristicBuffer (self ._nus_rx_char ,
63
+ timeout = timeout , buffer_size = buffer_size )
57
64
58
65
self ._periph .start_advertising ()
59
66
67
+ def start_advertising (self ):
68
+ """Start advertising the service. When a client connects, advertising will stop.
69
+ When the client disconnects, restart advertising by calling ``start_advertising()`` again.
70
+ """
71
+ self ._periph .start_advertising ()
72
+
73
+ def stop_advertising (self ):
74
+ """Stop advertising the service."""
75
+ self ._periph .stop_advertising ()
76
+
60
77
@property
61
78
def connected (self ):
62
- """Has someone connected to the service? """
79
+ """True if someone connected to the service. """
63
80
return self ._periph .connected
64
81
65
- def read (self ):
66
- """Read the next available byte from the buffered input.
67
- Return None if nothing is available.
82
+ def read (self , nbytes = None ):
83
+ """
84
+ Read characters. If ``nbytes`` is specified then read at most that many bytes.
85
+ Otherwise, read everything that arrives until the connection times out.
86
+ Providing the number of bytes expected is highly recommended because it will be faster.
87
+
88
+ :return: Data read
89
+ :rtype: bytes or None
90
+ """
91
+ return self ._rx_buffer .read (nbytes )
92
+
93
+ def readinto (self , buf , nbytes = None ):
68
94
"""
69
- return self ._rx_buffer .read ()
95
+ Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
96
+ that many bytes. Otherwise, read at most ``len(buf)`` bytes.
97
+
98
+ :return: number of bytes read and stored into ``buf``
99
+ :rtype: int or None (on a non-blocking error)
100
+ """
101
+ return self ._rx_buffer .readinto (buf , nbytes )
102
+
103
+ def readline (self ):
104
+ """
105
+ Read a line, ending in a newline character.
106
+
107
+ :return: the line read
108
+ :rtype: int or None
109
+ """
110
+ return self ._rx_buffer .readline ()
111
+
112
+ @property
113
+ def in_waiting (self ):
114
+ """The number of bytes in the input buffer, available to be read."""
115
+ return self ._rx_buffer .in_waiting
116
+
117
+ def reset_input_buffer (self ):
118
+ """Discard any unread characters in the input buffer."""
119
+ self ._rx_buffer .reset_input_buffer ()
70
120
71
- def write (self , bytes_to_write ):
121
+ def write (self , buf ):
72
122
"""Write a buffer of bytes."""
73
- self ._nus_tx_char .value = bytes_to_write
123
+ self ._nus_tx_char .value = buf
0 commit comments