Skip to content

Commit 4e3cb55

Browse files
committed
share more of _bleio dict; fix one doc error
1 parent f5a5fc4 commit 4e3cb55

File tree

2 files changed

+37
-56
lines changed

2 files changed

+37
-56
lines changed

shared-bindings/_bleio/Adapter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
//| communicate with the HCI co-processor in HCI mode.
7474
//| The `Adapter` object is enabled during this call.
7575
//|
76-
//| After instantiating the Adapter, assign it to _bleio.adapter
76+
//| After instantiating an Adapter, call `_bleio.set_adapter()` to set `_bleio.adapter`
7777
//|
7878
//| On boards with native BLE, you cannot create an instance of `_bleio.Adapter`;
7979
//| this constructor will raise `NotImplementedError`.

shared-bindings/_bleio/__init__.c

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ STATIC mp_obj_dict_t bleio_module_globals;
123123
#endif
124124

125125
//| def set_adapter(adapter: Optional[_bleio.Adapter]) -> None:
126-
//| """Set the adapter to use for BLE. Not settable when the adapter is a singleton."""
126+
//| """Set the adapter to use for BLE, such as when using an HCI adapter.
127+
//| Raises `NotImplementedError` when the adapter is a singleton and cannot be set."""
127128
//| ...
128129
//|
129130
mp_obj_t bleio_set_adapter(mp_obj_t adapter_obj) {
@@ -147,73 +148,53 @@ MP_DEFINE_CONST_FUN_OBJ_1(bleio_set_adapter_obj, bleio_set_adapter);
147148

148149
#if CIRCUITPY_BLEIO_HCI
149150
// Make the module dictionary be in RAM, so that _bleio.adapter can be set.
150-
151+
// Use a local macro to define how table entries should be converted.
152+
#define OBJ_FROM_PTR MP_OBJ_FROM_PTR
151153
STATIC mp_map_elem_t bleio_module_globals_table[] = {
154+
#else
155+
#define OBJ_FROM_PTR MP_ROM_PTR
156+
STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
157+
#endif
152158
// Name must be the first entry so that the exception printing below is correct.
153159
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__bleio) },
154-
{ MP_ROM_QSTR(MP_QSTR_Adapter), MP_OBJ_FROM_PTR(&bleio_adapter_type) },
155-
{ MP_ROM_QSTR(MP_QSTR_Address), MP_OBJ_FROM_PTR(&bleio_address_type) },
156-
{ MP_ROM_QSTR(MP_QSTR_Attribute), MP_OBJ_FROM_PTR(&bleio_attribute_type) },
157-
{ MP_ROM_QSTR(MP_QSTR_Connection), MP_OBJ_FROM_PTR(&bleio_connection_type) },
158-
{ MP_ROM_QSTR(MP_QSTR_Characteristic), MP_OBJ_FROM_PTR(&bleio_characteristic_type) },
159-
{ MP_ROM_QSTR(MP_QSTR_CharacteristicBuffer), MP_OBJ_FROM_PTR(&bleio_characteristic_buffer_type) },
160-
{ MP_ROM_QSTR(MP_QSTR_Descriptor), MP_OBJ_FROM_PTR(&bleio_descriptor_type) },
161-
{ MP_ROM_QSTR(MP_QSTR_PacketBuffer), MP_OBJ_FROM_PTR(&bleio_packet_buffer_type) },
162-
{ MP_ROM_QSTR(MP_QSTR_ScanEntry), MP_OBJ_FROM_PTR(&bleio_scanentry_type) },
163-
{ MP_ROM_QSTR(MP_QSTR_ScanResults), MP_OBJ_FROM_PTR(&bleio_scanresults_type) },
164-
{ MP_ROM_QSTR(MP_QSTR_Service), MP_OBJ_FROM_PTR(&bleio_service_type) },
165-
{ MP_ROM_QSTR(MP_QSTR_UUID), MP_OBJ_FROM_PTR(&bleio_uuid_type) },
166-
167-
// Attributes
168-
{ MP_ROM_QSTR(MP_QSTR_adapter), mp_const_none },
160+
{ MP_ROM_QSTR(MP_QSTR_Adapter), OBJ_FROM_PTR(&bleio_adapter_type) },
161+
{ MP_ROM_QSTR(MP_QSTR_Address), OBJ_FROM_PTR(&bleio_address_type) },
162+
{ MP_ROM_QSTR(MP_QSTR_Attribute), OBJ_FROM_PTR(&bleio_attribute_type) },
163+
{ MP_ROM_QSTR(MP_QSTR_Connection), OBJ_FROM_PTR(&bleio_connection_type) },
164+
{ MP_ROM_QSTR(MP_QSTR_Characteristic), OBJ_FROM_PTR(&bleio_characteristic_type) },
165+
{ MP_ROM_QSTR(MP_QSTR_CharacteristicBuffer), OBJ_FROM_PTR(&bleio_characteristic_buffer_type) },
166+
{ MP_ROM_QSTR(MP_QSTR_Descriptor), OBJ_FROM_PTR(&bleio_descriptor_type) },
167+
{ MP_ROM_QSTR(MP_QSTR_PacketBuffer), OBJ_FROM_PTR(&bleio_packet_buffer_type) },
168+
{ MP_ROM_QSTR(MP_QSTR_ScanEntry), OBJ_FROM_PTR(&bleio_scanentry_type) },
169+
{ MP_ROM_QSTR(MP_QSTR_ScanResults), OBJ_FROM_PTR(&bleio_scanresults_type) },
170+
{ MP_ROM_QSTR(MP_QSTR_Service), OBJ_FROM_PTR(&bleio_service_type) },
171+
{ MP_ROM_QSTR(MP_QSTR_UUID), OBJ_FROM_PTR(&bleio_uuid_type) },
169172

170-
// Functions
173+
#if CIRCUITPY_BLEIO_HCI
174+
// For HCI, _bleio.adapter is settable, and starts as None.
175+
{ MP_ROM_QSTR(MP_QSTR_adapter), mp_const_none },
171176
{ MP_ROM_QSTR(MP_QSTR_set_adapter), (mp_obj_t) &bleio_set_adapter_obj },
172-
173-
// Errors
174-
{ MP_ROM_QSTR(MP_QSTR_BluetoothError), MP_OBJ_FROM_PTR(&mp_type_bleio_BluetoothError) },
175-
{ MP_ROM_QSTR(MP_QSTR_ConnectionError), MP_OBJ_FROM_PTR(&mp_type_bleio_ConnectionError) },
176-
{ MP_ROM_QSTR(MP_QSTR_RoleError), MP_OBJ_FROM_PTR(&mp_type_bleio_RoleError) },
177-
{ MP_ROM_QSTR(MP_QSTR_SecurityError), MP_OBJ_FROM_PTR(&mp_type_bleio_SecurityError) },
178-
179-
// Initialization
180-
{ MP_ROM_QSTR(MP_QSTR___init__), MP_OBJ_FROM_PTR(&bleio___init___obj) },
181-
};
182-
183-
STATIC MP_DEFINE_MUTABLE_DICT(bleio_module_globals, bleio_module_globals_table);
184-
185177
#else
186-
// When _bleio.adapter is a singleton, and can't be set.
187-
188-
STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
189-
// Name must be the first entry so that the exception printing below is correct.
190-
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__bleio) },
191-
{ MP_ROM_QSTR(MP_QSTR_Adapter), MP_ROM_PTR(&bleio_adapter_type) },
192-
{ MP_ROM_QSTR(MP_QSTR_Address), MP_ROM_PTR(&bleio_address_type) },
193-
{ MP_ROM_QSTR(MP_QSTR_Attribute), MP_ROM_PTR(&bleio_attribute_type) },
194-
{ MP_ROM_QSTR(MP_QSTR_Connection), MP_ROM_PTR(&bleio_connection_type) },
195-
{ MP_ROM_QSTR(MP_QSTR_Characteristic), MP_ROM_PTR(&bleio_characteristic_type) },
196-
{ MP_ROM_QSTR(MP_QSTR_CharacteristicBuffer), MP_ROM_PTR(&bleio_characteristic_buffer_type) },
197-
{ MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_type) },
198-
{ MP_ROM_QSTR(MP_QSTR_PacketBuffer), MP_ROM_PTR(&bleio_packet_buffer_type) },
199-
{ MP_ROM_QSTR(MP_QSTR_ScanEntry), MP_ROM_PTR(&bleio_scanentry_type) },
200-
{ MP_ROM_QSTR(MP_QSTR_ScanResults), MP_ROM_PTR(&bleio_scanresults_type) },
201-
{ MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&bleio_service_type) },
202-
{ MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&bleio_uuid_type) },
203-
204-
// Properties
178+
// For non-HCI _bleio.adapter is a fixed singleton, and is not settable.
179+
// _bleio.set_adapter will raise NotImplementedError.
205180
{ MP_ROM_QSTR(MP_QSTR_adapter), MP_ROM_PTR(&common_hal_bleio_adapter_obj) },
181+
{ MP_ROM_QSTR(MP_QSTR_set_adapter), MP_ROM_PTR(&bleio_set_adapter_obj) },
182+
#endif
206183

207184
// Errors
208-
{ MP_ROM_QSTR(MP_QSTR_BluetoothError), MP_ROM_PTR(&mp_type_bleio_BluetoothError) },
209-
{ MP_ROM_QSTR(MP_QSTR_ConnectionError), MP_ROM_PTR(&mp_type_bleio_ConnectionError) },
210-
{ MP_ROM_QSTR(MP_QSTR_RoleError), MP_ROM_PTR(&mp_type_bleio_RoleError) },
211-
{ MP_ROM_QSTR(MP_QSTR_SecurityError), MP_ROM_PTR(&mp_type_bleio_SecurityError) },
185+
{ MP_ROM_QSTR(MP_QSTR_BluetoothError), OBJ_FROM_PTR(&mp_type_bleio_BluetoothError) },
186+
{ MP_ROM_QSTR(MP_QSTR_ConnectionError), OBJ_FROM_PTR(&mp_type_bleio_ConnectionError) },
187+
{ MP_ROM_QSTR(MP_QSTR_RoleError), OBJ_FROM_PTR(&mp_type_bleio_RoleError) },
188+
{ MP_ROM_QSTR(MP_QSTR_SecurityError), OBJ_FROM_PTR(&mp_type_bleio_SecurityError) },
212189

213190
// Initialization
214-
{ MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&bleio___init___obj) },
191+
{ MP_ROM_QSTR(MP_QSTR___init__), OBJ_FROM_PTR(&bleio___init___obj) },
215192
};
216193

194+
#if CIRCUITPY_BLEIO_HCI
195+
// Module dict is mutable to allow setting _bleio.adapter.
196+
STATIC MP_DEFINE_MUTABLE_DICT(bleio_module_globals, bleio_module_globals_table);
197+
#else
217198
STATIC MP_DEFINE_CONST_DICT(bleio_module_globals, bleio_module_globals_table);
218199
#endif
219200

0 commit comments

Comments
 (0)