/
menu.cpp
140 lines (121 loc) · 3.46 KB
/
menu.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
#include "common.h"
/* Defines */
#define IMG_SOUND_W 16
#define IMG_SOUNDM_W 6
#define MENU_COUNT_MAX 6
/* Typedefs */
typedef struct
{
void (*func)(void);
const __FlashStringHelper *label;
} ITEM_T;
/* Local Variables */
PROGMEM static const uint8_t imgSound[] = { // 16x8
0x00, 0x7C, 0x8E, 0xD6, 0xDA, 0xDA, 0x82, 0x7C, 0x00, 0x38, 0x38, 0x00, 0x38, 0x7C, 0xFE, 0x00
};
PROGMEM static const uint8_t imgSoundOffOn[][6] = { // 6x8 x2
{ 0x00, 0x00, 0xA0, 0x40, 0xA0, 0x00 },
{ 0x28, 0x10, 0x44, 0x38, 0x82, 0x7C },
};
static int8_t menuX, menuY, menuW, menuH;
static bool isFramed, isControlSound;
static int8_t menuItemCount;
static ITEM_T menuItemAry[MENU_COUNT_MAX];
static int8_t menuItemPos;
static bool isInvalidMenu;
/*---------------------------------------------------------------------------*/
void clearMenuItems(void)
{
menuItemCount = 0;
dprintln(F("Clear menu items"));
}
void addMenuItem(const __FlashStringHelper *label, void (*func)(void))
{
if (menuItemCount >= MENU_COUNT_MAX) return;
ITEM_T *pItem = &menuItemAry[menuItemCount];
pItem->label = label;
pItem->func = func;
menuItemCount++;
dprint(F("Add menu items: "));
dprintln(label);
}
int8_t getMenuItemPos(void)
{
return menuItemPos;
}
int8_t getMenuItemCount(void)
{
return menuItemCount;
}
void setMenuCoords(int8_t x, int8_t y, int8_t w, int8_t h, bool f, bool s)
{
menuX = x;
menuY = y;
menuW = w;
menuH = h;
isFramed = f;
isControlSound = s;
}
void setMenuItemPos(int8_t pos)
{
menuItemPos = pos;
isInvalidMenu = true;
dprint(F("menuItemPos="));
dprintln(menuItemPos);
}
void setConfirmMenu(int8_t y, void (*funcOk)(), void (*funcCancel)())
{
playSoundClick();
clearMenuItems();
addMenuItem(F("OK"), funcOk);
addMenuItem(F("CANCEL"), funcCancel);
setMenuCoords(40, y, 47, 11, true, true);
setMenuItemPos(1);
isInvalidMenu = true;
}
void handleMenu(void)
{
if (arduboy.buttonDown(UP_BUTTON) && menuItemPos > 0) {
menuItemPos--;
playSoundTick();
isInvalidMenu = true;
dprint(F("menuItemPos="));
dprintln(menuItemPos);
}
if (arduboy.buttonDown(DOWN_BUTTON) && menuItemPos < menuItemCount - 1) {
menuItemPos++;
playSoundTick();
isInvalidMenu = true;
dprint(F("menuItemPos="));
dprintln(menuItemPos);
}
if (isControlSound && arduboy.buttonDown(A_BUTTON)) {
setSound(!arduboy.isAudioEnabled());
playSoundClick();
isInvalidMenu = true;
}
if (arduboy.buttonDown(B_BUTTON)) {
menuItemAry[menuItemPos].func();
}
}
void drawMenuItems(bool isForced)
{
if (!isInvalidMenu && !isForced) return;
arduboy.fillRect2(menuX - 1, menuY - 1, menuW + 2, menuH + 2, BLACK);
if (isFramed) {
arduboy.drawRect2(menuX - 2, menuY - 2, menuW + 4, menuH + 4, WHITE);
}
ITEM_T *pItem = menuItemAry;
for (int i = 0; i < menuItemCount; i++, pItem++) {
arduboy.printEx(menuX + 12 - (i == menuItemPos) * 4, menuY + i * 6, pItem->label);
}
arduboy.fillRect2(menuX, menuY + menuItemPos * 6, 5, 5, WHITE);
if (isControlSound) drawSoundEnabled();
isInvalidMenu = false;
}
void drawSoundEnabled(void)
{
uint8_t *p = arduboy.getBuffer();
memcpy_P(p + WIDTH * 8 - IMG_SOUND_W - IMG_SOUNDM_W, imgSound, IMG_SOUND_W);
memcpy_P(p + WIDTH * 8 - IMG_SOUNDM_W, imgSoundOffOn[arduboy.audio.enabled()], IMG_SOUNDM_W);
}