Skip to content

Commit ed15dd7

Browse files
committed
use notify properly for pixel and sound services
1 parent 4fa9759 commit ed15dd7

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

adafruit_ble_adafruit/addressable_pixel_service.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,18 @@ class _PixelPacket(ComplexCharacteristic):
6969

7070
uuid = AdafruitService.adafruit_service_uuid(0x903)
7171

72-
def __init__(self, length):
72+
def __init__(self):
7373
super().__init__(
7474
properties=Characteristic.WRITE,
7575
read_perm=Attribute.NO_ACCESS,
76-
max_length=length,
76+
# max length will be set on binding.
7777
)
7878

7979
def bind(self, service):
8080
"""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
83+
self.max_length = service.data_length + 3
8184
bound_characteristic = super().bind(service)
8285
return _bleio.PacketBuffer(bound_characteristic, buffer_size=1)
8386

@@ -100,13 +103,14 @@ class AddressablePixelService(AdafruitService):
100103
0 = WS2812 (NeoPixel), 800kHz
101104
1 = SPI (APA102: DotStar)
102105
"""
106+
_pixel_packet = _PixelPacket()
107+
"""Pixel-setting data. max_length is supplied on binding."""
103108

104-
def __init__(self, length, service=None):
105-
# Delay creation of _pixel_packet until length is given.
106-
self.__class__._pixel_packet = _PixelPacket(length + 3)
107-
super().__init__(service=service)
109+
def __init__(self, data_length, service=None):
110+
# Used by _PixelPacket when binding is done.
111+
self.data_length = data_length
108112
self._pixel_packet_buf = None
109-
self._buf_length = length
113+
super().__init__(service=service)
110114

111115
@property
112116
def values(self):

adafruit_ble_adafruit/microphone_service.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,31 @@
3737
from adafruit_ble_adafruit.adafruit_service import AdafruitService
3838

3939

40+
class _SoundSamples(Characteristic): # pylint: disable=too-few-public-methods
41+
"""The Characteristic's max_length is passed from the service,
42+
so defer setting it until binding happens.
43+
"""
44+
45+
def __init__(self):
46+
super().__init__(
47+
uuid=AdafruitService.adafruit_service_uuid(0xB01),
48+
properties=(Characteristic.READ | Characteristic.NOTIFY),
49+
write_perm=Attribute.NO_ACCESS,
50+
)
51+
52+
def __bind_locally(self, service, initial_value):
53+
# The service instantiation determines how many samples
54+
# to send.
55+
self.max_length = service.num_samples * 2
56+
return super().__bind_locally(service, initial_value)
57+
58+
4059
class MicrophoneService(AdafruitService): # pylint: disable=too-few-public-methods
4160
"""Digital microphone data."""
4261

4362
uuid = AdafruitService.adafruit_service_uuid(0xB00)
44-
sound_samples = None
63+
64+
sound_samples = _SoundSamples()
4565
"""
4666
Array of 16-bit sound samples, varying based on period.
4767
If num_channel == 2, the samples alternate left and right channels.
@@ -57,11 +77,7 @@ class MicrophoneService(AdafruitService): # pylint: disable=too-few-public-meth
5777
measurement_period = AdafruitService.measurement_period_charac()
5878
"""Initially 1000ms."""
5979

60-
def __init__(self, max_samples, service=None):
61-
self.__class__.sound_samples = Characteristic(
62-
uuid=AdafruitService.adafruit_service_uuid(0xB01),
63-
properties=(Characteristic.READ | Characteristic.NOTIFY),
64-
write_perm=Attribute.NO_ACCESS,
65-
max_length=max_samples * 2,
66-
)
80+
def __init__(self, num_samples, service=None):
81+
# Used by _SoundSamples when binding is done.
82+
self.num_samples = num_samples
6783
super().__init__(service=service)

examples/ble_adafruit_circuitplayground_bluefruit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
accel_svc.measurement_period = 100
2626
accel_last_update = 0
2727

28-
NEOPIXEL_BUF_LENGTH = const(30)
28+
# 3 RGB bytes * 10 pixels.
29+
NEOPIXEL_BUF_LENGTH = const(3 * 10)
2930
neopixel_svc = AddressablePixelService(NEOPIXEL_BUF_LENGTH)
3031
neopixel_buf = bytearray(NEOPIXEL_BUF_LENGTH)
3132
# Take over NeoPixel control from cp.

examples/ble_adafruit_clue_bluefruit.py renamed to examples/ble_adafruit_clue.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
accel_svc.measurement_period = 100
3030
accel_last_update = 0
3131

32-
# CLUE has just one board pixel. The 3 is for RGB.
32+
# CLUE has just one board pixel. 3 RGB bytes * 1 pixel.
3333
NEOPIXEL_BUF_LENGTH = const(3 * 1)
3434
neopixel_svc = AddressablePixelService(NEOPIXEL_BUF_LENGTH)
3535
neopixel_buf = bytearray(NEOPIXEL_BUF_LENGTH)
@@ -106,6 +106,8 @@
106106
clue._mic.record( # pylint: disable=protected-access
107107
mic_samples, len(mic_samples)
108108
)
109+
# This subtraction yields unsigned values which are
110+
# reinterpreted as signed after passing.
109111
mic_svc.sound_samples = mic_samples - 32768
110112
mic_last_update = now_msecs
111113

0 commit comments

Comments
 (0)