Skip to content

Commit

Permalink
Merge pull request #150 from tekktrik/feature/configurable-eol
Browse files Browse the repository at this point in the history
Add configurable EOL to readline()
  • Loading branch information
tannewt authored Jan 14, 2022
2 parents 13102f3 + 4a21b4c commit c36a72d
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions adafruit_esp32spi/adafruit_esp32spi_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,21 @@ def write(self, data):
"""
self.send(data)

def readline(self):
"""Attempt to return as many bytes as we can up to but not including '\r\n'"""
def readline(self, eol=b"\r\n"):
"""Attempt to return as many bytes as we can up to but not including
end-of-line character (default is '\\r\\n')"""

# print("Socket readline")
stamp = time.monotonic()
while b"\r\n" not in self._buffer:
while eol not in self._buffer:
# there's no line already in there, read some more
avail = self.available()
if avail:
self._buffer += _the_interface.socket_read(self._socknum, avail)
elif self._timeout > 0 and time.monotonic() - stamp > self._timeout:
self.close() # Make sure to close socket so that we don't exhaust sockets.
raise RuntimeError("Didn't receive full response, failing out")
firstline, self._buffer = self._buffer.split(b"\r\n", 1)
firstline, self._buffer = self._buffer.split(eol, 1)
gc.collect()
return firstline

Expand Down

0 comments on commit c36a72d

Please sign in to comment.