Skip to content

Commit 9382c0a

Browse files
committed
standard charac lengths; use board names
1 parent ed15dd7 commit 9382c0a

File tree

4 files changed

+27
-39
lines changed

4 files changed

+27
-39
lines changed

adafruit_ble_adafruit/addressable_pixel_service.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,13 @@ def __init__(self):
7373
super().__init__(
7474
properties=Characteristic.WRITE,
7575
read_perm=Attribute.NO_ACCESS,
76-
# max length will be set on binding.
76+
max_length=512,
7777
)
7878

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

@@ -106,9 +105,7 @@ class AddressablePixelService(AdafruitService):
106105
_pixel_packet = _PixelPacket()
107106
"""Pixel-setting data. max_length is supplied on binding."""
108107

109-
def __init__(self, data_length, service=None):
110-
# Used by _PixelPacket when binding is done.
111-
self.data_length = data_length
108+
def __init__(self, service=None):
112109
self._pixel_packet_buf = None
113110
super().__init__(service=service)
114111

adafruit_ble_adafruit/microphone_service.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,17 @@
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-
5940
class MicrophoneService(AdafruitService): # pylint: disable=too-few-public-methods
6041
"""Digital microphone data."""
6142

6243
uuid = AdafruitService.adafruit_service_uuid(0xB00)
6344

64-
sound_samples = _SoundSamples()
45+
sound_samples = Characteristic(
46+
uuid=AdafruitService.adafruit_service_uuid(0xB01),
47+
properties=(Characteristic.READ | Characteristic.NOTIFY),
48+
write_perm=Attribute.NO_ACCESS,
49+
max_length=512,
50+
)
6551
"""
6652
Array of 16-bit sound samples, varying based on period.
6753
If num_channel == 2, the samples alternate left and right channels.
@@ -76,8 +62,3 @@ class MicrophoneService(AdafruitService): # pylint: disable=too-few-public-meth
7662

7763
measurement_period = AdafruitService.measurement_period_charac()
7864
"""Initially 1000ms."""
79-
80-
def __init__(self, num_samples, service=None):
81-
# Used by _SoundSamples when binding is done.
82-
self.num_samples = num_samples
83-
super().__init__(service=service)

examples/ble_adafruit_circuitplayground_bluefruit.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
# 3 RGB bytes * 10 pixels.
2929
NEOPIXEL_BUF_LENGTH = const(3 * 10)
30-
neopixel_svc = AddressablePixelService(NEOPIXEL_BUF_LENGTH)
30+
neopixel_svc = AddressablePixelService()
3131
neopixel_buf = bytearray(NEOPIXEL_BUF_LENGTH)
3232
# Take over NeoPixel control from cp.
3333
cp._pixels.deinit() # pylint: disable=protected-access
@@ -48,7 +48,12 @@
4848
tone_svc = ToneService()
4949

5050
ble = BLERadio()
51+
# The Web Bluetooth dashboard identifies known boards by their
52+
# advertised name, not by advertising manufacturer data.
53+
ble.name = "CPlay"
5154

55+
# The Bluefruit Playground app looks in the manufacturer data
56+
# in the advertisement. That data uses the USB PID as a unique ID.
5257
# Adafruit Circuit Playground Bluefruit USB PID:
5358
# Arduino: 0x8045, CircuitPython: 0x8046, app supports either
5459
adv = AdafruitServerAdvertisement(0x8046)
@@ -90,14 +95,14 @@
9095

9196
tone = tone_svc.tone
9297
if tone is not None:
93-
freq, duration = tone
98+
freq, duration_msecs = tone
9499
if freq != 0:
95-
if duration != 0:
100+
if duration_msecs != 0:
96101
# Note that this blocks. Alternatively we could
97102
# use now_msecs to time a tone in a non-blocking
98103
# way, but then the other updates might make the
99104
# tone interval less consistent.
100-
cp.play_tone(freq, duration)
105+
cp.play_tone(freq, duration_msecs / 1000)
101106
else:
102107
cp.stop_tone()
103108
cp.start_tone(freq)

examples/ble_adafruit_clue.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
# CLUE has just one board pixel. 3 RGB bytes * 1 pixel.
3333
NEOPIXEL_BUF_LENGTH = const(3 * 1)
34-
neopixel_svc = AddressablePixelService(NEOPIXEL_BUF_LENGTH)
34+
neopixel_svc = AddressablePixelService()
3535
neopixel_buf = bytearray(NEOPIXEL_BUF_LENGTH)
3636
# Take over NeoPixel control from clue.
3737
clue._pixel.deinit() # pylint: disable=protected-access
@@ -55,7 +55,7 @@
5555

5656
# Send 256 16-bit samples at a time.
5757
MIC_NUM_SAMPLES = const(256)
58-
mic_svc = MicrophoneService(MIC_NUM_SAMPLES)
58+
mic_svc = MicrophoneService()
5959
mic_svc.number_of_channels = 1
6060
mic_svc.measurement_period = 100
6161
mic_last_update = 0
@@ -68,7 +68,12 @@
6868
tone_svc = ToneService()
6969

7070
ble = BLERadio()
71+
# The Web Bluetooth dashboard identifies known boards by their
72+
# advertised name, not by advertising manufacturer data.
73+
ble.name = "CLUE"
7174

75+
# The Bluefruit Playground app looks in the manufacturer data
76+
# in the advertisement. That data uses the USB PID as a unique ID.
7277
# Adafruit CLUE USB PID:
7378
# Arduino: 0x8071, CircuitPython: 0x8072, app supports either
7479
adv = AdafruitServerAdvertisement(0x8072)
@@ -128,14 +133,14 @@
128133

129134
tone = tone_svc.tone
130135
if tone is not None:
131-
freq, duration = tone
136+
freq, duration_msecs = tone
132137
if freq != 0:
133-
if duration != 0:
138+
if duration_msecs != 0:
134139
# Note that this blocks. Alternatively we could
135140
# use now_msecs to time a tone in a non-blocking
136141
# way, but then the other updates might make the
137142
# tone interval less consistent.
138-
clue.play_tone(freq, duration)
143+
clue.play_tone(freq, duration_msecs / 1000)
139144
else:
140145
clue.stop_tone()
141146
clue.start_tone(freq)

0 commit comments

Comments
 (0)