bhi160-sys.c 2.53 KB
Newer Older
1
2
3
4
5
6
7
#include "py/obj.h"
#include "py/runtime.h"
#include "py/builtin.h"
#include "epicardium.h"
#include "api/common.h"
#include "mphalport.h"

8
9
extern const mp_obj_type_t mp_type_bhi160_sample;

10
11
12
13
14
15
16
17
18
STATIC mp_obj_t mp_bhi160_enable_sensor(size_t n_args, const mp_obj_t *args)
{
	int sensor_type = mp_obj_get_int(args[0]);

	struct bhi160_sensor_config cfg = { 0 };
	cfg.sample_buffer_len           = mp_obj_get_int(args[1]);
	cfg.sample_rate                 = mp_obj_get_int(args[2]);
	cfg.dynamic_range               = mp_obj_get_int(args[3]);

koalo's avatar
koalo committed
19
	int stream_id = epic_bhi160_enable_sensor(sensor_type, &cfg);
20

koalo's avatar
koalo committed
21
	return MP_OBJ_NEW_SMALL_INT(stream_id);
22
23
}

koalo's avatar
koalo committed
24
25
26
27
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	mp_bhi160_enable_sensor_obj, 4, 4, mp_bhi160_enable_sensor
);

28
29
30
STATIC mp_obj_t mp_bhi160_read_sensor(mp_obj_t stream_id_in)
{
	struct bhi160_data_vector buf[100];
koalo's avatar
koalo committed
31
	int stream_id = mp_obj_get_int(stream_id_in);
32

koalo's avatar
koalo committed
33
	int n = epic_stream_read(stream_id, buf, sizeof(buf));
34

35
36
	mp_obj_list_t *list = mp_obj_new_list(0, NULL);
	for (int i = 0; i < n; i++) {
37
38
39
40
41
42
43
44
45
		if (buf[i].data_type != BHI160_DATA_TYPE_VECTOR) {
			// other data types are currently not supported
			mp_raise_OSError(EINVAL);
		}
		mp_obj_t tuple[3];
		tuple[0] = mp_obj_new_int(buf[i].x);
		tuple[1] = mp_obj_new_int(buf[i].y);
		tuple[2] = mp_obj_new_int(buf[i].z);
		mp_obj_list_append(list, mp_obj_new_tuple(3, tuple));
46
47
48
49
50
	}

	return MP_OBJ_FROM_PTR(list);
}

koalo's avatar
koalo committed
51
52
STATIC MP_DEFINE_CONST_FUN_OBJ_1(
	mp_bhi160_read_sensor_obj, mp_bhi160_read_sensor
53
54
);

koalo's avatar
koalo committed
55
56
57
58
59
60
61
62
63
STATIC mp_obj_t mp_bhi160_disable_sensor(mp_obj_t sensor_type_in)
{
	int sensor_type = mp_obj_get_int(sensor_type_in);

	int ret = epic_bhi160_disable_sensor(sensor_type);

	return MP_OBJ_NEW_SMALL_INT(ret);
}

64
STATIC MP_DEFINE_CONST_FUN_OBJ_1(
koalo's avatar
koalo committed
65
	mp_bhi160_disable_sensor_obj, mp_bhi160_disable_sensor
66
67
);

koalo's avatar
koalo committed
68

69
70
71
72
73
74
STATIC const mp_rom_map_elem_t bhi160_module_globals_table[] = {
	{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys_bhi160) },
	{ MP_ROM_QSTR(MP_QSTR_enable_sensor),
	  MP_ROM_PTR(&mp_bhi160_enable_sensor_obj) },
	{ MP_ROM_QSTR(MP_QSTR_read_sensor),
	  MP_ROM_PTR(&mp_bhi160_read_sensor_obj) },
koalo's avatar
koalo committed
75
76
	{ MP_ROM_QSTR(MP_QSTR_disable_sensor),
	  MP_ROM_PTR(&mp_bhi160_disable_sensor_obj) },
77
78
79
80
81
82
83
84
85
86
87
88
};
STATIC MP_DEFINE_CONST_DICT(bhi160_module_globals, bhi160_module_globals_table);

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

/* clang-format off */
// Register the module to make it available in Python
MP_REGISTER_MODULE(MP_QSTR_sys_bhi160, bhi160_module, MODULE_BHI160_ENABLED);