Skip to content
Snippets Groups Projects
Commit 94fb6681 authored by rahix's avatar rahix
Browse files

Merge 'Select fonts in display.print()'

Implements a new method `epic_disp_print_adv()` and improves
`display.print()` with new font parameter.

`epic_disp_print_adv()` behaves like `epic_disp_print()` but also takes
a new font parameter which is a number to select the font.  The font
name is verbalized using a new enum `disp_font_name`. The enum names
correlate to the fonts under gfx/fonts.  On the MicroPython side, in the
display module, constants are defined in order to provide names instead
of numbers.

See merge request !214
parents 69b55ee7 965904ea
No related branches found
No related tags found
No related merge requests found
......@@ -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
*
......
......@@ -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;
}
}
......
......@@ -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,
......
......@@ -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);
......
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):
......
......@@ -85,6 +85,7 @@ Q(read_thermistor_voltage)
Q(sys_display)
Q(display)
Q(print)
Q(print_adv)
Q(pixel)
Q(backlight)
Q(line)
......
......@@ -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) },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment