Newer
Older
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#include "backlight.h"
#include "constants.h"
#include "keyboard.h"
#include "menu.h"
#include "oled.h"
#include "powersave.h"
#include "remote.h"
#include "scancodes.h"
int current_menu_y = 0;
int current_scroll_y = 0;
#ifdef KBD_VARIANT_STANDALONE
const MenuItem menu_items[] = {
{ "Exit Menu ESC", KEY_ESCAPE },
{ "Key Backlight- F1", KEY_F1 },
{ "Key Backlight+ F2", KEY_F2 },
{ "System Status s", KEY_S },
{ "USB Flashing Mode x", KEY_X },
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
};
#else
#define MENU_NUM_ITEMS 9
const MenuItem menu_items[] = {
{ "Exit Menu ESC", KEY_ESCAPE },
{ "Power On 1", KEY_1 },
{ "Power Off 0", KEY_0 },
{ "Reset r", KEY_R },
{ "Battery Status b", KEY_B },
{ "Key Backlight- F1", KEY_F1 },
{ "Key Backlight+ F2", KEY_F2 },
{ "Wake SPC", KEY_SPACE },
{ "System Status s", KEY_S },
// Only needed for debugging.
// The keyboard will go to sleep when turning off
// main system power.
{ "KBD Power-Off p", KEY_P },
};
#endif
void reset_and_render_menu() {
current_scroll_y = 0;
current_menu_y = 0;
render_menu(current_scroll_y);
}
void render_menu(int y) {
gfx_clear();
gfx_invert_row(current_menu_y-y);
for (int i=0; i<MENU_NUM_ITEMS; i++) {
gfx_poke_str(0,i-y,menu_items[i].title);
}
gfx_on();
gfx_flush();
}
int execute_menu_function(int y) {
if (y>=0 && y<MENU_NUM_ITEMS) {
return execute_meta_function(menu_items[y].keycode);
}
return execute_meta_function(KEY_ESCAPE);
}
#define BOOTLOADER_START_ADDRESS ((0x8000-0x1000) >> 1)
void jump_to_bootloader(void) {
((void (*)(void))BOOTLOADER_START_ADDRESS)();
}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// returns 1 for navigation function (stay in meta mode), 0 for terminal function
int execute_meta_function(int keycode) {
if (keycode == KEY_0) {
// TODO: are you sure?
anim_goodbye();
remote_turn_off_som();
keyboard_power_off();
reset_keyboard_state();
// Directly enter menu again
return 2;
}
else if (keycode == KEY_1) {
if (remote_turn_on_som()) {
anim_hello();
}
kbd_brightness_init();
return 0;
}
else if (keycode == KEY_R) {
// TODO: are you sure?
remote_reset_som();
}
else if (keycode == KEY_SPACE) {
remote_wake_som();
}
/*else if (keycode == KEY_V) {
remote_turn_off_aux();
}*/
else if (keycode == KEY_B) {
remote_get_voltages();
return 0;
}
else if (keycode == KEY_S) {
remote_get_status();
return 0;
}
else if (keycode == KEY_F1) {
kbd_brightness_dec();
return 1;
}
else if (keycode == KEY_F2) {
kbd_brightness_inc();
return 1;
}
else if (keycode == HID_KEYBOARD_SC_UP_ARROW) {
current_menu_y--;
if (current_menu_y<0) current_menu_y = 0;
if (current_menu_y<=current_scroll_y) current_scroll_y--;
if (current_scroll_y<0) current_scroll_y = 0;
render_menu(current_scroll_y);
return 1;
}
else if (keycode == HID_KEYBOARD_SC_DOWN_ARROW) {
current_menu_y++;
if (current_menu_y>=MENU_NUM_ITEMS) current_menu_y = MENU_NUM_ITEMS-1;
if (current_menu_y>=current_scroll_y+3) current_scroll_y++;
render_menu(current_scroll_y);
return 1;
}
else if (keycode == KEY_ENTER) {
return execute_menu_function(current_menu_y);
}
else if (keycode == KEY_ESCAPE) {
gfx_clear();
gfx_flush();
}
else if (keycode == KEY_X) {
jump_to_bootloader();
}
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
gfx_clear();
gfx_flush();
return 0;
}
void anim_hello(void) {
gfx_clear();
gfx_on();
for (int y=0; y<3; y++) {
for (int x=0; x<12; x++) {
gfx_poke(x+4,y+1,(5+y)*32+x);
gfx_flush();
}
}
for (int y=0; y<0xff; y++) {
gfx_contrast(y);
Delay_MS(2);
}
for (int y=0; y<0xff; y++) {
gfx_contrast(0xff-y);
Delay_MS(2);
}
}
void anim_goodbye(void) {
gfx_clear();
gfx_on();
for (int y=0; y<3; y++) {
for (int x=0; x<12; x++) {
gfx_poke(x+4,y+1,(5+y)*32+x);
}
}
for (int y=0; y<3; y++) {
for (int x=0; x<12; x++) {
gfx_poke(x+4,y+1,' ');
gfx_flush();
}
}
gfx_off();
}