8000 examples/usercmodule: Add finaliser to cexample. · micropython/micropython@d7ba712 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7ba712

Browse files
committed
examples/usercmodule: Add finaliser to cexample.
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent a00c9d5 commit d7ba712

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

examples/usercmodule/cexample/examplemodule.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
// Used to get the time in the Timer class example.
55
#include "py/mphal.h"
66

7+
// Enable this if you have resources that should
8+
// be de-initialised after use.
9+
#define ENABLE_DEINIT_FUNCTION (1)
10+
11+
#if ENABLE_DEINIT_FUNCTION && !MICROPY_ENABLE_FINALISER
12+
#error example deinit requires: MICROPY_ENABLE_FINALISER
13+
#endif
14+
715
// This is the function which will be called from Python as cexample.add_ints(a, b).
816
STATIC mp_obj_t example_add_ints(mp_obj_t a_obj, mp_obj_t b_obj) {
917
// Extract the ints from the micropython input objects.
@@ -24,6 +32,9 @@ typedef struct _example_Timer_obj_t {
2432
// cannot be accessed by MicroPython code directly. In this example we
2533
// store the time at which the object was created.
2634
mp_uint_t start_time;
35+
#if ENABLE_DEINIT_FUNCTION
36+
bool init;
37+
#endif
2738
} example_Timer_obj_t;
2839

2940
// This is the Timer.time() method. After creating a Timer object, this
@@ -39,12 +50,32 @@ STATIC mp_obj_t example_Timer_time(mp_obj_t self_in) {
3950
}
4051
STATIC MP_DEFINE_CONST_FUN_OBJ_1(example_Timer_time_obj, example_Timer_time);
4152

53+
#if ENABLE_DEINIT_FUNCTION
54+
STATIC mp_obj_t example_Timer___del__(mp_obj_t self_in) {
55+
// This __del__ function can be used to deinit hardware or
56+
// other resources after the class is destroyed / soft-reset.
57+
example_Timer_obj_t *self = MP_OBJ_TO_PTR(self_in);
58+
if (self->init) {
59+
self->init = false;
60+
mp_printf(MICROPY_ERROR_PRINTER, "de-init cexample resources.\n");
61+
}
62+
return mp_const_none;
63+
}
64+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(example_Timer___del___obj, example_Timer___del__);
65+
#endif
66+
4267
// This represents Timer.__new__ and Timer.__init__, which is called when
4368
// the user instantiates a Timer object.
4469
STATIC mp_obj_t example_Timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
70+
4571
// Allocates the new object and sets the type.
72+
#if ENABLE_DEINIT_FUNCTION
73+
example_Timer_obj_t *self = m_new_obj_with_finaliser(example_Timer_obj_t);
74+
self->base.type = type;
75+
self->init = true;
76+
#else
4677
example_Timer_obj_t *self = mp_obj_malloc(example_Timer_obj_t, type);
47-
78+
#endif
4879
// Initializes the time for this Timer instance.
4980
self->start_time = mp_hal_ticks_ms();
5081

@@ -56,6 +87,9 @@ STATIC mp_obj_t example_Timer_make_new(const mp_obj_type_t *type, size_t n_args,
5687
// The table structure is similar to the module table, as detailed below.
5788
STATIC const mp_rom_map_elem_t example_Timer_locals_dict_table[] = {
5889
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&example_Timer_time_obj) },
90+
#if ENABLE_DEINIT_FUNCTION
91+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&example_Timer___del___obj) },
92+
#endif
5993
};
6094
STATIC MP_DEFINE_CONST_DICT(example_Timer_locals_dict, example_Timer_locals_dict_table);
6195

tests/misc/cexample_class.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<Timer>
22
True
33
True
4+
de-init cexample resources.

0 commit comments

Comments
 (0)
0