Skip to content

Commit beee58a

Browse files
committed
bleio: Add scan_entry as an param for the Device constructor
1 parent 684f267 commit beee58a

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

shared-bindings/bleio/Device.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "shared-bindings/bleio/UUID.h"
4242
#include "shared-module/bleio/AdvertisementData.h"
4343
#include "shared-module/bleio/Device.h"
44+
#include "shared-module/bleio/ScanEntry.h"
4445

4546
//| .. currentmodule:: bleio
4647
//|
@@ -87,12 +88,13 @@
8788
//| central.connect()
8889
//|
8990

90-
//| .. class:: Device(address=None)
91+
//| .. class:: Device(address=None, scan_entry=None)
9192
//|
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`,
9394
//| the role is set to Central, otherwise it's set to Peripheral.
9495
//|
9596
//| :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`
9698
//|
9799

98100
//| .. attribute:: name
@@ -167,23 +169,30 @@ STATIC mp_obj_t bleio_device_make_new(const mp_obj_type_t *type, size_t n_args,
167169
mp_map_t kw_args;
168170
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
169171

170-
//TODO: Add ScanEntry
171-
enum { ARG_address };
172+
enum { ARG_address, ARG_scan_entry };
172173
static const mp_arg_t allowed_args[] = {
173174
{ 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} },
174176
};
175177

176178
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
177179
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
178180

179181
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;
180183

181184
if (address_obj != mp_const_none) {
182185
bleio_address_obj_t *address = MP_OBJ_TO_PTR(address_obj);
183186

184187
self->is_peripheral = false;
185188
self->address.type = address->type;
186189
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);
187196
} else {
188197
self->name = mp_obj_new_str(default_name, strlen(default_name), false);
189198
common_hal_bleio_adapter_get_address(&self->address);

0 commit comments

Comments
 (0)