From 965904ea3a70cde91c75faba43a2be483442d49b Mon Sep 17 00:00:00 2001 From: Hinerk <hinerk.stender@mail.de> Date: Tue, 27 Aug 2019 12:11:18 +0000 Subject: [PATCH] feat(display): Add API-call to print with different fonts --- epicardium/epicardium.h | 35 +++++++++++++++++++++++++++++++++ epicardium/modules/display.c | 30 +++++++++++++++++++++++++++- lib/gfx/gfx.c | 6 +++--- lib/gfx/gfx.h | 4 ++-- pycardium/modules/py/display.py | 29 +++++++++++++++++++++++++-- pycardium/modules/qstrdefs.h | 1 + pycardium/modules/sys_display.c | 25 +++++++++++++++++++++++ 7 files changed, 122 insertions(+), 8 deletions(-) diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 52c02b9f4..29476b026 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -54,6 +54,7 @@ typedef _Bool bool; #define API_DISP_PIXEL 0x28 #define API_DISP_FRAMEBUFFER 0x29 #define API_DISP_BACKLIGHT 0x2a +#define API_DISP_PRINT_ADV 0x2b /* API_BATTERY_VOLTAGE 0x30 */ #define API_BATTERY_CURRENT 0x31 @@ -1239,6 +1240,40 @@ API(API_DISP_PRINT, uint16_t bg) ); +/* + * Font Selection + */ +enum disp_font_name { + DISP_FONT8 = 0, + DISP_FONT12 = 1, + DISP_FONT16 = 2, + DISP_FONT20 = 3, + DISP_FONT24 = 4, +}; + +/** + * Prints a string into the display framebuffer with font type selectable + * + * :param fontName: number of font, use FontName enum + * :param posx: x position to print to. 0 <= x <= 160 + * :param posy: y position to print to. 0 <= y <= 80 + * :param pString: string to print + * :param fg: foreground color in rgb565 + * :param bg: background color in rgb565 + * :return: ``0`` on success or a negative value in case of an error: + * + * - ``-EBUSY``: Display was already locked from another task. + */ +API(API_DISP_PRINT_ADV, + int epic_disp_print_adv( + uint8_t font, + uint16_t posx, + uint16_t posy, + const char *pString, + uint16_t fg, + uint16_t bg) +); + /** * Fills the whole screen with one color * diff --git a/epicardium/modules/display.c b/epicardium/modules/display.c index ed8757fba..fe89fc324 100644 --- a/epicardium/modules/display.c +++ b/epicardium/modules/display.c @@ -27,12 +27,40 @@ int epic_disp_print( const char *pString, uint16_t fg, uint16_t bg +) { + return epic_disp_print_adv(DISP_FONT20, posx, posy, pString, fg, bg); +} + +static const sFONT *font_map[] = { + [DISP_FONT8] = &Font8, [DISP_FONT12] = &Font12, + [DISP_FONT16] = &Font16, [DISP_FONT20] = &Font20, + [DISP_FONT24] = &Font24, +}; + +int epic_disp_print_adv( + uint8_t font, + uint16_t posx, + uint16_t posy, + const char *pString, + uint16_t fg, + uint16_t bg ) { int cl = check_lock(); + if (font >= (sizeof(font_map) / sizeof(sFONT *))) { + return -EINVAL; + } if (cl < 0) { return cl; } else { - gfx_puts(&Font20, &display_screen, posx, posy, pString, fg, bg); + gfx_puts( + font_map[font], + &display_screen, + posx, + posy, + pString, + fg, + bg + ); return 0; } } diff --git a/lib/gfx/gfx.c b/lib/gfx/gfx.c index 9d27d2a55..5af5bbb36 100644 --- a/lib/gfx/gfx.c +++ b/lib/gfx/gfx.c @@ -33,7 +33,7 @@ struct gfx_region gfx_screen(struct framebuffer *fb) return r; } -static inline int letter_bit(sFONT *font, char c, int x, int y) +static inline int letter_bit(const sFONT *font, char c, int x, int y) { if (x < 0 || y < 0) return 0; @@ -53,7 +53,7 @@ static inline int letter_bit(sFONT *font, char c, int x, int y) } void gfx_putchar( - sFONT *font, + const sFONT *font, struct gfx_region *r, int x, int y, @@ -78,7 +78,7 @@ void gfx_putchar( } void gfx_puts( - sFONT *font, + const sFONT *font, struct gfx_region *r, int x, int y, diff --git a/lib/gfx/gfx.h b/lib/gfx/gfx.h index 428b196c6..0f64a429b 100644 --- a/lib/gfx/gfx.h +++ b/lib/gfx/gfx.h @@ -14,9 +14,9 @@ struct gfx_region { void gfx_setpixel(struct gfx_region *r, int x, int y, Color c); struct gfx_region gfx_screen(struct framebuffer *fb); -void gfx_putchar(sFONT *font, struct gfx_region *reg, int x, int y, char ch, +void gfx_putchar(const sFONT *font, struct gfx_region *reg, int x, int y, char ch, Color fg, Color bg); -void gfx_puts(sFONT *font, struct gfx_region *reg, int x, int y, +void gfx_puts(const sFONT *font, struct gfx_region *reg, int x, int y, const char *str, Color fg, Color bg); Color gfx_color_rgb_f(struct gfx_region *reg, float r, float g, float b); Color gfx_color_rgb(struct gfx_region *reg, uint8_t r, uint8_t g, uint8_t b); diff --git a/pycardium/modules/py/display.py b/pycardium/modules/py/display.py index e957977d6..7b51cef37 100644 --- a/pycardium/modules/py/display.py +++ b/pycardium/modules/py/display.py @@ -1,6 +1,13 @@ import sys_display import color +# font enumeration +FONT8 = 0 +FONT12 = 1 +FONT16 = 2 +FONT20 = 3 +FONT24 = 4 + class Display: """ @@ -61,7 +68,7 @@ class Display: sys_display.clear(col) return self - def print(self, text, *, fg=None, bg=None, posx=0, posy=0): + def print(self, text, *, fg=None, bg=None, posx=0, posy=0, font=FONT20): """ Prints a string on the display. Font size is locked to 20px @@ -70,11 +77,29 @@ class Display: :param bg: Background color (expects RGB triple) :param posx: X-Position of the first character, 0 <= posx <= 160 :param posy: Y-Position of the first character, 0 <= posy <= 80 + :param font: 0 <= font <= 4 (currently) selects a font + + Avaiable Fonts: + + - :py:data:`display.FONT8` + - :py:data:`display.FONT12` + - :py:data:`display.FONT16` + - :py:data:`display.FONT20` + - :py:data:`display.FONT24` + + **Example:** + + .. code-block:: python + + with display.open() as d: + d.clear() + d.print('Hello Earth!', font=display.FONT24) + d.update() """ fg = fg or color.WHITE bg = bg or color.BLACK - sys_display.print(text, posx, posy, fg, bg) + sys_display.print_adv(text, posx, posy, fg, bg, font) return self def pixel(self, x, y, *, col=None): diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index a02bcbd87..c5813602f 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -85,6 +85,7 @@ Q(read_thermistor_voltage) Q(sys_display) Q(display) Q(print) +Q(print_adv) Q(pixel) Q(backlight) Q(line) diff --git a/pycardium/modules/sys_display.c b/pycardium/modules/sys_display.c index f5523d548..4f92ab92d 100644 --- a/pycardium/modules/sys_display.c +++ b/pycardium/modules/sys_display.c @@ -53,6 +53,30 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( display_print_obj, 5, 5, mp_display_print ); +/* print something on the display */ +static mp_obj_t mp_display_print_adv(size_t n_args, const mp_obj_t *args) +{ + if (!mp_obj_is_str_or_bytes(args[0])) { + mp_raise_TypeError("input text must be a string"); + } + GET_STR_DATA_LEN(args[0], print, print_len); + uint32_t posx = mp_obj_get_int(args[1]); + uint32_t posy = mp_obj_get_int(args[2]); + uint32_t fg = get_color(args[3]); + uint32_t bg = get_color(args[4]); + uint8_t fontName = mp_obj_get_int(args[5]); + int res = epic_disp_print_adv( + fontName, posx, posy, (const char *)print, fg, bg + ); + if (res < 0) { + mp_raise_OSError(-res); + } + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( + display_print_adv_obj, 6, 6, mp_display_print_adv +); + /* draw pixel on the display */ static mp_obj_t mp_display_pixel(size_t n_args, const mp_obj_t *args) { @@ -234,6 +258,7 @@ static const mp_rom_map_elem_t display_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&display_open_obj) }, { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&display_close_obj) }, { MP_ROM_QSTR(MP_QSTR_print), MP_ROM_PTR(&display_print_obj) }, + { MP_ROM_QSTR(MP_QSTR_print_adv), MP_ROM_PTR(&display_print_adv_obj) }, { MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&display_pixel_obj) }, { MP_ROM_QSTR(MP_QSTR_backlight), MP_ROM_PTR(&display_backlight_obj) }, { MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&display_line_obj) }, -- GitLab