forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FramebufferDisplay.c
380 lines (331 loc) · 18.3 KB
/
FramebufferDisplay.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/framebufferio/FramebufferDisplay.h"
#include <stdint.h>
#include "shared/runtime/context_manager_helpers.h"
#include "py/binary.h"
#include "py/objproperty.h"
#include "py/objtype.h"
#include "py/runtime.h"
#include "shared-bindings/displayio/Group.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/util.h"
#include "shared-module/displayio/__init__.h"
//| class FramebufferDisplay:
//| """Manage updating a display with framebuffer in RAM
//|
//| This initializes a display and connects it into CircuitPython. Unlike other
//| objects in CircuitPython, Display objects live until `displayio.release_displays()`
//| is called. This is done so that CircuitPython can use the display itself."""
//|
//| def __init__(
//| self,
//| framebuffer: circuitpython_typing.FrameBuffer,
//| *,
//| rotation: int = 0,
//| auto_refresh: bool = True
//| ) -> None:
//| """Create a Display object with the given framebuffer (a buffer, array, ulab.array, etc)
//|
//| :param ~circuitpython_typing.FrameBuffer framebuffer: The framebuffer that the display is connected to
//| :param bool auto_refresh: Automatically refresh the screen
//| :param int rotation: The rotation of the display in degrees clockwise. Must be in 90 degree increments (0, 90, 180, 270)
//| """
//| ...
STATIC mp_obj_t framebufferio_framebufferdisplay_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_framebuffer, ARG_rotation, ARG_auto_refresh, NUM_ARGS };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_framebuffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_rotation, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_auto_refresh, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} },
};
MP_STATIC_ASSERT(MP_ARRAY_SIZE(allowed_args) == NUM_ARGS);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_obj_t framebuffer = args[ARG_framebuffer].u_obj;
mp_int_t rotation = args[ARG_rotation].u_int;
if (rotation % 90 != 0) {
mp_raise_ValueError(translate("Display rotation must be in 90 degree increments"));
}
primary_display_t *disp = allocate_display_or_raise();
framebufferio_framebufferdisplay_obj_t *self = &disp->framebuffer_display;
self->base.type = &framebufferio_framebufferdisplay_type;
common_hal_framebufferio_framebufferdisplay_construct(
self,
framebuffer,
rotation,
args[ARG_auto_refresh].u_bool
);
return self;
}
// Helper to ensure we have the native super class instead of a subclass.
static framebufferio_framebufferdisplay_obj_t *native_display(mp_obj_t display_obj) {
mp_obj_t native_display = mp_obj_cast_to_native_base(display_obj, &framebufferio_framebufferdisplay_type);
mp_obj_assert_native_inited(native_display);
return MP_OBJ_TO_PTR(native_display);
}
// Undocumented show() implementation with a friendly error message.
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_show(mp_obj_t self_in, mp_obj_t group_in) {
mp_raise_AttributeError(translate(".show(x) removed. Use .root_group = x"));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(framebufferio_framebufferdisplay_show_obj, framebufferio_framebufferdisplay_obj_show);
//| def refresh(
//| self,
//| *,
//| target_frames_per_second: Optional[int] = None,
//| minimum_frames_per_second: int = 0
//| ) -> bool:
//| """When auto_refresh is off, and :py:attr:`target_frames_per_second` is not `None` this waits
//| for the target frame rate and then refreshes the display,
//| returning `True`. If the call has taken too long since the last refresh call for the given
//| target frame rate, then the refresh returns `False` immediately without updating the screen to
//| hopefully help getting caught up.
//|
//| If the time since the last successful refresh is below the minimum frame rate, then an
//| exception will be raised. The default :py:attr:`minimum_frames_per_second` of 0 disables this behavior.
//|
//| When auto_refresh is off, and :py:attr:`target_frames_per_second` is `None` this
//| will update the display immediately.
//|
//| When auto_refresh is on, updates the display immediately. (The display will also update
//| without calls to this.)
//|
//| :param Optional[int] target_frames_per_second: The target frame rate that :py:func:`refresh` should try to
//| achieve. Set to `None` for immediate refresh.
//| :param int minimum_frames_per_second: The minimum number of times the screen should be updated per second.
//| """
//| ...
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_refresh(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_target_frames_per_second, ARG_minimum_frames_per_second };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_target_frames_per_second, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
{ MP_QSTR_minimum_frames_per_second, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 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);
framebufferio_framebufferdisplay_obj_t *self = native_display(pos_args[0]);
uint32_t maximum_ms_per_real_frame = NO_FPS_LIMIT;
mp_int_t minimum_frames_per_second = args[ARG_minimum_frames_per_second].u_int;
if (minimum_frames_per_second > 0) {
maximum_ms_per_real_frame = 1000 / minimum_frames_per_second;
}
uint32_t target_ms_per_frame;
if (args[ARG_target_frames_per_second].u_obj == mp_const_none) {
target_ms_per_frame = NO_FPS_LIMIT;
} else {
target_ms_per_frame = 1000 / mp_obj_get_int(args[ARG_target_frames_per_second].u_obj);
}
return mp_obj_new_bool(common_hal_framebufferio_framebufferdisplay_refresh(self, target_ms_per_frame, maximum_ms_per_real_frame));
}
MP_DEFINE_CONST_FUN_OBJ_KW(framebufferio_framebufferdisplay_refresh_obj, 1, framebufferio_framebufferdisplay_obj_refresh);
//| auto_refresh: bool
//| """True when the display is refreshed automatically."""
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_get_auto_refresh(mp_obj_t self_in) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
return mp_obj_new_bool(common_hal_framebufferio_framebufferdisplay_get_auto_refresh(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(framebufferio_framebufferdisplay_get_auto_refresh_obj, framebufferio_framebufferdisplay_obj_get_auto_refresh);
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_set_auto_refresh(mp_obj_t self_in, mp_obj_t auto_refresh) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
common_hal_framebufferio_framebufferdisplay_set_auto_refresh(self, mp_obj_is_true(auto_refresh));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(framebufferio_framebufferdisplay_set_auto_refresh_obj, framebufferio_framebufferdisplay_obj_set_auto_refresh);
MP_PROPERTY_GETSET(framebufferio_framebufferdisplay_auto_refresh_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_get_auto_refresh_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_set_auto_refresh_obj);
//| brightness: float
//| """The brightness of the display as a float. 0.0 is off and 1.0 is full brightness."""
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_get_brightness(mp_obj_t self_in) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
mp_float_t brightness = common_hal_framebufferio_framebufferdisplay_get_brightness(self);
if (brightness < 0) {
mp_raise_RuntimeError(translate("Brightness not adjustable"));
}
return mp_obj_new_float(brightness);
}
MP_DEFINE_CONST_FUN_OBJ_1(framebufferio_framebufferdisplay_get_brightness_obj, framebufferio_framebufferdisplay_obj_get_brightness);
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_set_brightness(mp_obj_t self_in, mp_obj_t brightness_obj) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
mp_float_t brightness = mp_obj_get_float(brightness_obj);
if (brightness < 0.0f || brightness > 1.0f) {
mp_raise_ValueError_varg(translate("%q must be %d-%d"), MP_QSTR_brightness, 0, 1);
}
bool ok = common_hal_framebufferio_framebufferdisplay_set_brightness(self, brightness);
if (!ok) {
mp_raise_RuntimeError(translate("Brightness not adjustable"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(framebufferio_framebufferdisplay_set_brightness_obj, framebufferio_framebufferdisplay_obj_set_brightness);
MP_PROPERTY_GETSET(framebufferio_framebufferdisplay_brightness_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_get_brightness_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_set_brightness_obj);
//| width: int
//| """Gets the width of the framebuffer"""
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_get_width(mp_obj_t self_in) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_framebufferio_framebufferdisplay_get_width(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(framebufferio_framebufferdisplay_get_width_obj, framebufferio_framebufferdisplay_obj_get_width);
MP_PROPERTY_GETTER(framebufferio_framebufferdisplay_width_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_get_width_obj);
//| height: int
//| """Gets the height of the framebuffer"""
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_get_height(mp_obj_t self_in) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_framebufferio_framebufferdisplay_get_height(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(framebufferio_framebufferdisplay_get_height_obj, framebufferio_framebufferdisplay_obj_get_height);
MP_PROPERTY_GETTER(framebufferio_framebufferdisplay_height_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_get_height_obj);
//| rotation: int
//| """The rotation of the display as an int in degrees."""
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_get_rotation(mp_obj_t self_in) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_framebufferio_framebufferdisplay_get_rotation(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(framebufferio_framebufferdisplay_get_rotation_obj, framebufferio_framebufferdisplay_obj_get_rotation);
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_set_rotation(mp_obj_t self_in, mp_obj_t value) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
common_hal_framebufferio_framebufferdisplay_set_rotation(self, mp_obj_get_int(value));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(framebufferio_framebufferdisplay_set_rotation_obj, framebufferio_framebufferdisplay_obj_set_rotation);
MP_PROPERTY_GETSET(framebufferio_framebufferdisplay_rotation_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_get_rotation_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_set_rotation_obj);
//| framebuffer: circuitpython_typing.FrameBuffer
//| """The framebuffer being used by the display"""
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_get_framebuffer(mp_obj_t self_in) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
return common_hal_framebufferio_framebufferdisplay_get_framebuffer(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(framebufferio_framebufferdisplay_get_framebuffer_obj, framebufferio_framebufferdisplay_obj_get_framebuffer);
MP_PROPERTY_GETTER(framebufferio_framebufferframebuffer_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_get_framebuffer_obj);
//| def fill_row(self, y: int, buffer: WriteableBuffer) -> WriteableBuffer:
//| """Extract the pixels from a single row
//|
//| :param int y: The top edge of the area
//| :param ~circuitpython_typing.WriteableBuffer buffer: The buffer in which to place the pixel data
//| """
//| ...
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_fill_row(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_y, ARG_buffer };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = -1} },
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED, {} },
};
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);
framebufferio_framebufferdisplay_obj_t *self = native_display(pos_args[0]);
mp_int_t y = args[ARG_y].u_int;
mp_obj_t *result = args[ARG_buffer].u_obj;
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(result, &bufinfo, MP_BUFFER_WRITE);
if (bufinfo.typecode != BYTEARRAY_TYPECODE) {
mp_raise_ValueError(translate("Buffer is not a bytearray."));
}
if (self->core.colorspace.depth != 16) {
mp_raise_ValueError(translate("Display must have a 16 bit colorspace."));
}
displayio_area_t area = {
.x1 = 0,
.y1 = y,
.x2 = self->core.width,
.y2 = y + 1
};
uint8_t pixels_per_word = (sizeof(uint32_t) * 8) / self->core.colorspace.depth;
uint16_t buffer_size = self->core.width / pixels_per_word;
uint16_t pixels_per_buffer = displayio_area_size(&area);
if (pixels_per_buffer % pixels_per_word) {
buffer_size += 1;
}
uint32_t *result_buffer = bufinfo.buf;
size_t result_buffer_size = bufinfo.len;
if (result_buffer_size >= (buffer_size * 4)) {
volatile uint32_t mask_length = (pixels_per_buffer / 32) + 1;
uint32_t mask[mask_length];
for (uint16_t k = 0; k < mask_length; k++) {
mask[k] = 0x00000000;
}
displayio_display_core_fill_area(&self->core, &area, mask, result_buffer);
return result;
} else {
mp_raise_ValueError(translate("Buffer too small"));
}
}
MP_DEFINE_CONST_FUN_OBJ_KW(framebufferio_framebufferdisplay_fill_row_obj, 1, framebufferio_framebufferdisplay_obj_fill_row);
//| root_group: displayio.Group
//| """The root group on the display.
//| If the root group is set to `displayio.CIRCUITPYTHON_TERMINAL`, the default CircuitPython terminal will be shown.
//| If the root group is set to ``None``, no output will be shown.
//| """
//|
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_get_root_group(mp_obj_t self_in) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
return common_hal_framebufferio_framebufferdisplay_get_root_group(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(framebufferio_framebufferdisplay_get_root_group_obj, framebufferio_framebufferdisplay_obj_get_root_group);
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_set_root_group(mp_obj_t self_in, mp_obj_t group_in) {
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
displayio_group_t *group = NULL;
if (group_in != mp_const_none) {
group = MP_OBJ_TO_PTR(native_group(group_in));
}
bool ok = common_hal_framebufferio_framebufferdisplay_set_root_group(self, group);
if (!ok) {
mp_raise_ValueError(translate("Group already used"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(framebufferio_framebufferdisplay_set_root_group_obj, framebufferio_framebufferdisplay_obj_set_root_group);
MP_PROPERTY_GETSET(framebufferio_framebufferdisplay_root_group_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_get_root_group_obj,
(mp_obj_t)&framebufferio_framebufferdisplay_set_root_group_obj);
STATIC const mp_rom_map_elem_t framebufferio_framebufferdisplay_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&framebufferio_framebufferdisplay_show_obj) },
{ MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&framebufferio_framebufferdisplay_refresh_obj) },
{ MP_ROM_QSTR(MP_QSTR_fill_row), MP_ROM_PTR(&framebufferio_framebufferdisplay_fill_row_obj) },
{ MP_ROM_QSTR(MP_QSTR_auto_refresh), MP_ROM_PTR(&framebufferio_framebufferdisplay_auto_refresh_obj) },
{ MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&framebufferio_framebufferdisplay_brightness_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&framebufferio_framebufferdisplay_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&framebufferio_framebufferdisplay_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_rotation), MP_ROM_PTR(&framebufferio_framebufferdisplay_rotation_obj) },
{ MP_ROM_QSTR(MP_QSTR_framebuffer), MP_ROM_PTR(&framebufferio_framebufferframebuffer_obj) },
{ MP_ROM_QSTR(MP_QSTR_root_group), MP_ROM_PTR(&framebufferio_framebufferdisplay_root_group_obj) },
};
STATIC MP_DEFINE_CONST_DICT(framebufferio_framebufferdisplay_locals_dict, framebufferio_framebufferdisplay_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
framebufferio_framebufferdisplay_type,
MP_QSTR_FramebufferDisplay,
MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
make_new, framebufferio_framebufferdisplay_make_new,
locals_dict, &framebufferio_framebufferdisplay_locals_dict
);