Skip to content

Don't allocate packet buf until connected #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions adafruit_ble_midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class MIDIService(Service):

def __init__(self, **kwargs):
super().__init__(**kwargs)
self._in_buffer = bytearray(self._raw.packet_size)
# Defer creating _in_buffer until we're definitely connected.
self._in_buffer = None
self._out_buffer = None
shared_buffer = memoryview(bytearray(4))
self._buffers = [
Expand All @@ -102,6 +103,8 @@ def readinto(self, buf, length):
"""Reads up to ``length`` bytes into ``buf`` starting at index 0.

Returns the number of bytes written into ``buf``."""
if self._in_buffer is None:
self._in_buffer = bytearray(self._raw.packet_size)
i = 0
while i < length:
if self._in_index < self._in_length:
Expand All @@ -119,8 +122,6 @@ def readinto(self, buf, length):
buf[i] = byte
i += 1
else:
if len(self._in_buffer) < self._raw.packet_size:
self._in_buffer = bytearray(self._raw.packet_size)
self._in_length = self._raw.readinto(self._in_buffer)
if self._in_length == 0:
break
Expand Down