Integrated getLineY() functionality in gfx_printLine()

replace/aa3463326040e420174bde39ddec61b6a1d3aa3b
Federico Amedeo Izzo 2021-04-10 09:34:39 +02:00
rodzic 765fb3f4c9
commit c3a8251ca9
3 zmienionych plików z 60 dodań i 34 usunięć

Wyświetl plik

@ -224,17 +224,7 @@ void gfx_drawRect(point_t start, uint16_t width, uint16_t height, color_t color,
void gfx_drawCircle(point_t start, uint16_t r, color_t color);
/**
* Calculates printing position to fit a number of text lines on the screen space.
* @param cur: current line number over total (1-based)
* @param tot: number of lines to fit in screen
* @param startY: starting Y coordinate to leave space for top bar
* @param endY: ending Y coordinate to leave space for bottom bar
* @return Y coordinates for text printing
*/
uint16_t gfx_getLineY(uint8_t cur, uint8_t tot, uint16_t startY, uint16_t endY);
/**
* Prints text on the screen.
* Prints text on the screen at the specified coordinates.
* @param start: text line start point, in pixel coordinates.
* @param size: text font size, defined as enum.
* @param alignment: text alignment type, defined as enum.
@ -244,6 +234,23 @@ uint16_t gfx_getLineY(uint8_t cur, uint8_t tot, uint16_t startY, uint16_t endY);
*/
point_t gfx_print(point_t start, fontSize_t size, textAlign_t alignment, color_t color, const char* fmt, ... );
/**
* Prints text on the screen, calculating the print position.
* The print position is calculated to fit the desired number of lines in the vertical space
* @param cur: current line number over total (1-based)
* @param tot: number of lines to fit in screen
* @param startY: starting Y coordinate to leave space for top bar
* @param endY: ending Y coordinate to leave space for bottom bar
* @param startX: starting X coordinate to leave space on the screen sides
* @param size: text font size, defined as enum.
* @param alignment: text alignment type, defined as enum.
* @param color: text color, in color_t format.
* @param fmt: printf style format string
* @return text width and height as point_t coordinates
*/
point_t gfx_printLine(uint8_t cur, uint8_t tot, uint16_t startY, uint16_t endY, uint16_t startX,
fontSize_t size, textAlign_t alignment, color_t color, const char* fmt, ... );
/**
* Prints an error message surrounded by a red box on the screen.
* @param text: text to print.

Wyświetl plik

@ -385,28 +385,15 @@ static inline uint16_t get_reset_x(textAlign_t alignment, uint16_t line_size,
return 0;
}
uint16_t gfx_getLineY(uint8_t cur, uint8_t tot, uint16_t startY, uint16_t endY)
point_t gfx_printBuffer(point_t start, fontSize_t size, textAlign_t alignment,
color_t color, const char *buf)
{
// e.g. to print 2 lines we need 3 padding spaces
uint16_t step = (endY - startY) / (tot + 1);
return startY + (step * cur);
}
point_t gfx_print(point_t start, fontSize_t size, textAlign_t alignment,
color_t color, const char *fmt, ... )
{
va_list ap;
va_start(ap, fmt);
vsnprintf(text, sizeof(text)-1, fmt, ap);
va_end(ap);
GFXfont f = fonts[size];
size_t len = strlen(text);
size_t len = strlen(buf);
// Compute size of the first row in pixels
uint16_t line_size = get_line_size(f, text, len);
uint16_t line_size = get_line_size(f, buf, len);
uint16_t reset_x = get_reset_x(alignment, line_size, start.x);
start.x = reset_x;
@ -417,7 +404,7 @@ point_t gfx_print(point_t start, fontSize_t size, textAlign_t alignment,
/* For each char in the string */
for(unsigned i = 0; i < len; i++)
{
char c = text[i];
char c = buf[i];
GFXglyph glyph = f.glyph[c - f.first];
uint8_t *bitmap = f.bitmap;
@ -445,7 +432,7 @@ point_t gfx_print(point_t start, fontSize_t size, textAlign_t alignment,
if (start.x + glyph.xAdvance > SCREEN_WIDTH)
{
// Compute size of the first row in pixels
line_size = get_line_size(f, text, len);
line_size = get_line_size(f, buf, len);
start.x = reset_x = get_reset_x(alignment, line_size, start.x);
start.y += f.yAdvance;
}
@ -486,6 +473,40 @@ point_t gfx_print(point_t start, fontSize_t size, textAlign_t alignment,
return text_size;
}
point_t gfx_print(point_t start, fontSize_t size, textAlign_t alignment,
color_t color, const char *fmt, ... )
{
// Get format string and arguments from var char
va_list ap;
va_start(ap, fmt);
vsnprintf(text, sizeof(text)-1, fmt, ap);
va_end(ap);
return gfx_printBuffer(start, size, alignment, color, text);
}
point_t gfx_printLine(uint8_t cur, uint8_t tot, uint16_t startY, uint16_t endY, uint16_t startX,
fontSize_t size, textAlign_t alignment, color_t color, const char* fmt, ... )
{
// Get format string and arguments from var char
va_list ap;
va_start(ap, fmt);
vsnprintf(text, sizeof(text)-1, fmt, ap);
va_end(ap);
// If endY is 0 set it to default value = SCREEN_HEIGHT
if(endY == 0)
endY = SCREEN_HEIGHT;
// Calculate print coordinates
// e.g. to print 2 lines we need 3 padding spaces
uint16_t step = (endY - startY) / (tot + 1);
uint16_t printY = startY + (step * cur);
point_t start = {startX, printY};
return gfx_printBuffer(start, size, alignment, color, text);
}
// Print an error message to the center of the screen, surronded by a red (when possible) box
void gfx_printError(const char *text, fontSize_t size)
{

Wyświetl plik

@ -40,10 +40,8 @@ int main(void)
gfx_clearScreen();
for(int cur=1; cur<=tot; cur++)
{
uint16_t y = gfx_getLineY(cur, tot, 0, SCREEN_HEIGHT);
point_t pos = {0, y};
gfx_print(pos, FONT_SIZE_8PT, TEXT_ALIGN_CENTER,
color_white, "Line %2d of %2d", cur, tot);
gfx_printLine(cur, tot, 0, 0, 0, FONT_SIZE_8PT, TEXT_ALIGN_CENTER,
color_white, "Line %2d of %2d", cur, tot);
}
gfx_render();
// Sleep for 1 second