No bufclosing of handles after track change
[Rockbox.git] / firmware / scroll_engine.c
blob599e7f58b52d22321a44ea5ea3951b9303f1ff9b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Michael Sevakis
12 * LCD scrolling driver and scheduler
14 * Much collected and combined from the various Rockbox LCD drivers.
16 * All files in this archive are subject to the GNU General Public License.
17 * See the file COPYING in the source tree root for full license agreement.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "config.h"
24 #include "cpu.h"
25 #include "kernel.h"
26 #include "thread.h"
27 #include "usb.h"
28 #include "lcd.h"
29 #include "font.h"
30 #ifdef HAVE_REMOTE_LCD
31 #include "lcd-remote.h"
32 #endif
33 #include "scroll_engine.h"
35 static const char scroll_tick_table[16] = {
36 /* Hz values:
37 1, 1.25, 1.55, 2, 2.5, 3.12, 4, 5, 6.25, 8.33, 10, 12.5, 16.7, 20, 25, 33 */
38 100, 80, 64, 50, 40, 32, 25, 20, 16, 12, 10, 8, 6, 5, 4, 3
41 static void scroll_thread(void);
42 static char scroll_stack[DEFAULT_STACK_SIZE*3];
43 static const char scroll_name[] = "scroll";
45 struct scrollinfo lcd_scroll[LCD_SCROLLABLE_LINES];
47 #ifdef HAVE_REMOTE_LCD
48 struct scrollinfo lcd_remote_scroll[LCD_REMOTE_SCROLLABLE_LINES];
49 struct event_queue scroll_queue;
50 #endif
52 struct scroll_screen_info lcd_scroll_info =
54 .scroll = lcd_scroll,
55 .lines = 0,
56 .ticks = 12,
57 .delay = HZ/2,
58 .bidir_limit = 50,
59 #ifdef HAVE_LCD_BITMAP
60 .step = 6,
61 #endif
62 #ifdef HAVE_LCD_CHARCELLS
63 .jump_scroll_delay = HZ/4,
64 .jump_scroll = 0,
65 #endif
68 #ifdef HAVE_REMOTE_LCD
69 struct scroll_screen_info lcd_remote_scroll_info =
71 .scroll = lcd_remote_scroll,
72 .lines = 0,
73 .ticks = 12,
74 .delay = HZ/2,
75 .bidir_limit = 50,
76 .step = 6,
78 #endif /* HAVE_REMOTE_LCD */
80 void lcd_stop_scroll(void)
82 lcd_scroll_info.lines = 0;
85 void lcd_scroll_speed(int speed)
87 lcd_scroll_info.ticks = scroll_tick_table[speed];
90 #if defined(HAVE_LCD_BITMAP)
91 void lcd_scroll_step(int step)
93 lcd_scroll_info.step = step;
95 #endif
97 void lcd_scroll_delay(int ms)
99 lcd_scroll_info.delay = ms / (HZ / 10);
102 void lcd_bidir_scroll(int percent)
104 lcd_scroll_info.bidir_limit = percent;
107 #ifdef HAVE_LCD_CHARCELLS
108 void lcd_jump_scroll(int mode) /* 0=off, 1=once, ..., JUMP_SCROLL_ALWAYS */
110 lcd_scroll_info.jump_scroll = mode;
113 void lcd_jump_scroll_delay(int ms)
115 lcd_scroll_info.jump_scroll_delay = ms / (HZ / 10);
117 #endif
119 #ifdef HAVE_REMOTE_LCD
120 void lcd_remote_stop_scroll(void)
122 lcd_remote_scroll_info.lines = 0;
125 void lcd_remote_scroll_speed(int speed)
127 lcd_remote_scroll_info.ticks = scroll_tick_table[speed];
130 void lcd_remote_scroll_step(int step)
132 lcd_remote_scroll_info.step = step;
135 void lcd_remote_scroll_delay(int ms)
137 lcd_remote_scroll_info.delay = ms / (HZ / 10);
140 void lcd_remote_bidir_scroll(int percent)
142 lcd_remote_scroll_info.bidir_limit = percent;
145 static void sync_display_ticks(void)
147 lcd_scroll_info.last_scroll =
148 lcd_remote_scroll_info.last_scroll = current_tick;
151 static bool scroll_process_message(int delay)
153 struct queue_event ev;
157 long tick = current_tick;
158 queue_wait_w_tmo(&scroll_queue, &ev, delay);
160 switch (ev.id)
162 case SYS_TIMEOUT:
163 return false;
164 case SYS_USB_CONNECTED:
165 usb_acknowledge(SYS_USB_CONNECTED_ACK);
166 usb_wait_for_disconnect(&scroll_queue);
167 sync_display_ticks();
168 return true;
169 #ifndef SIMULATOR
170 case SYS_REMOTE_PLUGGED:
171 if (!remote_initialized)
172 sync_display_ticks();
173 #endif
176 delay -= current_tick - tick;
178 while (delay > 0);
180 return false;
182 #endif /* HAVE_REMOTE_LCD */
184 static void scroll_thread(void) __attribute__((noreturn));
185 #ifdef HAVE_REMOTE_LCD
187 static void scroll_thread(void)
189 enum
191 SCROLL_LCD = 0x1,
192 SCROLL_LCD_REMOTE = 0x2,
195 sync_display_ticks();
197 while ( 1 )
199 long delay;
200 int scroll;
201 long tick_lcd, tick_remote;
203 tick_lcd = lcd_scroll_info.last_scroll + lcd_scroll_info.ticks;
204 delay = current_tick;
206 if (
207 #ifndef SIMULATOR
208 !remote_initialized ||
209 #endif
210 (tick_remote = lcd_remote_scroll_info.last_scroll +
211 lcd_remote_scroll_info.ticks,
212 TIME_BEFORE(tick_lcd, tick_remote)))
214 scroll = SCROLL_LCD;
215 delay = tick_lcd - delay;
217 /* TIME_BEFORE(tick_remote, tick_lcd) */
218 else if (tick_lcd != tick_remote)
220 scroll = SCROLL_LCD_REMOTE;
221 delay = tick_remote - delay;
223 else
225 scroll = SCROLL_LCD | SCROLL_LCD_REMOTE;
226 delay = tick_lcd - delay;
229 if (scroll_process_message(delay))
230 continue;
232 if (scroll & SCROLL_LCD)
234 #ifdef HAVE_LCD_ENABLE
235 if (lcd_enabled())
236 #endif
237 lcd_scroll_fn();
238 lcd_scroll_info.last_scroll = current_tick;
241 if (scroll == (SCROLL_LCD | SCROLL_LCD_REMOTE))
242 yield();
244 if (scroll & SCROLL_LCD_REMOTE)
246 lcd_remote_scroll_fn();
247 lcd_remote_scroll_info.last_scroll = current_tick;
251 #else
252 static void scroll_thread(void)
254 while (1)
256 sleep(lcd_scroll_info.ticks);
257 #ifdef HAVE_LCD_ENABLE
258 if (lcd_enabled())
259 #endif
260 lcd_scroll_fn();
263 #endif /* HAVE_REMOTE_LCD */
265 void scroll_init(void)
267 #ifdef HAVE_REMOTE_LCD
268 queue_init(&scroll_queue, true);
269 #endif
270 create_thread(scroll_thread, scroll_stack,
271 sizeof(scroll_stack), 0, scroll_name
272 IF_PRIO(, PRIORITY_USER_INTERFACE)
273 IF_COP(, CPU));