Reverting parts of r19760 that was mistakenly committed.
[kugel-rb.git] / apps / plugins / plasma.c
blob8e85bf08c4a9cfea92529f5a21e05f45ef492a6b
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 const struct plugin_api* rb; /* global api struct pointer */
42 static unsigned char wave_array[256]; /* Pre calculated wave array */
43 #ifdef HAVE_LCD_COLOR
44 static fb_data colours[256]; /* Smooth transition of shades */
45 static int redfactor = 1, greenfactor = 2, bluefactor = 3;
46 static int redphase = 0, greenphase = 50, bluephase = 100;
47 /* lower chance of gray at regular intervals */
48 #else
49 GREY_INFO_STRUCT
50 static unsigned char colours[256]; /* Smooth transition of shades */
51 static unsigned char greybuffer[LCD_HEIGHT*LCD_WIDTH]; /* off screen buffer */
52 static unsigned char *gbuf;
53 static size_t gbuf_size = 0;
54 #endif
55 static unsigned char sp1, sp2, sp3, sp4; /* Speed of plasma */
56 static int plasma_frequency;
58 /* FIXME: Could use plugin lib actions */
59 /* Key assignement, all bitmapped models */
60 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
61 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
62 #define PLASMA_QUIT BUTTON_MENU
63 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_FWD
64 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_BACK
66 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
67 #define PLASMA_QUIT BUTTON_POWER
68 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
69 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
71 /* FIXME: Clip/fuze should use HOME instead of POWER */
72 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
73 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
74 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
75 (CONFIG_KEYPAD == SANSA_M200_PAD) || \
76 (CONFIG_KEYPAD == SANSA_FUZE_PAD)
77 #define PLASMA_QUIT BUTTON_POWER
78 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
79 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
81 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
82 #define PLASMA_QUIT BUTTON_POWER
83 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
84 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
86 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
87 #define PLASMA_QUIT BUTTON_POWER
88 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
89 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
91 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
92 #define PLASMA_QUIT BUTTON_BACK
93 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
94 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
96 #elif (CONFIG_KEYPAD == MROBE100_PAD)
97 #define PLASMA_QUIT BUTTON_POWER
98 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
99 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
101 #elif (CONFIG_KEYPAD == IAUDIO_M3_PAD)
102 #define PLASMA_QUIT BUTTON_RC_REC
103 #define PLASMA_INCREASE_FREQUENCY BUTTON_RC_VOL_UP
104 #define PLASMA_DECREASE_FREQUENCY BUTTON_RC_VOL_DOWN
105 #define PLASMA_RC_QUIT BUTTON_REC
107 #elif (CONFIG_KEYPAD == COWOND2_PAD)
108 #define PLASMA_QUIT BUTTON_POWER
110 #elif (CONFIG_KEYPAD == IAUDIO67_PAD)
111 #define PLASMA_QUIT BUTTON_POWER
112 #define PLASMA_INCREASE_FREQUENCY BUTTON_RIGHT
113 #define PLASMA_DECREASE_FREQUENCY BUTTON_LEFT
114 #define PLASMA_RC_QUIT BUTTON_STOP
115 #define PLASMA_REGEN_COLORS BUTTON_PLAY
117 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
118 #define PLASMA_QUIT BUTTON_BACK
119 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
120 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
121 #define PLASMA_REGEN_COLORS BUTTON_SELECT
123 #endif
125 #ifdef HAVE_TOUCHSCREEN
126 #ifndef PLASMA_QUIT
127 #define PLASMA_QUIT BUTTON_TOPLEFT
128 #endif
129 #ifndef PLASMA_INCREASE_FREQUENCY
130 #define PLASMA_INCREASE_FREQUENCY BUTTON_MIDRIGHT
131 #endif
132 #ifndef PLASMA_DECREASE_FREQUENCY
133 #define PLASMA_DECREASE_FREQUENCY BUTTON_MIDLEFT
134 #endif
135 #endif /* HAVE_TOUCHSCREEN */
137 #ifndef PLASMA_QUIT
138 #define PLASMA_QUIT BUTTON_OFF
139 #endif
140 #ifndef PLASMA_INCREASE_FREQUENCY
141 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
142 #endif
143 #ifndef PLASMA_DECREASE_FREQUENCY
144 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
145 #endif
147 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
148 #define PLASMA_RC_QUIT BUTTON_RC_STOP
149 #endif
151 /* FIXME: cleanup */
152 #ifdef HAVE_LCD_COLOR
153 #if CONFIG_KEYPAD == IAUDIO_X5M5_PAD
154 #define PLASMA_REGEN_COLORS BUTTON_PLAY
155 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
156 #define PLASMA_REGEN_COLORS BUTTON_PLAY
157 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
158 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
159 (CONFIG_KEYPAD == SANSA_FUZE_PAD)
160 #define PLASMA_REGEN_COLORS BUTTON_SELECT
161 #elif CONFIG_KEYPAD == IPOD_4G_PAD
162 #define PLASMA_REGEN_COLORS BUTTON_SELECT
163 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
164 #define PLASMA_REGEN_COLORS BUTTON_SELECT
165 #elif CONFIG_KEYPAD == GIGABEAT_PAD
166 #define PLASMA_REGEN_COLORS BUTTON_SELECT
167 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
168 #define PLASMA_REGEN_COLORS BUTTON_SELECT
169 #endif
170 #ifdef HAVE_TOUCHSCREEN
171 #ifndef PLASMA_REGEN_COLORS
172 #define PLASMA_REGEN_COLORS BUTTON_CENTER
173 #endif
174 #endif /* HAVE_TOUCHSCREEN */
175 #endif /* HAVE_LCD_COLOR */
177 #define WAV_AMP 90
180 * Main wave function so we don't have to re-calc the sine
181 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
182 * weirder looking plasmas!
185 static void wave_table_generate(void)
187 int i;
188 for (i=0;i<256;++i)
190 wave_array[i] = (unsigned char)((WAV_AMP
191 * (sin_int((i * 360 * plasma_frequency) / 256))) / 16384);
195 #ifdef HAVE_LCD_COLOR
196 /* Make a smooth colour cycle. */
197 void shades_generate(int time)
199 int i;
200 unsigned red, green, blue;
201 unsigned r = time * redfactor + redphase;
202 unsigned g = time * greenfactor + greenphase;
203 unsigned b = time * bluefactor + bluephase;
205 for(i=0; i < 256; ++i)
207 r &= 0xFF; g &= 0xFF; b &= 0xFF;
209 red = 2 * r;
210 if (red > 255)
211 red = 510 - red;
212 green = 2 * g;
213 if (green > 255)
214 green = 510 - green;
215 blue = 2 * b;
216 if (blue > 255)
217 blue= 510 - blue;
219 colours[i] = LCD_RGBPACK(red, green, blue);
221 r++; g++; b++;
224 #else
225 /* Make a smooth shade from black into white and back into black again. */
226 static void shades_generate(void)
228 int i, y;
230 for(i=0; i < 256; ++i)
232 y = 2 * i;
233 if (y > 255)
234 y = 510 - y;
235 colours[i] = y;
238 #endif
240 void cleanup(void *parameter)
242 (void)parameter;
244 #ifndef HAVE_LCD_COLOR
245 grey_release();
246 #endif
247 /* Turn on backlight timeout (revert to settings) */
248 backlight_use_settings(rb); /* backlight control in lib/helper.c */
252 * Main function that also contain the main plasma
253 * algorithm.
256 int main(void)
258 plasma_frequency = 1;
259 int button, x, y;
260 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z;
261 #ifdef HAVE_LCD_COLOR
262 fb_data *ptr;
263 int time=0;
264 #else
265 unsigned char *ptr;
266 #endif
268 /*Generate the neccesary pre calced stuff*/
269 wave_table_generate();
271 #ifndef HAVE_LCD_COLOR
272 shades_generate(); /* statically */
274 /* get the remainder of the plugin buffer */
275 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
277 grey_init(rb, gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL);
278 /* switch on greyscale overlay */
279 grey_show(true);
280 #endif
281 sp1 = 4;
282 sp2 = 2;
283 sp3 = 4;
284 sp4 = 2;
285 p1=p2=p3=p4=0;
286 while (true)
288 #ifdef HAVE_LCD_COLOR
289 shades_generate(time++); /* dynamically */
290 ptr = rb->lcd_framebuffer;
291 #else
292 ptr = greybuffer;
293 #endif
294 t1=p1;
295 t2=p2;
296 for(y = 0; y < LCD_HEIGHT; ++y)
298 t3=p3;
299 t4=p4;
300 for(x = 0; x < LCD_WIDTH; ++x)
302 z = wave_array[t1] + wave_array[t2] + wave_array[t3]
303 + wave_array[t4];
304 *ptr++ = colours[z];
305 t3+=1;
306 t4+=2;
308 t1+=2;
309 t2+=1;
310 rb->yield();
312 p1+=sp1;
313 p2-=sp2;
314 p3+=sp3;
315 p4-=sp4;
316 #ifdef HAVE_LCD_COLOR
317 rb->lcd_update();
318 #else
319 grey_ub_gray_bitmap(greybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
320 #endif
322 button = rb->button_get(false);
324 switch(button)
326 #ifdef PLASMA_RC_QUIT
327 case PLASMA_RC_QUIT:
328 #endif
329 case(PLASMA_QUIT):
330 cleanup(NULL);
331 return PLUGIN_OK;
332 break;
334 case (PLASMA_INCREASE_FREQUENCY):
335 ++plasma_frequency;
336 wave_table_generate();
337 break;
339 case (PLASMA_DECREASE_FREQUENCY):
340 if(plasma_frequency>1)
342 --plasma_frequency;
343 wave_table_generate();
345 break;
346 #ifdef HAVE_LCD_COLOR
347 case (PLASMA_REGEN_COLORS):
348 redfactor=rb->rand()%4;
349 greenfactor=rb->rand()%4;
350 bluefactor=rb->rand()%4;
351 redphase=rb->rand()%256;
352 greenphase=rb->rand()%256;
353 bluephase=rb->rand()%256;
354 break;
355 #endif
357 default:
358 if (rb->default_event_handler_ex(button, cleanup, NULL)
359 == SYS_USB_CONNECTED)
360 return PLUGIN_USB_CONNECTED;
361 break;
366 /*************************** Plugin entry point ****************************/
368 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
370 int ret;
372 rb = api; /* copy to global api pointer */
373 (void)parameter;
374 #if LCD_DEPTH > 1
375 rb->lcd_set_backdrop(NULL);
376 #endif
377 /* Turn off backlight timeout */
378 backlight_force_on(rb); /* backlight control in lib/helper.c */
380 ret = main();
382 return ret;
385 #endif /* HAVE_LCD_BITMAP */