Fix yellow
[Rockbox.git] / apps / plugins / plasma.c
blobf013cefb85b16ccee0703ffbc554320f3382b6d4
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 * All files in this archive are subject to the GNU General Public License.
18 * See the file COPYING in the source tree root for full license agreement.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
25 #include "plugin.h"
26 #include "helper.h"
28 #ifdef HAVE_LCD_BITMAP
30 #ifndef HAVE_LCD_COLOR
31 #include "grey.h"
32 #endif
33 #include "fixedpoint.h"
35 PLUGIN_HEADER
37 /******************************* Globals ***********************************/
39 static struct plugin_api* rb; /* global api struct pointer */
40 static unsigned char wave_array[256]; /* Pre calculated wave array */
41 #ifdef HAVE_LCD_COLOR
42 static fb_data colours[256]; /* Smooth transition of shades */
43 static int redfactor = 1, greenfactor = 2, bluefactor = 3;
44 static int redphase = 0, greenphase = 50, bluephase = 100;
45 /* lower chance of gray at regular intervals */
46 #else
47 GREY_INFO_STRUCT
48 static unsigned char colours[256]; /* Smooth transition of shades */
49 static unsigned char greybuffer[LCD_HEIGHT*LCD_WIDTH]; /* off screen buffer */
50 static unsigned char *gbuf;
51 static size_t gbuf_size = 0;
52 #endif
53 static unsigned char sp1, sp2, sp3, sp4; /* Speed of plasma */
54 static int plasma_frequency;
56 /* Key assignement, all bitmapped models */
57 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
58 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
59 #define PLASMA_QUIT BUTTON_MENU
60 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_FWD
61 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_BACK
63 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
64 #define PLASMA_QUIT BUTTON_A
65 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
66 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
68 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
69 (CONFIG_KEYPAD == SANSA_C200_PAD)
70 #define PLASMA_QUIT BUTTON_POWER
71 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
72 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
74 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
75 #define PLASMA_QUIT BUTTON_POWER
76 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
77 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
79 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
80 #define PLASMA_QUIT BUTTON_POWER
81 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
82 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
84 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
85 #define PLASMA_QUIT BUTTON_BACK
86 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
87 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
89 #elif (CONFIG_KEYPAD == MROBE100_PAD)
90 #define PLASMA_QUIT BUTTON_POWER
91 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
92 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
94 #elif (CONFIG_KEYPAD == IAUDIO_M3_PAD)
95 #define PLASMA_QUIT BUTTON_RC_REC
96 #define PLASMA_INCREASE_FREQUENCY BUTTON_RC_VOL_UP
97 #define PLASMA_DECREASE_FREQUENCY BUTTON_RC_VOL_DOWN
98 #define PLASMA_RC_QUIT BUTTON_REC
100 #elif (CONFIG_KEYPAD == COWOND2_PAD)
101 #define PLASMA_QUIT BUTTON_POWER
102 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
103 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
105 #else
106 #define PLASMA_QUIT BUTTON_OFF
107 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
108 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
110 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
111 #define PLASMA_RC_QUIT BUTTON_RC_STOP
112 #endif
113 #endif
115 #ifdef HAVE_LCD_COLOR
116 #if CONFIG_KEYPAD == IAUDIO_X5M5_PAD
117 #define PLASMA_REGEN_COLORS BUTTON_PLAY
118 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
119 #define PLASMA_REGEN_COLORS BUTTON_PLAY
120 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
121 (CONFIG_KEYPAD == SANSA_C200_PAD)
122 #define PLASMA_REGEN_COLORS BUTTON_SELECT
123 #elif CONFIG_KEYPAD == IPOD_4G_PAD
124 #define PLASMA_REGEN_COLORS BUTTON_SELECT
125 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
126 #define PLASMA_REGEN_COLORS BUTTON_SELECT
127 #elif CONFIG_KEYPAD == GIGABEAT_PAD
128 #define PLASMA_REGEN_COLORS BUTTON_SELECT
129 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
130 #define PLASMA_REGEN_COLORS BUTTON_SELECT
131 #elif CONFIG_KEYPAD == COWOND2_PAD
132 #define PLASMA_REGEN_COLORS BUTTON_SELECT
133 #endif
134 #endif
136 #define WAV_AMP 90
139 * Main wave function so we don't have to re-calc the sine
140 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
141 * weirder looking plasmas!
144 static void wave_table_generate(void)
146 int i;
147 for (i=0;i<256;++i)
149 wave_array[i] = (unsigned char)((WAV_AMP
150 * (sin_int((i * 360 * plasma_frequency) / 256))) / 16384);
154 #ifdef HAVE_LCD_COLOR
155 /* Make a smooth colour cycle. */
156 void shades_generate(int time)
158 int i;
159 unsigned red, green, blue;
160 unsigned r = time * redfactor + redphase;
161 unsigned g = time * greenfactor + greenphase;
162 unsigned b = time * bluefactor + bluephase;
164 for(i=0; i < 256; ++i)
166 r &= 0xFF; g &= 0xFF; b &= 0xFF;
168 red = 2 * r;
169 if (red > 255)
170 red = 510 - red;
171 green = 2 * g;
172 if (green > 255)
173 green = 510 - green;
174 blue = 2 * b;
175 if (blue > 255)
176 blue= 510 - blue;
178 colours[i] = LCD_RGBPACK(red, green, blue);
180 r++; g++; b++;
183 #else
184 /* Make a smooth shade from black into white and back into black again. */
185 static void shades_generate(void)
187 int i, y;
189 for(i=0; i < 256; ++i)
191 y = 2 * i;
192 if (y > 255)
193 y = 510 - y;
194 colours[i] = y;
197 #endif
199 void cleanup(void *parameter)
201 (void)parameter;
203 #ifndef HAVE_LCD_COLOR
204 grey_release();
205 #endif
206 /* Turn on backlight timeout (revert to settings) */
207 backlight_use_settings(rb); /* backlight control in lib/helper.c */
211 * Main function that also contain the main plasma
212 * algorithm.
215 int main(void)
217 plasma_frequency = 1;
218 int button, x, y;
219 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z;
220 #ifdef HAVE_LCD_COLOR
221 fb_data *ptr;
222 int time=0;
223 #else
224 unsigned char *ptr;
225 #endif
227 /*Generate the neccesary pre calced stuff*/
228 wave_table_generate();
230 #ifndef HAVE_LCD_COLOR
231 shades_generate(); /* statically */
233 /* get the remainder of the plugin buffer */
234 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
236 grey_init(rb, gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL);
237 /* switch on greyscale overlay */
238 grey_show(true);
239 #endif
240 sp1 = 4;
241 sp2 = 2;
242 sp3 = 4;
243 sp4 = 2;
244 p1=p2=p3=p4=0;
245 while (true)
247 #ifdef HAVE_LCD_COLOR
248 shades_generate(time++); /* dynamically */
249 ptr = rb->lcd_framebuffer;
250 #else
251 ptr = greybuffer;
252 #endif
253 t1=p1;
254 t2=p2;
255 for(y = 0; y < LCD_HEIGHT; ++y)
257 t3=p3;
258 t4=p4;
259 for(x = 0; x < LCD_WIDTH; ++x)
261 z = wave_array[t1] + wave_array[t2] + wave_array[t3]
262 + wave_array[t4];
263 *ptr++ = colours[z];
264 t3+=1;
265 t4+=2;
267 t1+=2;
268 t2+=1;
269 rb->yield();
271 p1+=sp1;
272 p2-=sp2;
273 p3+=sp3;
274 p4-=sp4;
275 #ifdef HAVE_LCD_COLOR
276 rb->lcd_update();
277 #else
278 grey_ub_gray_bitmap(greybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
279 #endif
281 button = rb->button_get(false);
283 switch(button)
285 #ifdef PLASMA_RC_QUIT
286 case PLASMA_RC_QUIT:
287 #endif
288 case(PLASMA_QUIT):
289 cleanup(NULL);
290 return PLUGIN_OK;
291 break;
293 case (PLASMA_INCREASE_FREQUENCY):
294 ++plasma_frequency;
295 wave_table_generate();
296 break;
298 case (PLASMA_DECREASE_FREQUENCY):
299 if(plasma_frequency>1)
301 --plasma_frequency;
302 wave_table_generate();
304 break;
305 #ifdef HAVE_LCD_COLOR
306 case (PLASMA_REGEN_COLORS):
307 redfactor=rb->rand()%4;
308 greenfactor=rb->rand()%4;
309 bluefactor=rb->rand()%4;
310 redphase=rb->rand()%256;
311 greenphase=rb->rand()%256;
312 bluephase=rb->rand()%256;
313 break;
314 #endif
316 default:
317 if (rb->default_event_handler_ex(button, cleanup, NULL)
318 == SYS_USB_CONNECTED)
319 return PLUGIN_USB_CONNECTED;
320 break;
325 /*************************** Plugin entry point ****************************/
327 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
329 int ret;
331 rb = api; /* copy to global api pointer */
332 (void)parameter;
333 #if LCD_DEPTH > 1
334 rb->lcd_set_backdrop(NULL);
335 #endif
336 /* Turn off backlight timeout */
337 backlight_force_on(rb); /* backlight control in lib/helper.c */
339 ret = main();
341 return ret;
344 #endif /* HAVE_LCD_BITMAP */