rework display_btext into display_print

pull/25/head
Pavol Rusnak 7 years ago
parent 1d5a6dd1b4
commit d0c59fdf3d
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -240,28 +240,60 @@ static const uint8_t *get_glyph(uint8_t font, uint8_t c)
}
// display text using bitmap font
void display_btext(int x, int y, const char *text, int textlen, uint16_t color)
void display_print(const char *text, int textlen)
{
#define COLS (DISPLAY_RESX / 6)
#define ROWS (DISPLAY_RESY / 8)
static char textbuf[ROWS][COLS] = {0};
static uint8_t row = 0, col = 0;
// determine text length if not provided
if (textlen < 0) {
textlen = strlen(text);
}
for (int i = 0; i < textlen; i++) {
char c = text[i];
if (c < ' ' || c >= 0x80) c = ' ';
const uint8_t *g = Font_Bitmap + (5 * (c - ' '));
display_set_window(x + i * 6, y, x + i * 6 + 4, y + 7);
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 5; k++) {
if (g[k] & (1 << j)) {
DATA(color >> 8);
DATA(color & 0xFF);
} else {
DATA(0x00);
DATA(0x00);
}
switch (text[i]) {
case '\r':
break;
case '\n':
row++;
col = 0;
break;
default:
textbuf[row][col] = text[i];
col++;
break;
}
if (col >= COLS) {
col = 0;
row++;
}
if (row >= ROWS) {
for (int j = 0; j < ROWS - 1; j++) {
memcpy(textbuf[j], textbuf[j + 1], COLS);
}
memset(textbuf[ROWS - 1], 0x00, COLS);
row = ROWS - 1;
}
}
display_set_window(0, 0, DISPLAY_RESX - 1, DISPLAY_RESY - 1);
for (int i = 0; i < DISPLAY_RESX * DISPLAY_RESY; i++) {
int x = (i % DISPLAY_RESX);
int y = (i / DISPLAY_RESX);
int j = y % 8; y /= 8;
int k = x % 6; x /= 6;
char c = textbuf[y][x] & 0x7F;
// char invert = textbuf[y][x] & 0x80;
if (c < ' ') c = ' ';
const uint8_t *g = Font_Bitmap + (5 * (c - ' '));
if (k < 5 && (g[k] & (1 << j))) {
DATA(0xFF);
DATA(0xFF);
} else {
DATA(0x00);
DATA(0x00);
}
}
}

@ -35,7 +35,7 @@ void display_image(int x, int y, int w, int h, const void *data, int datalen);
void display_icon(int x, int y, int w, int h, const void *data, int datalen, uint16_t fgcolor, uint16_t bgcolor);
void display_qrcode(int x, int y, const char *data, int datalen, uint8_t scale);
void display_loader(uint16_t progress, int yoffset, uint16_t fgcolor, uint16_t bgcolor, const uint8_t *icon, uint32_t iconlen, uint16_t iconfgcolor);
void display_btext(int x, int y, const char *text, int textlen, uint16_t color);
void display_print(const char *text, int textlen);
void display_text(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor);
void display_text_center(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor);
void display_text_right(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor);

@ -126,24 +126,21 @@ STATIC mp_obj_t mod_TrezorUi_Display_icon(size_t n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_icon_obj, 6, 6, mod_TrezorUi_Display_icon);
/// def trezor.ui.display.btext(x: int, y: int, text: bytes, color: int) -> None:
/// def trezor.ui.display.print(text: str) -> None:
/// '''
/// Renders text using 5x8 bitmap font at position (x,y) using color
/// Renders text using 5x8 bitmap font (using special text mode)
/// '''
STATIC mp_obj_t mod_TrezorUi_Display_btext(size_t n_args, const mp_obj_t *args) {
mp_int_t x = mp_obj_get_int(args[1]);
mp_int_t y = mp_obj_get_int(args[2]);
mp_buffer_info_t text;
mp_get_buffer_raise(args[3], &text, MP_BUFFER_READ);
mp_int_t color = mp_obj_get_int(args[4]);
if (text.len > 0) {
display_btext(x, y, text.buf, text.len, color);
STATIC mp_obj_t mod_TrezorUi_Display_print(mp_obj_t self, mp_obj_t text) {
mp_buffer_info_t buf;
mp_get_buffer_raise(text, &buf, MP_BUFFER_READ);
if (buf.len > 0) {
display_print(buf.buf, buf.len);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_btext_obj, 5, 5, mod_TrezorUi_Display_btext);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorUi_Display_print_obj, mod_TrezorUi_Display_print);
/// def trezor.ui.display.text(x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None:
/// def trezor.ui.display.text(x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int) -> None:
/// '''
/// Renders left-aligned text at position (x,y) where x is left position and y is baseline.
/// Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background.
@ -163,7 +160,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_text(size_t n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_text_obj, 7, 7, mod_TrezorUi_Display_text);
/// def trezor.ui.display.text_center(x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None:
/// def trezor.ui.display.text_center(x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int) -> None:
/// '''
/// Renders text centered at position (x,y) where x is text center and y is baseline.
/// Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background.
@ -183,7 +180,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_text_center(size_t n_args, const mp_obj_t *
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_text_center_obj, 7, 7, mod_TrezorUi_Display_text_center);
/// def trezor.ui.display.text_right(x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None:
/// def trezor.ui.display.text_right(x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int) -> None:
/// '''
/// Renders right-aligned text at position (x,y) where x is right position and y is baseline.
/// Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background.
@ -203,7 +200,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_text_right(size_t n_args, const mp_obj_t *a
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_text_right_obj, 7, 7, mod_TrezorUi_Display_text_right);
/// def trezor.ui.display.text_width(text: bytes, font: int) -> int:
/// def trezor.ui.display.text_width(text: str, font: int) -> int:
/// '''
/// Returns a width of text in pixels. Font font is used for rendering.
/// '''
@ -393,7 +390,7 @@ STATIC const mp_rom_map_elem_t mod_TrezorUi_Display_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_bar_radius), MP_ROM_PTR(&mod_TrezorUi_Display_bar_radius_obj) },
{ MP_ROM_QSTR(MP_QSTR_image), MP_ROM_PTR(&mod_TrezorUi_Display_image_obj) },
{ MP_ROM_QSTR(MP_QSTR_icon), MP_ROM_PTR(&mod_TrezorUi_Display_icon_obj) },
{ MP_ROM_QSTR(MP_QSTR_btext), MP_ROM_PTR(&mod_TrezorUi_Display_btext_obj) },
{ MP_ROM_QSTR(MP_QSTR_print), MP_ROM_PTR(&mod_TrezorUi_Display_print_obj) },
{ MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&mod_TrezorUi_Display_text_obj) },
{ MP_ROM_QSTR(MP_QSTR_text_center), MP_ROM_PTR(&mod_TrezorUi_Display_text_center_obj) },
{ MP_ROM_QSTR(MP_QSTR_text_right), MP_ROM_PTR(&mod_TrezorUi_Display_text_right_obj) },

Loading…
Cancel
Save