PP502x: Add some important information about CPU/COP_CTL register to the header glean...
[Rockbox.git] / apps / plugins / plasma.c
blob64d73a37570018759abacd489d5b7e8187e7c77a
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"
27 #ifdef HAVE_LCD_BITMAP
29 #ifndef HAVE_LCD_COLOR
30 #include "gray.h"
31 #endif
32 #include "fixedpoint.h"
34 PLUGIN_HEADER
36 /******************************* Globals ***********************************/
38 static struct plugin_api* rb; /* global api struct pointer */
39 static unsigned char wave_array[256]; /* Pre calculated wave array */
40 #ifdef HAVE_LCD_COLOR
41 static fb_data colours[256]; /* Smooth transition of shades */
42 static int redfactor = 1, greenfactor = 2, bluefactor = 3;
43 static int redphase = 0, greenphase = 50, bluephase = 100;
44 /* lower chance of gray at regular intervals */
45 #else
46 static unsigned char colours[256]; /* Smooth transition of shades */
47 static unsigned char graybuffer[LCD_HEIGHT*LCD_WIDTH]; /* off screen buffer */
48 static unsigned char *gbuf;
49 static size_t gbuf_size = 0;
50 #endif
51 static unsigned char sp1, sp2, sp3, sp4; /* Speed of plasma */
52 static int plasma_frequency;
54 /* Key assignement, all bitmapped models */
55 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
56 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
57 #define PLASMA_QUIT BUTTON_MENU
58 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_FWD
59 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_BACK
60 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
61 #define PLASMA_QUIT BUTTON_A
62 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
63 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
65 #elif (CONFIG_KEYPAD == SANSA_E200_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 == IAUDIO_X5M5_PAD)
71 #define PLASMA_QUIT BUTTON_POWER
72 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
73 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
74 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
75 #define PLASMA_QUIT BUTTON_POWER
76 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
77 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
78 #else
79 #define PLASMA_QUIT BUTTON_OFF
80 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
81 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
82 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
83 #define PLASMA_RC_QUIT BUTTON_RC_STOP
84 #endif
85 #endif
87 #ifdef HAVE_LCD_COLOR
88 #if CONFIG_KEYPAD == IAUDIO_X5M5_PAD
89 #define PLASMA_REGEN_COLORS BUTTON_PLAY
90 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
91 #define PLASMA_REGEN_COLORS BUTTON_PLAY
92 #elif CONFIG_KEYPAD == SANSA_E200_PAD
93 #define PLASMA_REGEN_COLORS BUTTON_SELECT
94 #elif CONFIG_KEYPAD == IPOD_4G_PAD
95 #define PLASMA_REGEN_COLORS BUTTON_SELECT
96 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
97 #define PLASMA_REGEN_COLORS BUTTON_SELECT
98 #elif CONFIG_KEYPAD == GIGABEAT_PAD
99 #define PLASMA_REGEN_COLORS BUTTON_SELECT
100 #endif
101 #endif
103 #define WAV_AMP 90
106 * Main wave function so we don't have to re-calc the sine
107 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
108 * weirder looking plasmas!
111 static void wave_table_generate(void)
113 int i;
114 for (i=0;i<256;++i)
116 wave_array[i] = (unsigned char)((WAV_AMP
117 * (sin_int((i * 360 * plasma_frequency) / 256))) / 16384);
121 #ifdef HAVE_LCD_COLOR
122 /* Make a smooth colour cycle. */
123 void shades_generate(int time)
125 int i;
126 unsigned red, green, blue;
127 unsigned r = time * redfactor + redphase;
128 unsigned g = time * greenfactor + greenphase;
129 unsigned b = time * bluefactor + bluephase;
131 for(i=0; i < 256; ++i)
133 r &= 0xFF; g &= 0xFF; b &= 0xFF;
135 red = 2 * r;
136 if (red > 255)
137 red = 510 - red;
138 green = 2 * g;
139 if (green > 255)
140 green = 510 - green;
141 blue = 2 * b;
142 if (blue > 255)
143 blue= 510 - blue;
145 colours[i] = LCD_RGBPACK(red, green, blue);
147 r++; g++; b++;
150 #else
151 /* Make a smooth shade from black into white and back into black again. */
152 static void shades_generate(void)
154 int i, y;
156 for(i=0; i < 256; ++i)
158 y = 2 * i;
159 if (y > 255)
160 y = 510 - y;
161 colours[i] = y;
164 #endif
166 void cleanup(void *parameter)
168 (void)parameter;
170 #ifndef HAVE_LCD_COLOR
171 gray_release();
172 #endif
173 rb->backlight_set_timeout(rb->global_settings->backlight_timeout);
177 * Main function that also contain the main plasma
178 * algorithm.
181 int main(void)
183 plasma_frequency = 1;
184 int button, x, y;
185 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z;
186 #ifdef HAVE_LCD_COLOR
187 fb_data *ptr;
188 int time=0;
189 #else
190 unsigned char *ptr;
191 #endif
193 /*Generate the neccesary pre calced stuff*/
194 wave_table_generate();
196 #ifndef HAVE_LCD_COLOR
197 shades_generate(); /* statically */
199 /* get the remainder of the plugin buffer */
200 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
202 gray_init(rb, gbuf, gbuf_size, false, LCD_WIDTH, LCD_HEIGHT, 32, 2<<8, NULL);
203 /* switch on grayscale overlay */
204 gray_show(true);
205 #endif
206 sp1 = 4;
207 sp2 = 2;
208 sp3 = 4;
209 sp4 = 2;
210 p1=p2=p3=p4=0;
211 while (true)
213 #ifdef HAVE_LCD_COLOR
214 shades_generate(time++); /* dynamically */
215 ptr = rb->lcd_framebuffer;
216 #else
217 ptr = graybuffer;
218 #endif
219 t1=p1;
220 t2=p2;
221 for(y = 0; y < LCD_HEIGHT; ++y)
223 t3=p3;
224 t4=p4;
225 for(x = 0; x < LCD_WIDTH; ++x)
227 z = wave_array[t1] + wave_array[t2] + wave_array[t3]
228 + wave_array[t4];
229 *ptr++ = colours[z];
230 t3+=1;
231 t4+=2;
233 t1+=2;
234 t2+=1;
235 rb->yield();
237 p1+=sp1;
238 p2-=sp2;
239 p3+=sp3;
240 p4-=sp4;
241 #ifdef HAVE_LCD_COLOR
242 rb->lcd_update();
243 #else
244 gray_ub_gray_bitmap(graybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
245 #endif
247 button = rb->button_get(false);
249 switch(button)
251 #ifdef PLASMA_RC_QUIT
252 case PLASMA_RC_QUIT:
253 #endif
254 case(PLASMA_QUIT):
255 cleanup(NULL);
256 return PLUGIN_OK;
257 break;
259 case (PLASMA_INCREASE_FREQUENCY):
260 ++plasma_frequency;
261 wave_table_generate();
262 break;
264 case (PLASMA_DECREASE_FREQUENCY):
265 if(plasma_frequency>1)
267 --plasma_frequency;
268 wave_table_generate();
270 break;
271 #ifdef HAVE_LCD_COLOR
272 case (PLASMA_REGEN_COLORS):
273 redfactor=rb->rand()%4;
274 greenfactor=rb->rand()%4;
275 bluefactor=rb->rand()%4;
276 redphase=rb->rand()%256;
277 greenphase=rb->rand()%256;
278 bluephase=rb->rand()%256;
279 break;
280 #endif
282 default:
283 if (rb->default_event_handler_ex(button, cleanup, NULL)
284 == SYS_USB_CONNECTED)
285 return PLUGIN_USB_CONNECTED;
286 break;
291 /*************************** Plugin entry point ****************************/
293 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
295 int ret;
297 rb = api; /* copy to global api pointer */
298 (void)parameter;
299 #if LCD_DEPTH > 1
300 rb->lcd_set_backdrop(NULL);
301 #endif
302 if (rb->global_settings->backlight_timeout > 0)
303 rb->backlight_set_timeout(1);/* keep the light on */
305 ret = main();
307 return ret;
310 #endif /* HAVE_LCD_BITMAP */