]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/nova/uart.py
Fix libgpiod for 64-bit Pi by detecting and loading appropriate lib
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / nova / uart.py
1 """UART Class for Binho Nova"""
2
3 from adafruit_blinka.microcontroller.nova import Connection
4
5
6 class UART:
7     """Custom UART Class for Binho Nova"""
8
9     ESCAPE_SEQUENCE = "+++UART0"
10
11     # pylint: disable=too-many-arguments,unused-argument
12     def __init__(
13         self,
14         portid,
15         baudrate=9600,
16         bits=8,
17         parity=None,
18         stop=1,
19         timeout=1000,
20         read_buf_len=None,
21         flow=None,
22     ):
23         self._nova = Connection.getInstance()
24
25         self._id = portid
26         self._baudrate = baudrate
27         self._parity = parity
28         self._bits = bits
29         self._stop = stop
30         self._timeout = timeout
31
32         if flow is not None:  # default 0
33             raise NotImplementedError(
34                 "Parameter '{}' unsupported on Binho Nova".format("flow")
35             )
36
37         self._nova.setOperationMode(self._id, "UART")
38         self._nova.setBaudRateUART(self._id, baudrate)
39         self._nova.setDataBitsUART(self._id, bits)
40         self._nova.setParityUART(self._id, parity)
41         self._nova.setStopBitsUART(self._id, stop)
42         self._nova.setEscapeSequenceUART(self._id, UART.ESCAPE_SEQUENCE)
43         self._nova.beginBridgeUART(self._id)
44
45     # pylint: enable=too-many-arguments,unused-argument
46     def __del__(self):
47         """Close Nova on delete"""
48         self.deinit()
49         self._nova.close()
50
51     def deinit(self):
52         """Deinitialize"""
53         self._nova.writeBridgeUART(UART.ESCAPE_SEQUENCE)
54         self._nova.stopBridgeUART(UART.ESCAPE_SEQUENCE)
55
56     def read(self, nbytes=None):
57         """Read data from UART and return it"""
58         if nbytes is None:
59             return None
60         data = bytearray()
61         for _ in range(nbytes):
62             data.append(ord(self._nova.readBridgeUART()))
63         return data
64
65     def readinto(self, buf, nbytes=None):
66         """Read data from UART and into the buffer"""
67         if nbytes is None:
68             return None
69         for _ in range(nbytes):
70             buf.append(ord(self._nova.readBridgeUART()))
71         return buf
72
73     def readline(self):
74         """Read a single line of data from UART"""
75         out = self._nova.readBridgeUART()
76         line = out
77         while out != "\r":
78             out = self._nova.readBridgeUART()
79             line += out
80         return line
81
82     def write(self, buf):
83         """Write data from the buffer to UART"""
84         return self._nova.writeBridgeUART(buf)