Skip to content

Commit 20c1fc2

Browse files
committed
Added docstrings, reformatted
1 parent 74fbe6e commit 20c1fc2

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

adafruit_ble_beacon.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,22 @@
3636
from micropython import const
3737
import _bleio
3838
from adafruit_ble.advertising import Advertisement, Struct, AdvertisingDataField
39-
from adafruit_ble.advertising.standard import ManufacturerData
4039

4140
try:
42-
from typing import Optional, Union, Any, Type, Tuple, Sequence
41+
from typing import Optional, Union, Type, Tuple, Sequence
4342
except ImportError:
4443
pass
4544

4645
__version__ = "0.0.0+auto.0"
4746
__repo__ = "https://github.com/tekktrik/Adafruit_CircuitPython_BLE_Beacon.git"
4847

48+
4949
_MANUFACTURING_DATA_ADT = const(0xFF)
5050

5151
_APPLE_COMPANY_ID = const(0x004C)
5252
_IBEACON_TYPE = const(0x02)
5353
_IBEACON_LENGTH = const(0x15)
5454

55-
# TODO: fix this
56-
_APPLE_COMPANY_ID_FLIPPED = const(0x4C00)
57-
5855

5956
class MultiStruct(AdvertisingDataField):
6057
"""`struct` encoded data in an Advertisement."""
@@ -65,7 +62,7 @@ def __init__(self, struct_format: str, *, advertising_data_type: int) -> None:
6562

6663
def __get__(
6764
self, obj: Optional["Advertisement"], cls: Type["Advertisement"]
68-
) -> Optional[Union[Tuple, "Struct"]]:
65+
) -> Optional[Union[Tuple, "MultiStruct"]]:
6966
if obj is None:
7067
return self
7168
if self._adt not in obj.data_dict:
@@ -75,14 +72,16 @@ def __get__(
7572
def __set__(self, obj: "Advertisement", value: Sequence) -> None:
7673
obj.data_dict[self._adt] = struct.pack(self._format, *value)
7774

75+
7876
class _BeaconAdvertisement(Advertisement):
7977
"""Advertisement for location beacons like iBeacon"""
8078

81-
path_loss_const: int = 3
79+
path_loss_const: float = 3
8280
"""The path loss constant, typically between 2-4"""
8381

8482
@property
8583
def uuid(self) -> bytes:
84+
"""The UUID of the beacon"""
8685
raise NotImplementedError("Must be implemented in beacon subclass")
8786

8887
@uuid.setter
@@ -91,22 +90,31 @@ def uuid(self, id: bytes) -> None:
9190

9291
@property
9392
def distance(self) -> float:
94-
"""The approximate distance to the beacon"""
95-
96-
return 10**((self.beacon_tx_power - self.rssi) / (10*self.path_loss_const))
93+
"""The approximate distance to the beacon, in meters"""
94+
return 10 ** ((self.beacon_tx_power - self.rssi) / (10 * self.path_loss_const))
9795

9896
@property
9997
def beacon_tx_power(self) -> int:
98+
"""The beacon TX power"""
10099
raise NotImplementedError("Must be implemented in beacon subclass")
101100

102101
@beacon_tx_power.setter
103102
def beacon_txt_power(self, power: int) -> None:
104103
raise NotImplementedError("Must be implemented in beacon subclass")
105104

106-
107-
class iBeaconAdvertisement(_BeaconAdvertisement):
108105

109-
match_prefixes = (struct.pack("<BHBB", _MANUFACTURING_DATA_ADT, _APPLE_COMPANY_ID, _IBEACON_TYPE, _IBEACON_LENGTH),)
106+
class iBeaconAdvertisement(_BeaconAdvertisement):
107+
"""An iBeacon advertisement"""
108+
109+
match_prefixes = (
110+
struct.pack(
111+
"<BHBB",
112+
_MANUFACTURING_DATA_ADT,
113+
_APPLE_COMPANY_ID,
114+
_IBEACON_TYPE,
115+
_IBEACON_LENGTH,
116+
),
117+
)
110118

111119
_data_format = ">HBBQQHHb"
112120
_beacon_data = MultiStruct(_data_format, advertising_data_type=0xFF)
@@ -117,14 +125,14 @@ class iBeaconAdvertisement(_BeaconAdvertisement):
117125
_minor_index = 6
118126
_beacon_tx_power_index = 7
119127

120-
121-
def __init__(self, *, entry: Optional[_bleio.ScanEntry] = None, ) -> None:
128+
def __init__(self, *, entry: Optional[_bleio.ScanEntry] = None) -> None:
122129
super().__init__(entry=entry)
123130
if not entry:
124131
self._init_struct()
125-
132+
126133
@property
127134
def uuid(self) -> bytes:
135+
"""The UUID of the beacon"""
128136
uuid_msb = self._get_struct_index(self._uuid_msb_index)
129137
uuid_lsb = self._get_struct_index(self._uuid_lsb_index)
130138
return struct.pack(">QQ", uuid_msb, uuid_lsb)
@@ -137,6 +145,7 @@ def uuid(self, id: bytes) -> None:
137145

138146
@property
139147
def major(self) -> int:
148+
"""The major store number for the beacon"""
140149
return self._get_struct_index(self._major_index)
141150

142151
@major.setter
@@ -145,6 +154,7 @@ def major(self, number: int) -> None:
145154

146155
@property
147156
def minor(self) -> int:
157+
"""The minor store number for the beacon"""
148158
return self._get_struct_index(self._minor_index)
149159

150160
@minor.setter
@@ -153,6 +163,7 @@ def minor(self, number: int) -> None:
153163

154164
@property
155165
def beacon_tx_power(self) -> int:
166+
"""The beacon TX power"""
156167
return self._get_struct_index(self._beacon_tx_power_index)
157168

158169
@beacon_tx_power.setter
@@ -170,4 +181,13 @@ def _get_struct_index(self, index: int) -> int:
170181

171182
def _init_struct(self) -> None:
172183
apple_id_flipped = struct.unpack(">H", struct.pack("<H", _APPLE_COMPANY_ID))
173-
self._beacon_data = (apple_id_flipped, _IBEACON_TYPE, _IBEACON_LENGTH, 0, 0, 0, 0, 0)
184+
self._beacon_data = (
185+
apple_id_flipped,
186+
_IBEACON_TYPE,
187+
_IBEACON_LENGTH,
188+
0,
189+
0,
190+
0,
191+
0,
192+
0,
193+
)

0 commit comments

Comments
 (0)