Skip to content

Commit c1400ba

Browse files
committed
sharpmemory: Implement support for Sharp Memory Displays in framebufferio
1 parent 9c4f644 commit c1400ba

File tree

17 files changed

+608
-29
lines changed

17 files changed

+608
-29
lines changed

py/circuitpy_defns.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ endif
222222
ifeq ($(CIRCUITPY_SDIOIO),1)
223223
SRC_PATTERNS += sdioio/%
224224
endif
225+
ifeq ($(CIRCUITPY_SHARPDISPLAY),1)
226+
SRC_PATTERNS += sharpdisplay/%
227+
endif
225228
ifeq ($(CIRCUITPY_STAGE),1)
226229
SRC_PATTERNS += _stage/%
227230
endif
@@ -409,6 +412,8 @@ SRC_SHARED_MODULE_ALL = \
409412
random/__init__.c \
410413
rgbmatrix/RGBMatrix.c \
411414
rgbmatrix/__init__.c \
415+
sharpdisplay/SharpMemoryFramebuffer.c \
416+
sharpdisplay/__init__.c \
412417
socket/__init__.c \
413418
storage/__init__.c \
414419
struct/__init__.c \

py/circuitpy_mpconfig.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,6 @@ extern const struct _mp_obj_module_t pixelbuf_module;
499499
#define PIXELBUF_MODULE
500500
#endif
501501

502-
#if CIRCUITPY_RGBMATRIX
503-
extern const struct _mp_obj_module_t rgbmatrix_module;
504-
#define RGBMATRIX_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_rgbmatrix),(mp_obj_t)&rgbmatrix_module },
505-
#else
506-
#define RGBMATRIX_MODULE
507-
#endif
508-
509502
#if CIRCUITPY_PULSEIO
510503
extern const struct _mp_obj_module_t pulseio_module;
511504
#define PULSEIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module },
@@ -520,6 +513,13 @@ extern const struct _mp_obj_module_t ps2io_module;
520513
#define PS2IO_MODULE
521514
#endif
522515

516+
#if CIRCUITPY_RGBMATRIX
517+
extern const struct _mp_obj_module_t rgbmatrix_module;
518+
#define RGBMATRIX_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_rgbmatrix),(mp_obj_t)&rgbmatrix_module },
519+
#else
520+
#define RGBMATRIX_MODULE
521+
#endif
522+
523523
#if CIRCUITPY_RANDOM
524524
extern const struct _mp_obj_module_t random_module;
525525
#define RANDOM_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module },
@@ -562,6 +562,13 @@ extern const struct _mp_obj_module_t sdioio_module;
562562
#define SDIOIO_MODULE
563563
#endif
564564

565+
#if CIRCUITPY_SHARPDISPLAY
566+
extern const struct _mp_obj_module_t sharpdisplay_module;
567+
#define SHARPDISPLAY_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_sharpdisplay),(mp_obj_t)&sharpdisplay_module },
568+
#else
569+
#define SHARPDISPLAY_MODULE
570+
#endif
571+
565572
#if CIRCUITPY_STAGE
566573
extern const struct _mp_obj_module_t stage_module;
567574
#define STAGE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__stage), (mp_obj_t)&stage_module },
@@ -737,6 +744,7 @@ extern const struct _mp_obj_module_t watchdog_module;
737744
SAMD_MODULE \
738745
SDCARDIO_MODULE \
739746
SDIOIO_MODULE \
747+
SHARPDISPLAY_MODULE \
740748
STAGE_MODULE \
741749
STORAGE_MODULE \
742750
STRUCT_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ CFLAGS += -DCIRCUITPY_COUNTIO=$(CIRCUITPY_COUNTIO)
9595
CIRCUITPY_DISPLAYIO ?= $(CIRCUITPY_FULL_BUILD)
9696
CFLAGS += -DCIRCUITPY_DISPLAYIO=$(CIRCUITPY_DISPLAYIO)
9797

98-
CIRCUITPY_FRAMEBUFFERIO ?= 0
98+
CIRCUITPY_FRAMEBUFFERIO ?= $(CIRCUITPY_FULL_BUILD)
9999
CFLAGS += -DCIRCUITPY_FRAMEBUFFERIO=$(CIRCUITPY_FRAMEBUFFERIO)
100100

101101
CIRCUITPY_VECTORIO ?= $(CIRCUITPY_DISPLAYIO)
@@ -144,7 +144,6 @@ CFLAGS += -DCIRCUITPY_OS=$(CIRCUITPY_OS)
144144
CIRCUITPY_PIXELBUF ?= $(CIRCUITPY_FULL_BUILD)
145145
CFLAGS += -DCIRCUITPY_PIXELBUF=$(CIRCUITPY_PIXELBUF)
146146

147-
# Only for SAMD boards for the moment
148147
CIRCUITPY_RGBMATRIX ?= 0
149148
CFLAGS += -DCIRCUITPY_RGBMATRIX=$(CIRCUITPY_RGBMATRIX)
150149

@@ -176,6 +175,9 @@ CFLAGS += -DCIRCUITPY_SDCARDIO=$(CIRCUITPY_SDCARDIO)
176175
CIRCUITPY_SDIOIO ?= 0
177176
CFLAGS += -DCIRCUITPY_SDIOIO=$(CIRCUITPY_SDIOIO)
178177

178+
CIRCUITPY_SHARPDISPLAY ?= $(CIRCUITPY_FRAMEBUFFERIO)
179+
CFLAGS += -DCIRCUITPY_SHARPDISPLAY=$(CIRCUITPY_SHARPDISPLAY)
180+
179181
# Currently always off.
180182
CIRCUITPY_STAGE ?= 0
181183
CFLAGS += -DCIRCUITPY_STAGE=$(CIRCUITPY_STAGE)

py/qstr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ STATIC const byte *find_qstr(qstr q) {
137137
while (q < pool->total_prev_len) {
138138
pool = pool->prev;
139139
}
140+
assert(q - pool->total_prev_len < pool->len);
140141
return pool->qstrs[q - pool->total_prev_len];
141142
}
142143

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Jeff Epler 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 "py/objarray.h"
28+
#include "py/runtime.h"
29+
30+
#include "shared-bindings/busio/SPI.h"
31+
#include "shared-bindings/microcontroller/Pin.h"
32+
#include "shared-bindings/sharpdisplay/SharpMemoryFramebuffer.h"
33+
#include "shared-module/displayio/__init__.h"
34+
#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
35+
36+
STATIC mp_obj_t sharpdisplay_framebuffer_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
37+
enum { ARG_spi_bus, ARG_chip_select, ARG_width, ARG_height, ARG_baudrate, NUM_ARGS };
38+
static const mp_arg_t allowed_args[] = {
39+
{ MP_QSTR_spi_bus, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
40+
{ MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
41+
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} },
42+
{ MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} },
43+
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 2000000} },
44+
};
45+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
46+
MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS );
47+
48+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
49+
50+
mcu_pin_obj_t *chip_select = validate_obj_is_free_pin(args[ARG_chip_select].u_obj);
51+
busio_spi_obj_t *spi = validate_obj_is_spi_bus(args[ARG_spi_bus].u_obj);
52+
53+
sharpdisplay_framebuffer_obj_t* self = &allocate_display_bus_or_raise()->sharpdisplay;
54+
self->base.type = &sharpdisplay_framebuffer_type;
55+
56+
common_hal_sharpdisplay_framebuffer_construct(self, spi, chip_select, args[ARG_baudrate].u_int, args[ARG_width].u_int, args[ARG_height].u_int);
57+
58+
return MP_OBJ_FROM_PTR(self);
59+
}
60+
61+
62+
STATIC mp_int_t sharpdisplay_framebuffer_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
63+
sharpdisplay_framebuffer_obj_t *self = (sharpdisplay_framebuffer_obj_t*)self_in;
64+
// a readonly framebuffer would be unusual but not impossible
65+
if ((flags & MP_BUFFER_WRITE) && !(self->bufinfo.typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) {
66+
return 1;
67+
}
68+
*bufinfo = self->bufinfo;
69+
return 0;
70+
}
71+
72+
STATIC mp_obj_t sharpdisplay_framebuffer_deinit(mp_obj_t self_in) {
73+
sharpdisplay_framebuffer_obj_t *self = (sharpdisplay_framebuffer_obj_t*)self_in;
74+
common_hal_sharpdisplay_framebuffer_deinit(self);
75+
76+
return mp_const_none;
77+
}
78+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(sharpdisplay_framebuffer_deinit_obj, sharpdisplay_framebuffer_deinit);
79+
80+
STATIC const mp_rom_map_elem_t sharpdisplay_framebuffer_locals_dict_table[] = {
81+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&sharpdisplay_framebuffer_deinit_obj) },
82+
};
83+
STATIC MP_DEFINE_CONST_DICT(sharpdisplay_framebuffer_locals_dict, sharpdisplay_framebuffer_locals_dict_table);
84+
85+
const mp_obj_type_t sharpdisplay_framebuffer_type = {
86+
{ &mp_type_type },
87+
.name = MP_QSTR_SharpMemoryFramebuffer,
88+
.buffer_p = { .get_buffer = sharpdisplay_framebuffer_get_buffer, },
89+
.make_new = sharpdisplay_framebuffer_make_new,
90+
.protocol = &sharpdisplay_framebuffer_proto,
91+
.locals_dict = (mp_obj_dict_t*)&sharpdisplay_framebuffer_locals_dict,
92+
};
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) 2020 Jeff Epler 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+
#pragma once
28+
29+
// #include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
30+
// #include "shared-module/framebufferio/FramebufferDisplay.h"
31+
32+
#include "py/objtype.h"
33+
34+
extern const mp_obj_type_t sharpdisplay_framebuffer_type;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Jeff Epler 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 <stdint.h>
28+
29+
#include "py/obj.h"
30+
#include "py/runtime.h"
31+
32+
#include "shared-bindings/sharpdisplay/SharpMemoryFramebuffer.h"
33+
34+
//| """Support for Sharp Memory Display framebuffers"""
35+
//|
36+
37+
STATIC const mp_rom_map_elem_t sharpdisplay_module_globals_table[] = {
38+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sharpdisplay) },
39+
{ MP_ROM_QSTR(MP_QSTR_SharpMemoryFramebuffer), MP_ROM_PTR(&sharpdisplay_framebuffer_type) },
40+
};
41+
42+
STATIC MP_DEFINE_CONST_DICT(sharpdisplay_module_globals, sharpdisplay_module_globals_table);
43+
44+
const mp_obj_module_t sharpdisplay_module = {
45+
.base = { &mp_type_module },
46+
.globals = (mp_obj_dict_t*)&sharpdisplay_module_globals,
47+
};

shared-bindings/sharpdisplay/__init__.h

Whitespace-only changes.

shared-module/displayio/__init__.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@
1919
#include "supervisor/spi_flash_api.h"
2020
#include "py/mpconfig.h"
2121

22+
#if CIRCUITPY_SHARPDISPLAY
23+
#include "shared-bindings/sharpdisplay/SharpMemoryFramebuffer.h"
24+
#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
25+
#endif
26+
2227
primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT];
2328

24-
#if CIRCUITPY_RGBMATRIX
25-
STATIC bool any_display_uses_this_rgbmatrix(rgbmatrix_rgbmatrix_obj_t* pm) {
29+
#if CIRCUITPY_RGBMATRIX || CIRCUITPY_SHARPDISPLAY
30+
STATIC bool any_display_uses_this_framebuffer(mp_obj_base_t *obj) {
2631
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
27-
if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) {
32+
if (displays[i].display_base.type == &framebufferio_framebufferdisplay_type) {
2833
framebufferio_framebufferdisplay_obj_t* display = &displays[i].framebuffer_display;
29-
if (display->framebuffer == pm) {
34+
if (display->framebuffer == obj) {
3035
return true;
3136
}
3237
}
@@ -105,6 +110,10 @@ void common_hal_displayio_release_displays(void) {
105110
#if CIRCUITPY_FRAMEBUFFERIO
106111
} else if (bus_type == &rgbmatrix_RGBMatrix_type) {
107112
common_hal_rgbmatrix_rgbmatrix_deinit(&displays[i].rgbmatrix);
113+
#endif
114+
#if CIRCUITPY_SHARPDISPLAY
115+
} else if (displays[i].bus_base.type == &sharpdisplay_framebuffer_type) {
116+
common_hal_sharpdisplay_framebuffer_deinit(&displays[i].sharpdisplay);
108117
#endif
109118
}
110119
displays[i].fourwire_bus.base.type = &mp_type_NoneType;
@@ -170,9 +179,18 @@ void reset_displays(void) {
170179
#if CIRCUITPY_RGBMATRIX
171180
} else if (displays[i].rgbmatrix.base.type == &rgbmatrix_RGBMatrix_type) {
172181
rgbmatrix_rgbmatrix_obj_t * pm = &displays[i].rgbmatrix;
173-
if(!any_display_uses_this_rgbmatrix(pm)) {
182+
if(!any_display_uses_this_framebuffer(&pm->base)) {
174183
common_hal_rgbmatrix_rgbmatrix_deinit(pm);
175184
}
185+
#endif
186+
#if CIRCUITPY_SHARPDISPLAY
187+
} else if (displays[i].bus_base.type == &sharpdisplay_framebuffer_type) {
188+
sharpdisplay_framebuffer_obj_t * sharp = &displays[i].sharpdisplay;
189+
if(any_display_uses_this_framebuffer(&sharp->base)) {
190+
common_hal_sharpdisplay_framebuffer_reset(sharp);
191+
} else {
192+
common_hal_sharpdisplay_framebuffer_deinit(sharp);
193+
}
176194
#endif
177195
} else {
178196
// Not an active display bus.
@@ -203,6 +221,11 @@ void displayio_gc_collect(void) {
203221
rgbmatrix_rgbmatrix_collect_ptrs(&displays[i].rgbmatrix);
204222
}
205223
#endif
224+
#if CIRCUITPY_SHARPDISPLAY
225+
if (displays[i].rgbmatrix.base.type == &sharpdisplay_framebuffer_type) {
226+
common_hal_sharpdisplay_framebuffer_collect_ptrs(&displays[i].sharpdisplay);
227+
}
228+
#endif
206229

207230
if (displays[i].display.base.type == NULL) {
208231
continue;

shared-module/displayio/__init__.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,28 @@
3636
#include "shared-bindings/displayio/Group.h"
3737
#include "shared-bindings/displayio/I2CDisplay.h"
3838
#include "shared-bindings/displayio/ParallelBus.h"
39+
#if CIRCUITPY_RGBMATRIX
3940
#include "shared-bindings/rgbmatrix/RGBMatrix.h"
41+
#endif
42+
#if CIRCUITPY_SHARPDISPLAY
43+
#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
44+
#endif
4045

4146
typedef struct {
4247
union {
48+
mp_obj_base_t bus_base;
4349
displayio_fourwire_obj_t fourwire_bus;
4450
displayio_i2cdisplay_obj_t i2cdisplay_bus;
4551
displayio_parallelbus_obj_t parallel_bus;
4652
#if CIRCUITPY_RGBMATRIX
4753
rgbmatrix_rgbmatrix_obj_t rgbmatrix;
54+
#endif
55+
#if CIRCUITPY_SHARPDISPLAY
56+
sharpdisplay_framebuffer_obj_t sharpdisplay;
4857
#endif
4958
};
5059
union {
60+
mp_obj_base_t display_base;
5161
displayio_display_obj_t display;
5262
displayio_epaperdisplay_obj_t epaper_display;
5363
#if CIRCUITPY_FRAMEBUFFERIO

0 commit comments

Comments
 (0)