Skip to content

Commit e1b1f9c

Browse files
Williangalvanipatrickelectric
authored andcommitted
ping1d.py: Add deque buffer so we dont ask the bytes to the OS one at a time
1 parent 23ae00e commit e1b1f9c

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

brping/ping1d.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
# DO NOT EDIT
99
# ~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!
1010

11-
from brping import pingmessage
11+
from collections import deque
1212
import serial
1313
import time
1414

15+
from brping import pingmessage
16+
1517

1618
class Ping1D(object):
1719
_acked_id = None
@@ -41,6 +43,8 @@ class Ping1D(object):
4143
_speed_of_sound = None
4244
_voltage_5 = None
4345

46+
_input_buffer = deque()
47+
4448
def __init__(self, device_name, baudrate=115200):
4549
if device_name is None:
4650
print("Device name is required")
@@ -70,10 +74,12 @@ def __init__(self, device_name, baudrate=115200):
7074
# @return A new PingMessage: as soon as a message is parsed (there may be data remaining in the buffer to be parsed, thus requiring subsequent calls to read())
7175
# @return None: if the buffer is empty and no message has been parsed
7276
def read(self):
73-
while self.iodev.in_waiting:
74-
b = self.iodev.read()
77+
bytes = self.iodev.read(self.iodev.in_waiting)
78+
self._input_buffer.extendleft(bytes)
7579

76-
if self.parser.parse_byte(ord(b)) == pingmessage.PingParser.NEW_MESSAGE:
80+
while len(self._input_buffer):
81+
b = self._input_buffer.pop()
82+
if self.parser.parse_byte(b) == pingmessage.PingParser.NEW_MESSAGE:
7783
self.handle_message(self.parser.rx_msg)
7884
return self.parser.rx_msg
7985
return None

0 commit comments

Comments
 (0)