drop 'req' from gas macro, not available in binutils 2.16
[kugel-rb.git] / apps / plugins / plasma.c
blobadf7ab532a9ca039ae858cd1508ba7661beff326
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Plasma demo plugin
12 * My crack at making a 80's style retro plasma effect for the fantastic
13 * rockbox!
14 * Okay, I could've hard-coded the sine wave values, I just wanted the
15 * challange of calculating them! silly: maybe, fun: yes!
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
25 ****************************************************************************/
27 #include "plugin.h"
28 #include "lib/helper.h"
30 #ifdef HAVE_LCD_BITMAP
32 #ifndef HAVE_LCD_COLOR
33 #include "lib/grey.h"
34 #endif
35 #include "lib/fixedpoint.h"
37 PLUGIN_HEADER
39 /******************************* Globals ***********************************/
41 static unsigned char wave_array[256]; /* Pre calculated wave array */
42 #ifdef HAVE_LCD_COLOR
43 static fb_data colours[256]; /* Smooth transition of shades */
44 static int redfactor = 1, greenfactor = 2, bluefactor = 3;
45 static int redphase = 0, greenphase = 50, bluephase = 100;
46 /* lower chance of gray at regular intervals */
47 #else
48 GREY_INFO_STRUCT
49 static unsigned char colours[256]; /* Smooth transition of shades */
50 static unsigned char greybuffer[LCD_HEIGHT*LCD_WIDTH]; /* off screen buffer */
51 static unsigned char *gbuf;
52 static size_t gbuf_size = 0;
53 #endif
54 static unsigned char sp1, sp2, sp3, sp4; /* Speed of plasma */
55 static int plasma_frequency;
56 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
57 static bool boosted = false;
58 #endif
60 /* Key assignement, all bitmapped models */
61 #if (CONFIG_KEYPAD == RECORDER_PAD) || (CONFIG_KEYPAD == ONDIO_PAD)
62 #define PLASMA_QUIT BUTTON_OFF
63 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
64 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
66 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
67 #define PLASMA_QUIT BUTTON_OFF
68 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
69 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
70 #define PLASMA_REGEN_COLORS BUTTON_SELECT
71 #define PLASMA_RC_QUIT BUTTON_RC_STOP
73 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
74 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
75 #define PLASMA_QUIT BUTTON_MENU
76 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_FWD
77 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_BACK
78 #define PLASMA_REGEN_COLORS BUTTON_SELECT
80 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
81 #define PLASMA_QUIT BUTTON_POWER
82 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
83 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
84 #define PLASMA_REGEN_COLORS BUTTON_SELECT
86 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
87 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
88 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
89 (CONFIG_KEYPAD == SANSA_M200_PAD)
90 #define PLASMA_QUIT BUTTON_POWER
91 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
92 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
93 #define PLASMA_REGEN_COLORS BUTTON_SELECT
95 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
96 #define PLASMA_QUIT (BUTTON_HOME|BUTTON_REPEAT)
97 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
98 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
99 #define PLASMA_REGEN_COLORS BUTTON_SELECT
101 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
102 #define PLASMA_QUIT BUTTON_POWER
103 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
104 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
105 #define PLASMA_REGEN_COLORS BUTTON_PLAY
107 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
108 #define PLASMA_QUIT BUTTON_POWER
109 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
110 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
111 #define PLASMA_REGEN_COLORS BUTTON_PLAY
113 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
114 #define PLASMA_QUIT BUTTON_BACK
115 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
116 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
117 #define PLASMA_REGEN_COLORS BUTTON_SELECT
119 #elif (CONFIG_KEYPAD == MROBE100_PAD)
120 #define PLASMA_QUIT BUTTON_POWER
121 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
122 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
124 #elif (CONFIG_KEYPAD == IAUDIO_M3_PAD)
125 #define PLASMA_QUIT BUTTON_RC_REC
126 #define PLASMA_INCREASE_FREQUENCY BUTTON_RC_VOL_UP
127 #define PLASMA_DECREASE_FREQUENCY BUTTON_RC_VOL_DOWN
128 #define PLASMA_RC_QUIT BUTTON_REC
130 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
131 #define PLASMA_QUIT BUTTON_POWER
133 #elif (CONFIG_KEYPAD == IAUDIO67_PAD)
134 #define PLASMA_QUIT BUTTON_POWER
135 #define PLASMA_INCREASE_FREQUENCY BUTTON_RIGHT
136 #define PLASMA_DECREASE_FREQUENCY BUTTON_LEFT
137 #define PLASMA_REGEN_COLORS BUTTON_PLAY
138 #define PLASMA_RC_QUIT BUTTON_STOP
140 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
141 #define PLASMA_QUIT BUTTON_BACK
142 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
143 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
144 #define PLASMA_REGEN_COLORS BUTTON_SELECT
146 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
147 #define PLASMA_QUIT BUTTON_POWER
148 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
149 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
150 #define PLASMA_REGEN_COLORS BUTTON_SELECT
152 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
153 #define PLASMA_QUIT BUTTON_POWER
154 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
155 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
156 #define PLASMA_REGEN_COLORS BUTTON_PLAY
158 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || (CONFIG_KEYPAD == ONDAVX777_PAD)
159 #define PLASMA_QUIT BUTTON_POWER
161 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
162 #define PLASMA_QUIT BUTTON_PLAY
163 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
164 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
165 #define PLASMA_REGEN_COLORS BUTTON_LEFT
167 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
168 #define PLASMA_QUIT BUTTON_REC
169 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
170 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
171 #define PLASMA_REGEN_COLORS BUTTON_PLAY
173 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
174 #define PLASMA_QUIT (BUTTON_REC|BUTTON_PLAY)
175 #define PLASMA_INCREASE_FREQUENCY BUTTON_VOL_DOWN
176 #define PLASMA_DECREASE_FREQUENCY BUTTON_VOL_UP
177 #define PLASMA_REGEN_COLORS BUTTON_PLAY
179 #endif
181 #ifdef HAVE_TOUCHSCREEN
182 #ifndef PLASMA_QUIT
183 #define PLASMA_QUIT BUTTON_TOPLEFT
184 #endif
185 #ifndef PLASMA_INCREASE_FREQUENCY
186 #define PLASMA_INCREASE_FREQUENCY BUTTON_MIDRIGHT
187 #endif
188 #ifndef PLASMA_DECREASE_FREQUENCY
189 #define PLASMA_DECREASE_FREQUENCY BUTTON_MIDLEFT
190 #endif
191 #ifdef HAVE_LCD_COLOR
192 #ifndef PLASMA_REGEN_COLORS
193 #define PLASMA_REGEN_COLORS BUTTON_CENTER
194 #endif
195 #endif /* HAVE_LCD_COLOR */
196 #endif /* HAVE_TOUCHSCREEN */
198 #define WAV_AMP 90
201 * Main wave function so we don't have to re-calc the sine
202 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
203 * weirder looking plasmas!
206 static void wave_table_generate(void)
208 int i;
209 for (i=0;i<256;++i)
211 wave_array[i] = (unsigned char)((WAV_AMP
212 * (fp14_sin((i * 360 * plasma_frequency) / 256))) / 16384);
216 #ifdef HAVE_LCD_COLOR
217 /* Make a smooth colour cycle. */
218 void shades_generate(int time)
220 int i;
221 unsigned red, green, blue;
222 unsigned r = time * redfactor + redphase;
223 unsigned g = time * greenfactor + greenphase;
224 unsigned b = time * bluefactor + bluephase;
226 for(i=0; i < 256; ++i)
228 r &= 0xFF; g &= 0xFF; b &= 0xFF;
230 red = 2 * r;
231 if (red > 255)
232 red = 510 - red;
233 green = 2 * g;
234 if (green > 255)
235 green = 510 - green;
236 blue = 2 * b;
237 if (blue > 255)
238 blue= 510 - blue;
240 colours[i] = LCD_RGBPACK(red, green, blue);
242 r++; g++; b++;
244 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
245 rb->lcd_pal256_update_pal(colours);
246 #endif
248 #else
249 /* Make a smooth shade from black into white and back into black again. */
250 static void shades_generate(void)
252 int i, y;
254 for(i=0; i < 256; ++i)
256 y = 2 * i;
257 if (y > 255)
258 y = 510 - y;
259 colours[i] = y;
262 #endif
264 void cleanup(void *parameter)
266 (void)parameter;
268 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
269 if (boosted)
270 rb->cpu_boost(false);
271 #endif
272 #ifndef HAVE_LCD_COLOR
273 grey_release();
274 #endif
275 /* Turn on backlight timeout (revert to settings) */
276 backlight_use_settings(); /* backlight control in lib/helper.c */
280 * Main function that also contain the main plasma
281 * algorithm.
284 int main(void)
286 plasma_frequency = 1;
287 int button, delay, x, y;
288 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z,z0;
289 long last_tick = *rb->current_tick;
290 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
291 int cumulated_lag = 0;
292 #endif
293 #ifdef HAVE_LCD_COLOR
294 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
295 unsigned char *ptr;
296 #else
297 fb_data *ptr;
298 #endif
299 int time=0;
300 #else
301 unsigned char *ptr;
302 #endif
304 /*Generate the neccesary pre calced stuff*/
305 wave_table_generate();
307 #ifndef HAVE_LCD_COLOR
308 shades_generate(); /* statically */
310 /* get the remainder of the plugin buffer */
311 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
313 grey_init(gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL);
314 /* switch on greyscale overlay */
315 grey_show(true);
316 #endif
317 sp1 = 4;
318 sp2 = 2;
319 sp3 = 4;
320 sp4 = 2;
321 p1=p2=p3=p4=0;
322 while (true)
324 #ifdef HAVE_LCD_COLOR
325 shades_generate(time++); /* dynamically */
326 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
327 ptr = (unsigned char*)rb->lcd_framebuffer;
328 #else
329 ptr = rb->lcd_framebuffer;
330 #endif
332 #else
333 ptr = greybuffer;
334 #endif
335 t1=p1;
336 t2=p2;
337 for(y = 0; y < LCD_HEIGHT; ++y)
339 t3=p3;
340 t4=p4;
341 z0 = wave_array[t1] + wave_array[t2];
342 for(x = 0; x < LCD_WIDTH; ++x)
344 z = z0 + wave_array[t3] + wave_array[t4];
345 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
346 *ptr++ = z;
347 #else
348 *ptr++ = colours[z];
349 #endif
350 t3+=1;
351 t4+=2;
353 t1+=2;
354 t2+=1;
355 rb->yield();
357 p1+=sp1;
358 p2-=sp2;
359 p3+=sp3;
360 p4-=sp4;
361 #ifdef HAVE_LCD_COLOR
362 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
363 rb->lcd_blit_pal256( (unsigned char*)rb->lcd_framebuffer,
364 0,0,0,0,LCD_WIDTH,LCD_HEIGHT);
365 #else
366 rb->lcd_update();
367 #endif
368 #else
369 grey_ub_gray_bitmap(greybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
370 #endif
372 delay = last_tick - *rb->current_tick + HZ/33;
373 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
374 if (!boosted && delay < 0)
376 cumulated_lag -= delay; /* proportional increase */
377 if (cumulated_lag >= HZ)
378 rb->cpu_boost(boosted = true);
380 else if (boosted && delay > 1) /* account for jitter */
382 if (--cumulated_lag <= 0) /* slow decrease */
383 rb->cpu_boost(boosted = false);
385 #endif
386 button = rb->button_get_w_tmo(MAX(0, delay));
387 last_tick = *rb->current_tick;
389 switch(button)
391 #ifdef PLASMA_RC_QUIT
392 case PLASMA_RC_QUIT:
393 #endif
394 case(PLASMA_QUIT):
395 cleanup(NULL);
396 return PLUGIN_OK;
397 break;
399 case (PLASMA_INCREASE_FREQUENCY):
400 ++plasma_frequency;
401 wave_table_generate();
402 break;
404 case (PLASMA_DECREASE_FREQUENCY):
405 if(plasma_frequency>1)
407 --plasma_frequency;
408 wave_table_generate();
410 break;
411 #ifdef HAVE_LCD_COLOR
412 case (PLASMA_REGEN_COLORS):
413 redfactor=rb->rand()%4;
414 greenfactor=rb->rand()%4;
415 bluefactor=rb->rand()%4;
416 redphase=rb->rand()%256;
417 greenphase=rb->rand()%256;
418 bluephase=rb->rand()%256;
419 break;
420 #endif
422 default:
423 if (rb->default_event_handler_ex(button, cleanup, NULL)
424 == SYS_USB_CONNECTED)
425 return PLUGIN_USB_CONNECTED;
426 break;
431 /*************************** Plugin entry point ****************************/
433 enum plugin_status plugin_start(const void* parameter)
435 int ret;
437 (void)parameter;
438 #if LCD_DEPTH > 1
439 rb->lcd_set_backdrop(NULL);
440 #endif
441 /* Turn off backlight timeout */
442 backlight_force_on(); /* backlight control in lib/helper.c */
444 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
445 rb->lcd_set_mode(LCD_MODE_PAL256);
446 #endif
448 ret = main();
450 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
451 rb->lcd_set_mode(LCD_MODE_RGB565);
452 #endif
454 return ret;
457 #endif /* HAVE_LCD_BITMAP */