Skip to content

Commit 6c15ff7

Browse files
committed
Fix packet sizes for pixel and tone
1 parent 3e98a86 commit 6c15ff7

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Dependencies
2525
This driver depends on:
2626

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

2930
Please ensure all dependencies are available on the CircuitPython filesystem.
3031
This is easily achieved by downloading

adafruit_ble_adafruit/addressable_pixel_service.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
from collections import namedtuple
3535
import struct
3636

37-
import _bleio
38-
3937
from adafruit_ble.attributes import Attribute
4038
from adafruit_ble.characteristics import Characteristic, ComplexCharacteristic
41-
from adafruit_ble.characteristics.int import Uint8Characteristic
39+
from adafruit_ble.characteristics.int import Uint8Characteristic, Uint16Characteristic
4240
from adafruit_ble_adafruit.adafruit_service import AdafruitService
4341

42+
import _bleio
43+
4444
PixelValues = namedtuple("PixelValues", ("start", "write_now", "data"),)
4545
"""Namedtuple for pixel data and instructions.
4646
@@ -67,19 +67,19 @@ class _PixelPacket(ComplexCharacteristic):
6767
data: raw array of data for all pixels, in proper color order for type of pixel
6868
"""
6969

70+
MAX_LENGTH = 512
71+
7072
uuid = AdafruitService.adafruit_service_uuid(0x903)
7173

7274
def __init__(self):
7375
super().__init__(
7476
properties=Characteristic.WRITE,
7577
read_perm=Attribute.NO_ACCESS,
76-
max_length=512,
78+
max_length=self.MAX_LENGTH,
7779
)
7880

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

@@ -98,31 +98,35 @@ class AddressablePixelService(AdafruitService):
9898
uuid=AdafruitService.adafruit_service_uuid(0x902),
9999
properties=(Characteristic.READ | Characteristic.WRITE),
100100
)
101+
102+
pixel_buffer_size = Uint16Characteristic(
103+
uuid=AdafruitService.adafruit_service_uuid(0x904),
104+
properties=(Characteristic.READ | Characteristic.WRITE),
105+
initial_value=_PixelPacket.MAX_LENGTH,
106+
)
107+
101108
"""
102109
0 = WS2812 (NeoPixel), 800kHz
103110
1 = SPI (APA102: DotStar)
104111
"""
105112
_pixel_packet = _PixelPacket()
106-
"""Pixel-setting data. max_length is supplied on binding."""
113+
"""Pixel-setting data."""
107114

108115
def __init__(self, service=None):
109-
self._pixel_packet_buf = None
116+
self._pixel_packet_buf = bytearray(_PixelPacket.MAX_LENGTH)
110117
super().__init__(service=service)
111118

112119
@property
113120
def values(self):
114121
"""Return a tuple (start, write_now, data) corresponding to the
115122
different parts of ``_pixel_packet``.
116123
"""
117-
if self._pixel_packet_buf is None:
118-
self._pixel_packet_buf = bytearray(
119-
self._pixel_packet.packet_size # pylint: disable=no-member
120-
)
121124
buf = self._pixel_packet_buf
122-
if self._pixel_packet.readinto(buf) == 0: # pylint: disable=no-member
125+
num_read = self._pixel_packet.readinto(buf) # pylint: disable=no-member
126+
if num_read == 0:
123127
# No new values available
124128
return None
125129

126130
return PixelValues(
127-
struct.unpack_from("<H", buf)[0], bool(buf[2] & 0x1), buf[3:],
131+
struct.unpack_from("<H", buf)[0], bool(buf[2] & 0x1), buf[3:num_read],
128132
)

adafruit_ble_adafruit/tone_service.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@
3333

3434
import struct
3535

36-
from _bleio import PacketBuffer
37-
3836
from adafruit_ble.attributes import Attribute
3937
from adafruit_ble.characteristics import Characteristic, ComplexCharacteristic
4038
from adafruit_ble_adafruit.adafruit_service import AdafruitService
4139

40+
from _bleio import PacketBuffer
4241

4342
class _TonePacket(ComplexCharacteristic):
4443
uuid = AdafruitService.adafruit_service_uuid(0xC01)

0 commit comments

Comments
 (0)