Verified Commit faf33713 authored by Clayton Craft's avatar Clayton Craft
Browse files

stm_agps: use custom implementation of readline

Using python's readline here caused ~20-30% cpu utilization (on a librem
5), this uses the same readline from stm_agps_serial but with a slightly
longer delay when filling the buffer. This seems to be the sweet spot
between having too long of a delay sending data to clients, and low cpu
usage (~1-2%, on the librem 5)
parent f51c7ba0
......@@ -20,6 +20,7 @@ class STM_AGPS:
f"{serial_port}")
self._ser_port = serial_port
self._location = b""
self._buf = bytearray()
async def __aenter__(self):
await self.open()
......@@ -39,7 +40,24 @@ class STM_AGPS:
raise LoggedException(e)
async def readline(self):
return await self._ser.readline()
# based on this implementation of readline:
# https://github.com/pyserial/pyserial/issues/216#issuecomment-369414522
idx = self._buf.find(b'\n')
if idx >= 0:
line = self._buf[:idx+1]
self._buf = bytearray(self._buf[idx+1:])
return bytes(line)
while True:
data = await self._ser.receive_some(40)
idx = data.find(b'\n')
if idx >= 0:
line = self._buf + data[:idx+1]
self._buf = bytearray(data[idx+1:])
return bytes(line)
else:
self._buf.extend(data)
# sleep to prevent spinning faster than the device can write
await trio.sleep(0.5)
async def _write(self, data):
await self._ser.write(data)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment