Skip to content

Commit 216fced

Browse files
deshiputannewt
authored andcommitted
Add a _stage module (adafruit#398)
This is a C module with some low-level functions required for the CircuitPython "stage" library. It provides support for fast rendering of tile grids and sprites on SPI-based RGB displays.
1 parent d9a19c4 commit 216fced

13 files changed

Lines changed: 823 additions & 0 deletions

File tree

atmel-samd/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ SRC_SHARED_MODULE = \
333333
os/__init__.c \
334334
random/__init__.c \
335335
storage/__init__.c \
336+
_stage/__init__.c \
337+
_stage/Layer.c \
338+
_stage/Text.c \
336339
uheap/__init__.c \
337340
ustack/__init__.c
338341

shared-bindings/_stage/Layer.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Radomir Dopieralski
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <py/runtime.h>
28+
29+
#include "__init__.h"
30+
#include "Layer.h"
31+
32+
//| .. currentmodule:: _stage
33+
//|
34+
//| :class:`Layer` -- Keep information about a single layer of graphics
35+
//| ===================================================================
36+
//|
37+
//| .. class:: Layer(width, height, graphic, palette, [grid])
38+
//|
39+
//| Keep internal information about a layer of graphics (either a
40+
//| ``Grid`` or a ``Sprite``) in a format suitable for fast rendering
41+
//| with the ``render()`` function.
42+
//|
43+
//| :param int width: The width of the grid in tiles, or 1 for sprites.
44+
//| :param int height: The height of the grid in tiles, or 1 for sprites.
45+
//| :param bytearray graphic: The graphic data of the tiles.
46+
//| :param bytearray palette: The color palette to be used.
47+
//| :param bytearray grid: The contents of the grid map.
48+
//|
49+
//| This class is intended for internal use in the ``stage`` library and
50+
//| it shouldn't be used on its own.
51+
//|
52+
STATIC mp_obj_t layer_make_new(const mp_obj_type_t *type, size_t n_args,
53+
size_t n_kw, const mp_obj_t *args) {
54+
mp_arg_check_num(n_args, n_kw, 4, 5, false);
55+
56+
layer_obj_t *self = m_new_obj(layer_obj_t);
57+
self->base.type = type;
58+
59+
self->width = mp_obj_get_int(args[0]);
60+
self->height = mp_obj_get_int(args[1]);
61+
self->x = 0;
62+
self->y = 0;
63+
self->frame = 0;
64+
self->rotation = false;
65+
66+
mp_buffer_info_t bufinfo;
67+
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
68+
self->graphic = bufinfo.buf;
69+
if (bufinfo.len != 2048) {
70+
mp_raise_ValueError("graphic must be 2048 bytes long");
71+
}
72+
73+
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
74+
self->palette = bufinfo.buf;
75+
if (bufinfo.len != 32) {
76+
mp_raise_ValueError("palette must be 32 bytes long");
77+
}
78+
79+
if (n_args > 4) {
80+
mp_get_buffer_raise(args[4], &bufinfo, MP_BUFFER_READ);
81+
self->map = bufinfo.buf;
82+
if (bufinfo.len < (self->width * self->height) / 2) {
83+
mp_raise_ValueError("map buffer too small");
84+
}
85+
} else {
86+
self-> map = NULL;
87+
}
88+
89+
return MP_OBJ_FROM_PTR(self);
90+
}
91+
92+
//| .. method:: move(x, y)
93+
//|
94+
//| Set the offset of the layer to the specified values.
95+
//|
96+
STATIC mp_obj_t layer_move(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) {
97+
layer_obj_t *self = MP_OBJ_TO_PTR(self_in);
98+
self->x = mp_obj_get_int(x_in);
99+
self->y = mp_obj_get_int(y_in);
100+
return mp_const_none;
101+
}
102+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(layer_move_obj, layer_move);
103+
104+
//| .. method:: frame(frame, rotation)
105+
//|
106+
//| Set the animation frame of the sprite, and optionally rotation its
107+
//| graphic.
108+
//|
109+
STATIC mp_obj_t layer_frame(mp_obj_t self_in, mp_obj_t frame_in,
110+
mp_obj_t rotation_in) {
111+
layer_obj_t *self = MP_OBJ_TO_PTR(self_in);
112+
self->frame = mp_obj_get_int(frame_in);
113+
self->rotation = mp_obj_get_int(rotation_in);
114+
return mp_const_none;
115+
}
116+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(layer_frame_obj, layer_frame);
117+
118+
119+
STATIC const mp_rom_map_elem_t layer_locals_dict_table[] = {
120+
{ MP_ROM_QSTR(MP_QSTR_move), MP_ROM_PTR(&layer_move_obj) },
121+
{ MP_ROM_QSTR(MP_QSTR_frame), MP_ROM_PTR(&layer_frame_obj) },
122+
};
123+
STATIC MP_DEFINE_CONST_DICT(layer_locals_dict, layer_locals_dict_table);
124+
125+
const mp_obj_type_t mp_type_layer = {
126+
{ &mp_type_type },
127+
.name = MP_QSTR_Layer,
128+
.make_new = layer_make_new,
129+
.locals_dict = (mp_obj_dict_t*)&layer_locals_dict,
130+
};

shared-bindings/_stage/Layer.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Radomir Dopieralski
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED__STAGE_LAYER_H
28+
#define MICROPY_INCLUDED__STAGE_LAYER_H
29+
30+
#include "shared-module/_stage/Layer.h"
31+
32+
extern const mp_obj_type_t mp_type_layer;
33+
34+
#endif // MICROPY_INCLUDED__STAGE_LAYER

shared-bindings/_stage/Text.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Radomir Dopieralski
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <py/runtime.h>
28+
29+
#include "__init__.h"
30+
#include "Text.h"
31+
32+
//| .. currentmodule:: _stage
33+
//|
34+
//| :class:`Text` -- Keep information about a single text of text
35+
//| ==============================================================
36+
//|
37+
//| .. class:: Text(width, height, font, palette, chars)
38+
//|
39+
//| Keep internal information about a text of text
40+
//| in a format suitable for fast rendering
41+
//| with the ``render()`` function.
42+
//|
43+
//| :param int width: The width of the grid in tiles, or 1 for sprites.
44+
//| :param int height: The height of the grid in tiles, or 1 for sprites.
45+
//| :param bytearray font: The font data of the characters.
46+
//| :param bytearray palette: The color palette to be used.
47+
//| :param bytearray chars: The contents of the character grid.
48+
//|
49+
//| This class is intended for internal use in the ``stage`` library and
50+
//| it shouldn't be used on its own.
51+
//|
52+
STATIC mp_obj_t text_make_new(const mp_obj_type_t *type, size_t n_args,
53+
size_t n_kw, const mp_obj_t *args) {
54+
mp_arg_check_num(n_args, n_kw, 5, 5, false);
55+
56+
text_obj_t *self = m_new_obj(text_obj_t);
57+
self->base.type = type;
58+
59+
self->width = mp_obj_get_int(args[0]);
60+
self->height = mp_obj_get_int(args[1]);
61+
self->x = 0;
62+
self->y = 0;
63+
64+
mp_buffer_info_t bufinfo;
65+
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
66+
self->font = bufinfo.buf;
67+
if (bufinfo.len != 2048) {
68+
mp_raise_ValueError("font must be 2048 bytes long");
69+
}
70+
71+
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
72+
self->palette = bufinfo.buf;
73+
if (bufinfo.len != 32) {
74+
mp_raise_ValueError("palette must be 32 bytes long");
75+
}
76+
77+
mp_get_buffer_raise(args[4], &bufinfo, MP_BUFFER_READ);
78+
self->chars = bufinfo.buf;
79+
if (bufinfo.len < self->width * self->height) {
80+
mp_raise_ValueError("chars buffer too small");
81+
}
82+
83+
return MP_OBJ_FROM_PTR(self);
84+
}
85+
86+
//| .. method:: move(x, y)
87+
//|
88+
//| Set the offset of the text to the specified values.
89+
//|
90+
STATIC mp_obj_t text_move(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) {
91+
text_obj_t *self = MP_OBJ_TO_PTR(self_in);
92+
self->x = mp_obj_get_int(x_in);
93+
self->y = mp_obj_get_int(y_in);
94+
return mp_const_none;
95+
}
96+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(text_move_obj, text_move);
97+
98+
99+
STATIC const mp_rom_map_elem_t text_locals_dict_table[] = {
100+
{ MP_ROM_QSTR(MP_QSTR_move), MP_ROM_PTR(&text_move_obj) },
101+
};
102+
STATIC MP_DEFINE_CONST_DICT(text_locals_dict, text_locals_dict_table);
103+
104+
const mp_obj_type_t mp_type_text = {
105+
{ &mp_type_type },
106+
.name = MP_QSTR_Text,
107+
.make_new = text_make_new,
108+
.locals_dict = (mp_obj_dict_t*)&text_locals_dict,
109+
};

shared-bindings/_stage/Text.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Radomir Dopieralski
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED__STAGE_TEXT_H
28+
#define MICROPY_INCLUDED__STAGE_TEXT_H
29+
30+
#include "shared-module/_stage/Text.h"
31+
32+
extern const mp_obj_type_t mp_type_text;
33+
34+
#endif // MICROPY_INCLUDED__STAGE_TEXT

0 commit comments

Comments
 (0)