blob: aaa0c94ff6ddfbe48b649eb371a253f1f4a1dfe0 [file] [log] [blame]
Daniel Stenberg1c0c8612002-05-17 12:22:24 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Robert E. Hak
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include "lcd.h"
21#include "menu.h"
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000022#include "button.h"
23#include "kernel.h"
24#include "debug.h"
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000025
26/* Global values for menuing */
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000027static int menu_top;
28static int menu_bottom;
29static int cursor;
30static struct menu_items* items;
31static int itemcount;
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000032
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000033/*
34 * Move the cursor to a particular id,
35 * target: where you want it to be
36 */
37void put_cursor(int target)
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000038{
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000039 lcd_puts(0, cursor, " ");
40 cursor = target;
41 lcd_puts(0, cursor, "-");
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000042}
43
44int is_cursor_menu_top(void)
45{
46 return ((cursor == menu_top) ? 1 : 0);
47}
48
49int is_cursor_menu_bottom(void)
50{
51 return ((cursor == menu_bottom) ? 1 : 0);
52}
53
54void put_cursor_menu_top(void)
55{
56 put_cursor(menu_top);
57}
58
59void put_cursor_menu_bottom(void)
60{
61 put_cursor(menu_bottom);
62}
63
64void move_cursor_up(void)
65{
66 put_cursor(cursor-1);
67}
68
69void move_cursor_down(void)
70{
71 put_cursor(cursor+1);
72}
73
74void redraw_cursor(void)
75{
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000076 lcd_puts(0, cursor, "-");
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000077}
78
79/* We call the function pointer related to the current cursor position */
80void execute_menu_item(void)
81{
82 /* call the proper function for this line */
83 items[cursor].function();
84}
85
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000086void menu_init(struct menu_items* mitems, int count)
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000087{
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000088 items = mitems;
89 itemcount = count;
90 menu_top = items[0].id;
91 menu_bottom = count-1;
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000092 cursor = menu_top;
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000093#ifdef HAVE_LCD_BITMAP
94 lcd_setmargins(0,0);
95 lcd_setfont(0);
96#endif
97 lcd_clear_display();
98 lcd_update();
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000099}
100
101void menu_draw(void)
102{
103 int i = 0;
104
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000105 for (i = 0; i < itemcount; i++) {
106 lcd_puts(1, i, items[i].desc);
107 if (i < menu_top)
108 menu_top = i;
109 if (i > menu_bottom)
110 menu_bottom = i;
111 }
Daniel Stenberg1c0c8612002-05-17 12:22:24 +0000112
Daniel Stenberg1c0c8612002-05-17 12:22:24 +0000113 redraw_cursor();
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000114 lcd_update();
Daniel Stenberg1c0c8612002-05-17 12:22:24 +0000115}
116
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000117void menu_run(void)
Robert Hak7ec9aa32002-05-18 11:41:37 +0000118{
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000119 int key;
Robert Hakdfba2102002-05-21 08:47:34 +0000120
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000121 menu_draw();
122 put_cursor_menu_top();
123
124 while(1) {
125 key = button_get();
126 if(!key) {
127 sleep(1);
128 continue;
129 }
130
131 switch(key) {
132#ifdef HAVE_RECORDER_KEYPAD
133 case BUTTON_UP:
Robert Hak7ec9aa32002-05-18 11:41:37 +0000134#else
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000135 case BUTTON_LEFT:
Robert Hak7ec9aa32002-05-18 11:41:37 +0000136#endif
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000137 if(is_cursor_menu_top()){
138 /* wrap around to menu bottom */
139 put_cursor_menu_bottom();
140 } else {
141 /* move up */
142 move_cursor_up();
143 }
144 break;
Robert Hakdfba2102002-05-21 08:47:34 +0000145
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000146#ifdef HAVE_RECORDER_KEYPAD
147 case BUTTON_DOWN:
148#else
149 case BUTTON_RIGHT:
150#endif
151 if(is_cursor_menu_bottom() ){
152 /* wrap around to menu top */
153 put_cursor_menu_top();
154 } else {
155 /* move down */
156 move_cursor_down();
157 }
158 break;
159
160#ifdef HAVE_RECORDER_KEYPAD
161 case BUTTON_RIGHT:
162#endif
163 case BUTTON_PLAY:
164 /* Erase current display state */
165 lcd_clear_display();
166
167 execute_menu_item();
168
169 /* Return to previous display state */
170 lcd_clear_display();
171 menu_draw();
172 break;
173
174#ifdef HAVE_RECORDER_KEYPAD
175 case BUTTON_LEFT:
176#else
177 case BUTTON_STOP:
178#endif
179 return;
180
181 default:
182 break;
183 }
184
185 lcd_update();
186 }
Robert Hak7ec9aa32002-05-18 11:41:37 +0000187}