leds.c 1.48 KB
Newer Older
Rahix's avatar
Rahix committed
1
#include "py/obj.h"
2
3
4
#include "py/objlist.h"
#include "py/runtime.h"

Rahix's avatar
Rahix committed
5
6
#include "epicardium.h"

7
static mp_obj_t mp_leds_set(mp_obj_t led_in, mp_obj_t color_in)
Rahix's avatar
Rahix committed
8
{
9
10
	int led = mp_obj_get_int(led_in);

11
12
	if (mp_obj_get_int(mp_obj_len(color_in)) < 3) {
		mp_raise_ValueError("color must have 3 elements");
13
14
	}

15
16
17
18
19
	uint8_t red = mp_obj_get_int(
		mp_obj_subscr(color_in, mp_obj_new_int(0), MP_OBJ_SENTINEL)
	);
	uint8_t green = mp_obj_get_int(
		mp_obj_subscr(color_in, mp_obj_new_int(1), MP_OBJ_SENTINEL)
20
	);
21
22
23
24
25
	uint8_t blue = mp_obj_get_int(
		mp_obj_subscr(color_in, mp_obj_new_int(2), MP_OBJ_SENTINEL)
	);

	epic_leds_set(led, red, green, blue);
Rahix's avatar
Rahix committed
26
27
28

	return mp_const_none;
}
29
static MP_DEFINE_CONST_FUN_OBJ_2(leds_set_obj, mp_leds_set);
Rahix's avatar
Rahix committed
30
31

static const mp_rom_map_elem_t leds_module_globals_table[] = {
32
33
	{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_leds) },
	{ MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&leds_set_obj) },
34
35
36
37
	{ MP_ROM_QSTR(MP_QSTR_BOTTOM_RIGHT), MP_OBJ_NEW_SMALL_INT(11) },
	{ MP_ROM_QSTR(MP_QSTR_BOTTOM_LEFT), MP_OBJ_NEW_SMALL_INT(12) },
	{ MP_ROM_QSTR(MP_QSTR_TOP_RIGHT), MP_OBJ_NEW_SMALL_INT(13) },
	{ MP_ROM_QSTR(MP_QSTR_TOP_LEFT), MP_OBJ_NEW_SMALL_INT(14) },
Rahix's avatar
Rahix committed
38
39
40
41
};
static MP_DEFINE_CONST_DICT(leds_module_globals, leds_module_globals_table);

const mp_obj_module_t leds_module = {
Rahix's avatar
Rahix committed
42
	.base    = { &mp_type_module },
Rahix's avatar
Rahix committed
43
	.globals = (mp_obj_dict_t *)&leds_module_globals,
Rahix's avatar
Rahix committed
44
45
46
};

/* Register the module to make it available in Python */
47
/* clang-format off */
Rahix's avatar
Rahix committed
48
MP_REGISTER_MODULE(MP_QSTR_leds, leds_module, MODULE_LEDS_ENABLED);