blob: 387a180828da1a77c4e6f739c160dff148017fdf [file] [log] [blame]
Jens Arnold74e4b252005-10-07 20:35:27 +00001/***************************************************************************
2* __________ __ ___.
3* Open \______ \ ____ ____ | | _\_ |__ _______ ___
4* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7* \/ \/ \/ \/ \/
8* $Id$
9*
10* Plasma demo plugin
11*
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!
16*
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000017* This program is free software; you can redistribute it and/or
18* modify it under the terms of the GNU General Public License
19* as published by the Free Software Foundation; either version 2
20* of the License, or (at your option) any later version.
Jens Arnold74e4b252005-10-07 20:35:27 +000021*
22* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23* KIND, either express or implied.
24*
25****************************************************************************/
26
Jens Arnold74e4b252005-10-07 20:35:27 +000027#include "plugin.h"
Peter D'Hoyecb53e3c2007-08-15 12:42:09 +000028#include "helper.h"
Jens Arnold74e4b252005-10-07 20:35:27 +000029
Jens Arnold6a972e02006-02-26 13:37:42 +000030#ifdef HAVE_LCD_BITMAP
Jens Arnold0045bd32006-02-22 21:41:48 +000031
32#ifndef HAVE_LCD_COLOR
Jens Arnoldfeb5b152008-01-04 23:42:38 +000033#include "grey.h"
Dave Chapmanb0f05802006-02-16 16:07:21 +000034#endif
Kevin Ferraredf4f56b2007-07-31 04:59:03 +000035#include "fixedpoint.h"
Jens Arnold74e4b252005-10-07 20:35:27 +000036
Jens Arnolda36b1d42006-01-15 18:20:18 +000037PLUGIN_HEADER
38
Jens Arnold74e4b252005-10-07 20:35:27 +000039/******************************* Globals ***********************************/
40
Steve Bavin65265772008-05-13 09:57:56 +000041static const struct plugin_api* rb; /* global api struct pointer */
Jens Arnold74e4b252005-10-07 20:35:27 +000042static unsigned char wave_array[256]; /* Pre calculated wave array */
Dave Chapmanb0f05802006-02-16 16:07:21 +000043#ifdef HAVE_LCD_COLOR
Jens Arnold0045bd32006-02-22 21:41:48 +000044static fb_data colours[256]; /* Smooth transition of shades */
45static int redfactor = 1, greenfactor = 2, bluefactor = 3;
46static int redphase = 0, greenphase = 50, bluephase = 100;
47 /* lower chance of gray at regular intervals */
Dave Chapmanb0f05802006-02-16 16:07:21 +000048#else
Jens Arnolda72499a2008-01-13 00:11:43 +000049GREY_INFO_STRUCT
Jens Arnold0045bd32006-02-22 21:41:48 +000050static unsigned char colours[256]; /* Smooth transition of shades */
Jens Arnoldfeb5b152008-01-04 23:42:38 +000051static unsigned char greybuffer[LCD_HEIGHT*LCD_WIDTH]; /* off screen buffer */
Dave Chapmanb0f05802006-02-16 16:07:21 +000052static unsigned char *gbuf;
Michael Sevakis26d242a2007-04-21 18:38:25 +000053static size_t gbuf_size = 0;
Dave Chapmanb0f05802006-02-16 16:07:21 +000054#endif
Jens Arnold74e4b252005-10-07 20:35:27 +000055static unsigned char sp1, sp2, sp3, sp4; /* Speed of plasma */
56static int plasma_frequency;
57
58/* Key assignement, all bitmapped models */
Jens Arnoldb7013222007-07-27 09:57:27 +000059#if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
60 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
Dave Chapmand350e802006-02-05 19:27:23 +000061#define PLASMA_QUIT BUTTON_MENU
62#define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_FWD
63#define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_BACK
Jens Arnold40919d72008-03-25 23:21:36 +000064
Marcoen Hirschberg338e2bb2006-02-24 15:42:52 +000065#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
66#define PLASMA_QUIT BUTTON_A
67#define PLASMA_INCREASE_FREQUENCY BUTTON_UP
68#define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
Barry Wardell24f4a2a2006-10-26 13:38:09 +000069
Marianne Arnold12ddb8e2007-09-20 10:49:48 +000070#elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
71(CONFIG_KEYPAD == SANSA_C200_PAD)
Barry Wardell24f4a2a2006-10-26 13:38:09 +000072#define PLASMA_QUIT BUTTON_POWER
73#define PLASMA_INCREASE_FREQUENCY BUTTON_UP
74#define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
75
Jens Arnold85a226d2007-03-16 23:02:39 +000076#elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
Christian Gmeiner7f9e1eb2006-03-27 17:34:23 +000077#define PLASMA_QUIT BUTTON_POWER
Linus Nielsen Feltzing568ac3e2006-02-24 15:06:25 +000078#define PLASMA_INCREASE_FREQUENCY BUTTON_UP
79#define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
Jens Arnold40919d72008-03-25 23:21:36 +000080
Daniel Stenberg1e88be52006-08-03 20:17:25 +000081#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
82#define PLASMA_QUIT BUTTON_POWER
83#define PLASMA_INCREASE_FREQUENCY BUTTON_SCROLL_UP
84#define PLASMA_DECREASE_FREQUENCY BUTTON_SCROLL_DOWN
Jens Arnold40919d72008-03-25 23:21:36 +000085
Will Robertson8215b342008-02-17 12:23:02 +000086#elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
87#define PLASMA_QUIT BUTTON_BACK
88#define PLASMA_INCREASE_FREQUENCY BUTTON_UP
89#define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
Jens Arnold40919d72008-03-25 23:21:36 +000090
Robert Kuklad6c8b572008-03-01 22:55:09 +000091#elif (CONFIG_KEYPAD == MROBE100_PAD)
92#define PLASMA_QUIT BUTTON_POWER
93#define PLASMA_INCREASE_FREQUENCY BUTTON_UP
94#define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
Jens Arnold40919d72008-03-25 23:21:36 +000095
96#elif (CONFIG_KEYPAD == IAUDIO_M3_PAD)
97#define PLASMA_QUIT BUTTON_RC_REC
98#define PLASMA_INCREASE_FREQUENCY BUTTON_RC_VOL_UP
99#define PLASMA_DECREASE_FREQUENCY BUTTON_RC_VOL_DOWN
100#define PLASMA_RC_QUIT BUTTON_REC
101
Rob Purchase554d7ed2008-03-22 22:03:34 +0000102#elif (CONFIG_KEYPAD == COWOND2_PAD)
103#define PLASMA_QUIT BUTTON_POWER
Jens Arnold40919d72008-03-25 23:21:36 +0000104
Rob Purchase297e0502008-04-27 15:30:19 +0000105#endif
106
107#ifdef HAVE_TOUCHPAD
108#ifndef PLASMA_QUIT
109#define PLASMA_QUIT BUTTON_TOPLEFT
110#endif
111#ifndef PLASMA_INCREASE_FREQUENCY
112#define PLASMA_INCREASE_FREQUENCY BUTTON_MIDRIGHT
113#endif
114#ifndef PLASMA_DECREASE_FREQUENCY
115#define PLASMA_DECREASE_FREQUENCY BUTTON_MIDLEFT
116#endif
Rob Purchase3fa414d2008-04-27 16:13:18 +0000117#endif /* HAVE_TOUCHPAD */
Rob Purchase297e0502008-04-27 15:30:19 +0000118
Rob Purchase297e0502008-04-27 15:30:19 +0000119#ifndef PLASMA_QUIT
120#define PLASMA_QUIT BUTTON_OFF
121#endif
122#ifndef PLASMA_INCREASE_FREQUENCY
Jens Arnold74e4b252005-10-07 20:35:27 +0000123#define PLASMA_INCREASE_FREQUENCY BUTTON_UP
Rob Purchase297e0502008-04-27 15:30:19 +0000124#endif
125#ifndef PLASMA_DECREASE_FREQUENCY
Jens Arnold74e4b252005-10-07 20:35:27 +0000126#define PLASMA_DECREASE_FREQUENCY BUTTON_DOWN
Rob Purchase297e0502008-04-27 15:30:19 +0000127#endif
Jens Arnold40919d72008-03-25 23:21:36 +0000128
Kevin Ferrare0e027bd2006-06-30 16:43:47 +0000129#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
130#define PLASMA_RC_QUIT BUTTON_RC_STOP
131#endif
Jens Arnold74e4b252005-10-07 20:35:27 +0000132
Dave Chapmanb0f05802006-02-16 16:07:21 +0000133#ifdef HAVE_LCD_COLOR
Jens Arnold85a226d2007-03-16 23:02:39 +0000134#if CONFIG_KEYPAD == IAUDIO_X5M5_PAD
Jens Arnolda234ed82006-02-22 22:42:04 +0000135#define PLASMA_REGEN_COLORS BUTTON_PLAY
Daniel Stenberg1e88be52006-08-03 20:17:25 +0000136#elif CONFIG_KEYPAD == IRIVER_H10_PAD
137#define PLASMA_REGEN_COLORS BUTTON_PLAY
Marianne Arnold12ddb8e2007-09-20 10:49:48 +0000138#elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
139(CONFIG_KEYPAD == SANSA_C200_PAD)
Barry Wardell24f4a2a2006-10-26 13:38:09 +0000140#define PLASMA_REGEN_COLORS BUTTON_SELECT
Dave Chapmanb0f05802006-02-16 16:07:21 +0000141#elif CONFIG_KEYPAD == IPOD_4G_PAD
142#define PLASMA_REGEN_COLORS BUTTON_SELECT
143#elif CONFIG_KEYPAD == IRIVER_H300_PAD
144#define PLASMA_REGEN_COLORS BUTTON_SELECT
Marcoen Hirschberg338e2bb2006-02-24 15:42:52 +0000145#elif CONFIG_KEYPAD == GIGABEAT_PAD
146#define PLASMA_REGEN_COLORS BUTTON_SELECT
Will Robertson8215b342008-02-17 12:23:02 +0000147#elif CONFIG_KEYPAD == GIGABEAT_S_PAD
148#define PLASMA_REGEN_COLORS BUTTON_SELECT
Rob Purchase297e0502008-04-27 15:30:19 +0000149#endif
150#ifdef HAVE_TOUCHPAD
151#ifndef PLASMA_REGEN_COLORS
152#define PLASMA_REGEN_COLORS BUTTON_CENTER
Dave Chapmanb0f05802006-02-16 16:07:21 +0000153#endif
Rob Purchase3fa414d2008-04-27 16:13:18 +0000154#endif /* HAVE_TOUCHPAD */
155#endif /* HAVE_LCD_COLOR */
Dave Chapmanb0f05802006-02-16 16:07:21 +0000156
Jens Arnold74e4b252005-10-07 20:35:27 +0000157#define WAV_AMP 90
158
Jens Arnold74e4b252005-10-07 20:35:27 +0000159/*
160 * Main wave function so we don't have to re-calc the sine
161 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
162 * weirder looking plasmas!
163 */
164
165static void wave_table_generate(void)
166{
167 int i;
168 for (i=0;i<256;++i)
169 {
170 wave_array[i] = (unsigned char)((WAV_AMP
Kevin Ferraredf4f56b2007-07-31 04:59:03 +0000171 * (sin_int((i * 360 * plasma_frequency) / 256))) / 16384);
Jens Arnold74e4b252005-10-07 20:35:27 +0000172 }
173}
174
Jens Arnold0045bd32006-02-22 21:41:48 +0000175#ifdef HAVE_LCD_COLOR
176/* Make a smooth colour cycle. */
177void shades_generate(int time)
Jens Arnold74e4b252005-10-07 20:35:27 +0000178{
Jens Arnold0045bd32006-02-22 21:41:48 +0000179 int i;
180 unsigned red, green, blue;
181 unsigned r = time * redfactor + redphase;
182 unsigned g = time * greenfactor + greenphase;
183 unsigned b = time * bluefactor + bluephase;
Jens Arnold74e4b252005-10-07 20:35:27 +0000184
185 for(i=0; i < 256; ++i)
186 {
Jens Arnold0045bd32006-02-22 21:41:48 +0000187 r &= 0xFF; g &= 0xFF; b &= 0xFF;
Jens Arnold74e4b252005-10-07 20:35:27 +0000188
Jens Arnold0045bd32006-02-22 21:41:48 +0000189 red = 2 * r;
190 if (red > 255)
191 red = 510 - red;
192 green = 2 * g;
193 if (green > 255)
194 green = 510 - green;
195 blue = 2 * b;
196 if (blue > 255)
197 blue= 510 - blue;
198
199 colours[i] = LCD_RGBPACK(red, green, blue);
200
201 r++; g++; b++;
Jens Arnold74e4b252005-10-07 20:35:27 +0000202 }
203}
Jens Arnold0045bd32006-02-22 21:41:48 +0000204#else
205/* Make a smooth shade from black into white and back into black again. */
206static void shades_generate(void)
207{
208 int i, y;
209
210 for(i=0; i < 256; ++i)
211 {
212 y = 2 * i;
213 if (y > 255)
214 y = 510 - y;
215 colours[i] = y;
216 }
217}
218#endif
Jens Arnold74e4b252005-10-07 20:35:27 +0000219
220void cleanup(void *parameter)
221{
222 (void)parameter;
223
Dave Chapmanb0f05802006-02-16 16:07:21 +0000224#ifndef HAVE_LCD_COLOR
Jens Arnoldfeb5b152008-01-04 23:42:38 +0000225 grey_release();
Dave Chapmanb0f05802006-02-16 16:07:21 +0000226#endif
Peter D'Hoyecb53e3c2007-08-15 12:42:09 +0000227 /* Turn on backlight timeout (revert to settings) */
Peter D'Hoye767c0ec2007-08-16 23:01:18 +0000228 backlight_use_settings(rb); /* backlight control in lib/helper.c */
Jens Arnold74e4b252005-10-07 20:35:27 +0000229}
230
231/*
232 * Main function that also contain the main plasma
233 * algorithm.
234 */
235
236int main(void)
237{
238 plasma_frequency = 1;
Jens Arnold0045bd32006-02-22 21:41:48 +0000239 int button, x, y;
Jens Arnold74e4b252005-10-07 20:35:27 +0000240 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z;
Dave Chapmanb0f05802006-02-16 16:07:21 +0000241#ifdef HAVE_LCD_COLOR
Jens Arnold0045bd32006-02-22 21:41:48 +0000242 fb_data *ptr;
Dave Chapmanb0f05802006-02-16 16:07:21 +0000243 int time=0;
Jens Arnold0045bd32006-02-22 21:41:48 +0000244#else
245 unsigned char *ptr;
Dave Chapmanb0f05802006-02-16 16:07:21 +0000246#endif
Jens Arnold0045bd32006-02-22 21:41:48 +0000247
Jens Arnold74e4b252005-10-07 20:35:27 +0000248 /*Generate the neccesary pre calced stuff*/
249 wave_table_generate();
Jens Arnold74e4b252005-10-07 20:35:27 +0000250
Jens Arnold0045bd32006-02-22 21:41:48 +0000251#ifndef HAVE_LCD_COLOR
252 shades_generate(); /* statically */
253
Jens Arnold74e4b252005-10-07 20:35:27 +0000254 /* get the remainder of the plugin buffer */
255 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
256
Jens Arnolddf8749d2008-04-06 13:59:31 +0000257 grey_init(rb, gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL);
Jens Arnoldfeb5b152008-01-04 23:42:38 +0000258 /* switch on greyscale overlay */
259 grey_show(true);
Dave Chapmanb0f05802006-02-16 16:07:21 +0000260#endif
Jens Arnold74e4b252005-10-07 20:35:27 +0000261 sp1 = 4;
262 sp2 = 2;
263 sp3 = 4;
264 sp4 = 2;
265 p1=p2=p3=p4=0;
266 while (true)
267 {
Jens Arnold0045bd32006-02-22 21:41:48 +0000268#ifdef HAVE_LCD_COLOR
269 shades_generate(time++); /* dynamically */
270 ptr = rb->lcd_framebuffer;
271#else
Jens Arnoldfeb5b152008-01-04 23:42:38 +0000272 ptr = greybuffer;
Jens Arnold0045bd32006-02-22 21:41:48 +0000273#endif
Jens Arnold74e4b252005-10-07 20:35:27 +0000274 t1=p1;
275 t2=p2;
Jens Arnold74e4b252005-10-07 20:35:27 +0000276 for(y = 0; y < LCD_HEIGHT; ++y)
277 {
278 t3=p3;
279 t4=p4;
280 for(x = 0; x < LCD_WIDTH; ++x)
281 {
282 z = wave_array[t1] + wave_array[t2] + wave_array[t3]
Jens Arnold0045bd32006-02-22 21:41:48 +0000283 + wave_array[t4];
284 *ptr++ = colours[z];
Jens Arnold74e4b252005-10-07 20:35:27 +0000285 t3+=1;
286 t4+=2;
Jens Arnold74e4b252005-10-07 20:35:27 +0000287 }
288 t1+=2;
289 t2+=1;
290 rb->yield();
291 }
292 p1+=sp1;
293 p2-=sp2;
294 p3+=sp3;
295 p4-=sp4;
Dave Chapmanb0f05802006-02-16 16:07:21 +0000296#ifdef HAVE_LCD_COLOR
Dave Chapmanb0f05802006-02-16 16:07:21 +0000297 rb->lcd_update();
298#else
Jens Arnoldfeb5b152008-01-04 23:42:38 +0000299 grey_ub_gray_bitmap(greybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
Dave Chapmanb0f05802006-02-16 16:07:21 +0000300#endif
Jens Arnold74e4b252005-10-07 20:35:27 +0000301
302 button = rb->button_get(false);
303
304 switch(button)
305 {
Kevin Ferrare0e027bd2006-06-30 16:43:47 +0000306#ifdef PLASMA_RC_QUIT
307 case PLASMA_RC_QUIT:
308#endif
Jens Arnold74e4b252005-10-07 20:35:27 +0000309 case(PLASMA_QUIT):
310 cleanup(NULL);
311 return PLUGIN_OK;
312 break;
313
314 case (PLASMA_INCREASE_FREQUENCY):
315 ++plasma_frequency;
316 wave_table_generate();
317 break;
318
319 case (PLASMA_DECREASE_FREQUENCY):
320 if(plasma_frequency>1)
321 {
322 --plasma_frequency;
323 wave_table_generate();
324 }
325 break;
Dave Chapmanb0f05802006-02-16 16:07:21 +0000326#ifdef HAVE_LCD_COLOR
327 case (PLASMA_REGEN_COLORS):
328 redfactor=rb->rand()%4;
329 greenfactor=rb->rand()%4;
330 bluefactor=rb->rand()%4;
331 redphase=rb->rand()%256;
332 greenphase=rb->rand()%256;
333 bluephase=rb->rand()%256;
334 break;
335#endif
Jens Arnold74e4b252005-10-07 20:35:27 +0000336
337 default:
338 if (rb->default_event_handler_ex(button, cleanup, NULL)
339 == SYS_USB_CONNECTED)
340 return PLUGIN_USB_CONNECTED;
341 break;
342 }
343 }
344}
345
346/*************************** Plugin entry point ****************************/
347
Steve Bavin65265772008-05-13 09:57:56 +0000348enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
Jens Arnold74e4b252005-10-07 20:35:27 +0000349{
350 int ret;
Jens Arnold74e4b252005-10-07 20:35:27 +0000351
Daniel Stenberg220fafd2006-03-03 08:14:44 +0000352 rb = api; /* copy to global api pointer */
Jens Arnold74e4b252005-10-07 20:35:27 +0000353 (void)parameter;
Karl Kurbjun1a9442c2006-11-15 06:46:35 +0000354#if LCD_DEPTH > 1
Karl Kurbjund6b0c972006-11-15 06:14:27 +0000355 rb->lcd_set_backdrop(NULL);
Karl Kurbjun1a9442c2006-11-15 06:46:35 +0000356#endif
Peter D'Hoyecb53e3c2007-08-15 12:42:09 +0000357 /* Turn off backlight timeout */
Peter D'Hoye767c0ec2007-08-16 23:01:18 +0000358 backlight_force_on(rb); /* backlight control in lib/helper.c */
Jens Arnold74e4b252005-10-07 20:35:27 +0000359
360 ret = main();
361
362 return ret;
363}
364
Jens Arnold6a972e02006-02-26 13:37:42 +0000365#endif /* HAVE_LCD_BITMAP */