Skip to content

Commit

Permalink
Merge pull request #6 from dhalbert/fix-packet-sizes
Browse files Browse the repository at this point in the history
Fix packet sizes for pixel and tone
  • Loading branch information
kattni authored Aug 17, 2020
2 parents 3e98a86 + 57094b0 commit ecd36c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
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],
)

0 comments on commit ecd36c4

Please sign in to comment.