use lib display text to display help messages (FS#10099).
[kugel-rb.git] / apps / plugins / starfield.c
blob768ab5a20ca49574efe6ebfbb5a7fa72e073ef89
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 == COWOND2_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 == ONDAVX747_PAD) || CONFIG_KEYPAD == MROBE500_PAD
135 #define STARFIELD_QUIT BUTTON_POWER
137 #endif
139 #ifdef HAVE_TOUCHSCREEN
140 #ifndef STARFIELD_QUIT
141 #define STARFIELD_QUIT BUTTON_TOPLEFT
142 #endif
143 #ifndef STARFIELD_INCREASE_ZMOVE
144 #define STARFIELD_INCREASE_ZMOVE BUTTON_TOPMIDDLE
145 #endif
146 #ifndef STARFIELD_DECREASE_ZMOVE
147 #define STARFIELD_DECREASE_ZMOVE BUTTON_BOTTOMMIDDLE
148 #endif
149 #ifndef STARFIELD_INCREASE_NB_STARS
150 #define STARFIELD_INCREASE_NB_STARS BUTTON_MIDRIGHT
151 #endif
152 #ifndef STARFIELD_DECREASE_NB_STARS
153 #define STARFIELD_DECREASE_NB_STARS BUTTON_MIDLEFT
154 #endif
155 #ifndef STARFIELD_TOGGLE_COLOR
156 #define STARFIELD_TOGGLE_COLOR BUTTON_CENTER
157 #endif
158 #endif
160 #ifndef STARFIELD_QUIT
161 #define STARFIELD_QUIT BUTTON_OFF
162 #endif
163 #ifndef STARFIELD_INCREASE_ZMOVE
164 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
165 #endif
166 #ifndef STARFIELD_DECREASE_ZMOVE
167 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
168 #endif
169 #ifndef STARFIELD_INCREASE_NB_STARS
170 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
171 #endif
172 #ifndef STARFIELD_DECREASE_NB_STARS
173 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
174 #endif
176 #ifndef STARFIELD_TOGGLE_COLOR
177 #ifdef BUTTON_SELECT
178 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
179 #else
180 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
181 #endif
182 #endif
183 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
184 #define STARFIELD_RC_QUIT BUTTON_RC_STOP
185 #endif
188 #define LCD_CENTER_X (LCD_WIDTH/2)
189 #define LCD_CENTER_Y (LCD_HEIGHT/2)
190 #define Z_MAX_DIST 100
193 #define MAX_STARS (LCD_WIDTH*LCD_HEIGHT*20)/100
194 #define INIT_STARS 200
195 #define STARFIELD_INCREASE_STEP 50
196 #define INIT_SPACE_SPEED 1
197 #define STAR_MAX_VELOCITY 2
199 * max 3d coord in the 2d screen :
200 * example with x
201 * x2d=x3d/z+LCD_CENTER_X (+LCD_CENTER_X to center ...)
202 * so
203 * max_x2d=max_x3d/max_z+LCD_CENTER_X
204 * max_x3d=(max_x2d-LCD_CENTER_X)*max_z
205 * with
206 * max_x2d = LCD_WIDTH
207 * max_z = Z_MAX_DIST
208 * we have now
209 * max_x3d=(LCD_WIDTH-LCD_CENTER_X)*Z_MAX_DIST
210 * max_x3d=LCD_CENTER_X*Z_MAX_DIST
213 #define MAX_INIT_STAR_X LCD_CENTER_X*Z_MAX_DIST
214 #define MAX_INIT_STAR_Y LCD_CENTER_Y*Z_MAX_DIST
216 #define MSG_DISP_TIME 30
219 * Each star's stuffs
221 struct star
223 int x,y,z;
224 int velocity;
225 #ifdef HAVE_LCD_COLOR
226 int color;
227 #endif
230 static inline void star_init(struct star * star, int z_move, bool color)
232 star->velocity=rb->rand() % STAR_MAX_VELOCITY+1;
233 /* choose x between -MAX_INIT_STAR_X and MAX_INIT_STAR_X */
234 star->x=rb->rand() % (2*MAX_INIT_STAR_X)-MAX_INIT_STAR_X;
235 star->y=rb->rand() % (2*MAX_INIT_STAR_Y)-MAX_INIT_STAR_Y;
236 #ifdef HAVE_LCD_COLOR
237 if(color)
238 star->color=LCD_RGBPACK(rb->rand()%128+128,rb->rand()%128+128,
239 rb->rand()%128+128);
240 else
241 star->color=LCD_WHITE;
242 #else
243 (void)color;
244 #endif
245 if(z_move>=0)
246 star->z=Z_MAX_DIST;
247 else
248 star->z=rb->rand() %Z_MAX_DIST/2+1;
251 static inline void star_move(struct star * star, int z_move, bool color)
253 star->z -= z_move*star->velocity;
254 if (star->z <= 0 || star->z > Z_MAX_DIST)
255 star_init(star, z_move, color);
258 static inline void star_draw(struct star * star, int z_move, bool color)
260 int x_draw, y_draw;
262 * 3d -> 2d : projection on the screen : x2d=x3d/z (thales theorem)
263 * we put the star in the center of the screen
265 x_draw = star->x / star->z + LCD_CENTER_X;
266 if (x_draw < 1 || x_draw >= LCD_WIDTH)
268 star_init(star, z_move, color);
269 return;
271 y_draw = star->y / star->z + LCD_CENTER_Y;
272 if (y_draw < 1 || y_draw >= LCD_HEIGHT)
274 star_init(star, z_move, color);
275 return;
278 #ifdef HAVE_LCD_COLOR
279 rb->lcd_set_foreground(star->color);
280 #endif
282 rb->lcd_drawpixel(x_draw, y_draw);
283 if(star->z<5*Z_MAX_DIST/6)
285 rb->lcd_drawpixel(x_draw, y_draw - 1);
286 rb->lcd_drawpixel(x_draw - 1, y_draw);
287 if(star->z<Z_MAX_DIST/2)
289 rb->lcd_drawpixel(x_draw + 1, y_draw);
290 rb->lcd_drawpixel(x_draw, y_draw + 1);
296 * Whole starfield operations
298 struct starfield
300 struct star tab[MAX_STARS];
301 int nb_stars;
302 int z_move;
303 bool color;
306 static inline void starfield_init(struct starfield * starfield)
308 starfield->nb_stars=0;
309 starfield->z_move=INIT_SPACE_SPEED;
310 starfield->color=false;
313 static inline void starfield_add_stars(struct starfield * starfield,
314 int nb_to_add)
316 int i, old_nb_stars;
317 old_nb_stars=starfield->nb_stars;
318 starfield->nb_stars+=nb_to_add;
320 if(starfield->nb_stars > MAX_STARS)
321 starfield->nb_stars=MAX_STARS;
323 for( i=old_nb_stars ; i < starfield->nb_stars ; ++i )
325 star_init( &(starfield->tab[i]), starfield->z_move, starfield->color );
329 static inline void starfield_del_stars(struct starfield * starfield,
330 int nb_to_add)
332 starfield->nb_stars-=nb_to_add;
333 if(starfield->nb_stars<0)
334 starfield->nb_stars=0;
337 static inline void starfield_move_and_draw(struct starfield * starfield)
339 int i;
340 for(i=0;i<starfield->nb_stars;++i)
342 star_move(&(starfield->tab[i]), starfield->z_move, starfield->color);
343 star_draw(&(starfield->tab[i]), starfield->z_move, starfield->color);
347 static struct starfield starfield;
349 int plugin_main(void)
351 char str_buffer[40];
352 int button, avg_peak, t_disp=0;
353 int font_h, font_w;
354 bool pulse=true;
355 rb->lcd_getstringsize("A", &font_w, &font_h);
356 starfield_init(&starfield);
357 starfield_add_stars(&starfield, INIT_STARS);
359 #if LCD_DEPTH > 1
360 rb->lcd_set_backdrop(NULL);
361 #endif
362 #ifdef HAVE_LCD_COLOR
363 rb->lcd_set_background(LCD_BLACK);
364 rb->lcd_set_foreground(LCD_WHITE);
365 #endif
367 while (true)
369 rb->sleep(1);
370 rb->lcd_clear_display();
372 #if ((CONFIG_CODEC == SWCODEC) || !defined(SIMULATOR) && \
373 ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)))
375 /* This will make the stars pulse to the music */
376 if(pulse==true){
378 /* Get the peaks. ( Borrowed from vu_meter ) */
379 #if (CONFIG_CODEC == SWCODEC)
380 int left_peak, right_peak;
381 rb->pcm_calculate_peaks(&left_peak, &right_peak);
382 #else
383 int left_peak = rb->mas_codec_readreg(0xC);
384 int right_peak = rb->mas_codec_readreg(0xD);
385 #endif
386 /* Devide peak data by 4098 to bring the max
387 value down from ~32k to 8 */
388 left_peak = left_peak/0x1000;
389 right_peak = right_peak/0x1000;
391 /* Make sure they dont stop */
392 if(left_peak<0x1)
393 left_peak = 0x1;
394 if(right_peak<0x1)
395 right_peak = 0x1;
397 /* And make sure they dont go over 8 */
398 if(left_peak>0x8)
399 left_peak = 0x8;
400 if(right_peak>0x8)
401 right_peak = 0x8;
403 /* We need the average of both chanels */
404 avg_peak = ( left_peak + right_peak )/2;
406 /* Set the speed to the peak meter */
407 starfield.z_move = avg_peak;
409 } /* if pulse */
410 #else
411 (void) avg_peak;
412 #endif
413 starfield_move_and_draw(&starfield);
415 #ifdef HAVE_LCD_COLOR
416 rb->lcd_set_foreground(LCD_WHITE);
417 #endif
419 /* if a parameter is updated (by the user), we must print it */
420 if (t_disp > 0)
422 --t_disp;
423 rb->snprintf(str_buffer, sizeof(str_buffer),
424 "star:%d speed:%d",
425 starfield.nb_stars,
426 starfield.z_move);
427 #ifdef HAVE_LCD_COLOR
428 rb->lcd_set_foreground(LCD_WHITE);
429 #endif
430 rb->lcd_putsxy(0, LCD_HEIGHT-font_h, str_buffer);
432 rb->lcd_update();
434 button = rb->button_get(false);
435 switch(button)
437 case (STARFIELD_INCREASE_ZMOVE):
438 case (STARFIELD_INCREASE_ZMOVE | BUTTON_REPEAT):
439 ++(starfield.z_move);
440 pulse=false;
441 t_disp=MSG_DISP_TIME;
442 break;
443 case (STARFIELD_DECREASE_ZMOVE):
444 case (STARFIELD_DECREASE_ZMOVE | BUTTON_REPEAT):
445 --(starfield.z_move);
446 pulse=false;
447 t_disp=MSG_DISP_TIME;
448 break;
449 case(STARFIELD_INCREASE_NB_STARS):
450 case(STARFIELD_INCREASE_NB_STARS | BUTTON_REPEAT):
451 starfield_add_stars(&starfield, STARFIELD_INCREASE_STEP);
452 t_disp=MSG_DISP_TIME;
453 break;
454 case(STARFIELD_DECREASE_NB_STARS):
455 case(STARFIELD_DECREASE_NB_STARS | BUTTON_REPEAT):
456 starfield_del_stars(&starfield, STARFIELD_INCREASE_STEP);
457 t_disp=MSG_DISP_TIME;
458 break;
459 #ifdef HAVE_LCD_COLOR
460 case(STARFIELD_TOGGLE_COLOR):
461 starfield.color=!starfield.color;
462 break;
463 #endif
464 #ifdef STARFIELD_RC_QUIT
465 case STARFIELD_RC_QUIT:
466 #endif
467 case(STARFIELD_QUIT):
468 case(SYS_USB_CONNECTED):
469 /* Turn on backlight timeout (revert to settings) */
470 backlight_use_settings(); /* backlight control in lib/helper.c*/
471 return PLUGIN_OK;
472 break;
477 /*************************** Plugin entry point ****************************/
479 enum plugin_status plugin_start(const void* parameter)
481 int ret;
483 (void)parameter;
484 /* Turn off backlight timeout */
485 backlight_force_on(); /* backlight control in lib/helper.c */
487 ret = plugin_main();
489 return ret;
492 #endif /* #ifdef HAVE_LCD_BITMAP */