Skip to content

Commit 46b3535

Browse files
committed
extmod/modframebuf: Add 8-bit greyscale format (GS8).
1 parent 3424746 commit 46b3535

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

docs/library/framebuf.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,7 @@ Constants
155155
.. data:: framebuf.GS4_HMSB
156156

157157
Grayscale (4-bit) color format
158+
159+
.. data:: framebuf.GS8
160+
161+
Grayscale (8-bit) color format

extmod/modframebuf.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef struct _mp_framebuf_p_t {
5656
#define FRAMEBUF_RGB565 (1)
5757
#define FRAMEBUF_GS2_HMSB (5)
5858
#define FRAMEBUF_GS4_HMSB (2)
59+
#define FRAMEBUF_GS8 (6)
5960
#define FRAMEBUF_MHLSB (3)
6061
#define FRAMEBUF_MHMSB (4)
6162

@@ -206,11 +207,31 @@ STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w,
206207
}
207208
}
208209

210+
// Functions for GS8 format
211+
212+
STATIC void gs8_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
213+
uint8_t *pixel = &((uint8_t*)fb->buf)[(x + y * fb->stride)];
214+
*pixel = col & 0xff;
215+
}
216+
217+
STATIC uint32_t gs8_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
218+
return ((uint8_t*)fb->buf)[(x + y * fb->stride)];
219+
}
220+
221+
STATIC void gs8_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
222+
uint8_t *pixel = &((uint8_t*)fb->buf)[(x + y * fb->stride)];
223+
while (h--) {
224+
memset(pixel, col, w);
225+
pixel += fb->stride;
226+
}
227+
}
228+
209229
STATIC mp_framebuf_p_t formats[] = {
210230
[FRAMEBUF_MVLSB] = {mvlsb_setpixel, mvlsb_getpixel, mvlsb_fill_rect},
211231
[FRAMEBUF_RGB565] = {rgb565_setpixel, rgb565_getpixel, rgb565_fill_rect},
212232
[FRAMEBUF_GS2_HMSB] = {gs2_hmsb_setpixel, gs2_hmsb_getpixel, gs2_hmsb_fill_rect},
213233
[FRAMEBUF_GS4_HMSB] = {gs4_hmsb_setpixel, gs4_hmsb_getpixel, gs4_hmsb_fill_rect},
234+
[FRAMEBUF_GS8] = {gs8_setpixel, gs8_getpixel, gs8_fill_rect},
214235
[FRAMEBUF_MHLSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect},
215236
[FRAMEBUF_MHMSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect},
216237
};
@@ -272,6 +293,8 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size
272293
case FRAMEBUF_GS4_HMSB:
273294
o->stride = (o->stride + 1) & ~1;
274295
break;
296+
case FRAMEBUF_GS8:
297+
break;
275298
default:
276299
mp_raise_ValueError("invalid format");
277300
}
@@ -610,6 +633,7 @@ STATIC const mp_rom_map_elem_t framebuf_module_globals_table[] = {
610633
{ MP_ROM_QSTR(MP_QSTR_RGB565), MP_ROM_INT(FRAMEBUF_RGB565) },
611634
{ MP_ROM_QSTR(MP_QSTR_GS2_HMSB), MP_ROM_INT(FRAMEBUF_GS2_HMSB) },
612635
{ MP_ROM_QSTR(MP_QSTR_GS4_HMSB), MP_ROM_INT(FRAMEBUF_GS4_HMSB) },
636+
{ MP_ROM_QSTR(MP_QSTR_GS8), MP_ROM_INT(FRAMEBUF_GS8) },
613637
{ MP_ROM_QSTR(MP_QSTR_MONO_HLSB), MP_ROM_INT(FRAMEBUF_MHLSB) },
614638
{ MP_ROM_QSTR(MP_QSTR_MONO_HMSB), MP_ROM_INT(FRAMEBUF_MHMSB) },
615639
};

tests/extmod/framebuf8.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
try:
2+
import framebuf
3+
except ImportError:
4+
print("SKIP")
5+
raise SystemExit
6+
7+
def printbuf():
8+
print("--8<--")
9+
for y in range(h):
10+
for x in range(w):
11+
print('%02x' % buf[(x + y * w)], end='')
12+
print()
13+
print("-->8--")
14+
15+
w = 8
16+
h = 5
17+
buf = bytearray(w * h)
18+
fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS8)
19+
20+
# fill
21+
fbuf.fill(0x55)
22+
printbuf()
23+
24+
# put pixel
25+
fbuf.pixel(0, 0, 0x11)
26+
fbuf.pixel(w - 1, 0, 0x22)
27+
fbuf.pixel(0, h - 1, 0x33)
28+
fbuf.pixel(w - 1, h - 1, 0xff)
29+
printbuf()
30+
31+
# get pixel
32+
print(hex(fbuf.pixel(0, h - 1)), hex(fbuf.pixel(1, 1)))

tests/extmod/framebuf8.py.exp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--8<--
2+
5555555555555555
3+
5555555555555555
4+
5555555555555555
5+
5555555555555555
6+
5555555555555555
7+
-->8--
8+
--8<--
9+
1155555555555522
10+
5555555555555555
11+
5555555555555555
12+
5555555555555555
13+
33555555555555ff
14+
-->8--
15+
0x33 0x55

0 commit comments

Comments
 (0)