Pass plugin api pointer to funtion directly, fixes crashes when doing incremental...
[kugel-rb.git] / apps / plugins / plasma.c
blob1fae239e536e2625bf8c7e2f614e07f3d1235479
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 #define PLASMA_QUIT BUTTON_POWER
68 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
69 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
71 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
72 #define PLASMA_QUIT BUTTON_POWER
73 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
74 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
75 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
76 #define PLASMA_QUIT BUTTON_POWER
77 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
78 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
79 #else
80 #define PLASMA_QUIT BUTTON_OFF
81 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
82 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
83 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
84 #define PLASMA_RC_QUIT BUTTON_RC_STOP
85 #endif
86 #endif
88 #ifdef HAVE_LCD_COLOR
89 #if CONFIG_KEYPAD == IAUDIO_X5M5_PAD
90 #define PLASMA_REGEN_COLORS BUTTON_PLAY
91 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
92 #define PLASMA_REGEN_COLORS BUTTON_PLAY
93 #elif CONFIG_KEYPAD == SANSA_E200_PAD
94 #define PLASMA_REGEN_COLORS BUTTON_SELECT
95 #elif CONFIG_KEYPAD == IPOD_4G_PAD
96 #define PLASMA_REGEN_COLORS BUTTON_SELECT
97 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
98 #define PLASMA_REGEN_COLORS BUTTON_SELECT
99 #elif CONFIG_KEYPAD == GIGABEAT_PAD
100 #define PLASMA_REGEN_COLORS BUTTON_SELECT
101 #endif
102 #endif
104 #define WAV_AMP 90
107 * Main wave function so we don't have to re-calc the sine
108 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
109 * weirder looking plasmas!
112 static void wave_table_generate(void)
114 int i;
115 for (i=0;i<256;++i)
117 wave_array[i] = (unsigned char)((WAV_AMP
118 * (sin_int((i * 360 * plasma_frequency) / 256))) / 16384);
122 #ifdef HAVE_LCD_COLOR
123 /* Make a smooth colour cycle. */
124 void shades_generate(int time)
126 int i;
127 unsigned red, green, blue;
128 unsigned r = time * redfactor + redphase;
129 unsigned g = time * greenfactor + greenphase;
130 unsigned b = time * bluefactor + bluephase;
132 for(i=0; i < 256; ++i)
134 r &= 0xFF; g &= 0xFF; b &= 0xFF;
136 red = 2 * r;
137 if (red > 255)
138 red = 510 - red;
139 green = 2 * g;
140 if (green > 255)
141 green = 510 - green;
142 blue = 2 * b;
143 if (blue > 255)
144 blue= 510 - blue;
146 colours[i] = LCD_RGBPACK(red, green, blue);
148 r++; g++; b++;
151 #else
152 /* Make a smooth shade from black into white and back into black again. */
153 static void shades_generate(void)
155 int i, y;
157 for(i=0; i < 256; ++i)
159 y = 2 * i;
160 if (y > 255)
161 y = 510 - y;
162 colours[i] = y;
165 #endif
167 void cleanup(void *parameter)
169 (void)parameter;
171 #ifndef HAVE_LCD_COLOR
172 gray_release();
173 #endif
174 /* Turn on backlight timeout (revert to settings) */
175 backlight_use_settings(rb); /* backlight control in lib/helper.c */
179 * Main function that also contain the main plasma
180 * algorithm.
183 int main(void)
185 plasma_frequency = 1;
186 int button, x, y;
187 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z;
188 #ifdef HAVE_LCD_COLOR
189 fb_data *ptr;
190 int time=0;
191 #else
192 unsigned char *ptr;
193 #endif
195 /*Generate the neccesary pre calced stuff*/
196 wave_table_generate();
198 #ifndef HAVE_LCD_COLOR
199 shades_generate(); /* statically */
201 /* get the remainder of the plugin buffer */
202 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
204 gray_init(rb, gbuf, gbuf_size, false, LCD_WIDTH, LCD_HEIGHT, 32, 2<<8, NULL);
205 /* switch on grayscale overlay */
206 gray_show(true);
207 #endif
208 sp1 = 4;
209 sp2 = 2;
210 sp3 = 4;
211 sp4 = 2;
212 p1=p2=p3=p4=0;
213 while (true)
215 #ifdef HAVE_LCD_COLOR
216 shades_generate(time++); /* dynamically */
217 ptr = rb->lcd_framebuffer;
218 #else
219 ptr = graybuffer;
220 #endif
221 t1=p1;
222 t2=p2;
223 for(y = 0; y < LCD_HEIGHT; ++y)
225 t3=p3;
226 t4=p4;
227 for(x = 0; x < LCD_WIDTH; ++x)
229 z = wave_array[t1] + wave_array[t2] + wave_array[t3]
230 + wave_array[t4];
231 *ptr++ = colours[z];
232 t3+=1;
233 t4+=2;
235 t1+=2;
236 t2+=1;
237 rb->yield();
239 p1+=sp1;
240 p2-=sp2;
241 p3+=sp3;
242 p4-=sp4;
243 #ifdef HAVE_LCD_COLOR
244 rb->lcd_update();
245 #else
246 gray_ub_gray_bitmap(graybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
247 #endif
249 button = rb->button_get(false);
251 switch(button)
253 #ifdef PLASMA_RC_QUIT
254 case PLASMA_RC_QUIT:
255 #endif
256 case(PLASMA_QUIT):
257 cleanup(NULL);
258 return PLUGIN_OK;
259 break;
261 case (PLASMA_INCREASE_FREQUENCY):
262 ++plasma_frequency;
263 wave_table_generate();
264 break;
266 case (PLASMA_DECREASE_FREQUENCY):
267 if(plasma_frequency>1)
269 --plasma_frequency;
270 wave_table_generate();
272 break;
273 #ifdef HAVE_LCD_COLOR
274 case (PLASMA_REGEN_COLORS):
275 redfactor=rb->rand()%4;
276 greenfactor=rb->rand()%4;
277 bluefactor=rb->rand()%4;
278 redphase=rb->rand()%256;
279 greenphase=rb->rand()%256;
280 bluephase=rb->rand()%256;
281 break;
282 #endif
284 default:
285 if (rb->default_event_handler_ex(button, cleanup, NULL)
286 == SYS_USB_CONNECTED)
287 return PLUGIN_USB_CONNECTED;
288 break;
293 /*************************** Plugin entry point ****************************/
295 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
297 int ret;
299 rb = api; /* copy to global api pointer */
300 (void)parameter;
301 #if LCD_DEPTH > 1
302 rb->lcd_set_backdrop(NULL);
303 #endif
304 /* Turn off backlight timeout */
305 backlight_force_on(rb); /* backlight control in lib/helper.c */
307 ret = main();
309 return ret;
312 #endif /* HAVE_LCD_BITMAP */