Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / apps / plugins / matrix.c
blob8a4a629669b2aa00fae3fd3779fb6947d4b1043a
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
59 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
60 (CONFIG_KEYPAD == IRIVER_H300_PAD)
61 #define MATRIX_EXIT BUTTON_OFF
62 #define MATRIX_SLEEP_MORE BUTTON_UP
63 #define MATRIX_SLEEP_LESS BUTTON_DOWN
64 #define MATRIX_PAUSE BUTTON_SELECT
65 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
66 #define MATRIX_EXIT BUTTON_POWER
67 #define MATRIX_SLEEP_MORE BUTTON_SCROLL_UP|BUTTON_REPEAT
68 #define MATRIX_SLEEP_LESS BUTTON_SCROLL_DOWN|BUTTON_REPEAT
69 #define MATRIX_PAUSE BUTTON_PLAY
70 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
71 #define MATRIX_EXIT BUTTON_POWER
72 #define MATRIX_SLEEP_MORE BUTTON_UP
73 #define MATRIX_SLEEP_LESS BUTTON_DOWN
74 #define MATRIX_PAUSE BUTTON_PLAY
75 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
76 #define MATRIX_EXIT BUTTON_A
77 #define MATRIX_SLEEP_MORE BUTTON_UP
78 #define MATRIX_SLEEP_LESS BUTTON_DOWN
79 #define MATRIX_PAUSE BUTTON_SELECT
80 #elif CONFIG_KEYPAD == SANSA_E200_PAD
81 #define MATRIX_EXIT BUTTON_POWER
82 #define MATRIX_SLEEP_MORE BUTTON_SCROLL_BACK|BUTTON_REPEAT
83 #define MATRIX_SLEEP_LESS BUTTON_SCROLL_FWD|BUTTON_REPEAT
84 #define MATRIX_PAUSE BUTTON_SELECT
85 #elif CONFIG_KEYPAD == SANSA_C200_PAD
86 #define MATRIX_EXIT BUTTON_POWER
87 #define MATRIX_SLEEP_MORE BUTTON_UP
88 #define MATRIX_SLEEP_LESS BUTTON_DOWN
89 #define MATRIX_PAUSE BUTTON_SELECT
90 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
91 #define MATRIX_EXIT BUTTON_BACK
92 #define MATRIX_SLEEP_MORE BUTTON_UP
93 #define MATRIX_SLEEP_LESS BUTTON_DOWN
94 #define MATRIX_PAUSE BUTTON_SELECT
95 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
96 #define MATRIX_EXIT BUTTON_RC_REC
97 #define MATRIX_SLEEP_MORE BUTTON_RC_VOL_UP
98 #define MATRIX_SLEEP_LESS BUTTON_RC_VOL_DOWN
99 #define MATRIX_PAUSE BUTTON_RC_PLAY
100 #elif (CONFIG_KEYPAD == COWOND2_PAD)
101 #define MATRIX_EXIT BUTTON_POWER
102 #else
103 #error Unsupported keypad
104 #endif
106 #ifdef HAVE_TOUCHPAD
107 #ifndef MATRIX_EXIT
108 #define MATRIX_EXIT BUTTON_TOPLEFT
109 #endif
110 #ifndef MATRIX_SLEEP_MORE
111 #define MATRIX_SLEEP_MORE BUTTON_MIDRIGHT
112 #endif
113 #ifndef MATRIX_SLEEP_LESS
114 #define MATRIX_SLEEP_LESS BUTTON_MIDLEFT
115 #endif
116 #ifndef MATRIX_PAUSE
117 #define MATRIX_PAUSE BUTTON_CENTER
118 #endif
119 #endif
121 #define SLEEP HZ/50
123 /* Codec api pointer */
124 static const struct plugin_api* rb;
126 /* Each position is of this type */
127 typedef struct cmatrix {
128 int val;
129 int bold;
130 } cmatrix;
132 /* The matrix - who'd have guessed it was just a few hundred bytes? */
133 static cmatrix matrix[ROWS][COLS];
134 static int length[COLS];
135 static int spaces[COLS];
136 static int updates[COLS];
138 static void matrix_init(void) {
139 int i,j;
141 /* Seed rand */
142 rb->srand(*rb->current_tick);
144 /* Make the matrix */
145 for (i = 0; i <= ROWS; i++) {
146 for (j = 0; j <= COLS - 1; j++ ) {
147 matrix[i][j].val = -1;
148 matrix[i][j].bold = 0;
152 for (j = 0; j <= COLS - 1; j++) {
153 /* Set up spaces[] array of how many spaces to skip */
154 spaces[j] = rb->rand() % ROWS + 1;
156 /* And length of the stream */
157 length[j] = rb->rand() % (ROWS - 3) + 3;
159 /* Sentinel value for creation of new objects */
160 matrix[1][j].val = 129;
162 /* And set updates[] array for update speed. */
163 updates[j] = rb->rand() % 3 + 1;
167 static void matrix_blit_char(const int row, const int col, int cha)
169 if (cha == 129 || cha == 2 || cha > MAXCHARS)
170 cha = 0;
172 if (matrix[row][col].bold == 1) {
173 rb->lcd_bitmap_part(matrix_bold, cha*COL_W, 0, 392,
174 col*COL_W + LEFTMARGIN, row*COL_H + TOPMARGIN, COL_W, COL_H);
176 else {
177 rb->lcd_bitmap_part(matrix_normal, cha*COL_W, 0, 392,
178 col*COL_W + LEFTMARGIN, row*COL_H + TOPMARGIN, COL_W, COL_H);
182 static void matrix_loop(void)
184 int i, j = 0, y, z, firstcoldone = 0;
185 static int count = 0;
186 int randomness = 6;
188 count++;
189 if (count > 4)
190 count = 1;
192 for (j = 0; j <= COLS - 1; j++) {
193 if (count > updates[j]) {
194 /* New style scrolling */
195 if (matrix[0][j].val == -1 && matrix[1][j].val == 129
196 && spaces[j] > 0) {
197 matrix[0][j].val = -1;
198 spaces[j]--;
199 } else if (matrix[0][j].val == -1 && matrix[1][j].val == 129){
200 length[j] = rb->rand() % (ROWS - 3) + 3;
201 matrix[0][j].val = rb->rand() % (MAXCHARS-1) + 1;
202 if (rb->rand() % 2 == 1)
203 matrix[0][j].bold = 2;
204 spaces[j] = rb->rand() % ROWS + 1;
206 i = 0;
207 y = 0;
208 firstcoldone = 0;
209 while (i <= ROWS) {
210 /* Skip over spaces */
211 /* this is whear the characters were disappearing */
213 while (i <= ROWS && (matrix[i][j].val == 129 ||
214 matrix[i][j].val == -1))
215 i++;
217 /* A little more random now for spaces */
218 if (rb->rand() % randomness == 1){
219 while (i <= ROWS && (matrix[i][j].val == 129 ||
220 matrix[i][j].val == -1)){
221 i++;
222 randomness--;
223 if(randomness <=1)
224 randomness = 6;}
225 }else{
226 randomness++;
227 if(randomness >6)
228 randomness = 6;
232 if (i > ROWS)
233 break;
235 /* Go to the head of this collumn */
236 z = i;
237 y = 0;
238 while (i <= ROWS && (matrix[i][j].val != 129 &&
239 matrix[i][j].val != -1)) {
240 i++;
241 y++;
244 if (i > ROWS) {
245 matrix[z][j].val = 129;
246 matrix[ROWS][j].bold = 1;
247 matrix_blit_char(z - 1, j, matrix[z][j].val);
248 continue;
251 matrix[i][j].val = rb->rand() % (MAXCHARS-1) + 1;
253 if (matrix[i - 1][j].bold == 2) {
254 matrix[i - 1][j].bold = 1;
255 matrix[i][j].bold = 2;
258 /* If we're at the top of the collumn and it's reached its
259 * full length (about to start moving down), we do this
260 * to get it moving. This is also how we keep segments
261 * not already growing from growing accidentally =>
263 if (y > length[j] || firstcoldone) {
264 matrix[z][j].val = 129;
265 matrix[0][j].val = -1;
267 firstcoldone = 1;
268 i++;
270 for (i = 1; i <= ROWS; i++) {
271 if (matrix[i][j].val == 0 || matrix[i][j].bold == 2) {
272 if (matrix[i][j].val == 0)
273 matrix_blit_char(i - 1, j, 20);
274 else
275 matrix_blit_char(i - 1, j, matrix[i][j].val);
276 } else {
277 if (matrix[i][j].val == 1)
278 matrix_blit_char(i - 1, j, 2);
279 else if (matrix[i][j].val == -1)
280 matrix_blit_char(i - 1, j, 129);
281 else
282 matrix_blit_char(i - 1, j, matrix[i][j].val);
289 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) {
290 int button;
291 int sleep = SLEEP;
292 bool frozen = false;
294 (void)parameter;
295 rb = api;
297 rb->lcd_set_background(LCD_BLACK);
298 rb->lcd_set_backdrop(NULL);
299 rb->lcd_clear_display();
300 matrix_init();
302 while (1) {
303 if (!frozen) {
304 matrix_loop();
305 rb->lcd_update();
306 rb->sleep(sleep);
308 button = rb->button_get(false);
309 switch(button) {
310 case MATRIX_PAUSE:
311 frozen = !frozen;
312 break;
313 case MATRIX_EXIT:
314 return PLUGIN_OK;
315 break;
316 case MATRIX_SLEEP_MORE:
317 /* Sleep longer */
318 sleep += SLEEP;
319 break;
320 case MATRIX_SLEEP_LESS:
321 /* Sleep less */
322 sleep -= SLEEP;
323 if (sleep < 0) sleep = 0;
324 break;
325 default:
326 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
327 return PLUGIN_USB_CONNECTED;
329 break;
332 return PLUGIN_OK;