blob: 027f1fcbeac1b2d82df1ee62b1630a91f7d5b283 [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 ****************************************************************************/
Björn Stenberg2ac05722002-05-26 17:03:17 +000019#include <stdbool.h>
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000020#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
Björn Stenberg2ac05722002-05-26 17:03:17 +000026struct menu {
27 int menu_top;
28 int menu_bottom;
29 int cursor;
30 struct menu_items* items;
31 int itemcount;
32};
33
34#define MAX_MENUS 4
35
36static struct menu menus[MAX_MENUS];
37static bool inuse[MAX_MENUS] = { false };
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000038
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000039/*
40 * Move the cursor to a particular id,
41 * target: where you want it to be
42 */
Björn Stenberg2ac05722002-05-26 17:03:17 +000043static void put_cursor(int m, int target)
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000044{
Björn Stenberg2ac05722002-05-26 17:03:17 +000045 lcd_puts(0, menus[m].cursor, " ");
46 menus[m].cursor = target;
47 lcd_puts(0, menus[m].cursor, "-");
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000048}
49
Björn Stenberg2ac05722002-05-26 17:03:17 +000050int menu_init(struct menu_items* mitems, int count)
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000051{
Björn Stenberg2ac05722002-05-26 17:03:17 +000052 int i;
53
54 for ( i=0; i<MAX_MENUS; i++ ) {
55 if ( !inuse[i] ) {
56 inuse[i] = true;
57 break;
58 }
59 }
60 if ( i == MAX_MENUS ) {
61 DEBUGF("Out of menus!\n");
62 return -1;
63 }
64 menus[i].items = mitems;
65 menus[i].itemcount = count;
66 menus[i].menu_top = 0;
67 menus[i].menu_bottom = count-1;
68 menus[i].cursor = 0;
69
70 return i;
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000071}
72
Björn Stenberg2ac05722002-05-26 17:03:17 +000073void menu_exit(int m)
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000074{
Björn Stenberg2ac05722002-05-26 17:03:17 +000075 inuse[m] = false;
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000076}
77
Björn Stenberg2ac05722002-05-26 17:03:17 +000078static void menu_draw(int m)
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000079{
80 int i = 0;
81
Björn Stenbergc42644d2002-05-21 15:04:23 +000082 lcd_clear_display();
83#ifdef HAVE_LCD_BITMAP
84 lcd_setmargins(0,0);
85 lcd_setfont(0);
86#endif
Björn Stenberg2ac05722002-05-26 17:03:17 +000087 for (i = 0; i < menus[m].itemcount; i++) {
88 lcd_puts(1, i, menus[m].items[i].desc);
89 if (i < menus[m].menu_top)
90 menus[m].menu_top = i;
91 if (i > menus[m].menu_bottom)
92 menus[m].menu_bottom = i;
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000093 }
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000094
Björn Stenberg2ac05722002-05-26 17:03:17 +000095 lcd_puts(0, menus[m].cursor, "-");
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000096 lcd_update();
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000097}
98
Björn Stenberg2ac05722002-05-26 17:03:17 +000099void menu_run(int m)
Robert Hak7ec9aa32002-05-18 11:41:37 +0000100{
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000101 int key;
Robert Hakdfba2102002-05-21 08:47:34 +0000102
Björn Stenberg2ac05722002-05-26 17:03:17 +0000103 menu_draw(m);
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000104
105 while(1) {
106 key = button_get();
107 if(!key) {
108 sleep(1);
109 continue;
110 }
111
112 switch(key) {
113#ifdef HAVE_RECORDER_KEYPAD
114 case BUTTON_UP:
Robert Hak7ec9aa32002-05-18 11:41:37 +0000115#else
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000116 case BUTTON_LEFT:
Robert Hak7ec9aa32002-05-18 11:41:37 +0000117#endif
Björn Stenberg2ac05722002-05-26 17:03:17 +0000118 if (menus[m].cursor == menus[m].menu_top) {
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000119 /* wrap around to menu bottom */
Björn Stenberg2ac05722002-05-26 17:03:17 +0000120 put_cursor(m, menus[m].menu_bottom);
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000121 } else {
122 /* move up */
Björn Stenberg2ac05722002-05-26 17:03:17 +0000123 put_cursor(m, menus[m].cursor-1);
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000124 }
125 break;
Robert Hakdfba2102002-05-21 08:47:34 +0000126
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000127#ifdef HAVE_RECORDER_KEYPAD
128 case BUTTON_DOWN:
129#else
130 case BUTTON_RIGHT:
131#endif
Björn Stenberg2ac05722002-05-26 17:03:17 +0000132 if (menus[m].cursor == menus[m].menu_bottom) {
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000133 /* wrap around to menu top */
Björn Stenberg2ac05722002-05-26 17:03:17 +0000134 put_cursor(m, menus[m].menu_top);
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000135 } else {
136 /* move down */
Björn Stenberg2ac05722002-05-26 17:03:17 +0000137 put_cursor(m, menus[m].cursor+1);
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000138 }
139 break;
140
141#ifdef HAVE_RECORDER_KEYPAD
142 case BUTTON_RIGHT:
143#endif
144 case BUTTON_PLAY:
145 /* Erase current display state */
146 lcd_clear_display();
147
Björn Stenberg2ac05722002-05-26 17:03:17 +0000148 menus[m].items[menus[m].cursor].function();
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000149
150 /* Return to previous display state */
Björn Stenberg2ac05722002-05-26 17:03:17 +0000151 menu_draw(m);
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000152 break;
153
154#ifdef HAVE_RECORDER_KEYPAD
155 case BUTTON_LEFT:
156#else
157 case BUTTON_STOP:
158#endif
159 return;
160
161 default:
162 break;
163 }
164
165 lcd_update();
166 }
Robert Hak7ec9aa32002-05-18 11:41:37 +0000167}