Introduce __attribute__((unused)) (#defined to UNUSED_ATTR) to mark possibly unused...
[kugel-rb.git] / apps / plugins / plasma.c
blob94f1662569187ff102bc6b85f723aad9604050f4
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;
57 /* Key assignement, all bitmapped models */
58 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
59 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
60 #define PLASMA_QUIT BUTTON_MENU
61 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_FWD
62 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_BACK
64 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
65 #define PLASMA_QUIT BUTTON_POWER
66 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
67 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
69 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
70 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
71 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
72 (CONFIG_KEYPAD == SANSA_M200_PAD)
73 #define PLASMA_QUIT BUTTON_POWER
74 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
75 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
77 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
78 #define PLASMA_QUIT (BUTTON_HOME|BUTTON_REPEAT)
79 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
80 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
82 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
83 #define PLASMA_QUIT BUTTON_POWER
84 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
85 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
87 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
88 #define PLASMA_QUIT BUTTON_POWER
89 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
90 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
92 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
93 #define PLASMA_QUIT BUTTON_BACK
94 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
95 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
97 #elif (CONFIG_KEYPAD == MROBE100_PAD)
98 #define PLASMA_QUIT BUTTON_POWER
99 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
100 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
102 #elif (CONFIG_KEYPAD == IAUDIO_M3_PAD)
103 #define PLASMA_QUIT BUTTON_RC_REC
104 #define PLASMA_INCREASE_FREQUENCY BUTTON_RC_VOL_UP
105 #define PLASMA_DECREASE_FREQUENCY BUTTON_RC_VOL_DOWN
106 #define PLASMA_RC_QUIT BUTTON_REC
108 #elif (CONFIG_KEYPAD == COWOND2_PAD)
109 #define PLASMA_QUIT BUTTON_POWER
111 #elif (CONFIG_KEYPAD == IAUDIO67_PAD)
112 #define PLASMA_QUIT BUTTON_POWER
113 #define PLASMA_INCREASE_FREQUENCY BUTTON_RIGHT
114 #define PLASMA_DECREASE_FREQUENCY BUTTON_LEFT
115 #define PLASMA_RC_QUIT BUTTON_STOP
116 #define PLASMA_REGEN_COLORS BUTTON_PLAY
118 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
119 #define PLASMA_QUIT BUTTON_BACK
120 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
121 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
122 #define PLASMA_REGEN_COLORS BUTTON_SELECT
124 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
125 #define PLASMA_QUIT BUTTON_POWER
126 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
127 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
128 #define PLASMA_REGEN_COLORS BUTTON_SELECT
130 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
131 #define PLASMA_QUIT BUTTON_POWER
133 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
134 #define PLASMA_QUIT BUTTON_PLAY
135 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
136 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
137 #define PLASMA_REGEN_COLORS BUTTON_LEFT
139 #endif
141 #ifdef HAVE_TOUCHSCREEN
142 #ifndef PLASMA_QUIT
143 #define PLASMA_QUIT BUTTON_TOPLEFT
144 #endif
145 #ifndef PLASMA_INCREASE_FREQUENCY
146 #define PLASMA_INCREASE_FREQUENCY BUTTON_MIDRIGHT
147 #endif
148 #ifndef PLASMA_DECREASE_FREQUENCY
149 #define PLASMA_DECREASE_FREQUENCY BUTTON_MIDLEFT
150 #endif
151 #endif /* HAVE_TOUCHSCREEN */
153 #ifndef PLASMA_QUIT
154 #define PLASMA_QUIT BUTTON_OFF
155 #endif
156 #ifndef PLASMA_INCREASE_FREQUENCY
157 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
158 #endif
159 #ifndef PLASMA_DECREASE_FREQUENCY
160 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
161 #endif
163 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
164 #define PLASMA_RC_QUIT BUTTON_RC_STOP
165 #endif
167 /* FIXME: cleanup */
168 #ifdef HAVE_LCD_COLOR
169 #if CONFIG_KEYPAD == IAUDIO_X5M5_PAD
170 #define PLASMA_REGEN_COLORS BUTTON_PLAY
171 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
172 #define PLASMA_REGEN_COLORS BUTTON_PLAY
173 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
174 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
175 (CONFIG_KEYPAD == SANSA_FUZE_PAD)
176 #define PLASMA_REGEN_COLORS BUTTON_SELECT
177 #elif CONFIG_KEYPAD == IPOD_4G_PAD
178 #define PLASMA_REGEN_COLORS BUTTON_SELECT
179 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
180 #define PLASMA_REGEN_COLORS BUTTON_SELECT
181 #elif CONFIG_KEYPAD == GIGABEAT_PAD
182 #define PLASMA_REGEN_COLORS BUTTON_SELECT
183 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
184 #define PLASMA_REGEN_COLORS BUTTON_SELECT
185 #endif
186 #ifdef HAVE_TOUCHSCREEN
187 #ifndef PLASMA_REGEN_COLORS
188 #define PLASMA_REGEN_COLORS BUTTON_CENTER
189 #endif
190 #endif /* HAVE_TOUCHSCREEN */
191 #endif /* HAVE_LCD_COLOR */
193 #define WAV_AMP 90
196 * Main wave function so we don't have to re-calc the sine
197 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
198 * weirder looking plasmas!
201 static void wave_table_generate(void)
203 int i;
204 for (i=0;i<256;++i)
206 wave_array[i] = (unsigned char)((WAV_AMP
207 * (fp14_sin((i * 360 * plasma_frequency) / 256))) / 16384);
211 #ifdef HAVE_LCD_COLOR
212 /* Make a smooth colour cycle. */
213 void shades_generate(int time)
215 int i;
216 unsigned red, green, blue;
217 unsigned r = time * redfactor + redphase;
218 unsigned g = time * greenfactor + greenphase;
219 unsigned b = time * bluefactor + bluephase;
221 for(i=0; i < 256; ++i)
223 r &= 0xFF; g &= 0xFF; b &= 0xFF;
225 red = 2 * r;
226 if (red > 255)
227 red = 510 - red;
228 green = 2 * g;
229 if (green > 255)
230 green = 510 - green;
231 blue = 2 * b;
232 if (blue > 255)
233 blue= 510 - blue;
235 colours[i] = LCD_RGBPACK(red, green, blue);
237 r++; g++; b++;
240 #else
241 /* Make a smooth shade from black into white and back into black again. */
242 static void shades_generate(void)
244 int i, y;
246 for(i=0; i < 256; ++i)
248 y = 2 * i;
249 if (y > 255)
250 y = 510 - y;
251 colours[i] = y;
254 #endif
256 static void cleanup(UNUSED_ATTR void *parameter)
259 #ifndef HAVE_LCD_COLOR
260 grey_release();
261 #endif
262 /* Turn on backlight timeout (revert to settings) */
263 backlight_use_settings(); /* backlight control in lib/helper.c */
267 * Main function that also contain the main plasma
268 * algorithm.
271 int main(void)
273 plasma_frequency = 1;
274 int button, x, y;
275 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z;
276 #ifdef HAVE_LCD_COLOR
277 fb_data *ptr;
278 int time=0;
279 #else
280 unsigned char *ptr;
281 #endif
283 /*Generate the neccesary pre calced stuff*/
284 wave_table_generate();
286 #ifndef HAVE_LCD_COLOR
287 shades_generate(); /* statically */
289 /* get the remainder of the plugin buffer */
290 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
292 grey_init(gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL);
293 /* switch on greyscale overlay */
294 grey_show(true);
295 #endif
296 sp1 = 4;
297 sp2 = 2;
298 sp3 = 4;
299 sp4 = 2;
300 p1=p2=p3=p4=0;
301 while (true)
303 #ifdef HAVE_LCD_COLOR
304 shades_generate(time++); /* dynamically */
305 ptr = rb->lcd_framebuffer;
306 #else
307 ptr = greybuffer;
308 #endif
309 t1=p1;
310 t2=p2;
311 for(y = 0; y < LCD_HEIGHT; ++y)
313 t3=p3;
314 t4=p4;
315 for(x = 0; x < LCD_WIDTH; ++x)
317 z = wave_array[t1] + wave_array[t2] + wave_array[t3]
318 + wave_array[t4];
319 *ptr++ = colours[z];
320 t3+=1;
321 t4+=2;
323 t1+=2;
324 t2+=1;
325 rb->yield();
327 p1+=sp1;
328 p2-=sp2;
329 p3+=sp3;
330 p4-=sp4;
331 #ifdef HAVE_LCD_COLOR
332 rb->lcd_update();
333 #else
334 grey_ub_gray_bitmap(greybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
335 #endif
337 button = rb->button_get(false);
339 switch(button)
341 #ifdef PLASMA_RC_QUIT
342 case PLASMA_RC_QUIT:
343 #endif
344 case(PLASMA_QUIT):
345 cleanup(NULL);
346 return PLUGIN_OK;
347 break;
349 case (PLASMA_INCREASE_FREQUENCY):
350 ++plasma_frequency;
351 wave_table_generate();
352 break;
354 case (PLASMA_DECREASE_FREQUENCY):
355 if(plasma_frequency>1)
357 --plasma_frequency;
358 wave_table_generate();
360 break;
361 #ifdef HAVE_LCD_COLOR
362 case (PLASMA_REGEN_COLORS):
363 redfactor=rb->rand()%4;
364 greenfactor=rb->rand()%4;
365 bluefactor=rb->rand()%4;
366 redphase=rb->rand()%256;
367 greenphase=rb->rand()%256;
368 bluephase=rb->rand()%256;
369 break;
370 #endif
372 default:
373 if (rb->default_event_handler_ex(button, cleanup, NULL)
374 == SYS_USB_CONNECTED)
375 return PLUGIN_USB_CONNECTED;
376 break;
381 /*************************** Plugin entry point ****************************/
383 enum plugin_status plugin_start(UNUSED_ATTR const void* parameter)
385 int ret;
387 #if LCD_DEPTH > 1
388 rb->lcd_set_backdrop(NULL);
389 #endif
390 /* Turn off backlight timeout */
391 backlight_force_on(); /* backlight control in lib/helper.c */
393 ret = main();
395 return ret;
398 #endif /* HAVE_LCD_BITMAP */