light_sensor.c 1.69 KB
Newer Older
Rahix's avatar
Rahix committed
1
2
3
#include "epicardium.h"

#include "py/builtin.h"
4
5
6
#include "py/obj.h"
#include "py/runtime.h"

7
static mp_obj_t mp_light_sensor_start()
8
9
10
11
12
13
14
15
16
{
	int status = epic_light_sensor_run();
	if (status == -EBUSY) {
		mp_raise_msg(
			&mp_type_RuntimeError, "timer could not be scheduled"
		);
	}
	return mp_const_none;
}
17
static MP_DEFINE_CONST_FUN_OBJ_0(light_sensor_start_obj, mp_light_sensor_start);
18

19
static mp_obj_t mp_light_sensor_get_reading()
20
21
22
23
24
25
26
27
28
{
	uint16_t last;
	int status = epic_light_sensor_get(&last);
	if (status == -ENODATA) {
		mp_raise_ValueError("sensor not running");
		return mp_const_none;
	}
	return mp_obj_new_int_from_uint(last);
}
29
static MP_DEFINE_CONST_FUN_OBJ_0(
30
31
32
	light_sensor_get_obj, mp_light_sensor_get_reading
);

33
static mp_obj_t mp_light_sensor_stop()
34
35
36
37
38
39
40
41
42
{
	int status = epic_light_sensor_stop();
	if (status == -EBUSY) {
		mp_raise_msg(
			&mp_type_RuntimeError, "timer could not be stopped"
		);
	}
	return mp_const_none;
}
43
static MP_DEFINE_CONST_FUN_OBJ_0(light_sensor_stop_obj, mp_light_sensor_stop);
44

45
static const mp_rom_map_elem_t light_sensor_module_globals_table[] = {
46
47
48
49
50
	{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_light_sensor) },
	{ MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&light_sensor_start_obj) },
	{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&light_sensor_stop_obj) },
	{ MP_ROM_QSTR(MP_QSTR_get_reading), MP_ROM_PTR(&light_sensor_get_obj) }
};
51
static MP_DEFINE_CONST_DICT(
52
53
54
55
56
57
58
59
60
61
	light_sensor_module_globals, light_sensor_module_globals_table
);

const mp_obj_module_t light_sensor_module = {
	.base    = { &mp_type_module },
	.globals = (mp_obj_dict_t *)&light_sensor_module_globals,
};

/* clang-format off */
MP_REGISTER_MODULE(MP_QSTR_light_sensor, light_sensor_module, MODULE_LIGHT_SENSOR_ENABLED);