Skip to content

Fix packet sizes for pixel and tone #6

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 3 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Dependencies
This driver depends on:

* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
**CircuitPython must be at least version 6.0.0.**

Please ensure all dependencies are available on the CircuitPython filesystem.
This is easily achieved by downloading
Expand Down
28 changes: 16 additions & 12 deletions adafruit_ble_adafruit/addressable_pixel_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

from adafruit_ble.attributes import Attribute
from adafruit_ble.characteristics import Characteristic, ComplexCharacteristic
from adafruit_ble.characteristics.int import Uint8Characteristic
from adafruit_ble.characteristics.int import Uint8Characteristic, Uint16Characteristic
from adafruit_ble_adafruit.adafruit_service import AdafruitService

PixelValues = namedtuple("PixelValues", ("start", "write_now", "data"),)
Expand Down Expand Up @@ -67,19 +67,19 @@ class _PixelPacket(ComplexCharacteristic):
data: raw array of data for all pixels, in proper color order for type of pixel
"""

MAX_LENGTH = 512

uuid = AdafruitService.adafruit_service_uuid(0x903)

def __init__(self):
super().__init__(
properties=Characteristic.WRITE,
read_perm=Attribute.NO_ACCESS,
max_length=512,
max_length=self.MAX_LENGTH,
)

def bind(self, service):
"""Binds the characteristic to the given Service."""
# Set Characteristic's max length, based on value from AddressablePixelService.
# + 3 is for size of start and flags
bound_characteristic = super().bind(service)
return _bleio.PacketBuffer(bound_characteristic, buffer_size=1)

Expand All @@ -98,31 +98,35 @@ class AddressablePixelService(AdafruitService):
uuid=AdafruitService.adafruit_service_uuid(0x902),
properties=(Characteristic.READ | Characteristic.WRITE),
)

pixel_buffer_size = Uint16Characteristic(
uuid=AdafruitService.adafruit_service_uuid(0x904),
properties=(Characteristic.READ | Characteristic.WRITE),
initial_value=_PixelPacket.MAX_LENGTH,
)

"""
0 = WS2812 (NeoPixel), 800kHz
1 = SPI (APA102: DotStar)
"""
_pixel_packet = _PixelPacket()
"""Pixel-setting data. max_length is supplied on binding."""
"""Pixel-setting data."""

def __init__(self, service=None):
self._pixel_packet_buf = None
self._pixel_packet_buf = bytearray(_PixelPacket.MAX_LENGTH)
super().__init__(service=service)

@property
def values(self):
"""Return a tuple (start, write_now, data) corresponding to the
different parts of ``_pixel_packet``.
"""
if self._pixel_packet_buf is None:
self._pixel_packet_buf = bytearray(
self._pixel_packet.packet_size # pylint: disable=no-member
)
buf = self._pixel_packet_buf
if self._pixel_packet.readinto(buf) == 0: # pylint: disable=no-member
num_read = self._pixel_packet.readinto(buf) # pylint: disable=no-member
if num_read == 0:
# No new values available
return None

return PixelValues(
struct.unpack_from("<H", buf)[0], bool(buf[2] & 0x1), buf[3:],
struct.unpack_from("<H", buf)[0], bool(buf[2] & 0x1), buf[3:num_read],
)