4
4
// Used to get the time in the Timer class example.
5
5
#include "py/mphal.h"
6
6
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
+
7
15
// This is the function which will be called from Python as cexample.add_ints(a, b).
8
16
STATIC mp_obj_t example_add_ints (mp_obj_t a_obj , mp_obj_t b_obj ) {
9
17
// Extract the ints from the micropython input objects.
@@ -24,6 +32,9 @@ typedef struct _example_Timer_obj_t {
24
32
// cannot be accessed by MicroPython code directly. In this example we
25
33
// store the time at which the object was created.
26
34
mp_uint_t start_time ;
35
+ #if ENABLE_DEINIT_FUNCTION
36
+ bool init ;
37
+ #endif
27
38
} example_Timer_obj_t ;
28
39
29
40
// 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) {
39
50
}
40
51
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (example_Timer_time_obj , example_Timer_time );
41
52
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
+
42
67
// This represents Timer.__new__ and Timer.__init__, which is called when
43
68
// the user instantiates a Timer object.
44
69
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
+
45
71
// 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
46
77
example_Timer_obj_t * self = mp_obj_malloc (example_Timer_obj_t , type );
47
-
78
+ #endif
48
79
// Initializes the time for this Timer instance.
49
80
self -> start_time = mp_hal_ticks_ms ();
50
81
@@ -56,6 +87,9 @@ STATIC mp_obj_t example_Timer_make_new(const mp_obj_type_t *type, size_t n_args,
56
87
// The table structure is similar to the module table, as detailed below.
57
88
STATIC const mp_rom_map_elem_t example_Timer_locals_dict_table [] = {
58
89
{ 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
59
93
};
60
94
STATIC MP_DEFINE_CONST_DICT (example_Timer_locals_dict , example_Timer_locals_dict_table );
61
95
0 commit comments