-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.cpp
More file actions
50 lines (41 loc) · 1.59 KB
/
menu.cpp
File metadata and controls
50 lines (41 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "menu.h"
int menu(int state) {
while (1) {
lcd.clear();
bool up = buttonUP.read();
bool down = buttonDOWN.read();
//printf(" \n new frame: "); // new line of debug printouts
//printf("state: %d", state);
const int NUM_STATES = 4;
State menu_fsm[NUM_STATES] = { // State machine for menu
{1, "SONGS MENU", {1,3}},
{2, "SPACE GAME", {2,0}},
{3, "LABRINTH", {3,1}},
{4, "SNAKE", {0,2}},
};
if (up || joystick.get_direction() == N) { // Check if up or down button pressed
state = menu_fsm[state].next_state[1];
printf(" UP PRESSED");
}
if (down || joystick.get_direction() == S) {
state = menu_fsm[state].next_state[0];
printf(" DOWN PRESSED ");
}
state = state % NUM_STATES; // Wrap around state number
for (int i = 0; i < NUM_STATES; i++) { // Loop over all states
if (i == state) {
lcd.printString("> ", 2, i); // Highlight selected option
}
lcd.printString(menu_fsm[i].name, 7, i);
}
if (g_buttonA_flag || g_joystick_flag) { // check if menu button pressed
ThisThread::sleep_for(500ms);
g_buttonA_flag = 0;
g_joystick_flag = 0;
printf(" buttonA pressed \n");
return state;
}
lcd.refresh();
ThisThread::sleep_for(250ms);
}
}