bhi160-sys.c 2.14 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
24
25
26
}

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
27
	int stream_id = mp_obj_get_int(stream_id_in);
28

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

31
32
	mp_obj_list_t *list = mp_obj_new_list(0, NULL);
	for (int i = 0; i < n; i++) {
33
34
35
36
37
38
39
40
41
		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));
42
43
44
45
46
	}

	return MP_OBJ_FROM_PTR(list);
}

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	mp_bhi160_enable_sensor_obj, 4, 4, mp_bhi160_enable_sensor
);

STATIC MP_DEFINE_CONST_FUN_OBJ_1(
	mp_bhi160_read_sensor_obj, mp_bhi160_read_sensor
);

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) },
};
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);