And of course the stubs still need to be there...
[kugel-rb.git] / apps / plugins / starfield.c
blob1ae0f289ffcf988bbe7df9f3307540443216fa75
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Copyright (C) 2005 Kevin Ferrare
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "plugin.h"
21 #include "lib/helper.h"
23 #ifdef HAVE_LCD_BITMAP /* and also not for the Player */
25 PLUGIN_HEADER
27 /******************************* Globals ***********************************/
29 /* Key assignement */
30 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
31 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
32 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
33 #define STARFIELD_QUIT BUTTON_MENU
34 #define STARFIELD_INCREASE_ZMOVE BUTTON_SCROLL_FWD
35 #define STARFIELD_DECREASE_ZMOVE BUTTON_SCROLL_BACK
36 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
37 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
38 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
40 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
41 #define STARFIELD_QUIT BUTTON_POWER
42 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
43 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
44 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
45 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
46 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
48 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
49 #define STARFIELD_QUIT BUTTON_POWER
50 #define STARFIELD_INCREASE_ZMOVE BUTTON_SCROLL_UP
51 #define STARFIELD_DECREASE_ZMOVE BUTTON_SCROLL_DOWN
52 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
53 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
54 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
56 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
57 #define STARFIELD_QUIT BUTTON_POWER
58 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
59 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
60 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
61 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
62 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
64 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
65 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
66 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
67 (CONFIG_KEYPAD == SANSA_M200_PAD)
68 #define STARFIELD_QUIT BUTTON_POWER
69 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
70 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
71 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
72 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
73 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
75 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
76 #define STARFIELD_QUIT (BUTTON_HOME|BUTTON_REPEAT)
77 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
78 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
79 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
80 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
81 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
83 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
84 #define STARFIELD_QUIT BUTTON_BACK
85 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
86 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
87 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
88 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
89 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
91 #elif (CONFIG_KEYPAD == MROBE100_PAD)
92 #define STARFIELD_QUIT BUTTON_POWER
93 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
94 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
95 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
96 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
97 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
99 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
100 #define STARFIELD_QUIT BUTTON_RC_REC
101 #define STARFIELD_INCREASE_ZMOVE BUTTON_RC_VOL_UP
102 #define STARFIELD_DECREASE_ZMOVE BUTTON_RC_VOL_DOWN
103 #define STARFIELD_INCREASE_NB_STARS BUTTON_RC_FF
104 #define STARFIELD_DECREASE_NB_STARS BUTTON_RC_REW
105 #define STARFIELD_TOGGLE_COLOR BUTTON_RC_MODE
107 #elif CONFIG_KEYPAD == IAUDIO67_PAD
108 #define STARFIELD_QUIT BUTTON_POWER
109 #define STARFIELD_INCREASE_ZMOVE BUTTON_STOP
110 #define STARFIELD_DECREASE_ZMOVE BUTTON_PLAY
111 #define STARFIELD_INCREASE_NB_STARS BUTTON_LEFT
112 #define STARFIELD_DECREASE_NB_STARS BUTTON_RIGHT
113 #define STARFIELD_TOGGLE_COLOR BUTTON_MENU
115 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
116 #define STARFIELD_QUIT BUTTON_POWER
118 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
119 #define STARFIELD_QUIT BUTTON_BACK
120 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
121 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
122 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
123 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
124 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
126 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
127 #define STARFIELD_QUIT BUTTON_POWER
128 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
129 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
130 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
131 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
132 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
134 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
135 #define STARFIELD_QUIT BUTTON_POWER
136 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
137 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
138 #define STARFIELD_INCREASE_NB_STARS BUTTON_NEXT
139 #define STARFIELD_DECREASE_NB_STARS BUTTON_PREV
140 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
142 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || (CONFIG_KEYPAD == ONDAVX777_PAD) || (CONFIG_KEYPAD == MROBE500_PAD)
143 #define STARFIELD_QUIT BUTTON_POWER
145 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
146 #define STARFIELD_QUIT BUTTON_FFWD
147 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
148 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
149 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
150 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
151 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
153 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
154 #define STARFIELD_QUIT BUTTON_REC
155 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
156 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
157 #define STARFIELD_INCREASE_NB_STARS BUTTON_NEXT
158 #define STARFIELD_DECREASE_NB_STARS BUTTON_PREV
159 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
161 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
162 #define STARFIELD_QUIT (BUTTON_REC|BUTTON_PLAY)
163 #define STARFIELD_INCREASE_ZMOVE BUTTON_VOL_UP
164 #define STARFIELD_DECREASE_ZMOVE BUTTON_VOL_DOWN
165 #define STARFIELD_INCREASE_NB_STARS BUTTON_NEXT
166 #define STARFIELD_DECREASE_NB_STARS BUTTON_PREV
167 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
169 #endif
171 #ifdef HAVE_TOUCHSCREEN
172 #ifndef STARFIELD_QUIT
173 #define STARFIELD_QUIT BUTTON_TOPLEFT
174 #endif
175 #ifndef STARFIELD_INCREASE_ZMOVE
176 #define STARFIELD_INCREASE_ZMOVE BUTTON_TOPMIDDLE
177 #endif
178 #ifndef STARFIELD_DECREASE_ZMOVE
179 #define STARFIELD_DECREASE_ZMOVE BUTTON_BOTTOMMIDDLE
180 #endif
181 #ifndef STARFIELD_INCREASE_NB_STARS
182 #define STARFIELD_INCREASE_NB_STARS BUTTON_MIDRIGHT
183 #endif
184 #ifndef STARFIELD_DECREASE_NB_STARS
185 #define STARFIELD_DECREASE_NB_STARS BUTTON_MIDLEFT
186 #endif
187 #ifndef STARFIELD_TOGGLE_COLOR
188 #define STARFIELD_TOGGLE_COLOR BUTTON_CENTER
189 #endif
190 #endif
192 #ifndef STARFIELD_QUIT
193 #define STARFIELD_QUIT BUTTON_OFF
194 #endif
195 #ifndef STARFIELD_INCREASE_ZMOVE
196 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
197 #endif
198 #ifndef STARFIELD_DECREASE_ZMOVE
199 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
200 #endif
201 #ifndef STARFIELD_INCREASE_NB_STARS
202 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
203 #endif
204 #ifndef STARFIELD_DECREASE_NB_STARS
205 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
206 #endif
208 #ifndef STARFIELD_TOGGLE_COLOR
209 #ifdef BUTTON_SELECT
210 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
211 #else
212 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
213 #endif
214 #endif
215 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
216 #define STARFIELD_RC_QUIT BUTTON_RC_STOP
217 #endif
220 #define LCD_CENTER_X (LCD_WIDTH/2)
221 #define LCD_CENTER_Y (LCD_HEIGHT/2)
222 #define Z_MAX_DIST 100
225 #define MAX_STARS (LCD_WIDTH*LCD_HEIGHT*20)/100
226 #define INIT_STARS 200
227 #define STARFIELD_INCREASE_STEP 50
228 #define INIT_SPACE_SPEED 1
229 #define STAR_MAX_VELOCITY 2
231 * max 3d coord in the 2d screen :
232 * example with x
233 * x2d=x3d/z+LCD_CENTER_X (+LCD_CENTER_X to center ...)
234 * so
235 * max_x2d=max_x3d/max_z+LCD_CENTER_X
236 * max_x3d=(max_x2d-LCD_CENTER_X)*max_z
237 * with
238 * max_x2d = LCD_WIDTH
239 * max_z = Z_MAX_DIST
240 * we have now
241 * max_x3d=(LCD_WIDTH-LCD_CENTER_X)*Z_MAX_DIST
242 * max_x3d=LCD_CENTER_X*Z_MAX_DIST
245 #define MAX_INIT_STAR_X LCD_CENTER_X*Z_MAX_DIST
246 #define MAX_INIT_STAR_Y LCD_CENTER_Y*Z_MAX_DIST
248 #define MSG_DISP_TIME 30
251 * Each star's stuffs
253 struct star
255 int x,y,z;
256 int velocity;
257 #ifdef HAVE_LCD_COLOR
258 int color;
259 #endif
262 static inline void star_init(struct star * star, int z_move, bool color)
264 star->velocity=rb->rand() % STAR_MAX_VELOCITY+1;
265 /* choose x between -MAX_INIT_STAR_X and MAX_INIT_STAR_X */
266 star->x=rb->rand() % (2*MAX_INIT_STAR_X)-MAX_INIT_STAR_X;
267 star->y=rb->rand() % (2*MAX_INIT_STAR_Y)-MAX_INIT_STAR_Y;
268 #ifdef HAVE_LCD_COLOR
269 if(color)
270 star->color=LCD_RGBPACK(rb->rand()%128+128,rb->rand()%128+128,
271 rb->rand()%128+128);
272 else
273 star->color=LCD_WHITE;
274 #else
275 (void)color;
276 #endif
277 if(z_move>=0)
278 star->z=Z_MAX_DIST;
279 else
280 star->z=rb->rand() %Z_MAX_DIST/2+1;
283 static inline void star_move(struct star * star, int z_move, bool color)
285 star->z -= z_move*star->velocity;
286 if (star->z <= 0 || star->z > Z_MAX_DIST)
287 star_init(star, z_move, color);
290 static inline void star_draw(struct star * star, int z_move, bool color)
292 int x_draw, y_draw;
294 * 3d -> 2d : projection on the screen : x2d=x3d/z (thales theorem)
295 * we put the star in the center of the screen
297 x_draw = star->x / star->z + LCD_CENTER_X;
298 if (x_draw < 1 || x_draw >= LCD_WIDTH)
300 star_init(star, z_move, color);
301 return;
303 y_draw = star->y / star->z + LCD_CENTER_Y;
304 if (y_draw < 1 || y_draw >= LCD_HEIGHT)
306 star_init(star, z_move, color);
307 return;
310 #ifdef HAVE_LCD_COLOR
311 rb->lcd_set_foreground(star->color);
312 #endif
314 rb->lcd_drawpixel(x_draw, y_draw);
315 if(star->z<5*Z_MAX_DIST/6)
317 rb->lcd_drawpixel(x_draw, y_draw - 1);
318 rb->lcd_drawpixel(x_draw - 1, y_draw);
319 if(star->z<Z_MAX_DIST/2)
321 rb->lcd_drawpixel(x_draw + 1, y_draw);
322 rb->lcd_drawpixel(x_draw, y_draw + 1);
328 * Whole starfield operations
330 struct starfield
332 struct star tab[MAX_STARS];
333 int nb_stars;
334 int z_move;
335 bool color;
338 static inline void starfield_init(struct starfield * starfield)
340 starfield->nb_stars=0;
341 starfield->z_move=INIT_SPACE_SPEED;
342 starfield->color=false;
345 static inline void starfield_add_stars(struct starfield * starfield,
346 int nb_to_add)
348 int i, old_nb_stars;
349 old_nb_stars=starfield->nb_stars;
350 starfield->nb_stars+=nb_to_add;
352 if(starfield->nb_stars > MAX_STARS)
353 starfield->nb_stars=MAX_STARS;
355 for( i=old_nb_stars ; i < starfield->nb_stars ; ++i )
357 star_init( &(starfield->tab[i]), starfield->z_move, starfield->color );
361 static inline void starfield_del_stars(struct starfield * starfield,
362 int nb_to_add)
364 starfield->nb_stars-=nb_to_add;
365 if(starfield->nb_stars<0)
366 starfield->nb_stars=0;
369 static inline void starfield_move_and_draw(struct starfield * starfield)
371 int i;
372 for(i=0;i<starfield->nb_stars;++i)
374 star_move(&(starfield->tab[i]), starfield->z_move, starfield->color);
375 star_draw(&(starfield->tab[i]), starfield->z_move, starfield->color);
379 static struct starfield starfield;
381 int plugin_main(void)
383 char str_buffer[40];
384 int button, avg_peak, t_disp=0;
385 int font_h, font_w;
386 bool pulse=true;
387 rb->lcd_getstringsize("A", &font_w, &font_h);
388 starfield_init(&starfield);
389 starfield_add_stars(&starfield, INIT_STARS);
391 #if LCD_DEPTH > 1
392 rb->lcd_set_backdrop(NULL);
393 #endif
394 #ifdef HAVE_LCD_COLOR
395 rb->lcd_set_background(LCD_BLACK);
396 rb->lcd_set_foreground(LCD_WHITE);
397 #endif
399 while (true)
401 rb->sleep(1);
402 rb->lcd_clear_display();
404 #if ((CONFIG_CODEC == SWCODEC) || !defined(SIMULATOR) && \
405 ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)))
407 /* This will make the stars pulse to the music */
408 if(pulse){
410 /* Get the peaks. ( Borrowed from vu_meter ) */
411 #if (CONFIG_CODEC == SWCODEC)
412 int left_peak, right_peak;
413 rb->pcm_calculate_peaks(&left_peak, &right_peak);
414 #else
415 int left_peak = rb->mas_codec_readreg(0xC);
416 int right_peak = rb->mas_codec_readreg(0xD);
417 #endif
418 /* Devide peak data by 4098 to bring the max
419 value down from ~32k to 8 */
420 left_peak = left_peak/0x1000;
421 right_peak = right_peak/0x1000;
423 /* Make sure they dont stop */
424 if(left_peak<0x1)
425 left_peak = 0x1;
426 if(right_peak<0x1)
427 right_peak = 0x1;
429 /* And make sure they dont go over 8 */
430 if(left_peak>0x8)
431 left_peak = 0x8;
432 if(right_peak>0x8)
433 right_peak = 0x8;
435 /* We need the average of both chanels */
436 avg_peak = ( left_peak + right_peak )/2;
438 /* Set the speed to the peak meter */
439 starfield.z_move = avg_peak;
441 } /* if pulse */
442 #else
443 (void) avg_peak;
444 #endif
445 starfield_move_and_draw(&starfield);
447 #ifdef HAVE_LCD_COLOR
448 rb->lcd_set_foreground(LCD_WHITE);
449 #endif
451 /* if a parameter is updated (by the user), we must print it */
452 if (t_disp > 0)
454 --t_disp;
455 rb->snprintf(str_buffer, sizeof(str_buffer),
456 "star:%d speed:%d",
457 starfield.nb_stars,
458 starfield.z_move);
459 #ifdef HAVE_LCD_COLOR
460 rb->lcd_set_foreground(LCD_WHITE);
461 #endif
462 rb->lcd_putsxy(0, LCD_HEIGHT-font_h, str_buffer);
464 rb->lcd_update();
466 button = rb->button_get(false);
467 switch(button)
469 case (STARFIELD_INCREASE_ZMOVE):
470 case (STARFIELD_INCREASE_ZMOVE | BUTTON_REPEAT):
471 ++(starfield.z_move);
472 pulse=false;
473 t_disp=MSG_DISP_TIME;
474 break;
475 case (STARFIELD_DECREASE_ZMOVE):
476 case (STARFIELD_DECREASE_ZMOVE | BUTTON_REPEAT):
477 --(starfield.z_move);
478 pulse=false;
479 t_disp=MSG_DISP_TIME;
480 break;
481 case(STARFIELD_INCREASE_NB_STARS):
482 case(STARFIELD_INCREASE_NB_STARS | BUTTON_REPEAT):
483 starfield_add_stars(&starfield, STARFIELD_INCREASE_STEP);
484 t_disp=MSG_DISP_TIME;
485 break;
486 case(STARFIELD_DECREASE_NB_STARS):
487 case(STARFIELD_DECREASE_NB_STARS | BUTTON_REPEAT):
488 starfield_del_stars(&starfield, STARFIELD_INCREASE_STEP);
489 t_disp=MSG_DISP_TIME;
490 break;
491 #ifdef HAVE_LCD_COLOR
492 case(STARFIELD_TOGGLE_COLOR):
493 starfield.color=!starfield.color;
494 break;
495 #endif
496 #ifdef STARFIELD_RC_QUIT
497 case STARFIELD_RC_QUIT:
498 #endif
499 case(STARFIELD_QUIT):
500 case(SYS_USB_CONNECTED):
501 return PLUGIN_OK;
502 break;
507 /*************************** Plugin entry point ****************************/
509 enum plugin_status plugin_start(const void* parameter)
511 int ret;
513 (void)parameter;
514 /* Turn off backlight timeout */
515 backlight_force_on(); /* backlight control in lib/helper.c */
517 ret = plugin_main();
519 /* Turn on backlight timeout (revert to settings) */
520 backlight_use_settings(); /* backlight control in lib/helper.c*/
522 return ret;
525 #endif /* #ifdef HAVE_LCD_BITMAP */