Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bootloader/gfx/tui.c
3693 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2018 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include <bdk.h>
19
20
#include "tui.h"
21
#include "../config.h"
22
23
void tui_sbar(bool force_update)
24
{
25
u32 cx, cy;
26
static u32 sbar_time_keeping = 0;
27
28
u32 timePassed = get_tmr_s() - sbar_time_keeping;
29
if (!force_update)
30
if (timePassed < 5)
31
return;
32
33
u8 prevFontSize = gfx_con.fntsz;
34
gfx_con.fntsz = 16;
35
sbar_time_keeping = get_tmr_s();
36
37
u32 battPercent = 0;
38
int battVoltCurr = 0;
39
40
gfx_con_getpos(&cx, &cy);
41
gfx_con_setpos(0, 1260);
42
43
max17050_get_property(MAX17050_RepSOC, (int *)&battPercent);
44
max17050_get_property(MAX17050_VCELL, &battVoltCurr);
45
46
gfx_clear_partial_grey(0x30, 1256, 24);
47
gfx_printf("%K%k Battery: %d.%d%% (%d mV) - Charge:", TXT_CLR_GREY_D, TXT_CLR_GREY,
48
(battPercent >> 8) & 0xFF, (battPercent & 0xFF) / 26, battVoltCurr);
49
50
max17050_get_property(MAX17050_Current, &battVoltCurr);
51
52
gfx_printf(" %k%d mA%k%K\n", battVoltCurr >= 0 ? TXT_CLR_GREEN_D : TXT_CLR_RED_D,
53
battVoltCurr / 1000, TXT_CLR_DEFAULT, TXT_CLR_BG);
54
55
gfx_con.fntsz = prevFontSize;
56
gfx_con_setpos(cx, cy);
57
}
58
59
void tui_pbar(int x, int y, u32 val, u32 fgcol, u32 bgcol)
60
{
61
u32 cx, cy;
62
if (val > 200)
63
val = 200;
64
65
gfx_con_getpos(&cx, &cy);
66
67
gfx_con_setpos(x, y);
68
69
gfx_printf("%k[%3d%%]%k", fgcol, val, TXT_CLR_DEFAULT);
70
71
x += 7 * gfx_con.fntsz;
72
73
for (u32 i = 0; i < (gfx_con.fntsz >> 3) * 6; i++)
74
{
75
gfx_line(x, y + i + 1, x + 3 * val, y + i + 1, fgcol);
76
gfx_line(x + 3 * val, y + i + 1, x + 3 * 100, y + i + 1, bgcol);
77
}
78
79
gfx_con_setpos(cx, cy);
80
81
// Update status bar.
82
tui_sbar(false);
83
}
84
85
void *tui_do_menu(menu_t *menu)
86
{
87
int idx = 0, prev_idx = 0, cnt = 0x7FFFFFFF;
88
89
gfx_clear_partial_grey(0x1B, 0, 1256);
90
tui_sbar(true);
91
92
while (true)
93
{
94
gfx_con_setcol(TXT_CLR_DEFAULT, 1, TXT_CLR_BG);
95
gfx_con_setpos(menu->x, menu->y);
96
gfx_printf("[%s]\n\n", menu->caption);
97
98
// Skip caption or seperator lines selection.
99
while (menu->ents[idx].type == MENT_CAPTION ||
100
menu->ents[idx].type == MENT_CHGLINE)
101
{
102
if (prev_idx <= idx || (!idx && prev_idx == cnt - 1))
103
{
104
idx++;
105
if (idx > (cnt - 1))
106
{
107
idx = 0;
108
prev_idx = 0;
109
}
110
}
111
else
112
{
113
idx--;
114
if (idx < 0)
115
{
116
idx = cnt - 1;
117
prev_idx = cnt;
118
}
119
}
120
}
121
prev_idx = idx;
122
123
// Draw the menu.
124
for (cnt = 0; menu->ents[cnt].type != MENT_END; cnt++)
125
{
126
if (cnt == idx)
127
gfx_con_setcol(TXT_CLR_BG, 1, TXT_CLR_DEFAULT);
128
else
129
gfx_con_setcol(TXT_CLR_DEFAULT, 1, TXT_CLR_BG);
130
if (menu->ents[cnt].type == MENT_CAPTION)
131
gfx_printf("%k %s", menu->ents[cnt].color, menu->ents[cnt].caption);
132
else if (menu->ents[cnt].type != MENT_CHGLINE)
133
gfx_printf(" %s", menu->ents[cnt].caption);
134
if (menu->ents[cnt].type == MENT_MENU)
135
gfx_printf("%k...", TXT_CLR_CYAN_L);
136
gfx_printf(" \n");
137
}
138
gfx_con_setcol(TXT_CLR_DEFAULT, 1, TXT_CLR_BG);
139
gfx_putc('\n');
140
141
// Print errors, help and battery status.
142
gfx_con_setpos(0, 1127);
143
gfx_printf("%k Warning: %kNyx is missing!", TXT_CLR_RED_D, TXT_CLR_GREY_M);
144
gfx_con_setpos(0, 1191);
145
gfx_printf("%k VOL: Move up/down\n PWR: Select option%k", TXT_CLR_GREY_M, TXT_CLR_DEFAULT);
146
147
display_backlight_brightness(h_cfg.backlight, 1000);
148
149
// Wait for user command.
150
u32 btn = btn_wait();
151
152
if (btn & BTN_VOL_DOWN && idx < (cnt - 1))
153
idx++;
154
else if (btn & BTN_VOL_DOWN && idx == (cnt - 1))
155
{
156
idx = 0;
157
prev_idx = -1;
158
}
159
if (btn & BTN_VOL_UP && idx > 0)
160
idx--;
161
else if (btn & BTN_VOL_UP && idx == 0)
162
{
163
idx = cnt - 1;
164
prev_idx = cnt;
165
}
166
if (btn & BTN_POWER)
167
{
168
ment_t *ent = &menu->ents[idx];
169
switch (ent->type)
170
{
171
case MENT_HANDLER:
172
ent->handler(ent->data);
173
break;
174
case MENT_MENU:
175
return tui_do_menu(ent->menu);
176
break;
177
case MENT_DATA:
178
return ent->data;
179
break;
180
case MENT_BACK:
181
return NULL;
182
break;
183
case MENT_HDLR_RE:
184
ent->handler(ent);
185
if (!ent->data)
186
return NULL;
187
break;
188
default:
189
break;
190
}
191
gfx_con.fntsz = 16;
192
gfx_clear_partial_grey(0x1B, 0, 1256);
193
}
194
tui_sbar(false);
195
}
196
197
return NULL;
198
}
199
200