Don't objcopy simulator plugins.
[kugel-rb.git] / apps / plugins / plasma.c
blob5d12818beb1dac2c2000792040e5aa7812aead65
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 /* Key assignement, all bitmapped models */
59 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
60 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
61 #define PLASMA_QUIT BUTTON_MENU
62 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_FWD
63 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_BACK
65 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
66 #define PLASMA_QUIT BUTTON_POWER
67 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
68 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
70 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
71 (CONFIG_KEYPAD == SANSA_C200_PAD)
72 #define PLASMA_QUIT BUTTON_POWER
73 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
74 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
76 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_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 == IRIVER_H10_PAD)
82 #define PLASMA_QUIT BUTTON_POWER
83 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
84 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
86 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
87 #define PLASMA_QUIT BUTTON_BACK
88 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
89 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
91 #elif (CONFIG_KEYPAD == MROBE100_PAD)
92 #define PLASMA_QUIT BUTTON_POWER
93 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
94 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
96 #elif (CONFIG_KEYPAD == IAUDIO_M3_PAD)
97 #define PLASMA_QUIT BUTTON_RC_REC
98 #define PLASMA_INCREASE_FREQUENCY BUTTON_RC_VOL_UP
99 #define PLASMA_DECREASE_FREQUENCY BUTTON_RC_VOL_DOWN
100 #define PLASMA_RC_QUIT BUTTON_REC
102 #elif (CONFIG_KEYPAD == COWOND2_PAD)
103 #define PLASMA_QUIT BUTTON_POWER
105 #elif (CONFIG_KEYPAD == IAUDIO67_PAD)
106 #define PLASMA_QUIT BUTTON_POWER
107 #define PLASMA_INCREASE_FREQUENCY BUTTON_RIGHT
108 #define PLASMA_DECREASE_FREQUENCY BUTTON_LEFT
109 #define PLASMA_RC_QUIT BUTTON_STOP
110 #define PLASMA_REGEN_COLORS BUTTON_PLAY
112 #endif
114 #ifdef HAVE_TOUCHSCREEN
115 #ifndef PLASMA_QUIT
116 #define PLASMA_QUIT BUTTON_TOPLEFT
117 #endif
118 #ifndef PLASMA_INCREASE_FREQUENCY
119 #define PLASMA_INCREASE_FREQUENCY BUTTON_MIDRIGHT
120 #endif
121 #ifndef PLASMA_DECREASE_FREQUENCY
122 #define PLASMA_DECREASE_FREQUENCY BUTTON_MIDLEFT
123 #endif
124 #endif /* HAVE_TOUCHSCREEN */
126 #ifndef PLASMA_QUIT
127 #define PLASMA_QUIT BUTTON_OFF
128 #endif
129 #ifndef PLASMA_INCREASE_FREQUENCY
130 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
131 #endif
132 #ifndef PLASMA_DECREASE_FREQUENCY
133 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
134 #endif
136 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
137 #define PLASMA_RC_QUIT BUTTON_RC_STOP
138 #endif
140 #ifdef HAVE_LCD_COLOR
141 #if CONFIG_KEYPAD == IAUDIO_X5M5_PAD
142 #define PLASMA_REGEN_COLORS BUTTON_PLAY
143 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
144 #define PLASMA_REGEN_COLORS BUTTON_PLAY
145 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
146 (CONFIG_KEYPAD == SANSA_C200_PAD)
147 #define PLASMA_REGEN_COLORS BUTTON_SELECT
148 #elif CONFIG_KEYPAD == IPOD_4G_PAD
149 #define PLASMA_REGEN_COLORS BUTTON_SELECT
150 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
151 #define PLASMA_REGEN_COLORS BUTTON_SELECT
152 #elif CONFIG_KEYPAD == GIGABEAT_PAD
153 #define PLASMA_REGEN_COLORS BUTTON_SELECT
154 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
155 #define PLASMA_REGEN_COLORS BUTTON_SELECT
156 #endif
157 #ifdef HAVE_TOUCHSCREEN
158 #ifndef PLASMA_REGEN_COLORS
159 #define PLASMA_REGEN_COLORS BUTTON_CENTER
160 #endif
161 #endif /* HAVE_TOUCHSCREEN */
162 #endif /* HAVE_LCD_COLOR */
164 #define WAV_AMP 90
167 * Main wave function so we don't have to re-calc the sine
168 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
169 * weirder looking plasmas!
172 static void wave_table_generate(void)
174 int i;
175 for (i=0;i<256;++i)
177 wave_array[i] = (unsigned char)((WAV_AMP
178 * (sin_int((i * 360 * plasma_frequency) / 256))) / 16384);
182 #ifdef HAVE_LCD_COLOR
183 /* Make a smooth colour cycle. */
184 void shades_generate(int time)
186 int i;
187 unsigned red, green, blue;
188 unsigned r = time * redfactor + redphase;
189 unsigned g = time * greenfactor + greenphase;
190 unsigned b = time * bluefactor + bluephase;
192 for(i=0; i < 256; ++i)
194 r &= 0xFF; g &= 0xFF; b &= 0xFF;
196 red = 2 * r;
197 if (red > 255)
198 red = 510 - red;
199 green = 2 * g;
200 if (green > 255)
201 green = 510 - green;
202 blue = 2 * b;
203 if (blue > 255)
204 blue= 510 - blue;
206 colours[i] = LCD_RGBPACK(red, green, blue);
208 r++; g++; b++;
211 #else
212 /* Make a smooth shade from black into white and back into black again. */
213 static void shades_generate(void)
215 int i, y;
217 for(i=0; i < 256; ++i)
219 y = 2 * i;
220 if (y > 255)
221 y = 510 - y;
222 colours[i] = y;
225 #endif
227 void cleanup(void *parameter)
229 (void)parameter;
231 #ifndef HAVE_LCD_COLOR
232 grey_release();
233 #endif
234 /* Turn on backlight timeout (revert to settings) */
235 backlight_use_settings(rb); /* backlight control in lib/helper.c */
239 * Main function that also contain the main plasma
240 * algorithm.
243 int main(void)
245 plasma_frequency = 1;
246 int button, x, y;
247 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z;
248 #ifdef HAVE_LCD_COLOR
249 fb_data *ptr;
250 int time=0;
251 #else
252 unsigned char *ptr;
253 #endif
255 /*Generate the neccesary pre calced stuff*/
256 wave_table_generate();
258 #ifndef HAVE_LCD_COLOR
259 shades_generate(); /* statically */
261 /* get the remainder of the plugin buffer */
262 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
264 grey_init(rb, gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL);
265 /* switch on greyscale overlay */
266 grey_show(true);
267 #endif
268 sp1 = 4;
269 sp2 = 2;
270 sp3 = 4;
271 sp4 = 2;
272 p1=p2=p3=p4=0;
273 while (true)
275 #ifdef HAVE_LCD_COLOR
276 shades_generate(time++); /* dynamically */
277 ptr = rb->lcd_framebuffer;
278 #else
279 ptr = greybuffer;
280 #endif
281 t1=p1;
282 t2=p2;
283 for(y = 0; y < LCD_HEIGHT; ++y)
285 t3=p3;
286 t4=p4;
287 for(x = 0; x < LCD_WIDTH; ++x)
289 z = wave_array[t1] + wave_array[t2] + wave_array[t3]
290 + wave_array[t4];
291 *ptr++ = colours[z];
292 t3+=1;
293 t4+=2;
295 t1+=2;
296 t2+=1;
297 rb->yield();
299 p1+=sp1;
300 p2-=sp2;
301 p3+=sp3;
302 p4-=sp4;
303 #ifdef HAVE_LCD_COLOR
304 rb->lcd_update();
305 #else
306 grey_ub_gray_bitmap(greybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
307 #endif
309 button = rb->button_get(false);
311 switch(button)
313 #ifdef PLASMA_RC_QUIT
314 case PLASMA_RC_QUIT:
315 #endif
316 case(PLASMA_QUIT):
317 cleanup(NULL);
318 return PLUGIN_OK;
319 break;
321 case (PLASMA_INCREASE_FREQUENCY):
322 ++plasma_frequency;
323 wave_table_generate();
324 break;
326 case (PLASMA_DECREASE_FREQUENCY):
327 if(plasma_frequency>1)
329 --plasma_frequency;
330 wave_table_generate();
332 break;
333 #ifdef HAVE_LCD_COLOR
334 case (PLASMA_REGEN_COLORS):
335 redfactor=rb->rand()%4;
336 greenfactor=rb->rand()%4;
337 bluefactor=rb->rand()%4;
338 redphase=rb->rand()%256;
339 greenphase=rb->rand()%256;
340 bluephase=rb->rand()%256;
341 break;
342 #endif
344 default:
345 if (rb->default_event_handler_ex(button, cleanup, NULL)
346 == SYS_USB_CONNECTED)
347 return PLUGIN_USB_CONNECTED;
348 break;
353 /*************************** Plugin entry point ****************************/
355 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
357 int ret;
359 rb = api; /* copy to global api pointer */
360 (void)parameter;
361 #if LCD_DEPTH > 1
362 rb->lcd_set_backdrop(NULL);
363 #endif
364 /* Turn off backlight timeout */
365 backlight_force_on(rb); /* backlight control in lib/helper.c */
367 ret = main();
369 return ret;
372 #endif /* HAVE_LCD_BITMAP */