sys_leds.c 8.78 KB
Newer Older
Rahix's avatar
Rahix committed
1
2
#include "epicardium.h"

fleur's avatar
fleur committed
3
4
5
#include "py/obj.h"
#include "py/objlist.h"
#include "py/runtime.h"
Rahix's avatar
Rahix committed
6

fleur's avatar
fleur committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
73
74
75
76
77
78
79
80
#include <stdio.h>

static mp_obj_t mp_leds_set(mp_obj_t led_in, mp_obj_t color_in)
{
	int led = mp_obj_get_int(led_in);

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

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

	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(leds_set_obj, mp_leds_set);

static mp_obj_t mp_leds_set_hsv(mp_obj_t led_in, mp_obj_t color_in)
{
	int led = mp_obj_get_int(led_in);

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

	float h = mp_obj_get_float(
		mp_obj_subscr(color_in, mp_obj_new_int(0), MP_OBJ_SENTINEL)
	);
	float s = mp_obj_get_float(
		mp_obj_subscr(color_in, mp_obj_new_int(1), MP_OBJ_SENTINEL)
	);
	float v = mp_obj_get_float(
		mp_obj_subscr(color_in, mp_obj_new_int(2), MP_OBJ_SENTINEL)
	);

	epic_leds_set_hsv(led, h, s, v);

	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(leds_set_hsv_obj, mp_leds_set_hsv);

static mp_obj_t mp_leds_prep(mp_obj_t led_in, mp_obj_t color_in)
{
	int led = mp_obj_get_int(led_in);

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

	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)
	);
	uint8_t blue = mp_obj_get_int(
		mp_obj_subscr(color_in, mp_obj_new_int(2), MP_OBJ_SENTINEL)
	);

	epic_leds_prep(led, red, green, blue);

	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(leds_prep_obj, mp_leds_prep);

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
static mp_obj_t mp_leds_get_rgb(mp_obj_t led_in)
{
	int led       = mp_obj_get_int(led_in);
	uint8_t rgb[] = { 0, 0, 0 };
	int retAPI    = epic_leds_get_rgb(led, rgb);
	if (retAPI == -EPERM) {
		mp_raise_ValueError(
			"no permission: maybe blocked by personal state"
		);
	}
	if (retAPI == -EINVAL) {
		mp_raise_ValueError(
			"invalid value: maybe the led does not exists"
		);
	}

	mp_obj_t ret          = mp_obj_new_tuple(3, NULL);
	mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(ret);

	tuple->items[0] = MP_OBJ_NEW_SMALL_INT(rgb[0]);
	tuple->items[1] = MP_OBJ_NEW_SMALL_INT(rgb[1]);
	tuple->items[2] = MP_OBJ_NEW_SMALL_INT(rgb[2]);

	return ret;
}
static MP_DEFINE_CONST_FUN_OBJ_1(leds_get_rgb_obj, mp_leds_get_rgb);

fleur's avatar
fleur committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
static mp_obj_t mp_leds_prep_hsv(mp_obj_t led_in, mp_obj_t color_in)
{
	int led = mp_obj_get_int(led_in);

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

	float h = mp_obj_get_float(
		mp_obj_subscr(color_in, mp_obj_new_int(0), MP_OBJ_SENTINEL)
	);
	float s = mp_obj_get_float(
		mp_obj_subscr(color_in, mp_obj_new_int(1), MP_OBJ_SENTINEL)
	);
	float v = mp_obj_get_float(
		mp_obj_subscr(color_in, mp_obj_new_int(2), MP_OBJ_SENTINEL)
	);

	epic_leds_prep_hsv(led, h, s, v);

	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(leds_prep_hsv_obj, mp_leds_prep_hsv);

static mp_obj_t mp_leds_set_all(mp_obj_t color_in)
{
	uint8_t len = mp_obj_get_int(mp_obj_len(color_in));
	uint8_t pattern[len][3];
	for (int i = 0; i < len; i++) {
		mp_obj_t color = mp_obj_subscr(
			color_in, mp_obj_new_int(i), MP_OBJ_SENTINEL
		);
		pattern[i][0] = mp_obj_get_int(mp_obj_subscr(
			color, mp_obj_new_int(0), MP_OBJ_SENTINEL)
		);
		pattern[i][1] = mp_obj_get_int(mp_obj_subscr(
			color, mp_obj_new_int(1), MP_OBJ_SENTINEL)
		);
		pattern[i][2] = mp_obj_get_int(mp_obj_subscr(
			color, mp_obj_new_int(2), MP_OBJ_SENTINEL)
		);
	}
	epic_leds_set_all((uint8_t *)pattern, len);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(leds_set_all_obj, mp_leds_set_all);

static mp_obj_t mp_leds_set_all_hsv(mp_obj_t color_in)
{
	uint8_t len = mp_obj_get_int(mp_obj_len(color_in));
	float pattern[len][3];
	for (int i = 0; i < len; i++) {
		mp_obj_t color = mp_obj_subscr(
			color_in, mp_obj_new_int(i), MP_OBJ_SENTINEL
		);
		pattern[i][0] = mp_obj_get_float(mp_obj_subscr(
			color, mp_obj_new_int(0), MP_OBJ_SENTINEL)
		);
		pattern[i][1] = mp_obj_get_float(mp_obj_subscr(
			color, mp_obj_new_int(1), MP_OBJ_SENTINEL)
		);
		pattern[i][2] = mp_obj_get_float(mp_obj_subscr(
			color, mp_obj_new_int(2), MP_OBJ_SENTINEL)
		);
	}
	epic_leds_set_all_hsv((float *)pattern, len);

	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(leds_set_all_hsv_obj, mp_leds_set_all_hsv);

static mp_obj_t mp_leds_set_flashlight(mp_obj_t on_in)
{
	int on = mp_obj_get_int(on_in);
	epic_set_flashlight(on);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(
	leds_set_flashlight_obj, mp_leds_set_flashlight
);

static mp_obj_t mp_leds_set_rocket(mp_obj_t led_in, mp_obj_t value_in)
{
	int led   = mp_obj_get_int(led_in);
	int value = mp_obj_get_int(value_in);
Rahix's avatar
Rahix committed
193
194
195
196
197

	if (value > 31) {
		mp_raise_ValueError("brightness must by < 32");
	}

fleur's avatar
fleur committed
198
199
200
201
202
	epic_leds_set_rocket(led, value);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(leds_set_rocket_obj, mp_leds_set_rocket);

203
204
static mp_obj_t mp_leds_get_rocket(mp_obj_t led_in)
{
205
206
	int led = mp_obj_get_int(led_in);
	int ret = epic_leds_get_rocket(led);
207
208
209
210
211
212
213
214
215
	if (ret == -EINVAL) {
		mp_raise_ValueError(
			"invalid value: maybe the led does not exists"
		);
	}
	return MP_OBJ_NEW_SMALL_INT(ret);
}
static MP_DEFINE_CONST_FUN_OBJ_1(leds_get_rocket_obj, mp_leds_get_rocket);

fleur's avatar
fleur committed
216
217
218
static mp_obj_t mp_leds_dim_top(mp_obj_t dim_in)
{
	int dim = mp_obj_get_int(dim_in);
Rahix's avatar
Rahix committed
219
220
221
222
223

	if (dim > 8) {
		mp_raise_ValueError("maximum allowed dim is 8");
	}

fleur's avatar
fleur committed
224
225
226
227
228
229
230
231
	epic_leds_dim_top(dim);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(leds_dim_top_obj, mp_leds_dim_top);

static mp_obj_t mp_leds_dim_bottom(mp_obj_t dim_in)
{
	int dim = mp_obj_get_int(dim_in);
Rahix's avatar
Rahix committed
232
233
234
235
236

	if (dim > 8) {
		mp_raise_ValueError("maximum allowed dim is 8");
	}

fleur's avatar
fleur committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
	epic_leds_dim_bottom(dim);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(leds_dim_bottom_obj, mp_leds_dim_bottom);

static mp_obj_t mp_leds_update()
{
	epic_leds_update();
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_0(leds_update_obj, mp_leds_update);

static mp_obj_t mp_leds_set_powersave(mp_obj_t eco_in)
{
	int eco = mp_obj_get_int(eco_in);
	epic_leds_set_powersave(eco);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(leds_set_powersave_obj, mp_leds_set_powersave);

static mp_obj_t
mp_leds_set_gamma(mp_obj_t rgb_channel_in, mp_obj_t gamma_table_in)
{
	int rgb_channel = mp_obj_get_int(rgb_channel_in);
	if (mp_obj_get_int(mp_obj_len(gamma_table_in)) != 256) {
		mp_raise_ValueError("table must have 256 elements");
	}
	uint8_t gamma_table[256];
	for (int i = 0; i < 256; i++) {
		gamma_table[i] = mp_obj_get_int(mp_obj_subscr(
			gamma_table_in, mp_obj_new_int(i), MP_OBJ_SENTINEL)
		);
	}
	epic_leds_set_gamma_table(rgb_channel, gamma_table);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(leds_set_gamma_obj, mp_leds_set_gamma);

static const mp_rom_map_elem_t leds_module_globals_table[] = {
	{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys_leds) },
	{ MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&leds_set_obj) },
	{ MP_ROM_QSTR(MP_QSTR_set_hsv), MP_ROM_PTR(&leds_set_hsv_obj) },
	{ MP_ROM_QSTR(MP_QSTR_prep), MP_ROM_PTR(&leds_prep_obj) },
280
	{ MP_ROM_QSTR(MP_QSTR_get_rgb), MP_ROM_PTR(&leds_get_rgb_obj) },
fleur's avatar
fleur committed
281
282
283
284
	{ MP_ROM_QSTR(MP_QSTR_prep_hsv), MP_ROM_PTR(&leds_prep_hsv_obj) },
	{ MP_ROM_QSTR(MP_QSTR_set_all), MP_ROM_PTR(&leds_set_all_obj) },
	{ MP_ROM_QSTR(MP_QSTR_set_all_hsv), MP_ROM_PTR(&leds_set_all_hsv_obj) },
	{ MP_ROM_QSTR(MP_QSTR_set_rocket), MP_ROM_PTR(&leds_set_rocket_obj) },
285
	{ MP_ROM_QSTR(MP_QSTR_get_rocket), MP_ROM_PTR(&leds_get_rocket_obj) },
fleur's avatar
fleur committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
	{ MP_ROM_QSTR(MP_QSTR_set_flashlight),
	  MP_ROM_PTR(&leds_set_flashlight_obj) },
	{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&leds_update_obj) },
	{ MP_ROM_QSTR(MP_QSTR_set_powersave),
	  MP_ROM_PTR(&leds_set_powersave_obj) },
	{ MP_ROM_QSTR(MP_QSTR_set_gamma), MP_ROM_PTR(&leds_set_gamma_obj) },
	{ MP_ROM_QSTR(MP_QSTR_dim_top), MP_ROM_PTR(&leds_dim_top_obj) },
	{ MP_ROM_QSTR(MP_QSTR_dim_bottom), MP_ROM_PTR(&leds_dim_bottom_obj) },
	{ 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) },
};
static MP_DEFINE_CONST_DICT(leds_module_globals, leds_module_globals_table);

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

/* Register the module to make it available in Python */
/* clang-format off */
MP_REGISTER_MODULE(MP_QSTR_sys_leds, leds_module, MODULE_LEDS_ENABLED);