Another small bookmark.c revision, no functional change, saves bin size
[kugel-rb.git] / apps / plugins / matrix.c
blobe7326ea4f4c7c2175a6dc441ab0734ae490848c8
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 #include "pluginbitmaps/matrix_bold.h"
41 #include "pluginbitmaps/matrix_normal.h"
42 #define MAXCHARS 27 - 1
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 == COWON_D2_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 == PHILIPS_SA9200_PAD
137 #define MATRIX_EXIT BUTTON_POWER
138 #define MATRIX_SLEEP_MORE BUTTON_UP
139 #define MATRIX_SLEEP_LESS BUTTON_DOWN
140 #define MATRIX_PAUSE BUTTON_PLAY
142 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
143 CONFIG_KEYPAD == ONDAVX777_PAD || \
144 CONFIG_KEYPAD == MROBE500_PAD
145 #define MATRIX_EXIT BUTTON_POWER
147 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
148 #define MATRIX_EXIT BUTTON_REC
149 #define MATRIX_SLEEP_MORE BUTTON_UP
150 #define MATRIX_SLEEP_LESS BUTTON_DOWN
151 #define MATRIX_PAUSE BUTTON_PLAY
153 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
154 #define MATRIX_EXIT BUTTON_REC
155 #define MATRIX_SLEEP_MORE BUTTON_PREV
156 #define MATRIX_SLEEP_LESS BUTTON_NEXT
157 #define MATRIX_PAUSE BUTTON_PLAY
159 #else
160 #error Unsupported keypad
161 #endif
163 #ifdef HAVE_TOUCHSCREEN
164 #ifndef MATRIX_EXIT
165 #define MATRIX_EXIT BUTTON_TOPLEFT
166 #endif
167 #ifndef MATRIX_SLEEP_MORE
168 #define MATRIX_SLEEP_MORE BUTTON_MIDRIGHT
169 #endif
170 #ifndef MATRIX_SLEEP_LESS
171 #define MATRIX_SLEEP_LESS BUTTON_MIDLEFT
172 #endif
173 #ifndef MATRIX_PAUSE
174 #define MATRIX_PAUSE BUTTON_CENTER
175 #endif
176 #endif
178 #define SLEEP HZ/50
180 /* Each position is of this type */
181 typedef struct cmatrix {
182 int val;
183 int bold;
184 } cmatrix;
186 /* The matrix - who'd have guessed it was just a few hundred bytes? */
187 static cmatrix matrix[ROWS][COLS];
188 static int length[COLS];
189 static int spaces[COLS];
190 static int updates[COLS];
192 static void matrix_init(void) {
193 int i,j;
195 /* Seed rand */
196 rb->srand(*rb->current_tick);
198 /* Make the matrix */
199 for (i = 0; i <= ROWS; i++) {
200 for (j = 0; j <= COLS - 1; j++ ) {
201 matrix[i][j].val = -1;
202 matrix[i][j].bold = 0;
206 for (j = 0; j <= COLS - 1; j++) {
207 /* Set up spaces[] array of how many spaces to skip */
208 spaces[j] = rb->rand() % ROWS + 1;
210 /* And length of the stream */
211 length[j] = rb->rand() % (ROWS - 3) + 3;
213 /* Sentinel value for creation of new objects */
214 matrix[1][j].val = 129;
216 /* And set updates[] array for update speed. */
217 updates[j] = rb->rand() % 3 + 1;
221 static void matrix_blit_char(const int row, const int col, int cha)
223 if (cha == 129 || cha == 2 || cha > MAXCHARS)
224 cha = 0;
226 if (matrix[row][col].bold == 1) {
227 rb->lcd_bitmap_part(matrix_bold, cha*COL_W, 0,
228 STRIDE( SCREEN_MAIN,
229 BMPWIDTH_matrix_bold, BMPHEIGHT_matrix_bold),
230 col*COL_W + LEFTMARGIN, row*COL_H + TOPMARGIN, COL_W, COL_H);
232 else {
233 rb->lcd_bitmap_part(matrix_normal, cha*COL_W, 0,
234 STRIDE( SCREEN_MAIN,
235 BMPWIDTH_matrix_normal, BMPHEIGHT_matrix_normal),
236 col*COL_W + LEFTMARGIN, row*COL_H + TOPMARGIN, COL_W, COL_H);
240 static void matrix_loop(void)
242 int i, j = 0, y, z, firstcoldone = 0;
243 static int count = 0;
244 int randomness = 6;
246 count++;
247 if (count > 4)
248 count = 1;
250 for (j = 0; j <= COLS - 1; j++) {
251 if (count > updates[j]) {
252 /* New style scrolling */
253 if (matrix[0][j].val == -1 && matrix[1][j].val == 129
254 && spaces[j] > 0) {
255 matrix[0][j].val = -1;
256 spaces[j]--;
257 } else if (matrix[0][j].val == -1 && matrix[1][j].val == 129){
258 length[j] = rb->rand() % (ROWS - 3) + 3;
259 matrix[0][j].val = rb->rand() % (MAXCHARS-1) + 1;
260 if (rb->rand() % 2 == 1)
261 matrix[0][j].bold = 2;
262 spaces[j] = rb->rand() % ROWS + 1;
264 i = 0;
265 y = 0;
266 firstcoldone = 0;
267 while (i <= ROWS) {
268 /* Skip over spaces */
269 /* this is whear the characters were disappearing */
271 while (i <= ROWS && (matrix[i][j].val == 129 ||
272 matrix[i][j].val == -1))
273 i++;
275 /* A little more random now for spaces */
276 if (rb->rand() % randomness == 1){
277 while (i <= ROWS && (matrix[i][j].val == 129 ||
278 matrix[i][j].val == -1)){
279 i++;
280 randomness--;
281 if(randomness <=1)
282 randomness = 6;}
283 }else{
284 randomness++;
285 if(randomness >6)
286 randomness = 6;
290 if (i > ROWS)
291 break;
293 /* Go to the head of this collumn */
294 z = i;
295 y = 0;
296 while (i <= ROWS && (matrix[i][j].val != 129 &&
297 matrix[i][j].val != -1)) {
298 i++;
299 y++;
302 if (i > ROWS) {
303 matrix[z][j].val = 129;
304 matrix[ROWS - 1][j].bold = 1;
305 matrix_blit_char(z - 1, j, matrix[z][j].val);
306 continue;
309 matrix[i][j].val = rb->rand() % (MAXCHARS-1) + 1;
311 if (matrix[i - 1][j].bold == 2) {
312 matrix[i - 1][j].bold = 1;
313 matrix[i][j].bold = 2;
316 /* If we're at the top of the collumn and it's reached its
317 * full length (about to start moving down), we do this
318 * to get it moving. This is also how we keep segments
319 * not already growing from growing accidentally =>
321 if (y > length[j] || firstcoldone) {
322 matrix[z][j].val = 129;
323 matrix[0][j].val = -1;
325 firstcoldone = 1;
326 i++;
328 for (i = 1; i <= ROWS; i++) {
329 if (matrix[i][j].val == 0 || matrix[i][j].bold == 2) {
330 if (matrix[i][j].val == 0)
331 matrix_blit_char(i - 1, j, 20);
332 else
333 matrix_blit_char(i - 1, j, matrix[i][j].val);
334 } else {
335 if (matrix[i][j].val == 1)
336 matrix_blit_char(i - 1, j, 2);
337 else if (matrix[i][j].val == -1)
338 matrix_blit_char(i - 1, j, 129);
339 else
340 matrix_blit_char(i - 1, j, matrix[i][j].val);
347 enum plugin_status plugin_start(const void* parameter) {
348 int button;
349 int sleep = SLEEP;
350 bool frozen = false;
352 (void)parameter;
354 rb->lcd_set_background(LCD_BLACK);
355 rb->lcd_set_backdrop(NULL);
356 rb->lcd_clear_display();
357 matrix_init();
359 while (1) {
360 if (!frozen) {
361 matrix_loop();
362 rb->lcd_update();
363 rb->sleep(sleep);
365 button = rb->button_get(frozen);
366 switch(button) {
367 case MATRIX_PAUSE:
368 frozen = !frozen;
369 break;
370 case MATRIX_EXIT:
371 return PLUGIN_OK;
372 break;
373 case MATRIX_SLEEP_MORE:
374 /* Sleep longer */
375 sleep += SLEEP;
376 break;
377 case MATRIX_SLEEP_LESS:
378 /* Sleep less */
379 sleep -= SLEEP;
380 if (sleep < 0) sleep = 0;
381 break;
382 default:
383 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
384 return PLUGIN_USB_CONNECTED;
386 break;
389 return PLUGIN_OK;