From 507aa6435b9295bb61e339ce3fe8dc1d38f81c0e Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 2 May 2022 11:31:44 -0600 Subject: [PATCH 1/3] began typing --- adafruit_ble_radio.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/adafruit_ble_radio.py b/adafruit_ble_radio.py index 331e8f2..faea99a 100644 --- a/adafruit_ble_radio.py +++ b/adafruit_ble_radio.py @@ -22,6 +22,14 @@ https://github.com/adafruit/circuitpython/releases """ +try: + from typing import Optional + import _bleio + from circuitpython_typing import ReadableBuffer, WriteableBuffer +except ImportError: + pass + + import time import struct from micropython import const @@ -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 @@ -71,14 +79,14 @@ def matches(cls, entry): ) @property - def msg(self): + def msg(self) -> bytes: """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 @@ -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. @@ -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. @@ -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. @@ -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) -> str: """ Returns a message received on the channel on which the radio is listening. @@ -158,7 +166,7 @@ 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 From 5561db6aa8c43fc256cb81dec640a7a25f4d202f Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 2 May 2022 11:48:01 -0600 Subject: [PATCH 2/3] added optional tag in receive and cleaned up pylint issues --- adafruit_ble_radio.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/adafruit_ble_radio.py b/adafruit_ble_radio.py index faea99a..8ea8d39 100644 --- a/adafruit_ble_radio.py +++ b/adafruit_ble_radio.py @@ -25,7 +25,7 @@ try: from typing import Optional import _bleio - from circuitpython_typing import ReadableBuffer, WriteableBuffer + from circuitpython_typing import ReadableBuffer except ImportError: pass @@ -112,7 +112,7 @@ def __init__(self, **args): # Handle user related configuration. self.configure(**args) - def configure(self, channel: int=42) -> ValueError: + def configure(self, channel: int = 42) -> ValueError: """ Set configuration values for the radio. @@ -152,7 +152,7 @@ def send_bytes(self, message: bytes) -> None: time.sleep(AD_DURATION) self.ble.stop_advertising() - def receive(self, timeout: float=1.0) -> str: + def receive(self, timeout: float = 1.0) -> Optional[str]: """ Returns a message received on the channel on which the radio is listening. @@ -166,7 +166,9 @@ def receive(self, timeout: float=1.0) -> str: return msg[0].decode("utf-8").replace("\x00", "") return None - def receive_full(self, timeout: float=1.0) -> Optional[tuple[ReadableBuffer, int, float]]: + 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 From 9a154703212e721934e4e2706c3f69b992be8554 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 2 May 2022 13:39:40 -0600 Subject: [PATCH 3/3] errors fixed, possibly ready for final review --- adafruit_ble_radio.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_ble_radio.py b/adafruit_ble_radio.py index 8ea8d39..e8b0505 100644 --- a/adafruit_ble_radio.py +++ b/adafruit_ble_radio.py @@ -23,7 +23,7 @@ """ try: - from typing import Optional + from typing import Optional, Tuple import _bleio from circuitpython_typing import ReadableBuffer except ImportError: @@ -79,7 +79,7 @@ def matches(cls, entry: _bleio.ScanEntry) -> bool: ) @property - def msg(self) -> bytes: + def msg(self) -> ReadableBuffer: """Raw radio data""" if _RADIO_DATA_ID not in self.manufacturer_data.data: return b"" @@ -112,7 +112,7 @@ def __init__(self, **args): # Handle user related configuration. self.configure(**args) - def configure(self, channel: int = 42) -> ValueError: + def configure(self, channel: int = 42) -> None: """ Set configuration values for the radio. @@ -152,7 +152,7 @@ def send_bytes(self, message: bytes) -> None: time.sleep(AD_DURATION) self.ble.stop_advertising() - def receive(self, timeout: float = 1.0) -> Optional[str]: + def receive(self, timeout: float = 1.0) -> str: """ Returns a message received on the channel on which the radio is listening. @@ -168,7 +168,7 @@ def receive(self, timeout: float = 1.0) -> Optional[str]: def receive_full( self, timeout: float = 1.0 - ) -> Optional[tuple[ReadableBuffer, int, float]]: + ) -> 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