Skip to content

Commit f21cf15

Browse files
committed
Add OnDiskBitmap which loads pixel data straight from disk.
Also, renamed Sprite's palette to pixel_shader so it can be anything that produces colors based on values (including color values). Added a ColorConverter that converts RGB888 (found in bitmaps) to RGB565 for the display. Fixes #1182
1 parent 9ace50a commit f21cf15

File tree

18 files changed

+583
-38
lines changed

18 files changed

+583
-38
lines changed

ports/atmel-samd/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ SRC_SHARED_MODULE = \
384384
busio/OneWire.c \
385385
displayio/__init__.c \
386386
displayio/Bitmap.c \
387+
displayio/ColorConverter.c \
387388
displayio/Group.c \
389+
displayio/OnDiskBitmap.c \
388390
displayio/Palette.c \
389391
displayio/Sprite.c \
390392
gamepad/__init__.c \
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
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 "shared-bindings/displayio/ColorConverter.h"
28+
29+
#include <stdint.h>
30+
31+
#include "lib/utils/context_manager_helpers.h"
32+
#include "py/binary.h"
33+
#include "py/objproperty.h"
34+
#include "py/runtime.h"
35+
#include "shared-bindings/microcontroller/Pin.h"
36+
#include "shared-bindings/util.h"
37+
#include "supervisor/shared/translate.h"
38+
39+
//| .. currentmodule:: displayio
40+
//|
41+
//| :class:`ColorConverter` -- Converts one color format to another
42+
//| =========================================================================================
43+
//|
44+
//| Converts one color format to another.
45+
//|
46+
//| .. warning:: This will be changed before 4.0.0. Consider it very experimental.
47+
//|
48+
//| .. class:: ColorConverter()
49+
//|
50+
//| Create a ColorConverter object to convert color formats. Only supports RGB888 to RGB565
51+
//| currently.
52+
//|
53+
// TODO(tannewt): Add support for other color formats.
54+
//|
55+
STATIC mp_obj_t displayio_colorconverter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
56+
mp_arg_check_num(n_args, n_kw, 0, 0, true);
57+
58+
displayio_colorconverter_t *self = m_new_obj(displayio_colorconverter_t);
59+
self->base.type = &displayio_colorconverter_type;
60+
common_hal_displayio_colorconverter_construct(self);
61+
62+
return MP_OBJ_FROM_PTR(self);
63+
}
64+
65+
//| .. method:: convert(color)
66+
//|
67+
STATIC mp_obj_t displayio_colorconverter_obj_convert(mp_obj_t self_in, mp_obj_t color_obj) {
68+
displayio_colorconverter_t *self = MP_OBJ_TO_PTR(self_in);
69+
70+
mp_int_t color;
71+
if (!mp_obj_get_int_maybe(color_obj, &color)) {
72+
mp_raise_ValueError(translate("color should be an int"));
73+
}
74+
uint16_t output_color;
75+
common_hal_displayio_colorconverter_convert(self, color, &output_color);
76+
return MP_OBJ_NEW_SMALL_INT(output_color);
77+
}
78+
MP_DEFINE_CONST_FUN_OBJ_2(displayio_colorconverter_convert_obj, displayio_colorconverter_obj_convert);
79+
80+
STATIC const mp_rom_map_elem_t displayio_colorconverter_locals_dict_table[] = {
81+
{ MP_ROM_QSTR(MP_QSTR_convert), MP_ROM_PTR(&displayio_colorconverter_convert_obj) },
82+
};
83+
STATIC MP_DEFINE_CONST_DICT(displayio_colorconverter_locals_dict, displayio_colorconverter_locals_dict_table);
84+
85+
const mp_obj_type_t displayio_colorconverter_type = {
86+
{ &mp_type_type },
87+
.name = MP_QSTR_ColorConverter,
88+
.make_new = displayio_colorconverter_make_new,
89+
.locals_dict = (mp_obj_dict_t*)&displayio_colorconverter_locals_dict,
90+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
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_SHARED_BINDINGS_DISPLAYIO_COLORCONVERTER_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_COLORCONVERTER_H
29+
30+
#include "shared-module/displayio/ColorConverter.h"
31+
32+
extern const mp_obj_type_t displayio_colorconverter_type;
33+
34+
void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self);
35+
bool common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *colorconverter, uint32_t input_color, uint16_t* output_color);
36+
37+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_COLORCONVERTER_H

shared-bindings/displayio/Group.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,19 @@ STATIC mp_obj_t displayio_group_obj_append(mp_obj_t self_in, mp_obj_t layer) {
8383
}
8484
MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_append_obj, displayio_group_obj_append);
8585

86+
//| .. method:: pop()
87+
//|
88+
//| Remove the last item and return it.
89+
//|
90+
STATIC mp_obj_t displayio_group_obj_pop(mp_obj_t self_in) {
91+
displayio_group_t *self = MP_OBJ_TO_PTR(self_in);
92+
return common_hal_displayio_group_pop(self);
93+
}
94+
MP_DEFINE_CONST_FUN_OBJ_1(displayio_group_pop_obj, displayio_group_obj_pop);
95+
8696
STATIC const mp_rom_map_elem_t displayio_group_locals_dict_table[] = {
8797
{ MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&displayio_group_append_obj) },
98+
{ MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&displayio_group_pop_obj) },
8899
};
89100
STATIC MP_DEFINE_CONST_DICT(displayio_group_locals_dict, displayio_group_locals_dict_table);
90101

shared-bindings/displayio/Group.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ extern const mp_obj_type_t displayio_group_type;
3434

3535
void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size);
3636
void common_hal_displayio_group_append(displayio_group_t* self, mp_obj_t layer);
37+
mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self);
3738

3839
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_GROUP_H
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
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 "shared-bindings/displayio/OnDiskBitmap.h"
28+
29+
#include <stdint.h>
30+
31+
#include "py/runtime.h"
32+
#include "supervisor/shared/translate.h"
33+
34+
//| .. currentmodule:: displayio
35+
//|
36+
//| :class:`OnDiskBitmap` -- Loads pixels straight from disk
37+
//| ==========================================================================
38+
//|
39+
//| Loads values straight from disk. This minimizes memory use but can lead to
40+
//| much slower pixel load times. These load times may result in frame tearing where only part of
41+
//| the image is visible.
42+
//|
43+
//| .. warning:: This will likely be changed before 4.0.0. Consider it very experimental.
44+
//|
45+
//| It's easiest to use on a board with a built in display such as the `Hallowing M0 Express
46+
//| <https://www.adafruit.com/product/3900>`_.
47+
//|
48+
//| .. code-block:: Python
49+
//|
50+
//| import board
51+
//| import displayio
52+
//| import time
53+
//| import pulseio
54+
//|
55+
//| backlight = pulseio.PWMOut(board.TFT_BACKLIGHT)
56+
//| splash = displayio.Group()
57+
//| board.DISPLAY.show(splash)
58+
//|
59+
//| with open("/sample.bmp", "rb") as f:
60+
//| odb = displayio.OnDiskBitmap(f)
61+
//| face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), position=(0,0))
62+
//| splash.append(face)
63+
//| # Wait for the image to load.
64+
//| board.DISPLAY.wait_for_frame()
65+
//|
66+
//| # Fade up the backlight
67+
//| for i in range(100):
68+
//| backlight.duty_cycle = i * (2 ** 15) // 100
69+
//| time.sleep(0.01)
70+
//|
71+
//| # Wait forever
72+
//| while True:
73+
//| pass
74+
//|
75+
//| .. class:: OnDiskBitmap(file)
76+
//|
77+
//| Create an OnDiskBitmap object with the given file.
78+
//|
79+
//| :param file file: The open bitmap file
80+
//|
81+
STATIC mp_obj_t displayio_ondiskbitmap_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
82+
mp_arg_check_num(n_args, n_kw, 1, 1, false);
83+
84+
if (!MP_OBJ_IS_TYPE(pos_args[0], &mp_type_fileio)) {
85+
mp_raise_TypeError(translate("file must be a file opened in byte mode"));
86+
}
87+
88+
displayio_ondiskbitmap_t *self = m_new_obj(displayio_ondiskbitmap_t);
89+
self->base.type = &displayio_ondiskbitmap_type;
90+
common_hal_displayio_ondiskbitmap_construct(self, MP_OBJ_TO_PTR(pos_args[0]));
91+
92+
return MP_OBJ_FROM_PTR(self);
93+
}
94+
95+
STATIC const mp_rom_map_elem_t displayio_ondiskbitmap_locals_dict_table[] = {
96+
};
97+
STATIC MP_DEFINE_CONST_DICT(displayio_ondiskbitmap_locals_dict, displayio_ondiskbitmap_locals_dict_table);
98+
99+
const mp_obj_type_t displayio_ondiskbitmap_type = {
100+
{ &mp_type_type },
101+
.name = MP_QSTR_OnDiskBitmap,
102+
.make_new = displayio_ondiskbitmap_make_new,
103+
.locals_dict = (mp_obj_dict_t*)&displayio_ondiskbitmap_locals_dict,
104+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
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_SHARED_BINDINGS_DISPLAYIO_ONDISKBITMAP_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_ONDISKBITMAP_H
29+
30+
#include "shared-module/displayio/OnDiskBitmap.h"
31+
#include "extmod/vfs_fat.h"
32+
33+
extern const mp_obj_type_t displayio_ondiskbitmap_type;
34+
35+
void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self, pyb_file_obj_t* file);
36+
37+
uint32_t common_hal_displayio_ondiskbitmap_get_pixel(displayio_ondiskbitmap_t *bitmap,
38+
int16_t x, int16_t y);
39+
40+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_ONDISKBITMAP_H

0 commit comments

Comments
 (0)