Skip to content

Commit 7554ac8

Browse files
committed
UARTService: add stream operations; make advertising start/stop explicit
1 parent cf2d150 commit 7554ac8

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

adafruit_ble/uart.py

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@
3232

3333

3434
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.
3642
3743
Example::
3844
@@ -44,7 +50,7 @@ class UARTService:
4450
NUS_RX_CHAR_UUID = UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
4551
NUS_TX_CHAR_UUID = UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")
4652

47-
def __init__(self):
53+
def __init__(self, timeout=1.0, buffer_size=64):
4854
"""Define the NUS service and start advertising it."""
4955
self._nus_tx_char = Characteristic(self.NUS_TX_CHAR_UUID, notify=True)
5056
self._nus_rx_char = Characteristic(self.NUS_RX_CHAR_UUID,
@@ -53,21 +59,65 @@ def __init__(self):
5359
nus_uart_service = Service(self.NUS_SERVICE_UUID, (self._nus_tx_char, self._nus_rx_char))
5460

5561
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)
5764

5865
self._periph.start_advertising()
5966

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+
6077
@property
6178
def connected(self):
62-
"""Has someone connected to the service?"""
79+
"""True if someone connected to the service."""
6380
return self._periph.connected
6481

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):
6894
"""
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()
70120

71-
def write(self, bytes_to_write):
121+
def write(self, buf):
72122
"""Write a buffer of bytes."""
73-
self._nus_tx_char.value = bytes_to_write
123+
self._nus_tx_char.value = buf

0 commit comments

Comments
 (0)