Skip to content

Commit fd99690

Browse files
kamikazedpgeorge
authored andcommitted
extmod/modframebuf: Add GS4_HMSB format.
1 parent eaa7745 commit fd99690

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

extmod/modframebuf.c

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,66 @@ STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, i
9797
}
9898
}
9999

100+
// Functions for GS4_HMSB format
101+
102+
STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
103+
uint8_t *pixel = &((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1];
104+
105+
if (x % 2) {
106+
*pixel = ((uint8_t)col & 0x0f) | (*pixel & 0xf0);
107+
} else {
108+
*pixel = ((uint8_t)col << 4) | (*pixel & 0x0f);
109+
}
110+
}
111+
112+
STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
113+
if (x % 2) {
114+
return ((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1] & 0x0f;
115+
}
116+
117+
return ((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1] >> 4;
118+
}
119+
120+
STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
121+
col &= 0x0f;
122+
uint8_t *pixel_pair = &((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1];
123+
uint8_t col_shifted_left = col << 4;
124+
uint8_t colored_pixel_pair = col_shifted_left | col;
125+
int pixel_count_till_next_line = (fb->stride - w) >> 1;
126+
bool odd_x = (x % 2 == 1);
127+
128+
while (h--) {
129+
int ww = w;
130+
131+
if (odd_x && ww > 0) {
132+
*pixel_pair = (*pixel_pair & 0xf0) | col;
133+
pixel_pair++;
134+
ww--;
135+
}
136+
137+
memset(pixel_pair, colored_pixel_pair, ww >> 1);
138+
pixel_pair += ww >> 1;
139+
140+
if (ww % 2) {
141+
*pixel_pair = col_shifted_left | (*pixel_pair & 0x0f);
142+
if (!odd_x) {
143+
pixel_pair++;
144+
}
145+
}
146+
147+
pixel_pair += pixel_count_till_next_line;
148+
}
149+
}
150+
100151
// constants for formats
101-
#define FRAMEBUF_MVLSB (0)
102-
#define FRAMEBUF_RGB565 (1)
152+
#define FRAMEBUF_MVLSB (0)
153+
#define FRAMEBUF_RGB565 (1)
154+
#define FRAMEBUF_GS4_HMSB (2)
103155

104156
STATIC mp_framebuf_p_t formats[] = {
105157
[FRAMEBUF_MVLSB] = {mvlsb_setpixel, mvlsb_getpixel, mvlsb_fill_rect},
106158
[FRAMEBUF_RGB565] = {rgb565_setpixel, rgb565_getpixel, rgb565_fill_rect},
159+
[FRAMEBUF_GS4_HMSB] = {gs4_hmsb_setpixel, gs4_hmsb_getpixel, gs4_hmsb_fill_rect},
107160
};
108161

109162
static inline void setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t color) {
@@ -152,6 +205,7 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size
152205
switch (o->format) {
153206
case FRAMEBUF_MVLSB:
154207
case FRAMEBUF_RGB565:
208+
case FRAMEBUF_GS4_HMSB:
155209
break;
156210
default:
157211
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
@@ -490,6 +544,7 @@ STATIC const mp_rom_map_elem_t framebuf_module_globals_table[] = {
490544
{ MP_ROM_QSTR(MP_QSTR_FrameBuffer1), MP_ROM_PTR(&legacy_framebuffer1_obj) },
491545
{ MP_ROM_QSTR(MP_QSTR_MVLSB), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_MVLSB) },
492546
{ MP_ROM_QSTR(MP_QSTR_RGB565), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_RGB565) },
547+
{ MP_ROM_QSTR(MP_QSTR_GS4_HMSB), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_GS4_HMSB) },
493548
};
494549

495550
STATIC MP_DEFINE_CONST_DICT(framebuf_module_globals, framebuf_module_globals_table);

tests/extmod/framebuf1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191

9292
# test invalid constructor
9393
try:
94-
fbuf = framebuf.FrameBuffer(buf, w, h, 2, framebuf.MVLSB)
94+
fbuf = framebuf.FrameBuffer(buf, w, h, 3, framebuf.MVLSB)
9595
except ValueError:
9696
print("ValueError")
9797

0 commit comments

Comments
 (0)