interrupt.c 2.91 KB
Newer Older
1
#include "interrupt.h"
Rahix's avatar
Rahix committed
2
3
4
5
6

#include "api/common.h"
#include "epicardium.h"

#include "py/builtin.h"
7
8
9
10
#include "py/obj.h"
#include "py/runtime.h"

// TODO: these should be intialized as mp_const_none
11
mp_obj_t callbacks[EPIC_INT_NUM] = {
12
13
14
	0,
};

15
void epic_isr_default_handler(api_int_id_t id)
16
17
18
{
	// TODO: check if id is out of rante
	// TOOD: check against mp_const_none
19
	if (id < EPIC_INT_NUM) {
20
21
22
23
24
25
26
27
		if (callbacks[id]) {
			mp_sched_schedule(
				callbacks[id], MP_OBJ_NEW_SMALL_INT(id)
			);
		}
	}
}

28
mp_obj_t mp_interrupt_set_callback(mp_obj_t id_in, mp_obj_t callback_obj)
29
30
31
32
33
34
35
36
{
	api_int_id_t id = mp_obj_get_int(id_in);
	if (callback_obj != mp_const_none &&
	    !mp_obj_is_callable(callback_obj)) {
		mp_raise_ValueError("handler must be None or callable");
	}

	// TODO: throw error if id is out of range
37
	if (id < EPIC_INT_NUM) {
38
39
40
41
42
43
		callbacks[id] = callback_obj;
	}

	return mp_const_none;
}

44
mp_obj_t mp_interrupt_enable_callback(mp_obj_t id_in)
45
46
47
48
{
	api_int_id_t id = mp_obj_get_int(id_in);

	// TODO: throw error if id is out of range or epic_interrupt_enable failed
49
50
	if (epic_interrupt_enable(id) < 0) {
	}
51
52
53
54

	return mp_const_none;
}

55
mp_obj_t mp_interrupt_disable_callback(mp_obj_t id_in)
56
57
58
59
60
61
62
63
64
65
{
	api_int_id_t id = mp_obj_get_int(id_in);

	// TODO: throw error if id is out of range or epic_interrupt_enable failed
	if (epic_interrupt_disable(id) < 0) {
	}

	return mp_const_none;
}

66
static MP_DEFINE_CONST_FUN_OBJ_2(
67
68
	interrupt_set_callback_obj, mp_interrupt_set_callback
);
69
static MP_DEFINE_CONST_FUN_OBJ_1(
70
71
	interrupt_enable_callback_obj, mp_interrupt_enable_callback
);
72
static MP_DEFINE_CONST_FUN_OBJ_1(
73
74
	interrupt_disable_callback_obj, mp_interrupt_disable_callback
);
75

76
static const mp_rom_map_elem_t interrupt_module_globals_table[] = {
77
78
79
80
81
	{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_interrupt) },
	{ MP_ROM_QSTR(MP_QSTR_set_callback),
	  MP_ROM_PTR(&interrupt_set_callback_obj) },
	{ MP_ROM_QSTR(MP_QSTR_enable_callback),
	  MP_ROM_PTR(&interrupt_enable_callback_obj) },
82
83
	{ MP_ROM_QSTR(MP_QSTR_disable_callback),
	  MP_ROM_PTR(&interrupt_disable_callback_obj) },
84
85
86
87

	/* Interrupt Numbers */
	{ MP_ROM_QSTR(MP_QSTR_RTC_ALARM),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_RTC_ALARM) },
88
89
	{ MP_ROM_QSTR(MP_QSTR_BHI160_ACCELEROMETER),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_BHI160_ACCELEROMETER) },
koalo's avatar
koalo committed
90
91
	{ MP_ROM_QSTR(MP_QSTR_BHI160_ORIENTATION),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_BHI160_ORIENTATION) },
koalo's avatar
koalo committed
92
93
	{ MP_ROM_QSTR(MP_QSTR_BHI160_GYROSCOPE),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_BHI160_GYROSCOPE) },
schneider's avatar
schneider committed
94
95
96
	{ MP_ROM_QSTR(MP_QSTR_MAX30001_ECG),
	  MP_OBJ_NEW_SMALL_INT(EPIC_INT_MAX30001_ECG) },

97
};
98
static MP_DEFINE_CONST_DICT(
99
100
101
102
103
104
105
106
107
108
109
110
	interrupt_module_globals, interrupt_module_globals_table
);

// Define module object.
const mp_obj_module_t interrupt_module = {
	.base    = { &mp_type_module },
	.globals = (mp_obj_dict_t *)&interrupt_module_globals,
};

/* clang-format off */
// Register the module to make it available in Python
MP_REGISTER_MODULE(MP_QSTR_interrupt, interrupt_module, MODULE_INTERRUPT_ENABLED);