-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.c
353 lines (274 loc) · 6.77 KB
/
menu.c
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
141
142
143
144
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "config.h"
#include <util/delay.h>
#include "display.h"
#include "input.h"
#include "output.h"
#include "output_midi.h"
#include "output_normal.h"
#include <avr/eeprom.h>
#define TYPE_MENU 0
#define TYPE_INT 1
#define TYPE_LIST 2
#define TYPE_SHORT_POS 3
#define EEPROM_MODE 0
#define EEPROM_VELOCITY 1
#define EEPROM_MAXINC 2
#define EEPROM_MAXDEC 4
#define SELECTED_NONE 0x00
#define SELECTED_LABEL 0x01
#define SELECTED_VALUE 0x02
#define MODE_INT 0
#define MODE_MIDI 1
#define DISABLED 0
#define ENABLED 1
const char *modes[2] = { "INT", "MIDI" };
const char *bools[2] = { "DIS", "ENA" };
#define FLAG_SELECTED 1
struct s_menuitem_t {
char *label;
int type;
void *value;
struct s_menuitem_t *parent;
struct s_menuitem_t *next;
struct s_menuitem_t *prev;
void (*callback)(void*);
};
typedef struct s_menuitem_t menuitem_t;
struct s_menulist_t {
char id;
const char *label;
struct s_menulist_t *next;
struct s_menulist_t *prev;
char flags;
};
typedef struct s_menulist_t menulist_t;
void changeOutputType(void *t) {
menulist_t *ml = (menulist_t *)t;
while(ml->next != NULL && !(ml->flags & FLAG_SELECTED))
ml=ml->next;
switch(ml->id) {
case MODE_INT:
outputNormalInit();
break;
case MODE_MIDI:
outputMidiInit();
break;
}
eeprom_write_byte((uint8_t*)EEPROM_MODE, ml->id);
}
void changeVelocity(void *t) {
menulist_t *ml = (menulist_t *)t;
while(ml->next != NULL && !(ml->flags & FLAG_SELECTED))
ml=ml->next;
midi_velocity = ml->id;
eeprom_write_byte((uint8_t*)EEPROM_VELOCITY, midi_velocity);
}
void changeMaxInc(void *t) {
eeprom_write_byte((uint8_t*)EEPROM_MAXINC, noteMaxInc);
}
void changeMaxDec(void *t) {
eeprom_write_byte((uint8_t*)EEPROM_MAXDEC, noteMaxDec);
}
void drawMenuItem(lcd_t lcd, menuitem_t *mi, unsigned char line, char selected) {
putsAtLcd(mi->label, &lcd[line][1]);
if (selected == SELECTED_LABEL)
putsAtLcd(">", &lcd[line][0]);
else if (selected == SELECTED_VALUE)
putsAtLcd(">", &lcd[line][10]);
menulist_t *tmp;
int i;
switch (mi->type) {
case TYPE_INT:
case TYPE_SHORT_POS:
printIntAtLcd(*(int*)(mi->value), &lcd[line][11]);
break;
case TYPE_LIST:
tmp = mi->value;
while (( !(tmp->flags & FLAG_SELECTED)) && (tmp->next != NULL))
tmp=tmp->next;
putsAtLcd("[", &lcd[line][11]);
i = 12 + putsAtLcd(tmp->label, &lcd[line][12]);
putsAtLcd("]", &lcd[line][i]);
break;
}
}
static menulist_t mode_int, mode_midi;
static menulist_t velo_disabled, velo_enabled;
static menuitem_t mm_0, mm_1, mm_2, mm_3;
void menuInit() {
uint8_t tmp;
mode_int.id = MODE_INT;
mode_int.label = modes[MODE_INT];
mode_int.next = &mode_midi;
mode_int.prev = NULL;
mode_int.flags = FLAG_SELECTED;
mode_midi.id = MODE_MIDI;
mode_midi.label = modes[MODE_MIDI];
mode_midi.next = NULL;
mode_midi.prev = &mode_int;
mode_midi.flags = 0;
velo_enabled.id = ENABLED;
velo_enabled.label = bools[ENABLED];
velo_enabled.next = &velo_disabled;
velo_enabled.prev = NULL;
velo_enabled.flags = FLAG_SELECTED;
velo_disabled.id = DISABLED;
velo_disabled.label = bools[DISABLED];
velo_disabled.next = NULL;
velo_disabled.prev = &velo_enabled;
velo_disabled.flags = 0;
mm_0.label = "Mode:";
mm_0.parent = NULL;
mm_0.next = &mm_1;
mm_0.prev = NULL;
mm_0.type = TYPE_LIST;
mm_0.value = &mode_int;
mm_0.callback = &changeOutputType;
mm_1.label = "Velocity:";
mm_1.parent = NULL;
mm_1.next = &mm_2;
mm_1.prev = &mm_0;
mm_1.value = &velo_enabled;
mm_1.callback = &changeVelocity;
mm_1.type = TYPE_LIST;
mm_2.label = "MaxInc:";
mm_2.parent = NULL;
mm_2.next = &mm_3;
mm_2.prev = &mm_1;
mm_2.value = ¬eMaxInc;
mm_2.callback = &changeMaxInc;
mm_2.type = TYPE_SHORT_POS;
mm_3.label = "MaxDec:";
mm_3.parent = NULL;
mm_3.next = NULL;
mm_3.prev = &mm_2;
mm_3.value = ¬eMaxDec;
mm_3.callback = &changeMaxDec;
mm_3.type = TYPE_SHORT_POS;
tmp = eeprom_read_byte((uint8_t*)EEPROM_MODE);
if (tmp == MODE_INT) {
mode_int.flags = FLAG_SELECTED;
mode_midi.flags = 0;
outputNormalInit();
} else {
mode_midi.flags = FLAG_SELECTED;
mode_int.flags = 0;
outputMidiInit();
}
tmp = eeprom_read_byte((uint8_t*)EEPROM_VELOCITY);
if (tmp == ENABLED) {
velo_enabled.flags = FLAG_SELECTED;
velo_disabled.flags = 0;
midi_velocity = ENABLED;
} else {
velo_disabled.flags = FLAG_SELECTED;
velo_enabled.flags = 0;
midi_velocity = DISABLED;
}
noteMaxInc = eeprom_read_byte((uint8_t*)EEPROM_MAXINC);
noteMaxDec = eeprom_read_byte((uint8_t*)EEPROM_MAXDEC);
}
void menuEnter(lcd_t lcd) {
int rot = 0;
int btn = 0;
int loops=0;
int state = SELECTED_NONE;
waitForRelease(BTN_CONFIRM);
menuitem_t *cur = &mm_0;
menuitem_t *tmp;
menulist_t *tmp_ml;
while (1) {
loops++;
rot += rotaryHandle();
/* short btmp = buttonsHandle();
while (btmp == -1) {
btmp = buttonsHandle();
}
btn = btmp;*/
btn = buttonsHandleWait();
if(loops < LCD_REFRESH)
continue;
clearLcd(lcd);
loops = 0;
int *tmpi;
switch (state) {
case SELECTED_NONE:
if (btn & BTN_CANCEL) {
if (cur->parent == NULL) {
return;
} else {
cur = cur->parent;
}
}
if (btn & BTN_CONFIRM) {
state = SELECTED_LABEL;
waitForRelease(BTN_CONFIRM);
}
while ((rot < 0) && (cur->prev != NULL)) {
cur = cur->prev;
rot++;
}
while ((rot > 0) && (cur->next != NULL)) {
cur = cur->next;
rot--;
}
break;
case SELECTED_LABEL:
if (btn & BTN_CANCEL) {
state = SELECTED_NONE;
}
if (btn & BTN_CONFIRM) {
if (cur->callback != NULL)
cur->callback(cur->value);
state = SELECTED_NONE;
}
if (rot != 0 ) {
switch(cur->type) {
case TYPE_INT:
tmpi = (int*)cur->value;
*tmpi += rot;
break;
case TYPE_SHORT_POS:
tmpi = (int*)cur->value;
*tmpi += rot;
if ( (*tmpi) < 0)
(*tmpi) = 0;
if ( (*tmpi) > 255)
(*tmpi) = 255;
break;
case TYPE_LIST:
tmp_ml = cur->value;
while ( !(tmp_ml->flags & FLAG_SELECTED) && tmp_ml->next != NULL)
tmp_ml = tmp_ml->next;
if ((rot > 0) && (tmp_ml->next != NULL)) {
tmp_ml->flags &= ~(FLAG_SELECTED);
tmp_ml->next->flags |= FLAG_SELECTED;
}
if ((rot < 0) && (tmp_ml->prev != NULL)) {
tmp_ml->flags &= ~(FLAG_SELECTED);
tmp_ml->prev->flags |= FLAG_SELECTED;
}
break;
}
}
break;
}
int i;
if (cur->prev != NULL) {
drawMenuItem(lcd, cur->prev, 0, 0);
drawMenuItem(lcd, cur, 1, state+1);
i = 2;
} else {
drawMenuItem(lcd, cur, 0, state+1);
i = 1;
}
tmp = cur->next;
for(; i < LCD_LINES && tmp != NULL; i++) {
drawMenuItem(lcd, tmp, i, 0);
tmp = tmp->next;
}
redrawLcd(lcd);
waitForRelease(BTN_CONFIRM | BTN_CANCEL);
rot = 0;
}
}