fix FS#8701 - metronome doesnt change back to ui font after drawing
[Rockbox.git] / apps / plugins / plasma.c
blobb54e6a9ed29e7e5b4bc75402861b6625881c0793
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
62 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
63 #define PLASMA_QUIT BUTTON_A
64 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
65 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
67 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
68 (CONFIG_KEYPAD == SANSA_C200_PAD)
69 #define PLASMA_QUIT BUTTON_POWER
70 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
71 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
73 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
74 #define PLASMA_QUIT BUTTON_POWER
75 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
76 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
77 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
78 #define PLASMA_QUIT BUTTON_POWER
79 #define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
80 #define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
81 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
82 #define PLASMA_QUIT BUTTON_BACK
83 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
84 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
85 #elif (CONFIG_KEYPAD == MROBE100_PAD)
86 #define PLASMA_QUIT BUTTON_POWER
87 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
88 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
89 #else
90 #define PLASMA_QUIT BUTTON_OFF
91 #define PLASMA_INCREASE_FREQUENCY BUTTON_UP
92 #define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
93 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
94 #define PLASMA_RC_QUIT BUTTON_RC_STOP
95 #endif
96 #endif
98 #ifdef HAVE_LCD_COLOR
99 #if CONFIG_KEYPAD == IAUDIO_X5M5_PAD
100 #define PLASMA_REGEN_COLORS BUTTON_PLAY
101 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
102 #define PLASMA_REGEN_COLORS BUTTON_PLAY
103 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
104 (CONFIG_KEYPAD == SANSA_C200_PAD)
105 #define PLASMA_REGEN_COLORS BUTTON_SELECT
106 #elif CONFIG_KEYPAD == IPOD_4G_PAD
107 #define PLASMA_REGEN_COLORS BUTTON_SELECT
108 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
109 #define PLASMA_REGEN_COLORS BUTTON_SELECT
110 #elif CONFIG_KEYPAD == GIGABEAT_PAD
111 #define PLASMA_REGEN_COLORS BUTTON_SELECT
112 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
113 #define PLASMA_REGEN_COLORS BUTTON_SELECT
114 #endif
115 #endif
117 #define WAV_AMP 90
120 * Main wave function so we don't have to re-calc the sine
121 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
122 * weirder looking plasmas!
125 static void wave_table_generate(void)
127 int i;
128 for (i=0;i<256;++i)
130 wave_array[i] = (unsigned char)((WAV_AMP
131 * (sin_int((i * 360 * plasma_frequency) / 256))) / 16384);
135 #ifdef HAVE_LCD_COLOR
136 /* Make a smooth colour cycle. */
137 void shades_generate(int time)
139 int i;
140 unsigned red, green, blue;
141 unsigned r = time * redfactor + redphase;
142 unsigned g = time * greenfactor + greenphase;
143 unsigned b = time * bluefactor + bluephase;
145 for(i=0; i < 256; ++i)
147 r &= 0xFF; g &= 0xFF; b &= 0xFF;
149 red = 2 * r;
150 if (red > 255)
151 red = 510 - red;
152 green = 2 * g;
153 if (green > 255)
154 green = 510 - green;
155 blue = 2 * b;
156 if (blue > 255)
157 blue= 510 - blue;
159 colours[i] = LCD_RGBPACK(red, green, blue);
161 r++; g++; b++;
164 #else
165 /* Make a smooth shade from black into white and back into black again. */
166 static void shades_generate(void)
168 int i, y;
170 for(i=0; i < 256; ++i)
172 y = 2 * i;
173 if (y > 255)
174 y = 510 - y;
175 colours[i] = y;
178 #endif
180 void cleanup(void *parameter)
182 (void)parameter;
184 #ifndef HAVE_LCD_COLOR
185 grey_release();
186 #endif
187 /* Turn on backlight timeout (revert to settings) */
188 backlight_use_settings(rb); /* backlight control in lib/helper.c */
192 * Main function that also contain the main plasma
193 * algorithm.
196 int main(void)
198 plasma_frequency = 1;
199 int button, x, y;
200 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z;
201 #ifdef HAVE_LCD_COLOR
202 fb_data *ptr;
203 int time=0;
204 #else
205 unsigned char *ptr;
206 #endif
208 /*Generate the neccesary pre calced stuff*/
209 wave_table_generate();
211 #ifndef HAVE_LCD_COLOR
212 shades_generate(); /* statically */
214 /* get the remainder of the plugin buffer */
215 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
217 grey_init(rb, gbuf, gbuf_size, 0, LCD_WIDTH, LCD_HEIGHT, NULL);
218 /* switch on greyscale overlay */
219 grey_show(true);
220 #endif
221 sp1 = 4;
222 sp2 = 2;
223 sp3 = 4;
224 sp4 = 2;
225 p1=p2=p3=p4=0;
226 while (true)
228 #ifdef HAVE_LCD_COLOR
229 shades_generate(time++); /* dynamically */
230 ptr = rb->lcd_framebuffer;
231 #else
232 ptr = greybuffer;
233 #endif
234 t1=p1;
235 t2=p2;
236 for(y = 0; y < LCD_HEIGHT; ++y)
238 t3=p3;
239 t4=p4;
240 for(x = 0; x < LCD_WIDTH; ++x)
242 z = wave_array[t1] + wave_array[t2] + wave_array[t3]
243 + wave_array[t4];
244 *ptr++ = colours[z];
245 t3+=1;
246 t4+=2;
248 t1+=2;
249 t2+=1;
250 rb->yield();
252 p1+=sp1;
253 p2-=sp2;
254 p3+=sp3;
255 p4-=sp4;
256 #ifdef HAVE_LCD_COLOR
257 rb->lcd_update();
258 #else
259 grey_ub_gray_bitmap(greybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
260 #endif
262 button = rb->button_get(false);
264 switch(button)
266 #ifdef PLASMA_RC_QUIT
267 case PLASMA_RC_QUIT:
268 #endif
269 case(PLASMA_QUIT):
270 cleanup(NULL);
271 return PLUGIN_OK;
272 break;
274 case (PLASMA_INCREASE_FREQUENCY):
275 ++plasma_frequency;
276 wave_table_generate();
277 break;
279 case (PLASMA_DECREASE_FREQUENCY):
280 if(plasma_frequency>1)
282 --plasma_frequency;
283 wave_table_generate();
285 break;
286 #ifdef HAVE_LCD_COLOR
287 case (PLASMA_REGEN_COLORS):
288 redfactor=rb->rand()%4;
289 greenfactor=rb->rand()%4;
290 bluefactor=rb->rand()%4;
291 redphase=rb->rand()%256;
292 greenphase=rb->rand()%256;
293 bluephase=rb->rand()%256;
294 break;
295 #endif
297 default:
298 if (rb->default_event_handler_ex(button, cleanup, NULL)
299 == SYS_USB_CONNECTED)
300 return PLUGIN_USB_CONNECTED;
301 break;
306 /*************************** Plugin entry point ****************************/
308 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
310 int ret;
312 rb = api; /* copy to global api pointer */
313 (void)parameter;
314 #if LCD_DEPTH > 1
315 rb->lcd_set_backdrop(NULL);
316 #endif
317 /* Turn off backlight timeout */
318 backlight_force_on(rb); /* backlight control in lib/helper.c */
320 ret = main();
322 return ret;
325 #endif /* HAVE_LCD_BITMAP */