Fix FS#10486 - "fuze screen not drawn correctly on backlight turn-on if playlist...
[kugel-rb/myfork.git] / apps / plugins / matrix.c
blob12ec4e9093915775fb850e519dc0c73fe75fb0d2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 1999 Chris Allegretta
11 * Copyright (C) 2005 Alastair S - ported to podzilla
12 * Copyright (C) 2005 Jonas Häggqvist - ported to rockbox
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
26 * TODO:
27 * - The font is a bit large, create smaller one
28 * - For colour/greyscale displays, the font from the xscreensaver xmatrix
29 * should be converted and used
30 * BUGS:
31 * - The animation "dies" after a few seconds, not sure why. Works in sim.
32 * Symtom Fixed Oct 2007 GRaTT (Gerritt Gonzales)
36 #include "plugin.h"
37 PLUGIN_HEADER
39 /* Images */
40 #define MAXCHARS 27 - 1
41 extern const fb_data matrix_bold[];
42 extern const fb_data matrix_normal[];
43 #define COL_W 14
44 #define COL_H 15
46 #define COLS LCD_WIDTH/COL_W
47 #define ROWS LCD_HEIGHT/COL_H
49 #define LEFTMARGIN (LCD_WIDTH-(COLS*COL_W))/2
50 #define TOPMARGIN (LCD_HEIGHT-(ROWS*COL_H))/2
52 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
53 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
54 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
55 #define MATRIX_EXIT BUTTON_MENU
56 #define MATRIX_SLEEP_MORE BUTTON_SCROLL_BACK|BUTTON_REPEAT
57 #define MATRIX_SLEEP_LESS BUTTON_SCROLL_FWD|BUTTON_REPEAT
58 #define MATRIX_PAUSE BUTTON_PLAY
60 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
61 (CONFIG_KEYPAD == IRIVER_H300_PAD)
62 #define MATRIX_EXIT BUTTON_OFF
63 #define MATRIX_SLEEP_MORE BUTTON_UP
64 #define MATRIX_SLEEP_LESS BUTTON_DOWN
65 #define MATRIX_PAUSE BUTTON_SELECT
67 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
68 #define MATRIX_EXIT BUTTON_POWER
69 #define MATRIX_SLEEP_MORE BUTTON_SCROLL_UP|BUTTON_REPEAT
70 #define MATRIX_SLEEP_LESS BUTTON_SCROLL_DOWN|BUTTON_REPEAT
71 #define MATRIX_PAUSE BUTTON_PLAY
73 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
74 #define MATRIX_EXIT BUTTON_POWER
75 #define MATRIX_SLEEP_MORE BUTTON_UP
76 #define MATRIX_SLEEP_LESS BUTTON_DOWN
77 #define MATRIX_PAUSE BUTTON_PLAY
79 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
80 #define MATRIX_EXIT BUTTON_POWER
81 #define MATRIX_SLEEP_MORE BUTTON_UP
82 #define MATRIX_SLEEP_LESS BUTTON_DOWN
83 #define MATRIX_PAUSE BUTTON_SELECT
85 #elif CONFIG_KEYPAD == SANSA_E200_PAD
86 #define MATRIX_EXIT BUTTON_POWER
87 #define MATRIX_SLEEP_MORE BUTTON_SCROLL_BACK|BUTTON_REPEAT
88 #define MATRIX_SLEEP_LESS BUTTON_SCROLL_FWD|BUTTON_REPEAT
89 #define MATRIX_PAUSE BUTTON_SELECT
91 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
92 #define MATRIX_EXIT (BUTTON_HOME|BUTTON_REPEAT)
93 #define MATRIX_SLEEP_MORE BUTTON_SCROLL_BACK|BUTTON_REPEAT
94 #define MATRIX_SLEEP_LESS BUTTON_SCROLL_FWD|BUTTON_REPEAT
95 #define MATRIX_PAUSE BUTTON_SELECT
97 #elif CONFIG_KEYPAD == SANSA_C200_PAD
98 #define MATRIX_EXIT BUTTON_POWER
99 #define MATRIX_SLEEP_MORE BUTTON_UP
100 #define MATRIX_SLEEP_LESS BUTTON_DOWN
101 #define MATRIX_PAUSE BUTTON_SELECT
103 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
104 #define MATRIX_EXIT BUTTON_BACK
105 #define MATRIX_SLEEP_MORE BUTTON_UP
106 #define MATRIX_SLEEP_LESS BUTTON_DOWN
107 #define MATRIX_PAUSE BUTTON_SELECT
109 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
110 #define MATRIX_EXIT BUTTON_RC_REC
111 #define MATRIX_SLEEP_MORE BUTTON_RC_VOL_UP
112 #define MATRIX_SLEEP_LESS BUTTON_RC_VOL_DOWN
113 #define MATRIX_PAUSE BUTTON_RC_PLAY
115 #elif (CONFIG_KEYPAD == COWOND2_PAD)
116 #define MATRIX_EXIT BUTTON_POWER
118 #elif CONFIG_KEYPAD == IAUDIO67_PAD
119 #define MATRIX_EXIT BUTTON_POWER
120 #define MATRIX_SLEEP_MORE BUTTON_VOLUP
121 #define MATRIX_SLEEP_LESS BUTTON_VOLDOWN
122 #define MATRIX_PAUSE BUTTON_PLAY
124 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
125 #define MATRIX_EXIT BUTTON_BACK
126 #define MATRIX_SLEEP_MORE BUTTON_UP
127 #define MATRIX_SLEEP_LESS BUTTON_DOWN
128 #define MATRIX_PAUSE BUTTON_PLAY
130 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
131 #define MATRIX_EXIT BUTTON_POWER
132 #define MATRIX_SLEEP_MORE BUTTON_UP
133 #define MATRIX_SLEEP_LESS BUTTON_DOWN
134 #define MATRIX_PAUSE BUTTON_SELECT
136 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || CONFIG_KEYPAD == MROBE500_PAD
137 #define MATRIX_EXIT BUTTON_POWER
139 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
140 #define MATRIX_EXIT BUTTON_REC
141 #define MATRIX_SLEEP_MORE BUTTON_UP
142 #define MATRIX_SLEEP_LESS BUTTON_DOWN
143 #define MATRIX_PAUSE BUTTON_PLAY
145 #else
146 #error Unsupported keypad
147 #endif
149 #ifdef HAVE_TOUCHSCREEN
150 #ifndef MATRIX_EXIT
151 #define MATRIX_EXIT BUTTON_TOPLEFT
152 #endif
153 #ifndef MATRIX_SLEEP_MORE
154 #define MATRIX_SLEEP_MORE BUTTON_MIDRIGHT
155 #endif
156 #ifndef MATRIX_SLEEP_LESS
157 #define MATRIX_SLEEP_LESS BUTTON_MIDLEFT
158 #endif
159 #ifndef MATRIX_PAUSE
160 #define MATRIX_PAUSE BUTTON_CENTER
161 #endif
162 #endif
164 #define SLEEP HZ/50
166 /* Each position is of this type */
167 typedef struct cmatrix {
168 int val;
169 int bold;
170 } cmatrix;
172 /* The matrix - who'd have guessed it was just a few hundred bytes? */
173 static cmatrix matrix[ROWS][COLS];
174 static int length[COLS];
175 static int spaces[COLS];
176 static int updates[COLS];
178 static void matrix_init(void) {
179 int i,j;
181 /* Seed rand */
182 rb->srand(*rb->current_tick);
184 /* Make the matrix */
185 for (i = 0; i <= ROWS; i++) {
186 for (j = 0; j <= COLS - 1; j++ ) {
187 matrix[i][j].val = -1;
188 matrix[i][j].bold = 0;
192 for (j = 0; j <= COLS - 1; j++) {
193 /* Set up spaces[] array of how many spaces to skip */
194 spaces[j] = rb->rand() % ROWS + 1;
196 /* And length of the stream */
197 length[j] = rb->rand() % (ROWS - 3) + 3;
199 /* Sentinel value for creation of new objects */
200 matrix[1][j].val = 129;
202 /* And set updates[] array for update speed. */
203 updates[j] = rb->rand() % 3 + 1;
207 static void matrix_blit_char(const int row, const int col, int cha)
209 if (cha == 129 || cha == 2 || cha > MAXCHARS)
210 cha = 0;
212 if (matrix[row][col].bold == 1) {
213 rb->lcd_bitmap_part(matrix_bold, cha*COL_W, 0, 392,
214 col*COL_W + LEFTMARGIN, row*COL_H + TOPMARGIN, COL_W, COL_H);
216 else {
217 rb->lcd_bitmap_part(matrix_normal, cha*COL_W, 0, 392,
218 col*COL_W + LEFTMARGIN, row*COL_H + TOPMARGIN, COL_W, COL_H);
222 static void matrix_loop(void)
224 int i, j = 0, y, z, firstcoldone = 0;
225 static int count = 0;
226 int randomness = 6;
228 count++;
229 if (count > 4)
230 count = 1;
232 for (j = 0; j <= COLS - 1; j++) {
233 if (count > updates[j]) {
234 /* New style scrolling */
235 if (matrix[0][j].val == -1 && matrix[1][j].val == 129
236 && spaces[j] > 0) {
237 matrix[0][j].val = -1;
238 spaces[j]--;
239 } else if (matrix[0][j].val == -1 && matrix[1][j].val == 129){
240 length[j] = rb->rand() % (ROWS - 3) + 3;
241 matrix[0][j].val = rb->rand() % (MAXCHARS-1) + 1;
242 if (rb->rand() % 2 == 1)
243 matrix[0][j].bold = 2;
244 spaces[j] = rb->rand() % ROWS + 1;
246 i = 0;
247 y = 0;
248 firstcoldone = 0;
249 while (i <= ROWS) {
250 /* Skip over spaces */
251 /* this is whear the characters were disappearing */
253 while (i <= ROWS && (matrix[i][j].val == 129 ||
254 matrix[i][j].val == -1))
255 i++;
257 /* A little more random now for spaces */
258 if (rb->rand() % randomness == 1){
259 while (i <= ROWS && (matrix[i][j].val == 129 ||
260 matrix[i][j].val == -1)){
261 i++;
262 randomness--;
263 if(randomness <=1)
264 randomness = 6;}
265 }else{
266 randomness++;
267 if(randomness >6)
268 randomness = 6;
272 if (i > ROWS)
273 break;
275 /* Go to the head of this collumn */
276 z = i;
277 y = 0;
278 while (i <= ROWS && (matrix[i][j].val != 129 &&
279 matrix[i][j].val != -1)) {
280 i++;
281 y++;
284 if (i > ROWS) {
285 matrix[z][j].val = 129;
286 matrix[ROWS - 1][j].bold = 1;
287 matrix_blit_char(z - 1, j, matrix[z][j].val);
288 continue;
291 matrix[i][j].val = rb->rand() % (MAXCHARS-1) + 1;
293 if (matrix[i - 1][j].bold == 2) {
294 matrix[i - 1][j].bold = 1;
295 matrix[i][j].bold = 2;
298 /* If we're at the top of the collumn and it's reached its
299 * full length (about to start moving down), we do this
300 * to get it moving. This is also how we keep segments
301 * not already growing from growing accidentally =>
303 if (y > length[j] || firstcoldone) {
304 matrix[z][j].val = 129;
305 matrix[0][j].val = -1;
307 firstcoldone = 1;
308 i++;
310 for (i = 1; i <= ROWS; i++) {
311 if (matrix[i][j].val == 0 || matrix[i][j].bold == 2) {
312 if (matrix[i][j].val == 0)
313 matrix_blit_char(i - 1, j, 20);
314 else
315 matrix_blit_char(i - 1, j, matrix[i][j].val);
316 } else {
317 if (matrix[i][j].val == 1)
318 matrix_blit_char(i - 1, j, 2);
319 else if (matrix[i][j].val == -1)
320 matrix_blit_char(i - 1, j, 129);
321 else
322 matrix_blit_char(i - 1, j, matrix[i][j].val);
329 enum plugin_status plugin_start(const void* parameter) {
330 int button;
331 int sleep = SLEEP;
332 bool frozen = false;
334 (void)parameter;
336 rb->lcd_set_background(LCD_BLACK);
337 rb->lcd_set_backdrop(NULL);
338 rb->lcd_clear_display();
339 matrix_init();
341 while (1) {
342 if (!frozen) {
343 matrix_loop();
344 rb->lcd_update();
345 rb->sleep(sleep);
347 button = rb->button_get(frozen);
348 switch(button) {
349 case MATRIX_PAUSE:
350 frozen = !frozen;
351 break;
352 case MATRIX_EXIT:
353 return PLUGIN_OK;
354 break;
355 case MATRIX_SLEEP_MORE:
356 /* Sleep longer */
357 sleep += SLEEP;
358 break;
359 case MATRIX_SLEEP_LESS:
360 /* Sleep less */
361 sleep -= SLEEP;
362 if (sleep < 0) sleep = 0;
363 break;
364 default:
365 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
366 return PLUGIN_USB_CONNECTED;
368 break;
371 return PLUGIN_OK;