Skip to content

Commit

Permalink
using fixed PacketBuffer for ToneService
Browse files Browse the repository at this point in the history
  • Loading branch information
dhalbert committed Apr 21, 2020
1 parent b122846 commit 83ba8f2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
47 changes: 38 additions & 9 deletions adafruit_ble_adafruit/tone_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,62 @@

import struct

from _bleio import PacketBuffer

from adafruit_ble.attributes import Attribute
from adafruit_ble.characteristics import Characteristic, StructCharacteristic
from adafruit_ble.characteristics import Characteristic, ComplexCharacteristic

from .adafruit_service import AdafruitService

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

format = "<HI"
format_size = struct.calcsize(format)

def __init__(self):
super().__init__(
properties=Characteristic.WRITE,
read_perm=Attribute.NO_ACCESS,
max_length=self.format_size,
fixed_length=True,
)

def bind(self, service):
"""Binds the characteristic to the given Service."""
bound_characteristic = super().bind(service)
return PacketBuffer(bound_characteristic, buffer_size=1)


class ToneService(AdafruitService):
"""Play tones."""

uuid = AdafruitService.adafruit_service_uuid(0xC00)
tone = StructCharacteristic(
"<HW",
uuid=AdafruitService.adafruit_service_uuid(0xC01),
properties=Characteristic.WRITE,
read_perm=Attribute.NO_ACCESS,
)
_tone_packet = _TonePacket()
"""
Tuple of (frequency: 16 bits, in Hz, duration: 32 bits, in msecs).
If frequency == 0, a tone being played is turned off.
if duration == 0, play indefinitely.
"""

def __init__(self, service=None):
super().__init__(service=service)
self._tone_packet_buf = bytearray(_TonePacket.format_size)

@property
def tone(self):
"""Return (frequency, duration), or None if no value available"""
buf = self._tone_packet_buf
if self._tone_packet.readinto(buf) == 0:
# No new values available.
return None
return struct.unpack(_TonePacket.format, buf)

def play(self, frequency, duration):
"""
Frequency is in Hz. If frequency == 0, a tone being played is turned off.
Duration is in seconds. If duration == 0, play indefinitely.
"""
self.tone = struct.pack(
"<HW", frequency, 0 if duration == 0 else int(duration * 1000 + 0.5)
self._tone_packet = struct.pack(
_TonePacket.format, frequency, 0 if duration == 0 else int(duration * 1000 + 0.5)
)
7 changes: 1 addition & 6 deletions examples/ble_adafruit_circuitplayground_bluefruit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
temp_last_update = 0

tone_svc = ToneService()
# Nothing playing now.
last_tone = (0, 0)
tone_last_freq = 0
tone_playing = False

ble = BLERadio()

Expand Down Expand Up @@ -70,8 +66,7 @@
button_svc.set_pressed(cp.switch, cp.button_a, cp.button_b)

tone = tone_svc.tone
if tone != last_tone:
print(tone)
if tone is not None:
freq, duration = tone
if freq != 0:
if duration != 0:
Expand Down

0 comments on commit 83ba8f2

Please sign in to comment.