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