|
41 | 41 | #include "shared-bindings/bleio/UUID.h" |
42 | 42 | #include "shared-module/bleio/AdvertisementData.h" |
43 | 43 | #include "shared-module/bleio/Device.h" |
| 44 | +#include "shared-module/bleio/ScanEntry.h" |
44 | 45 |
|
45 | 46 | //| .. currentmodule:: bleio |
46 | 47 | //| |
|
87 | 88 | //| central.connect() |
88 | 89 | //| |
89 | 90 |
|
90 | | -//| .. class:: Device(address=None) |
| 91 | +//| .. class:: Device(address=None, scan_entry=None) |
91 | 92 | //| |
92 | | -//| Create a new Device object. If the `address` parameter is not `None`, |
| 93 | +//| Create a new Device object. If the `address` or `scan_entry` parameters are not `None`, |
93 | 94 | //| the role is set to Central, otherwise it's set to Peripheral. |
94 | 95 | //| |
95 | 96 | //| :param bleio.Address address: The address of the device to connect to |
| 97 | +//| :param bleio.ScanEntry scan_entry: The scan entry returned from `bleio.Scanner` |
96 | 98 | //| |
97 | 99 |
|
98 | 100 | //| .. attribute:: name |
@@ -167,23 +169,30 @@ STATIC mp_obj_t bleio_device_make_new(const mp_obj_type_t *type, size_t n_args, |
167 | 169 | mp_map_t kw_args; |
168 | 170 | mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); |
169 | 171 |
|
170 | | - //TODO: Add ScanEntry |
171 | | - enum { ARG_address }; |
| 172 | + enum { ARG_address, ARG_scan_entry }; |
172 | 173 | static const mp_arg_t allowed_args[] = { |
173 | 174 | { ARG_address, MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 175 | + { MP_QSTR_scan_entry, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} }, |
174 | 176 | }; |
175 | 177 |
|
176 | 178 | mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
177 | 179 | mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
178 | 180 |
|
179 | 181 | const mp_obj_t address_obj = args[ARG_address].u_obj; |
| 182 | + const mp_obj_t scan_entry_obj = args[ARG_scan_entry].u_obj; |
180 | 183 |
|
181 | 184 | if (address_obj != mp_const_none) { |
182 | 185 | bleio_address_obj_t *address = MP_OBJ_TO_PTR(address_obj); |
183 | 186 |
|
184 | 187 | self->is_peripheral = false; |
185 | 188 | self->address.type = address->type; |
186 | 189 | memcpy(self->address.value, address->value, BLEIO_ADDRESS_BYTES); |
| 190 | + } else if (scan_entry_obj != mp_const_none) { |
| 191 | + bleio_scanentry_obj_t *scan_entry = MP_OBJ_TO_PTR(scan_entry_obj); |
| 192 | + |
| 193 | + self->is_peripheral = false; |
| 194 | + self->address.type = scan_entry->address.type; |
| 195 | + memcpy(self->address.value, scan_entry->address.value, BLEIO_ADDRESS_BYTES); |
187 | 196 | } else { |
188 | 197 | self->name = mp_obj_new_str(default_name, strlen(default_name), false); |
189 | 198 | common_hal_bleio_adapter_get_address(&self->address); |
|
0 commit comments