Pong: small improvement in the c200 buttonmap; the left paddle is now controlled...
[Rockbox.git] / apps / plugins / plasma.c
blob76c54122e0f8a18e4155884ddcd730d188a17be6
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 "gray.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 static unsigned char colours[256]; /* Smooth transition of shades */
48 static unsigned char graybuffer[LCD_HEIGHT*LCD_WIDTH]; /* off screen buffer */
49 static unsigned char *gbuf;
50 static size_t gbuf_size = 0;
51 #endif
52 static unsigned char sp1, sp2, sp3, sp4; /* Speed of plasma */
53 static int plasma_frequency;
55 /* Key assignement, all bitmapped models */
56 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
57 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
58 #define PLASMA_QUIT BUTTON_MENU
59 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_FWD
60 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_BACK
61 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
62 #define PLASMA_QUIT BUTTON_A
63 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
64 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
66 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
67 (CONFIG_KEYPAD == SANSA_C200_PAD)
68 #define PLASMA_QUIT BUTTON_POWER
69 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
70 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
72 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
73 #define PLASMA_QUIT BUTTON_POWER
74 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
75 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
76 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
77 #define PLASMA_QUIT BUTTON_POWER
78 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
79 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
80 #else
81 #define PLASMA_QUIT BUTTON_OFF
82 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
83 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
84 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
85 #define PLASMA_RC_QUIT BUTTON_RC_STOP
86 #endif
87 #endif
89 #ifdef HAVE_LCD_COLOR
90 #if CONFIG_KEYPAD == IAUDIO_X5M5_PAD
91 #define PLASMA_REGEN_COLORS BUTTON_PLAY
92 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
93 #define PLASMA_REGEN_COLORS BUTTON_PLAY
94 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
95 (CONFIG_KEYPAD == SANSA_C200_PAD)
96 #define PLASMA_REGEN_COLORS BUTTON_SELECT
97 #elif CONFIG_KEYPAD == IPOD_4G_PAD
98 #define PLASMA_REGEN_COLORS BUTTON_SELECT
99 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
100 #define PLASMA_REGEN_COLORS BUTTON_SELECT
101 #elif CONFIG_KEYPAD == GIGABEAT_PAD
102 #define PLASMA_REGEN_COLORS BUTTON_SELECT
103 #endif
104 #endif
106 #define WAV_AMP 90
109 * Main wave function so we don't have to re-calc the sine
110 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
111 * weirder looking plasmas!
114 static void wave_table_generate(void)
116 int i;
117 for (i=0;i<256;++i)
119 wave_array[i] = (unsigned char)((WAV_AMP
120 * (sin_int((i * 360 * plasma_frequency) / 256))) / 16384);
124 #ifdef HAVE_LCD_COLOR
125 /* Make a smooth colour cycle. */
126 void shades_generate(int time)
128 int i;
129 unsigned red, green, blue;
130 unsigned r = time * redfactor + redphase;
131 unsigned g = time * greenfactor + greenphase;
132 unsigned b = time * bluefactor + bluephase;
134 for(i=0; i < 256; ++i)
136 r &= 0xFF; g &= 0xFF; b &= 0xFF;
138 red = 2 * r;
139 if (red > 255)
140 red = 510 - red;
141 green = 2 * g;
142 if (green > 255)
143 green = 510 - green;
144 blue = 2 * b;
145 if (blue > 255)
146 blue= 510 - blue;
148 colours[i] = LCD_RGBPACK(red, green, blue);
150 r++; g++; b++;
153 #else
154 /* Make a smooth shade from black into white and back into black again. */
155 static void shades_generate(void)
157 int i, y;
159 for(i=0; i < 256; ++i)
161 y = 2 * i;
162 if (y > 255)
163 y = 510 - y;
164 colours[i] = y;
167 #endif
169 void cleanup(void *parameter)
171 (void)parameter;
173 #ifndef HAVE_LCD_COLOR
174 gray_release();
175 #endif
176 /* Turn on backlight timeout (revert to settings) */
177 backlight_use_settings(rb); /* backlight control in lib/helper.c */
181 * Main function that also contain the main plasma
182 * algorithm.
185 int main(void)
187 plasma_frequency = 1;
188 int button, x, y;
189 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z;
190 #ifdef HAVE_LCD_COLOR
191 fb_data *ptr;
192 int time=0;
193 #else
194 unsigned char *ptr;
195 #endif
197 /*Generate the neccesary pre calced stuff*/
198 wave_table_generate();
200 #ifndef HAVE_LCD_COLOR
201 shades_generate(); /* statically */
203 /* get the remainder of the plugin buffer */
204 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
206 gray_init(rb, gbuf, gbuf_size, false, LCD_WIDTH, LCD_HEIGHT, 32, 2<<8, NULL);
207 /* switch on grayscale overlay */
208 gray_show(true);
209 #endif
210 sp1 = 4;
211 sp2 = 2;
212 sp3 = 4;
213 sp4 = 2;
214 p1=p2=p3=p4=0;
215 while (true)
217 #ifdef HAVE_LCD_COLOR
218 shades_generate(time++); /* dynamically */
219 ptr = rb->lcd_framebuffer;
220 #else
221 ptr = graybuffer;
222 #endif
223 t1=p1;
224 t2=p2;
225 for(y = 0; y < LCD_HEIGHT; ++y)
227 t3=p3;
228 t4=p4;
229 for(x = 0; x < LCD_WIDTH; ++x)
231 z = wave_array[t1] + wave_array[t2] + wave_array[t3]
232 + wave_array[t4];
233 *ptr++ = colours[z];
234 t3+=1;
235 t4+=2;
237 t1+=2;
238 t2+=1;
239 rb->yield();
241 p1+=sp1;
242 p2-=sp2;
243 p3+=sp3;
244 p4-=sp4;
245 #ifdef HAVE_LCD_COLOR
246 rb->lcd_update();
247 #else
248 gray_ub_gray_bitmap(graybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
249 #endif
251 button = rb->button_get(false);
253 switch(button)
255 #ifdef PLASMA_RC_QUIT
256 case PLASMA_RC_QUIT:
257 #endif
258 case(PLASMA_QUIT):
259 cleanup(NULL);
260 return PLUGIN_OK;
261 break;
263 case (PLASMA_INCREASE_FREQUENCY):
264 ++plasma_frequency;
265 wave_table_generate();
266 break;
268 case (PLASMA_DECREASE_FREQUENCY):
269 if(plasma_frequency>1)
271 --plasma_frequency;
272 wave_table_generate();
274 break;
275 #ifdef HAVE_LCD_COLOR
276 case (PLASMA_REGEN_COLORS):
277 redfactor=rb->rand()%4;
278 greenfactor=rb->rand()%4;
279 bluefactor=rb->rand()%4;
280 redphase=rb->rand()%256;
281 greenphase=rb->rand()%256;
282 bluephase=rb->rand()%256;
283 break;
284 #endif
286 default:
287 if (rb->default_event_handler_ex(button, cleanup, NULL)
288 == SYS_USB_CONNECTED)
289 return PLUGIN_USB_CONNECTED;
290 break;
295 /*************************** Plugin entry point ****************************/
297 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
299 int ret;
301 rb = api; /* copy to global api pointer */
302 (void)parameter;
303 #if LCD_DEPTH > 1
304 rb->lcd_set_backdrop(NULL);
305 #endif
306 /* Turn off backlight timeout */
307 backlight_force_on(rb); /* backlight control in lib/helper.c */
309 ret = main();
311 return ret;
314 #endif /* HAVE_LCD_BITMAP */