Gigabeat S: Do simple direct keypad scanning rather than triggering a separate scan...
[kugel-rb.git] / apps / plugins / starfield.c
blobd06998ee2c536dc30cba34e5cef1fb9c8558b9f3
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"
22 #include "lib/pluginlib_exit.h"
24 /******************************* Globals ***********************************/
26 /* Key assignement */
27 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
28 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
29 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
30 #define STARFIELD_QUIT BUTTON_MENU
31 #define STARFIELD_INCREASE_ZMOVE BUTTON_SCROLL_FWD
32 #define STARFIELD_DECREASE_ZMOVE BUTTON_SCROLL_BACK
33 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
34 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
35 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
37 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
38 #define STARFIELD_QUIT BUTTON_POWER
39 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
40 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
41 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
42 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
43 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
45 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
46 #define STARFIELD_QUIT BUTTON_POWER
47 #define STARFIELD_INCREASE_ZMOVE BUTTON_SCROLL_UP
48 #define STARFIELD_DECREASE_ZMOVE BUTTON_SCROLL_DOWN
49 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
50 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
51 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
53 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
54 #define STARFIELD_QUIT BUTTON_POWER
55 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
56 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
57 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
58 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
59 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
61 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
62 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
63 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
64 (CONFIG_KEYPAD == SANSA_M200_PAD)
65 #define STARFIELD_QUIT BUTTON_POWER
66 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
67 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
68 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
69 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
70 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
72 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
73 #define STARFIELD_QUIT (BUTTON_HOME|BUTTON_REPEAT)
74 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
75 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
76 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
77 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
78 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
80 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
81 #define STARFIELD_QUIT BUTTON_BACK
82 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
83 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
84 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
85 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
86 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
88 #elif (CONFIG_KEYPAD == MROBE100_PAD)
89 #define STARFIELD_QUIT BUTTON_POWER
90 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
91 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
92 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
93 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
94 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
96 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
97 #define STARFIELD_QUIT BUTTON_RC_REC
98 #define STARFIELD_INCREASE_ZMOVE BUTTON_RC_VOL_UP
99 #define STARFIELD_DECREASE_ZMOVE BUTTON_RC_VOL_DOWN
100 #define STARFIELD_INCREASE_NB_STARS BUTTON_RC_FF
101 #define STARFIELD_DECREASE_NB_STARS BUTTON_RC_REW
102 #define STARFIELD_TOGGLE_COLOR BUTTON_RC_MODE
104 #elif CONFIG_KEYPAD == IAUDIO67_PAD
105 #define STARFIELD_QUIT BUTTON_POWER
106 #define STARFIELD_INCREASE_ZMOVE BUTTON_STOP
107 #define STARFIELD_DECREASE_ZMOVE BUTTON_PLAY
108 #define STARFIELD_INCREASE_NB_STARS BUTTON_LEFT
109 #define STARFIELD_DECREASE_NB_STARS BUTTON_RIGHT
110 #define STARFIELD_TOGGLE_COLOR BUTTON_MENU
112 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
113 #define STARFIELD_QUIT BUTTON_POWER
115 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
116 #define STARFIELD_QUIT BUTTON_BACK
117 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
118 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
119 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
120 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
121 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
123 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
124 #define STARFIELD_QUIT BUTTON_POWER
125 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
126 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
127 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
128 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
129 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
131 #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
132 #define STARFIELD_QUIT BUTTON_POWER
133 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
134 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
135 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
136 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
137 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
139 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
140 #define STARFIELD_QUIT BUTTON_POWER
141 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
142 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
143 #define STARFIELD_INCREASE_NB_STARS BUTTON_NEXT
144 #define STARFIELD_DECREASE_NB_STARS BUTTON_PREV
145 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
147 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || (CONFIG_KEYPAD == ONDAVX777_PAD) || (CONFIG_KEYPAD == MROBE500_PAD)
148 #define STARFIELD_QUIT BUTTON_POWER
150 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
151 #define STARFIELD_QUIT BUTTON_FFWD
152 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
153 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
154 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
155 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
156 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
158 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
159 #define STARFIELD_QUIT BUTTON_REC
160 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
161 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
162 #define STARFIELD_INCREASE_NB_STARS BUTTON_NEXT
163 #define STARFIELD_DECREASE_NB_STARS BUTTON_PREV
164 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
166 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
167 #define STARFIELD_QUIT (BUTTON_REC|BUTTON_PLAY)
168 #define STARFIELD_INCREASE_ZMOVE BUTTON_VOL_UP
169 #define STARFIELD_DECREASE_ZMOVE BUTTON_VOL_DOWN
170 #define STARFIELD_INCREASE_NB_STARS BUTTON_FF
171 #define STARFIELD_DECREASE_NB_STARS BUTTON_REW
172 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
174 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
175 #define STARFIELD_QUIT (BUTTON_REC|BUTTON_REPEAT)
176 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
177 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
178 #define STARFIELD_INCREASE_NB_STARS BUTTON_FF
179 #define STARFIELD_DECREASE_NB_STARS BUTTON_REW
180 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
182 #endif
184 #ifdef HAVE_TOUCHSCREEN
185 #ifndef STARFIELD_QUIT
186 #define STARFIELD_QUIT BUTTON_TOPLEFT
187 #endif
188 #ifndef STARFIELD_INCREASE_ZMOVE
189 #define STARFIELD_INCREASE_ZMOVE BUTTON_TOPMIDDLE
190 #endif
191 #ifndef STARFIELD_DECREASE_ZMOVE
192 #define STARFIELD_DECREASE_ZMOVE BUTTON_BOTTOMMIDDLE
193 #endif
194 #ifndef STARFIELD_INCREASE_NB_STARS
195 #define STARFIELD_INCREASE_NB_STARS BUTTON_MIDRIGHT
196 #endif
197 #ifndef STARFIELD_DECREASE_NB_STARS
198 #define STARFIELD_DECREASE_NB_STARS BUTTON_MIDLEFT
199 #endif
200 #ifndef STARFIELD_TOGGLE_COLOR
201 #define STARFIELD_TOGGLE_COLOR BUTTON_CENTER
202 #endif
203 #endif
205 #ifndef STARFIELD_QUIT
206 #define STARFIELD_QUIT BUTTON_OFF
207 #endif
208 #ifndef STARFIELD_INCREASE_ZMOVE
209 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
210 #endif
211 #ifndef STARFIELD_DECREASE_ZMOVE
212 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
213 #endif
214 #ifndef STARFIELD_INCREASE_NB_STARS
215 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
216 #endif
217 #ifndef STARFIELD_DECREASE_NB_STARS
218 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
219 #endif
221 #ifndef STARFIELD_TOGGLE_COLOR
222 #ifdef BUTTON_SELECT
223 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
224 #else
225 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
226 #endif
227 #endif
228 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
229 #define STARFIELD_RC_QUIT BUTTON_RC_STOP
230 #endif
233 #define LCD_CENTER_X (LCD_WIDTH/2)
234 #define LCD_CENTER_Y (LCD_HEIGHT/2)
235 #define Z_MAX_DIST 100
238 #define MAX_STARS (LCD_WIDTH*LCD_HEIGHT*20)/100
239 #define INIT_STARS 200
240 #define STARFIELD_INCREASE_STEP 50
241 #define INIT_SPACE_SPEED 1
242 #define STAR_MAX_VELOCITY 2
244 * max 3d coord in the 2d screen :
245 * example with x
246 * x2d=x3d/z+LCD_CENTER_X (+LCD_CENTER_X to center ...)
247 * so
248 * max_x2d=max_x3d/max_z+LCD_CENTER_X
249 * max_x3d=(max_x2d-LCD_CENTER_X)*max_z
250 * with
251 * max_x2d = LCD_WIDTH
252 * max_z = Z_MAX_DIST
253 * we have now
254 * max_x3d=(LCD_WIDTH-LCD_CENTER_X)*Z_MAX_DIST
255 * max_x3d=LCD_CENTER_X*Z_MAX_DIST
258 #define MAX_INIT_STAR_X LCD_CENTER_X*Z_MAX_DIST
259 #define MAX_INIT_STAR_Y LCD_CENTER_Y*Z_MAX_DIST
261 #define MSG_DISP_TIME 30
264 * Each star's stuffs
266 struct star
268 int x,y,z;
269 int velocity;
270 #ifdef HAVE_LCD_COLOR
271 int color;
272 #endif
275 static inline void star_init(struct star * star, int z_move, bool color)
277 star->velocity=rb->rand() % STAR_MAX_VELOCITY+1;
278 /* choose x between -MAX_INIT_STAR_X and MAX_INIT_STAR_X */
279 star->x=rb->rand() % (2*MAX_INIT_STAR_X)-MAX_INIT_STAR_X;
280 star->y=rb->rand() % (2*MAX_INIT_STAR_Y)-MAX_INIT_STAR_Y;
281 #ifdef HAVE_LCD_COLOR
282 if(color)
283 star->color=LCD_RGBPACK(rb->rand()%128+128,rb->rand()%128+128,
284 rb->rand()%128+128);
285 else
286 star->color=LCD_WHITE;
287 #else
288 (void)color;
289 #endif
290 if(z_move>=0)
291 star->z=Z_MAX_DIST;
292 else
293 star->z=rb->rand() %Z_MAX_DIST/2+1;
296 static inline void star_move(struct star * star, int z_move, bool color)
298 star->z -= z_move*star->velocity;
299 if (star->z <= 0 || star->z > Z_MAX_DIST)
300 star_init(star, z_move, color);
303 static inline void star_draw(struct star * star, int z_move, bool color)
305 int x_draw, y_draw;
307 * 3d -> 2d : projection on the screen : x2d=x3d/z (thales theorem)
308 * we put the star in the center of the screen
310 x_draw = star->x / star->z + LCD_CENTER_X;
311 if (x_draw < 1 || x_draw >= LCD_WIDTH)
313 star_init(star, z_move, color);
314 return;
316 y_draw = star->y / star->z + LCD_CENTER_Y;
317 if (y_draw < 1 || y_draw >= LCD_HEIGHT)
319 star_init(star, z_move, color);
320 return;
323 #ifdef HAVE_LCD_COLOR
324 rb->lcd_set_foreground(star->color);
325 #endif
327 rb->lcd_drawpixel(x_draw, y_draw);
328 if(star->z<5*Z_MAX_DIST/6)
330 rb->lcd_drawpixel(x_draw, y_draw - 1);
331 rb->lcd_drawpixel(x_draw - 1, y_draw);
332 if(star->z<Z_MAX_DIST/2)
334 rb->lcd_drawpixel(x_draw + 1, y_draw);
335 rb->lcd_drawpixel(x_draw, y_draw + 1);
341 * Whole starfield operations
343 struct starfield
345 struct star tab[MAX_STARS];
346 int nb_stars;
347 int z_move;
348 bool color;
351 static inline void starfield_init(struct starfield * starfield)
353 starfield->nb_stars=0;
354 starfield->z_move=INIT_SPACE_SPEED;
355 starfield->color=false;
358 static inline void starfield_add_stars(struct starfield * starfield,
359 int nb_to_add)
361 int i, old_nb_stars;
362 old_nb_stars=starfield->nb_stars;
363 starfield->nb_stars+=nb_to_add;
365 if(starfield->nb_stars > MAX_STARS)
366 starfield->nb_stars=MAX_STARS;
368 for( i=old_nb_stars ; i < starfield->nb_stars ; ++i )
370 star_init( &(starfield->tab[i]), starfield->z_move, starfield->color );
374 static inline void starfield_del_stars(struct starfield * starfield,
375 int nb_to_add)
377 starfield->nb_stars-=nb_to_add;
378 if(starfield->nb_stars<0)
379 starfield->nb_stars=0;
382 static inline void starfield_move_and_draw(struct starfield * starfield)
384 int i;
385 for(i=0;i<starfield->nb_stars;++i)
387 star_move(&(starfield->tab[i]), starfield->z_move, starfield->color);
388 star_draw(&(starfield->tab[i]), starfield->z_move, starfield->color);
392 static struct starfield starfield;
394 int plugin_main(void)
396 int button, avg_peak, t_disp=0;
397 int font_h, font_w;
398 bool pulse=true;
399 rb->lcd_getstringsize("A", &font_w, &font_h);
400 starfield_init(&starfield);
401 starfield_add_stars(&starfield, INIT_STARS);
403 #if LCD_DEPTH > 1
404 rb->lcd_set_backdrop(NULL);
405 #endif
406 #ifdef HAVE_LCD_COLOR
407 rb->lcd_set_background(LCD_BLACK);
408 rb->lcd_set_foreground(LCD_WHITE);
409 #endif
411 while (true)
413 rb->sleep(1);
414 rb->lcd_clear_display();
416 #if ((CONFIG_CODEC == SWCODEC) || !defined(SIMULATOR) && \
417 ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)))
419 /* This will make the stars pulse to the music */
420 if(pulse){
422 /* Get the peaks. ( Borrowed from vu_meter ) */
423 #if (CONFIG_CODEC == SWCODEC)
424 int left_peak, right_peak;
425 rb->pcm_calculate_peaks(&left_peak, &right_peak);
426 #else
427 int left_peak = rb->mas_codec_readreg(0xC);
428 int right_peak = rb->mas_codec_readreg(0xD);
429 #endif
430 /* Devide peak data by 4098 to bring the max
431 value down from ~32k to 8 */
432 left_peak = left_peak/0x1000;
433 right_peak = right_peak/0x1000;
435 /* Make sure they dont stop */
436 if(left_peak<0x1)
437 left_peak = 0x1;
438 if(right_peak<0x1)
439 right_peak = 0x1;
441 /* And make sure they dont go over 8 */
442 if(left_peak>0x8)
443 left_peak = 0x8;
444 if(right_peak>0x8)
445 right_peak = 0x8;
447 /* We need the average of both chanels */
448 avg_peak = ( left_peak + right_peak )/2;
450 /* Set the speed to the peak meter */
451 starfield.z_move = avg_peak;
453 } /* if pulse */
454 #else
455 (void) avg_peak;
456 #endif
457 starfield_move_and_draw(&starfield);
459 #ifdef HAVE_LCD_COLOR
460 rb->lcd_set_foreground(LCD_WHITE);
461 #endif
463 /* if a parameter is updated (by the user), we must print it */
464 if (t_disp > 0)
466 --t_disp;
467 #ifdef HAVE_LCD_COLOR
468 rb->lcd_set_foreground(LCD_WHITE);
469 #endif
470 rb->lcd_putsxyf(0, LCD_HEIGHT-font_h, "star:%d speed:%d",
471 starfield.nb_stars, starfield.z_move);
473 rb->lcd_update();
475 button = rb->button_get(false);
476 switch(button)
478 case (STARFIELD_INCREASE_ZMOVE):
479 case (STARFIELD_INCREASE_ZMOVE | BUTTON_REPEAT):
480 ++(starfield.z_move);
481 pulse=false;
482 t_disp=MSG_DISP_TIME;
483 break;
484 case (STARFIELD_DECREASE_ZMOVE):
485 case (STARFIELD_DECREASE_ZMOVE | BUTTON_REPEAT):
486 --(starfield.z_move);
487 pulse=false;
488 t_disp=MSG_DISP_TIME;
489 break;
490 case(STARFIELD_INCREASE_NB_STARS):
491 case(STARFIELD_INCREASE_NB_STARS | BUTTON_REPEAT):
492 starfield_add_stars(&starfield, STARFIELD_INCREASE_STEP);
493 t_disp=MSG_DISP_TIME;
494 break;
495 case(STARFIELD_DECREASE_NB_STARS):
496 case(STARFIELD_DECREASE_NB_STARS | BUTTON_REPEAT):
497 starfield_del_stars(&starfield, STARFIELD_INCREASE_STEP);
498 t_disp=MSG_DISP_TIME;
499 break;
500 #ifdef HAVE_LCD_COLOR
501 case(STARFIELD_TOGGLE_COLOR):
502 starfield.color=!starfield.color;
503 break;
504 #endif
505 #ifdef STARFIELD_RC_QUIT
506 case STARFIELD_RC_QUIT:
507 #endif
508 case(STARFIELD_QUIT):
509 return PLUGIN_OK;
510 break;
511 default:
512 exit_on_usb(button);
513 break;
518 /*************************** Plugin entry point ****************************/
520 enum plugin_status plugin_start(const void* parameter)
522 int ret;
524 (void)parameter;
525 /* Turn off backlight timeout */
526 backlight_force_on(); /* backlight control in lib/helper.c */
528 ret = plugin_main();
530 /* Turn on backlight timeout (revert to settings) */
531 backlight_use_settings(); /* backlight control in lib/helper.c*/
533 return ret;