ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_ui.c
Go to the documentation of this file.
1
17
18#include "ra8_ui.h"
19
20#include <stddef.h>
21#include <stdint.h>
22
23#include "ra8_check.h"
24#include "ra8_err.h"
25
27static const char* const s_tag = "ra8_ui";
28
29/* ===========================================================================
30 * Geometry + hit-testing
31 * ===========================================================================
32 */
33
34bool ra8_ui_rect_contains(const ra8_ui_rect_t* r, int32_t px, int32_t py)
35{
36 if (r == nullptr) {
37 return false;
38 }
39 return ((px >= r->x) && (px < (r->x + r->w)) && (py >= r->y) && (py < (r->y + r->h)));
40}
41
43 uint16_t count,
44 int32_t px,
45 int32_t py,
46 uint16_t* out_action,
47 bool* out_hit)
48{
49 RA8_CHECK_NULL_PTR(out_action, s_tag, "out_action must not be nullptr");
50 RA8_CHECK_NULL_PTR(out_hit, s_tag, "out_hit must not be nullptr");
51 if (count > 0U) {
52 RA8_CHECK_NULL_PTR(targets, s_tag, "targets must not be nullptr when count > 0");
53 }
54
55 *out_hit = false;
56 for (uint16_t i = 0U; i < count; ++i) {
57 if (ra8_ui_rect_contains(&targets[i].rect, px, py)) {
58 *out_action = targets[i].action_id;
59 *out_hit = true;
60 return k_ra8_ok;
61 }
62 }
63 return k_ra8_ok;
64}
65
66/* ===========================================================================
67 * Screen-stack navigation
68 * ===========================================================================
69 */
70
71ra8_err_t ra8_ui_nav_init(ra8_ui_nav_t* nav, uint16_t root_screen)
72{
73 RA8_CHECK_NULL_PTR(nav, s_tag, "nav must not be nullptr");
74 nav->stack[0] = root_screen;
75 nav->depth = 1U;
76 return k_ra8_ok;
77}
78
80{
81 RA8_CHECK_NULL_PTR(nav, s_tag, "nav must not be nullptr");
82 if (nav->depth == 0U) {
84 }
85 if (nav->depth >= (uint8_t)k_ra8_ui_nav_max_depth) {
86 return k_ra8_err_no_mem;
87 }
88 nav->stack[nav->depth] = screen;
89 nav->depth++;
90 return k_ra8_ok;
91}
92
93ra8_err_t ra8_ui_nav_pop(ra8_ui_nav_t* nav, uint16_t* out_screen)
94{
95 RA8_CHECK_NULL_PTR(nav, s_tag, "nav must not be nullptr");
96 RA8_CHECK_NULL_PTR(out_screen, s_tag, "out_screen must not be nullptr");
97 if (nav->depth <= 1U) {
99 }
100 nav->depth--;
101 *out_screen = nav->stack[nav->depth - 1U];
102 return k_ra8_ok;
103}
104
106{
107 RA8_CHECK_NULL_PTR(nav, s_tag, "nav must not be nullptr");
108 if (nav->depth == 0U) {
110 }
111 nav->stack[nav->depth - 1U] = screen;
112 return k_ra8_ok;
113}
114
115ra8_err_t ra8_ui_nav_top(const ra8_ui_nav_t* nav, uint16_t* out_screen)
116{
117 RA8_CHECK_NULL_PTR(nav, s_tag, "nav must not be nullptr");
118 RA8_CHECK_NULL_PTR(out_screen, s_tag, "out_screen must not be nullptr");
119 if (nav->depth == 0U) {
121 }
122 *out_screen = nav->stack[nav->depth - 1U];
123 return k_ra8_ok;
124}
125
126/* ===========================================================================
127 * Paging
128 * ===========================================================================
129 */
130
132{
133 RA8_CHECK_NULL_PTR(p, s_tag, "p must not be nullptr");
134 if (total == 0U) {
136 }
137 p->current = 0U;
138 p->total = total;
139 return k_ra8_ok;
140}
141
143{
144 RA8_CHECK_NULL_PTR(p, s_tag, "p must not be nullptr");
145 RA8_CHECK_NULL_PTR(out_changed, s_tag, "out_changed must not be nullptr");
146 if ((p->total > 0U) && ((uint32_t)p->current < ((uint32_t)p->total - 1U))) {
147 p->current++;
148 *out_changed = true;
149 } else {
150 *out_changed = false;
151 }
152 return k_ra8_ok;
153}
154
156{
157 RA8_CHECK_NULL_PTR(p, s_tag, "p must not be nullptr");
158 RA8_CHECK_NULL_PTR(out_changed, s_tag, "out_changed must not be nullptr");
159 if (p->current > 0U) {
160 p->current--;
161 *out_changed = true;
162 } else {
163 *out_changed = false;
164 }
165 return k_ra8_ok;
166}
167
168ra8_err_t ra8_ui_pager_goto(ra8_ui_pager_t* p, uint16_t page, bool* out_changed)
169{
170 RA8_CHECK_NULL_PTR(p, s_tag, "p must not be nullptr");
171 RA8_CHECK_NULL_PTR(out_changed, s_tag, "out_changed must not be nullptr");
172 uint16_t target = page;
173 if ((p->total > 0U) && (target > (uint16_t)(p->total - 1U))) {
174 target = (uint16_t)(p->total - 1U);
175 }
176 *out_changed = (target != p->current);
177 p->current = target;
178 return k_ra8_ok;
179}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_err_no_mem
Static buffer exhausted (no dynamic memory on this project).
Definition ra8_err.h:142
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
Definition ra8_err.h:161
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
ra8_err_t ra8_ui_nav_init(ra8_ui_nav_t *nav, uint16_t root_screen)
Initialise a navigation stack with a root screen.
Definition ra8_ui.c:71
ra8_err_t ra8_ui_nav_push(ra8_ui_nav_t *nav, uint16_t screen)
Push a new screen onto the stack.
Definition ra8_ui.c:79
ra8_err_t ra8_ui_nav_replace(ra8_ui_nav_t *nav, uint16_t screen)
Replace the top screen in place (no depth change).
Definition ra8_ui.c:105
ra8_err_t ra8_ui_pager_init(ra8_ui_pager_t *p, uint16_t total)
Initialise a pager over total pages at page 0.
Definition ra8_ui.c:131
ra8_err_t ra8_ui_pager_next(ra8_ui_pager_t *p, bool *out_changed)
Advance to the next page, clamping at the last page.
Definition ra8_ui.c:142
ra8_err_t ra8_ui_nav_pop(ra8_ui_nav_t *nav, uint16_t *out_screen)
Pop the top screen, revealing the one beneath.
Definition ra8_ui.c:93
bool ra8_ui_rect_contains(const ra8_ui_rect_t *r, int32_t px, int32_t py)
Test whether a point lies inside a rectangle.
Definition ra8_ui.c:34
ra8_err_t ra8_ui_nav_top(const ra8_ui_nav_t *nav, uint16_t *out_screen)
Read the current (top) screen id.
Definition ra8_ui.c:115
ra8_err_t ra8_ui_pager_prev(ra8_ui_pager_t *p, bool *out_changed)
Step to the previous page, clamping at page 0.
Definition ra8_ui.c:155
ra8_err_t ra8_ui_hit_test(const ra8_ui_target_t *targets, uint16_t count, int32_t px, int32_t py, uint16_t *out_action, bool *out_hit)
Find the first tap target containing a point.
Definition ra8_ui.c:42
ra8_err_t ra8_ui_pager_goto(ra8_ui_pager_t *p, uint16_t page, bool *out_changed)
Jump to an absolute page, clamping into [0, total-1].
Definition ra8_ui.c:168
Bounded UI interaction core: hit-testing, screen stack, paging.
@ k_ra8_ui_nav_max_depth
Max screen-stack depth.
Definition ra8_ui.h:55
Fixed-depth screen-id stack (caller-owned).
Definition ra8_ui.h:163
uint8_t depth
Occupied slots (1..max).
Definition ra8_ui.h:165
uint16_t stack[k_ra8_ui_nav_max_depth]
Screen-id stack.
Definition ra8_ui.h:164
Clamped (current, total) page cursor.
Definition ra8_ui.h:285
uint16_t current
Current 0-based page index.
Definition ra8_ui.h:286
uint16_t total
Total page count (>= 1).
Definition ra8_ui.h:287
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
One tap target: a rectangle bound to an opaque action id.
Definition ra8_ui.h:83