|
1 | 1 | # SPDX-License-Identifier: MIT |
2 | | -# Copyright (c) 2021 The Pybricks Authors |
| 2 | +# Copyright (c) 2021-2022 The Pybricks Authors |
3 | 3 |
|
4 | 4 | """This module and its submodules are used for Bluetooth Low Energy |
5 | 5 | communications with devices that provide the LEGO Wireless Protocol v3. |
6 | 6 | """ |
7 | 7 |
|
8 | 8 | from enum import IntEnum |
9 | 9 |
|
| 10 | +from .bytecodes import Capabilities, HubKind, LastNetwork, Status, Version |
| 11 | + |
10 | 12 | # LEGO Wireless Protocol v3 is defined at: |
11 | 13 | # https://lego.github.io/lego-ble-wireless-protocol-docs/ |
12 | 14 |
|
@@ -105,3 +107,91 @@ class BootloaderError(IntEnum): |
105 | 107 |
|
106 | 108 | UNKNOWN_COMMAND = 0x05 |
107 | 109 | """The command was not recognized.""" |
| 110 | + |
| 111 | + |
| 112 | +class BootloaderAdvertisementData: |
| 113 | + """ |
| 114 | + The 6-byte manufacturer-specific advertisement data sent when a hub in |
| 115 | + bootloader mode is advertising. |
| 116 | + """ |
| 117 | + |
| 118 | + def __init__(self, data: bytes) -> None: |
| 119 | + if len(data) != 6: |
| 120 | + raise ValueError("expecting 6 bytes of data") |
| 121 | + |
| 122 | + self._data = data |
| 123 | + |
| 124 | + def __bytes__(self) -> bytes: |
| 125 | + return self._data |
| 126 | + |
| 127 | + @property |
| 128 | + def version(self) -> Version: |
| 129 | + """ |
| 130 | + Gets the bootloader software version. |
| 131 | + """ |
| 132 | + return Version(int.from_bytes(self._data[:4], "little")) |
| 133 | + |
| 134 | + @property |
| 135 | + def hub_kind(self) -> HubKind: |
| 136 | + """ |
| 137 | + Gets the hub type identifier. |
| 138 | + """ |
| 139 | + return HubKind(self._data[4]) |
| 140 | + |
| 141 | + @property |
| 142 | + def hub_capabilities(self) -> Capabilities: |
| 143 | + """ |
| 144 | + Gets the hub capability flags. |
| 145 | + """ |
| 146 | + return Capabilities(self._data[5]) |
| 147 | + |
| 148 | + |
| 149 | +class AdvertisementData: |
| 150 | + """ |
| 151 | + The 6-byte manufacturer-specific advertisement data sent when a hub running |
| 152 | + official LEGO firmware is advertising. |
| 153 | + """ |
| 154 | + |
| 155 | + def __init__(self, data: bytes) -> None: |
| 156 | + if len(data) != 6: |
| 157 | + raise ValueError("expecting 6 bytes of data") |
| 158 | + |
| 159 | + self._data = data |
| 160 | + |
| 161 | + def __bytes__(self) -> bytes: |
| 162 | + return self._data |
| 163 | + |
| 164 | + @property |
| 165 | + def is_button_pressed(self) -> bool: |
| 166 | + """ |
| 167 | + Gets the state of the button on the hub. |
| 168 | + """ |
| 169 | + return bool(self._data[0]) |
| 170 | + |
| 171 | + @property |
| 172 | + def hub_kind(self) -> HubKind: |
| 173 | + """ |
| 174 | + Gets the hub type identifier. |
| 175 | + """ |
| 176 | + return HubKind(self._data[1]) |
| 177 | + |
| 178 | + @property |
| 179 | + def hub_capabilities(self) -> Capabilities: |
| 180 | + """ |
| 181 | + Gets the hub capability flags. |
| 182 | + """ |
| 183 | + return Capabilities(self._data[2]) |
| 184 | + |
| 185 | + @property |
| 186 | + def last_network(self) -> LastNetwork: |
| 187 | + """ |
| 188 | + Gets the ID of the last network the hub was connected to. |
| 189 | + """ |
| 190 | + return LastNetwork(self._data[3]) |
| 191 | + |
| 192 | + @property |
| 193 | + def status(self) -> Status: |
| 194 | + """ |
| 195 | + Gets the status of the advertising hub. |
| 196 | + """ |
| 197 | + return Status(self._data[4]) |
0 commit comments