12
12
#include "shared-bindings/_bleio/Characteristic.h"
13
13
#include "shared-bindings/_bleio/Service.h"
14
14
#include "shared-bindings/_bleio/UUID.h"
15
+ #include "shared-bindings/util.h"
16
+
15
17
16
18
//| class Characteristic:
17
19
//| """Stores information about a BLE service characteristic and allows reading
@@ -137,14 +139,29 @@ static mp_obj_t bleio_characteristic_add_to_service(size_t n_args, const mp_obj_
137
139
static MP_DEFINE_CONST_FUN_OBJ_KW (bleio_characteristic_add_to_service_fun_obj , 1 , bleio_characteristic_add_to_service ) ;
138
140
static MP_DEFINE_CONST_CLASSMETHOD_OBJ (bleio_characteristic_add_to_service_obj , MP_ROM_PTR (& bleio_characteristic_add_to_service_fun_obj )) ;
139
141
142
+ //| def deinit(self) -> None:
143
+ //| """Deinitialises the Characteristic and releases any hardware resources for reuse."""
144
+ //| ...
145
+ static mp_obj_t bleio_characteristic_deinit (mp_obj_t self_in ) {
146
+ bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
147
+ common_hal_bleio_characteristic_deinit (self );
148
+ return mp_const_none ;
149
+ }
150
+ static MP_DEFINE_CONST_FUN_OBJ_1 (bleio_characteristic_deinit_obj , bleio_characteristic_deinit ) ;
140
151
152
+ static void check_for_deinit (bleio_characteristic_obj_t * self ) {
153
+ if (common_hal_bleio_characteristic_deinited (self )) {
154
+ raise_deinited_error ();
155
+ }
156
+ }
141
157
142
158
//| properties: int
143
159
//| """An int bitmask representing which properties are set, specified as bitwise or'ing of
144
160
//| of these possible values.
145
161
//| `BROADCAST`, `INDICATE`, `NOTIFY`, `READ`, `WRITE`, `WRITE_NO_RESPONSE`."""
146
162
static mp_obj_t bleio_characteristic_get_properties (mp_obj_t self_in ) {
147
163
bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
164
+ check_for_deinit (self );
148
165
149
166
return MP_OBJ_NEW_SMALL_INT (common_hal_bleio_characteristic_get_properties (self ));
150
167
}
@@ -159,6 +176,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_properties_obj,
159
176
//| Will be ``None`` if the 128-bit UUID for this characteristic is not known."""
160
177
static mp_obj_t bleio_characteristic_get_uuid (mp_obj_t self_in ) {
161
178
bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
179
+ check_for_deinit (self );
162
180
163
181
bleio_uuid_obj_t * uuid = common_hal_bleio_characteristic_get_uuid (self );
164
182
return uuid ? MP_OBJ_FROM_PTR (uuid ) : mp_const_none ;
@@ -172,6 +190,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_uuid_obj,
172
190
//| """The value of this characteristic."""
173
191
static mp_obj_t bleio_characteristic_get_value (mp_obj_t self_in ) {
174
192
bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
193
+ check_for_deinit (self );
175
194
176
195
uint8_t temp [512 ];
177
196
size_t actual_len = common_hal_bleio_characteristic_get_value (self , temp , sizeof (temp ));
@@ -181,6 +200,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_value_obj, bleio_chara
181
200
182
201
static mp_obj_t bleio_characteristic_set_value (mp_obj_t self_in , mp_obj_t value_in ) {
183
202
bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
203
+ check_for_deinit (self );
184
204
185
205
mp_buffer_info_t bufinfo ;
186
206
mp_get_buffer_raise (value_in , & bufinfo , MP_BUFFER_READ );
@@ -199,6 +219,7 @@ MP_PROPERTY_GETSET(bleio_characteristic_value_obj,
199
219
//| """The max length of this characteristic."""
200
220
static mp_obj_t bleio_characteristic_get_max_length (mp_obj_t self_in ) {
201
221
bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
222
+ check_for_deinit (self );
202
223
203
224
return MP_OBJ_NEW_SMALL_INT (common_hal_bleio_characteristic_get_max_length (self ));
204
225
}
@@ -211,6 +232,8 @@ MP_PROPERTY_GETTER(bleio_characteristic_max_length_obj,
211
232
//| """A tuple of :py:class:`Descriptor` objects related to this characteristic. (read-only)"""
212
233
static mp_obj_t bleio_characteristic_get_descriptors (mp_obj_t self_in ) {
213
234
bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
235
+ check_for_deinit (self );
236
+
214
237
// Return list as a tuple so user won't be able to change it.
215
238
return MP_OBJ_FROM_PTR (common_hal_bleio_characteristic_get_descriptors (self ));
216
239
}
@@ -224,6 +247,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_descriptors_obj,
224
247
//| """The Service this Characteristic is a part of."""
225
248
static mp_obj_t bleio_characteristic_get_service (mp_obj_t self_in ) {
226
249
bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
250
+ check_for_deinit (self );
227
251
228
252
return common_hal_bleio_characteristic_get_service (self );
229
253
}
@@ -241,6 +265,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_service_obj,
241
265
//| ...
242
266
static mp_obj_t bleio_characteristic_set_cccd (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
243
267
bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
268
+ check_for_deinit (self );
244
269
245
270
enum { ARG_notify , ARG_indicate };
246
271
static const mp_arg_t allowed_args [] = {
@@ -258,6 +283,9 @@ static mp_obj_t bleio_characteristic_set_cccd(mp_uint_t n_args, const mp_obj_t *
258
283
static MP_DEFINE_CONST_FUN_OBJ_KW (bleio_characteristic_set_cccd_obj , 1 , bleio_characteristic_set_cccd ) ;
259
284
260
285
static const mp_rom_map_elem_t bleio_characteristic_locals_dict_table [] = {
286
+ { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& bleio_characteristic_deinit_obj ) },
287
+ { MP_ROM_QSTR (MP_QSTR___del__ ), MP_ROM_PTR (& bleio_characteristic_deinit_obj ) },
288
+
261
289
{ MP_ROM_QSTR (MP_QSTR_add_to_service ), MP_ROM_PTR (& bleio_characteristic_add_to_service_obj ) },
262
290
{ MP_ROM_QSTR (MP_QSTR_descriptors ), MP_ROM_PTR (& bleio_characteristic_descriptors_obj ) },
263
291
{ MP_ROM_QSTR (MP_QSTR_properties ), MP_ROM_PTR (& bleio_characteristic_properties_obj ) },
0 commit comments