ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
sh_util.c
Go to the documentation of this file.
1
17#include <string.h>
18
19#include "ra8_gfx.h"
20#include "ra8_gfx_font.h"
21#include "sh_app.h"
22
24typedef enum : uint32_t {
27
28int32_t sh_cells(int32_t pixels)
29{
30 return pixels / (int32_t)k_sh_glyph_w;
31}
32
33void sh_fit(char* dst, size_t cap, const char* src, int32_t max_chars)
34{
35 size_t limit = (max_chars < 1) ? 1U : (size_t)max_chars;
36 if (limit >= cap) {
37 limit = cap - 1U;
38 }
39 const size_t len = strlen(src);
40 if (len <= limit) {
41 memcpy(dst, src, len);
42 dst[len] = '\0';
43 return;
44 }
45 const size_t keep = (limit < k_sh_ellipsis_min) ? 0U : (limit - k_sh_ellipsis_min);
46 memcpy(dst, src, keep);
47 dst[keep] = '.';
48 dst[keep + 1U] = '.';
49 dst[keep + 2U] = '\0';
50}
51
52size_t sh_fmt_uint(char* dst, size_t pos, uint32_t v)
53{
54 char tmp[k_sh_dec_base];
55 uint32_t n = 0U;
56 if (v == 0U) {
57 tmp[n++] = '0';
58 }
59 while ((v > 0U) && (n < (uint32_t)k_sh_dec_base)) {
60 tmp[n++] = (char)('0' + (v % (uint32_t)k_sh_dec_base));
61 v /= (uint32_t)k_sh_dec_base;
62 }
63 for (uint32_t k = 0U; k < n; ++k) {
64 dst[pos++] = tmp[n - 1U - k];
65 }
66 return pos;
67}
68
69void sh_border(ra8_ui_rect_t r, uint32_t colour, int32_t width)
70{
71 for (int32_t i = 0; i < width; ++i) {
72 (void)ra8_gfx_rect(r.x + i, r.y + i, r.w - (2 * i), r.h - (2 * i), colour, false);
73 }
74}
75
76void sh_titlebar(const char* title, const char* right)
77{
78 (void)ra8_gfx_rect(0, 0, (int32_t)k_sh_fb_w, (int32_t)k_sh_bar_h, (uint32_t)k_sh_col_bar, true);
79 (void)ra8_gfx_text_out((int32_t)k_sh_pad,
80 (int32_t)((k_sh_bar_h - k_sh_glyph_h) / 2U),
81 title,
83 (uint32_t)k_sh_col_barfg,
84 (uint32_t)k_sh_col_bar);
85 if (right != nullptr) {
86 const int32_t rx =
87 (int32_t)k_sh_fb_w - (int32_t)k_sh_pad - ((int32_t)strlen(right) * (int32_t)k_sh_glyph_w);
88 (void)ra8_gfx_text_out(rx,
89 (int32_t)((k_sh_bar_h - k_sh_glyph_h) / 2U),
90 right,
92 (uint32_t)k_sh_col_barfg,
93 (uint32_t)k_sh_col_bar);
94 }
95}
size_t strlen(const char *s)
Calculate string length.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
Software 2D graphics primitives layered on top of a caller-ownedframebuffer (DRW / D/AVE 2D / GLCDC r...
ra8_err_t ra8_gfx_text_out(int32_t x, int32_t y, const char *str, const ra8_gfx_font_t *font, uint32_t fg_color, uint32_t bg_color)
Render a NUL-terminated ASCII string.
ra8_err_t ra8_gfx_rect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color, bool filled)
Draw an axis-aligned rectangle.
Bitmap font descriptor + bundled font handles for ra8_gfx.
const ra8_gfx_font_t ra8_gfx_font_8x16
Bundled 8x16 IBM PC VGA bitmap font, ASCII 0x20..0x7E.
Shared contract for the ereader_shelf multi-screen e-reader.
@ k_sh_glyph_h
Bitmap font cell height.
Definition sh_app.h:49
@ k_sh_glyph_w
Bitmap font cell width.
Definition sh_app.h:48
@ k_sh_pad
Outer margin / content inset.
Definition sh_app.h:52
@ k_sh_bar_h
Header / title-bar height.
Definition sh_app.h:51
@ k_sh_fb_w
Panel width in pixels.
Definition sh_app.h:46
@ k_sh_dec_base
Decimal formatting base.
Definition sh_app.h:71
@ k_sh_col_barfg
Text on the bar.
Definition sh_app.h:106
@ k_sh_col_bar
Header / title-bar fill.
Definition sh_app.h:99
int32_t sh_cells(int32_t pixels)
Cells of the 8px bitmap font that fit in pixels.
Definition sh_util.c:28
void sh_border(ra8_ui_rect_t r, uint32_t colour, int32_t width)
Draw a width-thick rectangle outline in colour.
Definition sh_util.c:69
void sh_titlebar(const char *title, const char *right)
Fill the header bar and draw title left + optional right text.
Definition sh_util.c:76
sh_util_const_t
Local layout constants.
Definition sh_util.c:24
@ k_sh_ellipsis_min
Chars kept before the ".." truncation.
Definition sh_util.c:25
size_t sh_fmt_uint(char *dst, size_t pos, uint32_t v)
Append unsigned v as decimal into dst at pos; returns new pos.
Definition sh_util.c:52
void sh_fit(char *dst, size_t cap, const char *src, int32_t max_chars)
Copy src into dst (cap bytes), truncating past max_chars with "..".
Definition sh_util.c:33
Axis-aligned rectangle in framebuffer pixel coordinates.
Definition ra8_ui.h:72
int32_t w
Width (pixels, >= 0).
Definition ra8_ui.h:75
int32_t h
Height (pixels, >= 0).
Definition ra8_ui.h:76
int32_t x
Left edge (pixels).
Definition ra8_ui.h:73
int32_t y
Top edge (pixels).
Definition ra8_ui.h:74