/ adafruit_ble_berrymed_pulse_oximeter / adafruit_ble_transparent_uart.py
adafruit_ble_transparent_uart.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2019 Dan Halbert for Adafruit Industries 4 # Copyright (c) 2019 Scott Shawcroft for Adafruit Industries 5 # 6 # Permission is hereby granted, free of charge, to any person obtaining a copy 7 # of this software and associated documentation files (the "Software"), to deal 8 # in the Software without restriction, including without limitation the rights 9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 # copies of the Software, and to permit persons to whom the Software is 11 # furnished to do so, subject to the following conditions: 12 # 13 # The above copyright notice and this permission notice shall be included in 14 # all copies or substantial portions of the Software. 15 # 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 # THE SOFTWARE. 23 """ 24 `adafruit_ble_berrymed_pulse_oximeter.adafruit_ble_transparent_uart` 25 ============================================================================= 26 27 This module provides Services used by MicroChip 28 29 """ 30 31 from adafruit_ble import Service 32 from adafruit_ble.uuid import VendorUUID 33 from adafruit_ble.characteristics.stream import StreamOut, StreamIn 34 35 __version__ = "0.0.0-auto.0" 36 __repo__ = ( 37 "https://github.com/adafruit/Adafruit_CircuitPython_BLE_Contec_Pulse_Oximeter.git" 38 ) 39 40 41 class TransparentUARTService(Service): 42 """ 43 Provide UART-like functionality via MicroChip 44 45 :param int timeout: the timeout in seconds to wait 46 for the first character and between subsequent characters. 47 :param int buffer_size: buffer up to this many bytes. 48 If more bytes are received, older bytes will be discarded. 49 """ 50 51 # pylint: disable=no-member 52 uuid = VendorUUID("49535343-FE7D-4AE5-8FA9-9FAFD205E455") 53 _server_tx = StreamOut( 54 uuid=VendorUUID("49535343-1E4D-4BD9-BA61-23C647249616"), 55 timeout=1.0, 56 buffer_size=64, 57 ) 58 _server_rx = StreamIn( 59 uuid=VendorUUID("49535343-8841-43F4-A8D4-ECBE34729BB3"), 60 timeout=1.0, 61 buffer_size=64, 62 ) 63 64 def __init__(self, service=None): 65 super().__init__(service=service) 66 self.connectable = True 67 if not service: 68 self._rx = self._server_rx 69 self._tx = self._server_tx 70 else: 71 # If we're a client then swap the characteristics we use. 72 self._tx = self._server_rx 73 self._rx = self._server_tx 74 75 def read(self, nbytes=None): 76 """ 77 Read characters. If ``nbytes`` is specified then read at most that many bytes. 78 Otherwise, read everything that arrives until the connection times out. 79 Providing the number of bytes expected is highly recommended because it will be faster. 80 81 :return: Data read 82 :rtype: bytes or None 83 """ 84 return self._rx.read(nbytes) 85 86 def readinto(self, buf, nbytes=None): 87 """ 88 Read bytes into the ``buf``. If ``nbytes`` is specified then read at most 89 that many bytes. Otherwise, read at most ``len(buf)`` bytes. 90 91 :return: number of bytes read and stored into ``buf`` 92 :rtype: int or None (on a non-blocking error) 93 """ 94 return self._rx.readinto(buf, nbytes) 95 96 def readline(self): 97 """ 98 Read a line, ending in a newline character. 99 100 :return: the line read 101 :rtype: bytes or None 102 """ 103 return self._rx.readline() 104 105 @property 106 def in_waiting(self): 107 """The number of bytes in the input buffer, available to be read.""" 108 return self._rx.in_waiting 109 110 def reset_input_buffer(self): 111 """Discard any unread characters in the input buffer.""" 112 self._rx.reset_input_buffer() 113 114 def write(self, buf): 115 """Write a buffer of bytes.""" 116 self._tx.write(buf)