-
Notifications
You must be signed in to change notification settings - Fork 2
/
modugfx.c
311 lines (255 loc) · 12 KB
/
modugfx.c
1
2
3
4
5
6
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
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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* Copyright (C) 2021 Sean D'Epagnier <seandepagnier@gmail.com>
*
* This Program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*/
// micropython ugfx driver for esp32 and ili9341
#include "sdkconfig.h"
#include <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "upy_wrap.h"
typedef struct _ugfx_surface_obj_t {
mp_obj_base_t base;
void *ptr;
int contrast, hue;
int backlight;
} ugfx_surface_obj_t;
const mp_obj_type_t ugfx_surface_type;
STATIC mp_obj_t ugfx_surface_load(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum {ARG_filename, ARG_bypp};
const mp_arg_t allowed_args[] = {
{ MP_QSTR_filename, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = mp_const_none } },
{ MP_QSTR_bypp, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } }
};
ugfx_surface_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_obj_t *filename = MP_OBJ_TO_PTR(args[ARG_filename].u_obj);
char *path = (char *)mp_obj_str_get_str(filename);
mp_int_t bypp = args[ARG_bypp].u_int;
ugfx_surface_c_load(self->ptr, path, bypp);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ugfx_surface_load_obj, 0, ugfx_surface_load);
STATIC mp_obj_t ugfx_surface_blit(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum {ARG_src, ARG_xoff, ARG_yoff, ARG_flip};
const mp_arg_t allowed_args[] = {
{ MP_QSTR_src, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = mp_const_none } },
{ MP_QSTR_xoff, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_yoff, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_flip, MP_ARG_KW_ONLY | MP_ARG_BOOL, { .u_bool = false } },
};
ugfx_surface_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
ugfx_surface_obj_t *src = MP_OBJ_TO_PTR(args[ARG_src].u_obj);
mp_int_t xoff = args[ARG_xoff].u_int;
mp_int_t yoff = args[ARG_yoff].u_int;
uint8_t flip = args[ARG_flip].u_bool;
ugfx_surface_c_blit(self->ptr, src->ptr, xoff, yoff, flip);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ugfx_surface_blit_obj, 0, ugfx_surface_blit);
STATIC mp_obj_t ugfx_surface_line(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_c};
const mp_arg_t allowed_args[] = {
{ MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_c, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
};
ugfx_surface_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_int_t x1 = args[ARG_x1].u_int;
mp_int_t y1 = args[ARG_y1].u_int;
mp_int_t x2 = args[ARG_x2].u_int;
mp_int_t y2 = args[ARG_y2].u_int;
mp_int_t c = args[ARG_c].u_int;
ugfx_surface_c_line(self->ptr, x1, y1, x2, y2, c);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ugfx_surface_line_obj, 0, ugfx_surface_line);
STATIC mp_obj_t ugfx_surface_box(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_c};
const mp_arg_t allowed_args[] = {
{ MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_c, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
};
ugfx_surface_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_int_t x1 = args[ARG_x1].u_int;
mp_int_t y1 = args[ARG_y1].u_int;
mp_int_t x2 = args[ARG_x2].u_int;
mp_int_t y2 = args[ARG_y2].u_int;
mp_int_t c = args[ARG_c].u_int;
ugfx_surface_c_box(self->ptr, x1, y1, x2, y2, c);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ugfx_surface_box_obj, 0, ugfx_surface_box);
STATIC mp_obj_t ugfx_surface_invert(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum {ARG_x1, ARG_y1, ARG_x2, ARG_y2};
const mp_arg_t allowed_args[] = {
{ MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
};
ugfx_surface_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_int_t x1 = args[ARG_x1].u_int;
mp_int_t y1 = args[ARG_y1].u_int;
mp_int_t x2 = args[ARG_x2].u_int;
mp_int_t y2 = args[ARG_y2].u_int;
ugfx_surface_c_invert(self->ptr, x1, y1, x2, y2);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ugfx_surface_invert_obj, 0, ugfx_surface_invert);
STATIC mp_obj_t ugfx_surface_fill(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum {ARG_c};
const mp_arg_t allowed_args[] = {
{ MP_QSTR_c, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
};
ugfx_surface_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_int_t c = args[ARG_c].u_int;
ugfx_surface_c_fill(self->ptr, c);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ugfx_surface_fill_obj, 0, ugfx_surface_fill);
void ili_refresh(int contrast, int hue, int backlight, int width, int height, char *data);
STATIC mp_obj_t ugfx_display_surface_refresh(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
ugfx_surface_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
int width, height, bypp;
char *data;
ugfx_surface_c_info(self->ptr, &width, &height, &bypp, &data);
ili_refresh(self->contrast, self->hue, self->backlight, width, height, data);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ugfx_display_surface_refresh_obj, 0, ugfx_display_surface_refresh);
STATIC mp_obj_t ugfx_surface_free(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
ugfx_surface_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
ugfx_surface_c_free(self->ptr);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ugfx_surface_free_obj, 0, ugfx_surface_free);
STATIC const mp_rom_map_elem_t ugfx_surface_locals_dict_table[] = {
// instance methods
{ MP_ROM_QSTR(MP_QSTR_load), MP_ROM_PTR(&ugfx_surface_load_obj) },
{ MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&ugfx_surface_blit_obj) },
{ MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&ugfx_surface_line_obj) },
{ MP_ROM_QSTR(MP_QSTR_box), MP_ROM_PTR(&ugfx_surface_box_obj) },
{ MP_ROM_QSTR(MP_QSTR_invert), MP_ROM_PTR(&ugfx_surface_invert_obj) },
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&ugfx_surface_fill_obj) },
{ MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&ugfx_display_surface_refresh_obj) }
};
STATIC MP_DEFINE_CONST_DICT(ugfx_surface_locals_dict, ugfx_surface_locals_dict_table);
//-----------------------------------------------------------------------------------------------
STATIC void ugfx_surface_printinfo(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
{
ugfx_surface_obj_t *self = self_in;
int width, height, bypp;
char *data;
ugfx_surface_c_info(self->ptr, &width, &height, &bypp, &data);
mp_printf(print, "surface %dx%dx%d %p", width, height, bypp, data);
}
// constructor(id, ...)
//-----------------------------------------------------------------------------------------------------------------
STATIC mp_obj_t ugfx_surface_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
// this checks the number of arguments (min 1, max 1);
// on error -> raise python exception
ugfx_surface_obj_t *self = m_new_obj(ugfx_surface_obj_t);
self->base.type = &ugfx_surface_type;
if(n_args == 2) {
mp_arg_check_num(n_args, n_kw, 1, 2, true);
char *path = (char *)mp_obj_str_get_str(args[0]);
self->ptr = ugfx_surface_from_file_c(path, mp_obj_get_int(args[1]));
return MP_OBJ_FROM_PTR(self);
}
mp_arg_check_num(n_args, n_kw, 1, 4, true);
int w = mp_obj_get_int(args[0]);
int h = mp_obj_get_int(args[1]);
int bypp = mp_obj_get_int(args[2]);
self->contrast = 60;
self->hue = 170;
self->backlight = 1;
self->ptr = ugfx_surface_from_data_c(w, h, bypp, 0);
return MP_OBJ_FROM_PTR(self);
}
STATIC void ugfx_surface_attr(mp_obj_t self_in, qstr attribute, mp_obj_t *dest) {
ugfx_surface_obj_t *self = MP_OBJ_TO_PTR(self_in);
int width, height, bypp;
char *data;
ugfx_surface_c_info(self->ptr, &width, &height, &bypp, &data);
if(attribute == MP_QSTR_width)
dest[0] = mp_obj_new_int(width);
else
if(attribute == MP_QSTR_height)
dest[0] = mp_obj_new_int(height);
else
if(attribute == MP_QSTR_bypp)
dest[0] = mp_obj_new_int(bypp);
else
if(attribute == MP_QSTR_contrast)
if(dest[0] == MP_OBJ_SENTINEL) {
self->contrast = mp_obj_get_int(dest[1]);
dest[0] = MP_OBJ_NULL;
} else
dest[0] = mp_obj_new_int(self->contrast);
else
if(attribute == MP_QSTR_hue)
if(dest[0] == MP_OBJ_SENTINEL) {
self->hue = mp_obj_get_int(dest[1]);
dest[0] = MP_OBJ_NULL;
} else
dest[0] = mp_obj_new_int(self->hue);
else
if(attribute == MP_QSTR_backlight)
if(dest[0] == MP_OBJ_SENTINEL) {
self->backlight = mp_obj_get_int(dest[1]);
dest[0] = MP_OBJ_NULL;
} else
dest[0] = mp_obj_new_int(self->backlight);
else {
// fallback to lookup
const mp_obj_type_t *type = mp_obj_get_type(self_in);
mp_map_t *locals_map = &type->locals_dict->map;
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attribute), MP_MAP_LOOKUP);
if (elem != NULL)
mp_convert_member_lookup(self_in, type, elem->value, dest);
}
}
const mp_obj_type_t ugfx_surface_type = {
{ &mp_type_type },
.name = MP_QSTR_surface,
.print = ugfx_surface_printinfo,
.make_new = ugfx_surface_make_new,
.attr = ugfx_surface_attr,
.locals_dict = (mp_obj_t)&ugfx_surface_locals_dict,
};
typedef struct _ugfx_obj_t {
mp_obj_base_t base;
} ugfx_obj_t;
//===============================================================
STATIC const mp_rom_map_elem_t ugfx_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ugfx) },
{ MP_ROM_QSTR(MP_QSTR_surface), MP_ROM_PTR(&ugfx_surface_type) },
};
//===============================================================================
STATIC MP_DEFINE_CONST_DICT(ugfx_module_globals, ugfx_module_globals_table);
const mp_obj_module_t mp_module_ugfx = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&ugfx_module_globals,
};
// Register the module to make it available in Python
MP_REGISTER_MODULE(MP_QSTR_ugfx, mp_module_ugfx, MODULE_UGFX_ENABLED);