Fix red in bootloaders
[maemo-rb.git] / apps / plugins / matrix.c
blob154a2e5378a9a29755f2129545934873c121658b
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 #else
140 #error Unsupported keypad
141 #endif
143 #ifdef HAVE_TOUCHSCREEN
144 #ifndef MATRIX_EXIT
145 #define MATRIX_EXIT BUTTON_TOPLEFT
146 #endif
147 #ifndef MATRIX_SLEEP_MORE
148 #define MATRIX_SLEEP_MORE BUTTON_MIDRIGHT
149 #endif
150 #ifndef MATRIX_SLEEP_LESS
151 #define MATRIX_SLEEP_LESS BUTTON_MIDLEFT
152 #endif
153 #ifndef MATRIX_PAUSE
154 #define MATRIX_PAUSE BUTTON_CENTER
155 #endif
156 #endif
158 #define SLEEP HZ/50
160 /* Each position is of this type */
161 typedef struct cmatrix {
162 int val;
163 int bold;
164 } cmatrix;
166 /* The matrix - who'd have guessed it was just a few hundred bytes? */
167 static cmatrix matrix[ROWS][COLS];
168 static int length[COLS];
169 static int spaces[COLS];
170 static int updates[COLS];
172 static void matrix_init(void) {
173 int i,j;
175 /* Seed rand */
176 rb->srand(*rb->current_tick);
178 /* Make the matrix */
179 for (i = 0; i <= ROWS; i++) {
180 for (j = 0; j <= COLS - 1; j++ ) {
181 matrix[i][j].val = -1;
182 matrix[i][j].bold = 0;
186 for (j = 0; j <= COLS - 1; j++) {
187 /* Set up spaces[] array of how many spaces to skip */
188 spaces[j] = rb->rand() % ROWS + 1;
190 /* And length of the stream */
191 length[j] = rb->rand() % (ROWS - 3) + 3;
193 /* Sentinel value for creation of new objects */
194 matrix[1][j].val = 129;
196 /* And set updates[] array for update speed. */
197 updates[j] = rb->rand() % 3 + 1;
201 static void matrix_blit_char(const int row, const int col, int cha)
203 if (cha == 129 || cha == 2 || cha > MAXCHARS)
204 cha = 0;
206 if (matrix[row][col].bold == 1) {
207 rb->lcd_bitmap_part(matrix_bold, cha*COL_W, 0, 392,
208 col*COL_W + LEFTMARGIN, row*COL_H + TOPMARGIN, COL_W, COL_H);
210 else {
211 rb->lcd_bitmap_part(matrix_normal, cha*COL_W, 0, 392,
212 col*COL_W + LEFTMARGIN, row*COL_H + TOPMARGIN, COL_W, COL_H);
216 static void matrix_loop(void)
218 int i, j = 0, y, z, firstcoldone = 0;
219 static int count = 0;
220 int randomness = 6;
222 count++;
223 if (count > 4)
224 count = 1;
226 for (j = 0; j <= COLS - 1; j++) {
227 if (count > updates[j]) {
228 /* New style scrolling */
229 if (matrix[0][j].val == -1 && matrix[1][j].val == 129
230 && spaces[j] > 0) {
231 matrix[0][j].val = -1;
232 spaces[j]--;
233 } else if (matrix[0][j].val == -1 && matrix[1][j].val == 129){
234 length[j] = rb->rand() % (ROWS - 3) + 3;
235 matrix[0][j].val = rb->rand() % (MAXCHARS-1) + 1;
236 if (rb->rand() % 2 == 1)
237 matrix[0][j].bold = 2;
238 spaces[j] = rb->rand() % ROWS + 1;
240 i = 0;
241 y = 0;
242 firstcoldone = 0;
243 while (i <= ROWS) {
244 /* Skip over spaces */
245 /* this is whear the characters were disappearing */
247 while (i <= ROWS && (matrix[i][j].val == 129 ||
248 matrix[i][j].val == -1))
249 i++;
251 /* A little more random now for spaces */
252 if (rb->rand() % randomness == 1){
253 while (i <= ROWS && (matrix[i][j].val == 129 ||
254 matrix[i][j].val == -1)){
255 i++;
256 randomness--;
257 if(randomness <=1)
258 randomness = 6;}
259 }else{
260 randomness++;
261 if(randomness >6)
262 randomness = 6;
266 if (i > ROWS)
267 break;
269 /* Go to the head of this collumn */
270 z = i;
271 y = 0;
272 while (i <= ROWS && (matrix[i][j].val != 129 &&
273 matrix[i][j].val != -1)) {
274 i++;
275 y++;
278 if (i > ROWS) {
279 matrix[z][j].val = 129;
280 matrix[ROWS][j].bold = 1;
281 matrix_blit_char(z - 1, j, matrix[z][j].val);
282 continue;
285 matrix[i][j].val = rb->rand() % (MAXCHARS-1) + 1;
287 if (matrix[i - 1][j].bold == 2) {
288 matrix[i - 1][j].bold = 1;
289 matrix[i][j].bold = 2;
292 /* If we're at the top of the collumn and it's reached its
293 * full length (about to start moving down), we do this
294 * to get it moving. This is also how we keep segments
295 * not already growing from growing accidentally =>
297 if (y > length[j] || firstcoldone) {
298 matrix[z][j].val = 129;
299 matrix[0][j].val = -1;
301 firstcoldone = 1;
302 i++;
304 for (i = 1; i <= ROWS; i++) {
305 if (matrix[i][j].val == 0 || matrix[i][j].bold == 2) {
306 if (matrix[i][j].val == 0)
307 matrix_blit_char(i - 1, j, 20);
308 else
309 matrix_blit_char(i - 1, j, matrix[i][j].val);
310 } else {
311 if (matrix[i][j].val == 1)
312 matrix_blit_char(i - 1, j, 2);
313 else if (matrix[i][j].val == -1)
314 matrix_blit_char(i - 1, j, 129);
315 else
316 matrix_blit_char(i - 1, j, matrix[i][j].val);
323 enum plugin_status plugin_start(const void* parameter) {
324 int button;
325 int sleep = SLEEP;
326 bool frozen = false;
328 (void)parameter;
330 rb->lcd_set_background(LCD_BLACK);
331 rb->lcd_set_backdrop(NULL);
332 rb->lcd_clear_display();
333 matrix_init();
335 while (1) {
336 if (!frozen) {
337 matrix_loop();
338 rb->lcd_update();
339 rb->sleep(sleep);
341 button = rb->button_get(frozen);
342 switch(button) {
343 case MATRIX_PAUSE:
344 frozen = !frozen;
345 break;
346 case MATRIX_EXIT:
347 return PLUGIN_OK;
348 break;
349 case MATRIX_SLEEP_MORE:
350 /* Sleep longer */
351 sleep += SLEEP;
352 break;
353 case MATRIX_SLEEP_LESS:
354 /* Sleep less */
355 sleep -= SLEEP;
356 if (sleep < 0) sleep = 0;
357 break;
358 default:
359 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
360 return PLUGIN_USB_CONNECTED;
362 break;
365 return PLUGIN_OK;