Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bootloader/gfx/tui.c
1476 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
extern hekate_config h_cfg;
24
25
void tui_sbar(bool force_update)
26
{
27
u32 cx, cy;
28
static u32 sbar_time_keeping = 0;
29
30
u32 timePassed = get_tmr_s() - sbar_time_keeping;
31
if (!force_update)
32
if (timePassed < 5)
33
return;
34
35
u8 prevFontSize = gfx_con.fntsz;
36
gfx_con.fntsz = 16;
37
sbar_time_keeping = get_tmr_s();
38
39
u32 battPercent = 0;
40
int battVoltCurr = 0;
41
42
gfx_con_getpos(&cx, &cy);
43
gfx_con_setpos(0, 1260);
44
45
max17050_get_property(MAX17050_RepSOC, (int *)&battPercent);
46
max17050_get_property(MAX17050_VCELL, &battVoltCurr);
47
48
gfx_clear_partial_grey(0x30, 1256, 24);
49
gfx_printf("%K%k Battery: %d.%d%% (%d mV) - Charge:", TXT_CLR_GREY_D, TXT_CLR_GREY,
50
(battPercent >> 8) & 0xFF, (battPercent & 0xFF) / 26, battVoltCurr);
51
52
max17050_get_property(MAX17050_Current, &battVoltCurr);
53
54
gfx_printf(" %k%d mA%k%K\n", battVoltCurr >= 0 ? TXT_CLR_GREEN_D : TXT_CLR_RED_D,
55
battVoltCurr / 1000, TXT_CLR_DEFAULT, TXT_CLR_BG);
56
57
gfx_con.fntsz = prevFontSize;
58
gfx_con_setpos(cx, cy);
59
}
60
61
void tui_pbar(int x, int y, u32 val, u32 fgcol, u32 bgcol)
62
{
63
u32 cx, cy;
64
if (val > 200)
65
val = 200;
66
67
gfx_con_getpos(&cx, &cy);
68
69
gfx_con_setpos(x, y);
70
71
gfx_printf("%k[%3d%%]%k", fgcol, val, TXT_CLR_DEFAULT);
72
73
x += 7 * gfx_con.fntsz;
74
75
for (u32 i = 0; i < (gfx_con.fntsz >> 3) * 6; i++)
76
{
77
gfx_line(x, y + i + 1, x + 3 * val, y + i + 1, fgcol);
78
gfx_line(x + 3 * val, y + i + 1, x + 3 * 100, y + i + 1, bgcol);
79
}
80
81
gfx_con_setpos(cx, cy);
82
83
// Update status bar.
84
tui_sbar(false);
85
}
86
87
void *tui_do_menu(menu_t *menu)
88
{
89
int idx = 0, prev_idx = 0, cnt = 0x7FFFFFFF;
90
91
gfx_clear_partial_grey(0x1B, 0, 1256);
92
tui_sbar(true);
93
94
while (true)
95
{
96
gfx_con_setcol(TXT_CLR_DEFAULT, 1, TXT_CLR_BG);
97
gfx_con_setpos(menu->x, menu->y);
98
gfx_printf("[%s]\n\n", menu->caption);
99
100
// Skip caption or seperator lines selection.
101
while (menu->ents[idx].type == MENT_CAPTION ||
102
menu->ents[idx].type == MENT_CHGLINE)
103
{
104
if (prev_idx <= idx || (!idx && prev_idx == cnt - 1))
105
{
106
idx++;
107
if (idx > (cnt - 1))
108
{
109
idx = 0;
110
prev_idx = 0;
111
}
112
}
113
else
114
{
115
idx--;
116
if (idx < 0)
117
{
118
idx = cnt - 1;
119
prev_idx = cnt;
120
}
121
}
122
}
123
prev_idx = idx;
124
125
// Draw the menu.
126
for (cnt = 0; menu->ents[cnt].type != MENT_END; cnt++)
127
{
128
if (cnt == idx)
129
gfx_con_setcol(TXT_CLR_BG, 1, TXT_CLR_DEFAULT);
130
else
131
gfx_con_setcol(TXT_CLR_DEFAULT, 1, TXT_CLR_BG);
132
if (menu->ents[cnt].type == MENT_CAPTION)
133
gfx_printf("%k %s", menu->ents[cnt].color, menu->ents[cnt].caption);
134
else if (menu->ents[cnt].type != MENT_CHGLINE)
135
gfx_printf(" %s", menu->ents[cnt].caption);
136
if (menu->ents[cnt].type == MENT_MENU)
137
gfx_printf("%k...", TXT_CLR_CYAN_L);
138
gfx_printf(" \n");
139
}
140
gfx_con_setcol(TXT_CLR_DEFAULT, 1, TXT_CLR_BG);
141
gfx_putc('\n');
142
143
// Print errors, help and battery status.
144
gfx_con_setpos(0, 1127);
145
gfx_printf("%k Warning: %kNyx is missing!", TXT_CLR_RED_D, TXT_CLR_GREY_M);
146
gfx_con_setpos(0, 1191);
147
gfx_printf("%k VOL: Move up/down\n PWR: Select option%k", TXT_CLR_GREY_M, TXT_CLR_DEFAULT);
148
149
display_backlight_brightness(h_cfg.backlight, 1000);
150
151
// Wait for user command.
152
u32 btn = btn_wait();
153
154
if (btn & BTN_VOL_DOWN && idx < (cnt - 1))
155
idx++;
156
else if (btn & BTN_VOL_DOWN && idx == (cnt - 1))
157
{
158
idx = 0;
159
prev_idx = -1;
160
}
161
if (btn & BTN_VOL_UP && idx > 0)
162
idx--;
163
else if (btn & BTN_VOL_UP && idx == 0)
164
{
165
idx = cnt - 1;
166
prev_idx = cnt;
167
}
168
if (btn & BTN_POWER)
169
{
170
ment_t *ent = &menu->ents[idx];
171
switch (ent->type)
172
{
173
case MENT_HANDLER:
174
ent->handler(ent->data);
175
break;
176
case MENT_MENU:
177
return tui_do_menu(ent->menu);
178
break;
179
case MENT_DATA:
180
return ent->data;
181
break;
182
case MENT_BACK:
183
return NULL;
184
break;
185
case MENT_HDLR_RE:
186
ent->handler(ent);
187
if (!ent->data)
188
return NULL;
189
break;
190
default:
191
break;
192
}
193
gfx_con.fntsz = 16;
194
gfx_clear_partial_grey(0x1B, 0, 1256);
195
}
196
tui_sbar(false);
197
}
198
199
return NULL;
200
}
201
202