Minor corrections to the .colours file editing; added .colours to the list of support...
[kugel-rb.git] / apps / plugins / plasma.c
bloba172372da492c7668ca0241f13b3e6949166f0b7
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 (CONFIG_KEYPAD == SANSA_FUZE_PAD)
74 #define PLASMA_QUIT BUTTON_POWER
75 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
76 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
78 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
79 #define PLASMA_QUIT BUTTON_POWER
80 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
81 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
83 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
84 #define PLASMA_QUIT BUTTON_POWER
85 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
86 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
88 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
89 #define PLASMA_QUIT BUTTON_BACK
90 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
91 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
93 #elif (CONFIG_KEYPAD == MROBE100_PAD)
94 #define PLASMA_QUIT BUTTON_POWER
95 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
96 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
98 #elif (CONFIG_KEYPAD == IAUDIO_M3_PAD)
99 #define PLASMA_QUIT BUTTON_RC_REC
100 #define PLASMA_INCREASE_FREQUENCY BUTTON_RC_VOL_UP
101 #define PLASMA_DECREASE_FREQUENCY BUTTON_RC_VOL_DOWN
102 #define PLASMA_RC_QUIT BUTTON_REC
104 #elif (CONFIG_KEYPAD == COWOND2_PAD)
105 #define PLASMA_QUIT BUTTON_POWER
107 #elif (CONFIG_KEYPAD == IAUDIO67_PAD)
108 #define PLASMA_QUIT BUTTON_POWER
109 #define PLASMA_INCREASE_FREQUENCY BUTTON_RIGHT
110 #define PLASMA_DECREASE_FREQUENCY BUTTON_LEFT
111 #define PLASMA_RC_QUIT BUTTON_STOP
112 #define PLASMA_REGEN_COLORS BUTTON_PLAY
114 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
115 #define PLASMA_QUIT BUTTON_BACK
116 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
117 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
118 #define PLASMA_REGEN_COLORS BUTTON_SELECT
120 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
121 #define PLASMA_QUIT BUTTON_POWER
122 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
123 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
124 #define PLASMA_REGEN_COLORS BUTTON_SELECT
126 #endif
128 #ifdef HAVE_TOUCHSCREEN
129 #ifndef PLASMA_QUIT
130 #define PLASMA_QUIT BUTTON_TOPLEFT
131 #endif
132 #ifndef PLASMA_INCREASE_FREQUENCY
133 #define PLASMA_INCREASE_FREQUENCY BUTTON_MIDRIGHT
134 #endif
135 #ifndef PLASMA_DECREASE_FREQUENCY
136 #define PLASMA_DECREASE_FREQUENCY BUTTON_MIDLEFT
137 #endif
138 #endif /* HAVE_TOUCHSCREEN */
140 #ifndef PLASMA_QUIT
141 #define PLASMA_QUIT BUTTON_OFF
142 #endif
143 #ifndef PLASMA_INCREASE_FREQUENCY
144 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
145 #endif
146 #ifndef PLASMA_DECREASE_FREQUENCY
147 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
148 #endif
150 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
151 #define PLASMA_RC_QUIT BUTTON_RC_STOP
152 #endif
154 /* FIXME: cleanup */
155 #ifdef HAVE_LCD_COLOR
156 #if CONFIG_KEYPAD == IAUDIO_X5M5_PAD
157 #define PLASMA_REGEN_COLORS BUTTON_PLAY
158 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
159 #define PLASMA_REGEN_COLORS BUTTON_PLAY
160 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
161 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
162 (CONFIG_KEYPAD == SANSA_FUZE_PAD)
163 #define PLASMA_REGEN_COLORS BUTTON_SELECT
164 #elif CONFIG_KEYPAD == IPOD_4G_PAD
165 #define PLASMA_REGEN_COLORS BUTTON_SELECT
166 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
167 #define PLASMA_REGEN_COLORS BUTTON_SELECT
168 #elif CONFIG_KEYPAD == GIGABEAT_PAD
169 #define PLASMA_REGEN_COLORS BUTTON_SELECT
170 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
171 #define PLASMA_REGEN_COLORS BUTTON_SELECT
172 #endif
173 #ifdef HAVE_TOUCHSCREEN
174 #ifndef PLASMA_REGEN_COLORS
175 #define PLASMA_REGEN_COLORS BUTTON_CENTER
176 #endif
177 #endif /* HAVE_TOUCHSCREEN */
178 #endif /* HAVE_LCD_COLOR */
180 #define WAV_AMP 90
183 * Main wave function so we don't have to re-calc the sine
184 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
185 * weirder looking plasmas!
188 static void wave_table_generate(void)
190 int i;
191 for (i=0;i<256;++i)
193 wave_array[i] = (unsigned char)((WAV_AMP
194 * (sin_int((i * 360 * plasma_frequency) / 256))) / 16384);
198 #ifdef HAVE_LCD_COLOR
199 /* Make a smooth colour cycle. */
200 void shades_generate(int time)
202 int i;
203 unsigned red, green, blue;
204 unsigned r = time * redfactor + redphase;
205 unsigned g = time * greenfactor + greenphase;
206 unsigned b = time * bluefactor + bluephase;
208 for(i=0; i < 256; ++i)
210 r &= 0xFF; g &= 0xFF; b &= 0xFF;
212 red = 2 * r;
213 if (red > 255)
214 red = 510 - red;
215 green = 2 * g;
216 if (green > 255)
217 green = 510 - green;
218 blue = 2 * b;
219 if (blue > 255)
220 blue= 510 - blue;
222 colours[i] = LCD_RGBPACK(red, green, blue);
224 r++; g++; b++;
227 #else
228 /* Make a smooth shade from black into white and back into black again. */
229 static void shades_generate(void)
231 int i, y;
233 for(i=0; i < 256; ++i)
235 y = 2 * i;
236 if (y > 255)
237 y = 510 - y;
238 colours[i] = y;
241 #endif
243 void cleanup(void *parameter)
245 (void)parameter;
247 #ifndef HAVE_LCD_COLOR
248 grey_release();
249 #endif
250 /* Turn on backlight timeout (revert to settings) */
251 backlight_use_settings(); /* backlight control in lib/helper.c */
255 * Main function that also contain the main plasma
256 * algorithm.
259 int main(void)
261 plasma_frequency = 1;
262 int button, x, y;
263 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z;
264 #ifdef HAVE_LCD_COLOR
265 fb_data *ptr;
266 int time=0;
267 #else
268 unsigned char *ptr;
269 #endif
271 /*Generate the neccesary pre calced stuff*/
272 wave_table_generate();
274 #ifndef HAVE_LCD_COLOR
275 shades_generate(); /* statically */
277 /* get the remainder of the plugin buffer */
278 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
280 grey_init(gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL);
281 /* switch on greyscale overlay */
282 grey_show(true);
283 #endif
284 sp1 = 4;
285 sp2 = 2;
286 sp3 = 4;
287 sp4 = 2;
288 p1=p2=p3=p4=0;
289 while (true)
291 #ifdef HAVE_LCD_COLOR
292 shades_generate(time++); /* dynamically */
293 ptr = rb->lcd_framebuffer;
294 #else
295 ptr = greybuffer;
296 #endif
297 t1=p1;
298 t2=p2;
299 for(y = 0; y < LCD_HEIGHT; ++y)
301 t3=p3;
302 t4=p4;
303 for(x = 0; x < LCD_WIDTH; ++x)
305 z = wave_array[t1] + wave_array[t2] + wave_array[t3]
306 + wave_array[t4];
307 *ptr++ = colours[z];
308 t3+=1;
309 t4+=2;
311 t1+=2;
312 t2+=1;
313 rb->yield();
315 p1+=sp1;
316 p2-=sp2;
317 p3+=sp3;
318 p4-=sp4;
319 #ifdef HAVE_LCD_COLOR
320 rb->lcd_update();
321 #else
322 grey_ub_gray_bitmap(greybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
323 #endif
325 button = rb->button_get(false);
327 switch(button)
329 #ifdef PLASMA_RC_QUIT
330 case PLASMA_RC_QUIT:
331 #endif
332 case(PLASMA_QUIT):
333 cleanup(NULL);
334 return PLUGIN_OK;
335 break;
337 case (PLASMA_INCREASE_FREQUENCY):
338 ++plasma_frequency;
339 wave_table_generate();
340 break;
342 case (PLASMA_DECREASE_FREQUENCY):
343 if(plasma_frequency>1)
345 --plasma_frequency;
346 wave_table_generate();
348 break;
349 #ifdef HAVE_LCD_COLOR
350 case (PLASMA_REGEN_COLORS):
351 redfactor=rb->rand()%4;
352 greenfactor=rb->rand()%4;
353 bluefactor=rb->rand()%4;
354 redphase=rb->rand()%256;
355 greenphase=rb->rand()%256;
356 bluephase=rb->rand()%256;
357 break;
358 #endif
360 default:
361 if (rb->default_event_handler_ex(button, cleanup, NULL)
362 == SYS_USB_CONNECTED)
363 return PLUGIN_USB_CONNECTED;
364 break;
369 /*************************** Plugin entry point ****************************/
371 enum plugin_status plugin_start(const void* parameter)
373 int ret;
375 (void)parameter;
376 #if LCD_DEPTH > 1
377 rb->lcd_set_backdrop(NULL);
378 #endif
379 /* Turn off backlight timeout */
380 backlight_force_on(); /* backlight control in lib/helper.c */
382 ret = main();
384 return ret;
387 #endif /* HAVE_LCD_BITMAP */