Skip to content

Commit a0c676f

Browse files
committed
ble.pybricks: use IntFlag for Status
This data is received as bit flags from the hub, so it makes sense to use IntFlag so we can benefit from extras that come with that type.
1 parent 0146f46 commit a0c676f

File tree

3 files changed

+13
-32
lines changed

3 files changed

+13
-32
lines changed

pybricksdev/ble/pybricks.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
version can be used to determine the capabilities of the device.
1717
"""
1818

19-
from enum import IntEnum
19+
from enum import IntEnum, IntFlag
2020
from struct import unpack
2121
from typing import Literal, Tuple
2222

@@ -72,65 +72,57 @@ class Event(IntEnum):
7272
"""
7373

7474

75-
class Status(IntEnum):
76-
"""Hub status indicators.
75+
class StatusFlag(IntFlag):
76+
"""Hub status indicators."""
7777

78-
Use the :attr:`flag` property to get the value as a bit flag.
79-
"""
80-
81-
BATTERY_LOW_VOLTAGE_WARNING = 0
78+
BATTERY_LOW_VOLTAGE_WARNING = 1 << 0
8279
"""Battery voltage is low.
8380
8481
.. availability:: Since Pybricks protocol v1.0.0.
8582
"""
8683

87-
BATTERY_LOW_VOLTAGE_SHUTDOWN = 1
84+
BATTERY_LOW_VOLTAGE_SHUTDOWN = 1 << 1
8885
"""Battery voltage is critically low.
8986
9087
.. availability:: Since Pybricks protocol v1.0.0.
9188
"""
9289

93-
BATTERY_HIGH_CURRENT = 2
90+
BATTERY_HIGH_CURRENT = 1 << 2
9491
"""Battery current is too high.
9592
9693
.. availability:: Since Pybricks protocol v1.0.0.
9794
"""
9895

99-
BLE_ADVERTISING = 3
96+
BLE_ADVERTISING = 1 << 3
10097
"""Bluetooth Low Energy is advertising/discoverable.
10198
10299
.. availability:: Since Pybricks protocol v1.0.0.
103100
"""
104101

105-
BLE_LOW_SIGNAL = 4
102+
BLE_LOW_SIGNAL = 1 << 4
106103
"""Bluetooth Low Energy has low signal.
107104
108105
.. availability:: Since Pybricks protocol v1.0.0.
109106
"""
110107

111-
POWER_BUTTON_PRESSED = 5
108+
POWER_BUTTON_PRESSED = 1 << 5
112109
"""Power button is currently pressed.
113110
114111
.. availability:: Since Pybricks protocol v1.0.0.
115112
"""
116113

117-
USER_PROGRAM_RUNNING = 6
114+
USER_PROGRAM_RUNNING = 1 << 6
118115
"""User program is currently running.
119116
120117
.. availability:: Since Pybricks protocol v1.0.0.
121118
"""
122119

123-
SHUTDOWN = 7
120+
SHUTDOWN = 1 << 7
124121
"""Hub shutdown was requested.
125122
126123
.. availability:: Since Pybricks protocol v1.1.0.
127124
"""
128125

129-
@property
130-
def flag(self) -> int:
131-
"""Gets the value of the enum as a bit flag."""
132-
return 1 << self.value
133-
134126

135127
# The Pybricks Protocol version also implies certain other services and
136128
# and characteristics are present.

pybricksdev/connections.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
SW_REV_UUID,
2525
PNP_ID_UUID,
2626
Event,
27-
Status,
27+
StatusFlag,
2828
unpack_pnp_id,
2929
)
3030
from .compile import compile_file
@@ -247,7 +247,7 @@ def pybricks_service_handler(self, _: int, data: bytes) -> None:
247247
if data[0] == Event.STATUS_REPORT:
248248
# decode the payload
249249
(flags,) = struct.unpack("<I", data[1:])
250-
program_running_now = bool(flags & Status.USER_PROGRAM_RUNNING.flag)
250+
program_running_now = bool(flags & StatusFlag.USER_PROGRAM_RUNNING)
251251

252252
# If we are currently downloading a program, we must ignore user
253253
# program running state changes, otherwise the checksum will be

tests/ble/test_pybrick.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
PYBRICKS_CONTROL_UUID,
55
PYBRICKS_SERVICE_UUID,
66
SW_REV_UUID,
7-
Status,
87
)
98

109

@@ -16,16 +15,6 @@ def test_pybricks_control_characteristic_uuid():
1615
assert PYBRICKS_CONTROL_UUID == "c5f50002-8280-46da-89f4-6d8051e4aeef"
1716

1817

19-
def test_status_flags():
20-
assert Status.BATTERY_LOW_VOLTAGE_WARNING.flag == 0x1
21-
assert Status.BATTERY_LOW_VOLTAGE_SHUTDOWN.flag == 0x2
22-
assert Status.BATTERY_HIGH_CURRENT.flag == 0x4
23-
assert Status.BLE_ADVERTISING.flag == 0x8
24-
assert Status.BLE_LOW_SIGNAL.flag == 0x10
25-
assert Status.POWER_BUTTON_PRESSED.flag == 0x20
26-
assert Status.USER_PROGRAM_RUNNING.flag == 0x40
27-
28-
2918
def test_device_information_service_uuid():
3019
assert DI_SERVICE_UUID == "0000180a-0000-1000-8000-00805f9b34fb"
3120

0 commit comments

Comments
 (0)