diff options
Diffstat (limited to 'firmware/scroll_engine.c')
-rw-r--r-- | firmware/scroll_engine.c | 302 |
1 files changed, 302 insertions, 0 deletions
diff --git a/firmware/scroll_engine.c b/firmware/scroll_engine.c new file mode 100644 index 0000000000..d4a2e174cc --- /dev/null +++ b/firmware/scroll_engine.c | |||
@@ -0,0 +1,302 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2007 by Michael Sevakis | ||
11 | * | ||
12 | * LCD scrolling driver and scheduler | ||
13 | * | ||
14 | * Much collected and combined from the various Rockbox LCD drivers. | ||
15 | * | ||
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. | ||
18 | * | ||
19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
20 | * KIND, either express or implied. | ||
21 | * | ||
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" | ||
34 | |||
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 | ||
39 | }; | ||
40 | |||
41 | static void scroll_thread(void); | ||
42 | static char scroll_stack[DEFAULT_STACK_SIZE*3]; | ||
43 | static const char scroll_name[] = "scroll"; | ||
44 | |||
45 | struct scrollinfo lcd_scroll[LCD_SCROLLABLE_LINES]; | ||
46 | |||
47 | #ifdef HAVE_REMOTE_LCD | ||
48 | struct scrollinfo lcd_remote_scroll[LCD_REMOTE_SCROLLABLE_LINES]; | ||
49 | struct event_queue scroll_queue; | ||
50 | #endif | ||
51 | |||
52 | struct scroll_screen_info lcd_scroll_info = | ||
53 | { | ||
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 | ||
66 | }; | ||
67 | |||
68 | #ifdef HAVE_REMOTE_LCD | ||
69 | struct scroll_screen_info lcd_remote_scroll_info = | ||
70 | { | ||
71 | .scroll = lcd_remote_scroll, | ||
72 | .lines = 0, | ||
73 | .ticks = 12, | ||
74 | .delay = HZ/2, | ||
75 | .bidir_limit = 50, | ||
76 | .step = 6, | ||
77 | }; | ||
78 | #endif /* HAVE_REMOTE_LCD */ | ||
79 | |||
80 | void lcd_stop_scroll(void) | ||
81 | { | ||
82 | lcd_scroll_info.lines = 0; | ||
83 | } | ||
84 | |||
85 | void lcd_scroll_speed(int speed) | ||
86 | { | ||
87 | lcd_scroll_info.ticks = scroll_tick_table[speed]; | ||
88 | } | ||
89 | |||
90 | #if defined(HAVE_LCD_BITMAP) | ||
91 | /* Reverse the invert setting of the scrolling line (if any) at given char | ||
92 | position. Setting will go into affect next time line scrolls. */ | ||
93 | void lcd_invertscroll(int x, int y) | ||
94 | { | ||
95 | struct scrollinfo *s; | ||
96 | |||
97 | (void)x; | ||
98 | |||
99 | if((unsigned)y>=LCD_SCROLLABLE_LINES) return; | ||
100 | |||
101 | s = &lcd_scroll_info.scroll[y]; | ||
102 | s->invert = !s->invert; | ||
103 | } | ||
104 | |||
105 | void lcd_scroll_step(int step) | ||
106 | { | ||
107 | lcd_scroll_info.step = step; | ||
108 | } | ||
109 | #endif | ||
110 | |||
111 | void lcd_scroll_delay(int ms) | ||
112 | { | ||
113 | lcd_scroll_info.delay = ms / (HZ / 10); | ||
114 | } | ||
115 | |||
116 | void lcd_bidir_scroll(int percent) | ||
117 | { | ||
118 | lcd_scroll_info.bidir_limit = percent; | ||
119 | } | ||
120 | |||
121 | #ifdef HAVE_LCD_CHARCELLS | ||
122 | void lcd_jump_scroll(int mode) /* 0=off, 1=once, ..., JUMP_SCROLL_ALWAYS */ | ||
123 | { | ||
124 | lcd_scroll_info.jump_scroll = mode; | ||
125 | } | ||
126 | |||
127 | void lcd_jump_scroll_delay(int ms) | ||
128 | { | ||
129 | lcd_scroll_info.jump_scroll_delay = ms / (HZ / 10); | ||
130 | } | ||
131 | #endif | ||
132 | |||
133 | #ifdef HAVE_REMOTE_LCD | ||
134 | /* Reverse the invert setting of the scrolling line (if any) at given char | ||
135 | position. Setting will go into affect next time line scrolls. */ | ||
136 | void lcd_remote_invertscroll(int x, int y) | ||
137 | { | ||
138 | struct scrollinfo *s; | ||
139 | |||
140 | (void)x; | ||
141 | |||
142 | if((unsigned)y>=LCD_REMOTE_SCROLLABLE_LINES) return; | ||
143 | |||
144 | s = &lcd_remote_scroll_info.scroll[y]; | ||
145 | s->invert = !s->invert; | ||
146 | } | ||
147 | |||
148 | void lcd_remote_stop_scroll(void) | ||
149 | { | ||
150 | lcd_remote_scroll_info.lines = 0; | ||
151 | } | ||
152 | |||
153 | void lcd_remote_scroll_speed(int speed) | ||
154 | { | ||
155 | lcd_remote_scroll_info.ticks = scroll_tick_table[speed]; | ||
156 | } | ||
157 | |||
158 | void lcd_remote_scroll_step(int step) | ||
159 | { | ||
160 | lcd_remote_scroll_info.step = step; | ||
161 | } | ||
162 | |||
163 | void lcd_remote_scroll_delay(int ms) | ||
164 | { | ||
165 | lcd_remote_scroll_info.delay = ms / (HZ / 10); | ||
166 | } | ||
167 | |||
168 | void lcd_remote_bidir_scroll(int percent) | ||
169 | { | ||
170 | lcd_remote_scroll_info.bidir_limit = percent; | ||
171 | } | ||
172 | |||
173 | static void sync_display_ticks(void) | ||
174 | { | ||
175 | lcd_scroll_info.last_scroll = | ||
176 | lcd_remote_scroll_info.last_scroll = current_tick; | ||
177 | } | ||
178 | |||
179 | static bool scroll_process_message(int delay) | ||
180 | { | ||
181 | struct event ev; | ||
182 | |||
183 | do | ||
184 | { | ||
185 | long tick = current_tick; | ||
186 | queue_wait_w_tmo(&scroll_queue, &ev, delay); | ||
187 | |||
188 | switch (ev.id) | ||
189 | { | ||
190 | case SYS_TIMEOUT: | ||
191 | return false; | ||
192 | case SYS_USB_CONNECTED: | ||
193 | usb_acknowledge(SYS_USB_CONNECTED_ACK); | ||
194 | usb_wait_for_disconnect(&scroll_queue); | ||
195 | sync_display_ticks(); | ||
196 | return true; | ||
197 | #ifndef SIMULATOR | ||
198 | case SYS_REMOTE_PLUGGED: | ||
199 | if (!remote_initialized) | ||
200 | sync_display_ticks(); | ||
201 | #endif | ||
202 | } | ||
203 | |||
204 | delay -= current_tick - tick; | ||
205 | } | ||
206 | while (delay > 0); | ||
207 | |||
208 | return false; | ||
209 | } | ||
210 | #endif /* HAVE_REMOTE_LCD */ | ||
211 | |||
212 | static void scroll_thread(void) __attribute__((noreturn)); | ||
213 | #ifdef HAVE_REMOTE_LCD | ||
214 | |||
215 | static void scroll_thread(void) | ||
216 | { | ||
217 | enum | ||
218 | { | ||
219 | SCROLL_LCD = 0x1, | ||
220 | SCROLL_LCD_REMOTE = 0x2, | ||
221 | }; | ||
222 | |||
223 | sync_display_ticks(); | ||
224 | |||
225 | while ( 1 ) | ||
226 | { | ||
227 | long delay; | ||
228 | int scroll; | ||
229 | long tick_lcd, tick_remote; | ||
230 | |||
231 | tick_lcd = lcd_scroll_info.last_scroll + lcd_scroll_info.ticks; | ||
232 | delay = current_tick; | ||
233 | |||
234 | if ( | ||
235 | #ifndef SIMULATOR | ||
236 | !remote_initialized || | ||
237 | #endif | ||
238 | (tick_remote = lcd_remote_scroll_info.last_scroll + | ||
239 | lcd_remote_scroll_info.ticks, | ||
240 | TIME_BEFORE(tick_lcd, tick_remote))) | ||
241 | { | ||
242 | scroll = SCROLL_LCD; | ||
243 | delay = tick_lcd - delay; | ||
244 | } | ||
245 | /* TIME_BEFORE(tick_remote, tick_lcd) */ | ||
246 | else if (tick_lcd != tick_remote) | ||
247 | { | ||
248 | scroll = SCROLL_LCD_REMOTE; | ||
249 | delay = tick_remote - delay; | ||
250 | } | ||
251 | else | ||
252 | { | ||
253 | scroll = SCROLL_LCD | SCROLL_LCD_REMOTE; | ||
254 | delay = tick_lcd - delay; | ||
255 | } | ||
256 | |||
257 | if (scroll_process_message(delay)) | ||
258 | continue; | ||
259 | |||
260 | if (scroll & SCROLL_LCD) | ||
261 | { | ||
262 | #ifdef HAVE_LCD_ENABLE | ||
263 | if (lcd_enabled()) | ||
264 | #endif | ||
265 | lcd_scroll_fn(); | ||
266 | lcd_scroll_info.last_scroll = current_tick; | ||
267 | } | ||
268 | |||
269 | if (scroll == (SCROLL_LCD | SCROLL_LCD_REMOTE)) | ||
270 | yield(); | ||
271 | |||
272 | if (scroll & SCROLL_LCD_REMOTE) | ||
273 | { | ||
274 | lcd_remote_scroll_fn(); | ||
275 | lcd_remote_scroll_info.last_scroll = current_tick; | ||
276 | } | ||
277 | } | ||
278 | } | ||
279 | #else | ||
280 | static void scroll_thread(void) | ||
281 | { | ||
282 | while (1) | ||
283 | { | ||
284 | sleep(lcd_scroll_info.ticks); | ||
285 | #ifdef HAVE_LCD_ENABLE | ||
286 | if (lcd_enabled()) | ||
287 | #endif | ||
288 | lcd_scroll_fn(); | ||
289 | } | ||
290 | } | ||
291 | #endif /* HAVE_REMOTE_LCD */ | ||
292 | |||
293 | void scroll_init(void) | ||
294 | { | ||
295 | #ifdef HAVE_REMOTE_LCD | ||
296 | queue_init(&scroll_queue, true); | ||
297 | #endif | ||
298 | create_thread(scroll_thread, scroll_stack, | ||
299 | sizeof(scroll_stack), scroll_name | ||
300 | IF_PRIO(, PRIORITY_USER_INTERFACE) | ||
301 | IF_COP(, CPU, false)); | ||
302 | } | ||