Skip to content

Began typing for bluetooth messaging #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions adafruit_ble_radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
https://github.com/adafruit/circuitpython/releases

"""
try:
from typing import Optional
import _bleio
from circuitpython_typing import ReadableBuffer
except ImportError:
pass


import time
import struct
from micropython import const
Expand Down Expand Up @@ -60,7 +68,7 @@ class _RadioAdvertisement(Advertisement):
)

@classmethod
def matches(cls, entry):
def matches(cls, entry: _bleio.ScanEntry) -> bool:
"""Checks for ID matches"""
if len(entry.advertisement_bytes) < 6:
return False
Expand All @@ -71,14 +79,14 @@ def matches(cls, entry):
)

@property
def msg(self):
def msg(self) -> bytes:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is the getter method for the msg property. The setter method allows it to take ReadableBuffer so the return could either be bytes or ReadableBuffer. And since bytes is subset of ReadableBuffer, the return type should be just ReadableBuffer!

"""Raw radio data"""
if _RADIO_DATA_ID not in self.manufacturer_data.data:
return b""
return self.manufacturer_data.data[_RADIO_DATA_ID]

@msg.setter
def msg(self, value):
def msg(self, value: ReadableBuffer) -> None:
self.manufacturer_data.data[_RADIO_DATA_ID] = value


Expand All @@ -104,7 +112,7 @@ def __init__(self, **args):
# Handle user related configuration.
self.configure(**args)

def configure(self, channel=42):
def configure(self, channel: int = 42) -> ValueError:
"""
Set configuration values for the radio.

Expand All @@ -116,7 +124,7 @@ def configure(self, channel=42):
else:
raise ValueError("Channel must be in range 0-255")

def send(self, message):
def send(self, message: str) -> None:
"""
Send a message string on the channel to which the radio is
broadcasting.
Expand All @@ -125,7 +133,7 @@ def send(self, message):
"""
return self.send_bytes(message.encode("utf-8"))

def send_bytes(self, message):
def send_bytes(self, message: bytes) -> None:
"""
Send bytes on the channel to which the radio is broadcasting.

Expand All @@ -144,7 +152,7 @@ def send_bytes(self, message):
time.sleep(AD_DURATION)
self.ble.stop_advertising()

def receive(self, timeout=1):
def receive(self, timeout: float = 1.0) -> Optional[str]:
"""
Returns a message received on the channel on which the radio is
listening.
Expand All @@ -158,7 +166,9 @@ def receive(self, timeout=1):
return msg[0].decode("utf-8").replace("\x00", "")
return None

def receive_full(self, timeout=1):
def receive_full(
self, timeout: float = 1.0
) -> Optional[tuple[ReadableBuffer, int, float]]:
"""
Returns a tuple containing three values representing a message received
on the channel on which the radio is listening. If no message was
Expand Down