1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Kevin Ferrare
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
25 #include "lib/helper.h"
27 #include "lib/pluginlib_actions.h"
28 #include "lib/fixedpoint.h"
30 #ifndef HAVE_LCD_COLOR
34 #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64)
35 /* Archos has not enough plugin RAM for full-width fire :( */
36 #define FIRE_WIDTH 106
39 #define FIRE_WIDTH LCD_WIDTH
43 #ifndef HAVE_LCD_COLOR
45 static unsigned char draw_buffer
[FIRE_WIDTH
];
50 const struct button_mapping
* plugin_contexts
[]= {
52 #if defined(HAVE_REMOTE_LCD)
57 #define FIRE_QUIT PLA_CANCEL
58 #define FIRE_SWITCH_FLAMES_TYPE PLA_LEFT
59 #define FIRE_SWITCH_FLAMES_MOVING PLA_RIGHT
61 #ifdef HAVE_SCROLLWHEEL
62 #define FIRE_INCREASE_MULT PLA_SCROLL_FWD
63 #define FIRE_INCREASE_MULT_REP PLA_SCROLL_FWD_REPEAT
64 #define FIRE_DECREASE_MULT PLA_SCROLL_BACK
65 #define FIRE_DECREASE_MULT_REP PLA_SCROLL_BACK_REPEAT
67 #define FIRE_INCREASE_MULT PLA_UP
68 #define FIRE_INCREASE_MULT_REP PLA_UP_REPEAT
69 #define FIRE_DECREASE_MULT PLA_DOWN
70 #define FIRE_DECREASE_MULT_REP PLA_DOWN_REPEAT
73 #define MIN_FLAME_VALUE 0
74 #define COOL_MAX (440/LCD_HEIGHT+2)
76 #ifndef HAVE_LCD_COLOR
77 static unsigned char palette
[256];
79 void color_palette_init(unsigned char* palette
)
82 for(i
=0;i
<=160;i
++)//palette[i]=(3/2)*i
85 /* 'regular' fire doesn't exceed this value */
86 for(;i
<=255;i
++)//palette[i]=(3/20)*i+216
87 palette
[i
]=(i
*3+20*217)/20;
91 static fb_data palette
[256];
94 * Color palette generation algorithm taken from
95 * the "The Demo Effects Collection" GPL project
96 * Copyright (C) 2002 W.P. van Paassen
98 void color_palette_init(fb_data
* palette
)
101 for (i
= 0; i
< 32; i
++){
102 /* black to blue, 32 values*/
103 palette
[i
]=LCD_RGBPACK(0, 0, 2*i
);
105 /* blue to red, 32 values*/
106 palette
[i
+ 32]=LCD_RGBPACK(8*i
, 0, 64 - 2*i
);
108 /* red to yellow, 32 values*/
109 palette
[i
+ 64]=LCD_RGBPACK(255, 8*i
, 0);
111 /* yellow to white, 162 values */
112 palette
[i
+ 96]=LCD_RGBPACK(255, 255, 0 + 4*i
);
113 palette
[i
+ 128]=LCD_RGBPACK(255, 255, 64 + 4*i
);
114 palette
[i
+ 160]=LCD_RGBPACK(255, 255, 128 + 4*i
);
115 palette
[i
+ 192]=LCD_RGBPACK(255, 255, 192 + i
);
116 palette
[i
+ 224]=LCD_RGBPACK(255, 255, 224 + i
);
118 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
119 rb
->lcd_pal256_update_pal(palette
);
125 static void tab_init_rand(unsigned char *tab
, unsigned int tab_size
,
128 unsigned char *end
= tab
+ tab_size
;
131 *tab
++ = (unsigned char)rb
->rand() % rand_max
;
135 unsigned char fire
[LCD_HEIGHT
+3][FIRE_WIDTH
];
136 unsigned char cooling_map
[LCD_HEIGHT
][FIRE_WIDTH
];
141 /* makes the instance a global variable since it's too big to fit on the target's stack */
142 static struct fire fire
;
144 static inline void fire_convolve(struct fire
* fire
)
146 unsigned int pixel_value
;
147 unsigned int cooling_value
;
148 unsigned char *ptr
, *end
, *cool
;
149 unsigned int mult
=fire
->mult
;
152 /* Convolve the pixels and handle cooling (to add nice shapes effects later) */
153 cool
= &fire
->cooling_map
[0][0];
154 ptr
= &fire
->fire
[0][0];
155 end
= ptr
+ LCD_HEIGHT
*FIRE_WIDTH
;
157 switch (fire
->flames_type
){
160 pixel_value
= ptr
[FIRE_WIDTH
-1] /* fire[y+1][x-1] */
161 + ptr
[2*FIRE_WIDTH
] /* fire[y+2][x] */
162 + ptr
[FIRE_WIDTH
+1] /* fire[y+1][x+1] */
163 + ptr
[3*FIRE_WIDTH
]; /* fire[y+3][x] */
164 pixel_value
= FMULU(pixel_value
, mult
) >> 10;
166 cooling_value
= *cool
++;
167 if (cooling_value
<= pixel_value
)
168 pixel_value
-= cooling_value
;
169 /* else it's too cold, don't frost the pixels !!! */
171 if (pixel_value
> 255)
174 *ptr
++ = pixel_value
;
181 pixel_value
= ptr
[FIRE_WIDTH
-1] /* fire[y+1][x-1] */
182 + ptr
[FIRE_WIDTH
] /* fire[y+1][x] */
183 + ptr
[FIRE_WIDTH
+1] /* fire[y+1][x+1] */
184 + ptr
[2*FIRE_WIDTH
]; /* fire[y+2][x] */
185 pixel_value
= FMULU(pixel_value
, mult
) >> 10;
187 cooling_value
= *cool
++;
188 if (cooling_value
<= pixel_value
)
189 pixel_value
-= cooling_value
;
190 /* else it's too cold, don't frost the pixels !!! */
192 if (pixel_value
> 255)
195 *ptr
++ = pixel_value
;
199 default: /* We should never reach this */
205 static void fire_generate_bottom_seed(struct fire
* fire
)
207 unsigned char *ptr
, *end
;
208 ptr
= &fire
->fire
[LCD_HEIGHT
][0];
209 end
= ptr
+ FIRE_WIDTH
;
211 *ptr
++ = (MIN_FLAME_VALUE
+ rb
->rand() % (256-MIN_FLAME_VALUE
));
215 static inline void fire_step(struct fire
* fire
)
218 /* Randomize the bottom line */
219 fire_generate_bottom_seed(fire
);
220 /* Add here further effects like fire letters, ball ... */
225 static void fire_init(struct fire
* fire
)
230 rb
->memset(&fire
->fire
[0][0], 0, sizeof(fire
->fire
));
231 tab_init_rand(&fire
->cooling_map
[0][0], LCD_HEIGHT
*FIRE_WIDTH
, COOL_MAX
);
232 fire_generate_bottom_seed(fire
);
235 static inline void fire_draw(struct fire
* fire
)
237 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
238 rb
->lcd_blit_pal256((unsigned char*)&fire
->fire
[0][0],0,0,0,0,LCD_WIDTH
,LCD_HEIGHT
);
241 unsigned char *src
= &fire
->fire
[0][0];
243 #ifndef HAVE_LCD_COLOR
244 unsigned char *dest
, *end
;
249 for (y
= 0; y
< LCD_HEIGHT
; y
++){
250 #ifndef HAVE_LCD_COLOR
253 dest
= rb
->lcd_framebuffer
+ LCD_WIDTH
* y
+ FIRE_XPOS
;
255 end
= dest
+ FIRE_WIDTH
;
258 *dest
++ = palette
[*src
++];
260 #ifndef HAVE_LCD_COLOR
261 grey_ub_gray_bitmap(draw_buffer
, 0, y
, FIRE_WIDTH
, 1);
264 #ifdef HAVE_LCD_COLOR
271 void cleanup(void *parameter
)
274 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
275 rb
->cpu_boost(false);
277 #ifndef HAVE_LCD_COLOR
280 /* Turn on backlight timeout (revert to settings) */
281 backlight_use_settings(); /* backlight control in lib/helper.c */
285 #ifndef HAVE_LCD_COLOR
289 size_t gbuf_size
= 0;
291 /* get the remainder of the plugin buffer */
292 gbuf
= (unsigned char *) rb
->plugin_get_buffer(&gbuf_size
);
294 if (!grey_init(gbuf
, gbuf_size
, GREY_ON_COP
,
295 FIRE_WIDTH
, LCD_HEIGHT
, NULL
)){
296 rb
->splash(HZ
, "not enough memory");
299 /* switch on greyscale overlay */
300 grey_set_position(FIRE_XPOS
, 0);
310 #ifndef HAVE_LCD_COLOR
311 if(init_grey()!=PLUGIN_OK
)
312 return(PLUGIN_ERROR
);
314 color_palette_init(palette
);
316 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
326 action
= pluginlib_getaction(0, plugin_contexts
,
327 ARRAYLEN(plugin_contexts
));
334 case FIRE_INCREASE_MULT
:
338 case FIRE_DECREASE_MULT
:
343 case FIRE_SWITCH_FLAMES_TYPE
:
344 fire
.flames_type
= (fire
.flames_type
+ 1) % 2;
347 case FIRE_SWITCH_FLAMES_MOVING
:
348 fire
.moving
= !fire
.moving
;
352 if (rb
->default_event_handler_ex(action
, cleanup
, NULL
)
353 == SYS_USB_CONNECTED
)
354 return PLUGIN_USB_CONNECTED
;
359 /*************************** Plugin entry point ****************************/
361 enum plugin_status
plugin_start(const void* parameter
)
367 rb
->lcd_set_backdrop(NULL
);
369 /* Turn off backlight timeout */
370 backlight_force_on(); /* backlight control in lib/helper.c */
372 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
373 rb
->lcd_set_mode(LCD_MODE_PAL256
);
378 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
379 rb
->lcd_set_mode(LCD_MODE_RGB565
);