-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.c
363 lines (325 loc) · 7.79 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
354
355
356
357
358
359
360
361
362
363
#include "contiki.h"
#include "mc1322x.h"
#include "button-sensors.h"
#include "rot_enc.h"
#include "pwm.h"
#include "random.h"
#include <string.h>
#define LEVEL_ROOT 0
#define LEVEL_SUBMENU 1
#define LEVEL_ACTION 2
#define MENU_SETTINGS 0
#define MENU_LCD 1
#define MENU_BLE 2
#define MENU_BUZZER 3
#define MENU_CLOCK 4
#define SETTINGS_WRITE 1
#define SETTINGS_SET_RIMEADDR 2
#define SETTINGS_UART1_SHELL 3
#define SETTINGS_UART2_SHELL 4
#define SETTINGS_UART2_LOG 5
#define LCD_BACKLIGHT 1
#define LCD_CLEAR 2
#define LCD_CURSOR 3
#define LCD_CURSOR_BLINK 4
#define BLE_RESET 1
#define BUZZER_PLAY_TONE 1
#define BUZZER_PLAY_FREQ 2
#define BUZZER_RINGTONE_INIT 3
#define BUZZER_RINGTONE_PLAY 4
#define BUZZER_OFF 5
#define CLOCK_SHOW 1
#define CLOCK_SET 2
static char* settings_menu[] = {"settings", "write to SD", "set rimeaddr", "uart1 shell", "uart2 shell", "uart2 log"};
static char* lcd_menu[] = {"lcd", "backlight", "clear", "cursor", "cursor blink"};
static char* ble_menu[] = {"ble", "reset"};
static char* buzzer_menu[] = {"buzzer", "play tone", "play freq","player init", "play ringtone", "off"};
static char* clock_menu[] = {"clock", "show", "set"};
static char** menu[] = {settings_menu, lcd_menu, ble_menu, buzzer_menu, clock_menu};
// TODO: change to defines
static char menu_count = 5;
static char submenu_count[] = {6, 5, 2, 6, 3};
static char menu_level;
static char menu_id;
static char submenu_id;
static char on_off;
static char menu_action;
void print_menu();
void menu_action_on_off(char* name, char* shell_on, char* shell_off)
{
lcd_print(name);
lcd_set_cursor(1,0);
if(on_off == 1)
lcd_print(" ~on off ");
else
lcd_print(" on ~off ");
if(menu_action == 1)
{
menu_action = 0;
menu_level--;
if(on_off == 1)
shell_input(shell_on, strlen(shell_on));
else
shell_input(shell_off, strlen(shell_off));
print_menu();
}
}
// TODO: change name to something more adequate, cleanup, move common lines to functions...
void print_menu()
{
lcd_clear();
if (menu_level == LEVEL_ROOT)
{
submenu_id = 0;
if(menu_id+1 == menu_count)
{
lcd_set_cursor(0,1);
lcd_print(menu[menu_id-1][submenu_id]);
lcd_set_cursor(1,0);
lcd_print("~");
lcd_print(menu[menu_id][submenu_id]);
}
else
{
lcd_print("~");
lcd_print(menu[menu_id][submenu_id]);
lcd_set_cursor(1,1);
lcd_print(menu[menu_id+1][submenu_id]);
}
}
else if (menu_level == LEVEL_SUBMENU)
{
if(submenu_id+1 == submenu_count[menu_id])
{
lcd_set_cursor(0,1);
lcd_print(menu[menu_id][submenu_id-1]);
lcd_set_cursor(1,0);
lcd_print("~");
lcd_print(menu[menu_id][submenu_id]);
}
else
{
submenu_id == 0 ? lcd_print("^") : lcd_print("~");
lcd_print(menu[menu_id][submenu_id]);
lcd_set_cursor(1,1);
lcd_print(menu[menu_id][submenu_id+1]);
}
}
else
{
if(menu_id == MENU_SETTINGS)
{
if (submenu_id == SETTINGS_WRITE)
{
}
else if (submenu_id == SETTINGS_SET_RIMEADDR)
{
}
else if (submenu_id == SETTINGS_UART1_SHELL)
{
menu_action_on_off("uart1 shell:","u1 -s 1","u1 -s 0");
}
else if (submenu_id == SETTINGS_UART2_SHELL)
{
menu_action_on_off("uart2 shell:","u2 -s 1","u2 -s 0");
}
else if (submenu_id == SETTINGS_UART2_LOG)
{
menu_action_on_off("uart1 shell:","u2 -l 1","u2 -l 0");
}
}
else if (menu_id == MENU_LCD)
{
if (submenu_id == LCD_BACKLIGHT)
{
menu_action_on_off("backlight:","lcd -l 1","lcd -l 0");
}
else if (submenu_id == LCD_CLEAR)
{
lcd_print("press again to ");
lcd_set_cursor(1,0);
lcd_print("clear");
if(menu_action == 1)
{
menu_action = 0;
menu_level--;
shell_input("lcd -C", 6);
}
}
else if (submenu_id == LCD_CURSOR)
{
menu_action_on_off("cursor:","lcd -c 1","lcd -c 0");
}
else if (submenu_id == LCD_CURSOR_BLINK)
{
menu_action_on_off("cursor blink:","lcd -b 1","lcd -b 0");
}
}
else if (menu_id == MENU_BLE)
{
if (submenu_id == BLE_RESET)
{
}
}
else if (menu_id == MENU_BUZZER)
{
if (submenu_id == BUZZER_PLAY_TONE)
{
}
else if (submenu_id == BUZZER_PLAY_FREQ)
{
printf("test\n\r");
char buf[6];
lcd_print("freq:");
lcd_set_cursor(1,0);
sprintf(buf,"%d",rotary_counter);
printf("counter: %d, string: %s\n\r",rotary_counter, buf);
lcd_print(buf);
pwm_init(TMR1,rotary_counter,32768);
}
else if (submenu_id == BUZZER_RINGTONE_INIT)
{
lcd_print("press again to ");
lcd_set_cursor(1,0);
lcd_print("initialize.");
if(menu_action == 1)
{
menu_action = 0;
menu_level--;
shell_input("buzz -R", 7);
}
}
else if (submenu_id == BUZZER_RINGTONE_PLAY)
{
lcd_print("random ringtone:");
lcd_set_cursor(1,0);
if(on_off == 1)
lcd_print(" ~play cancel ");
else
lcd_print(" play ~cancel ");
if(menu_action == 1)
{
menu_action = 0;
menu_level--;
if(on_off == 1)
{
#define RINGTONES_COUNT 10403
static char cmd[18];
int random = (random_rand() % RINGTONES_COUNT) + 1;
//choose random one
sprintf(cmd, "buzz -r %d.txt", random);
//printf(cmd);
shell_input(cmd, strlen(cmd));
}
print_menu();
}
}
else if (submenu_id == BUZZER_OFF)
{
}
}
else if (menu_id == MENU_CLOCK)
{
if (submenu_id == CLOCK_SHOW)
{
if(menu_action == 1)
{
menu_action = 0;
if(on_off == 0)
{
shell_input("clock on", 8);
on_off++;
}
else
{
menu_level--;
shell_input("clock off", 9);
}
}
}
else if (submenu_id == CLOCK_SET)
{
}
}
}
}
void menu_select()
{
if(menu_level != LEVEL_ROOT && submenu_id == 0)
{
if (menu_level > 0)
menu_level--;
}
else
{
if(menu_level < LEVEL_ACTION)
{
menu_level++;
on_off = 0;
}
else
menu_action = 1;
}
// if (menu_level == LEVEL_ACTION)
// reset_rotary_counter();
}
void menu_increase()
{
if (menu_level == LEVEL_ROOT)
{
if(menu_id < menu_count-1)
menu_id++;
}
else if (menu_level == LEVEL_SUBMENU)
{
//printf("submenu_count: %d\n\r", submenu_count[menu_id]);
if(submenu_id < submenu_count[menu_id]-1)
submenu_id++;
}
else if(menu_level == LEVEL_ACTION)
{
on_off = 0;
}
//printf("menu_level: %d\n\rmenu_id: %d\n\rsubmenu_id: %d\n\r", menu_level, menu_id, submenu_id);
}
void menu_decrease()
{
if (menu_level == LEVEL_ROOT)
{
if (menu_id > 0)
menu_id--;
}
else if (menu_level == LEVEL_SUBMENU)
{
if (submenu_id > 0)
submenu_id--;
}
else if(menu_level == LEVEL_ACTION)
{
on_off = 1;
}
//printf("menu_id: %d\n\rsubmenu_id: %d\n\r", menu_id, submenu_id);
}
PROCESS(menu_process, "Menu Process");
PROCESS_THREAD(menu_process, ev, data)
{
PROCESS_BEGIN();
menu_level = LEVEL_ROOT;
menu_id = 0;
submenu_id = 0;
on_off = 1;
menu_action = 0;
SENSORS_ACTIVATE(button_sensor);
while(1)
{
PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
//printf("button pressed\n\r");
menu_select();
print_menu();
}
PROCESS_END();
}
void menu_init()
{
process_start(&menu_process, NULL);
}