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 ****************************************************************************/
26 #ifdef HAVE_LCD_BITMAP
27 #include "pluginlib_actions.h"
31 #define DEFAULT_WAIT_TIME 3
32 #define DEFAULT_NB_POLYGONS 7
34 #define MAX_STEP_RANGE 7
35 #define MIN_STEP_RANGE 3
36 #define MAX_POLYGONS 40
37 #define MIN_POLYGONS 1
40 #define DEMYSTIFY_QUIT PLA_QUIT
42 #define DEMYSTIFY_INCREASE_SPEED PLA_RIGHT
43 #define DEMYSTIFY_DECREASE_SPEED PLA_LEFT
44 #define DEMYSTIFY_INCREASE_SPEED_REPEAT PLA_RIGHT_REPEAT
45 #define DEMYSTIFY_DECREASE_SPEED_REPEAT PLA_LEFT_REPEAT
47 #define DEMYSTIFY_ADD_POLYGON PLA_UP
48 #define DEMYSTIFY_REMOVE_POLYGON PLA_DOWN
49 #define DEMYSTIFY_ADD_POLYGON_REPEAT PLA_UP_REPEAT
50 #define DEMYSTIFY_REMOVE_POLYGON_REPEAT PLA_DOWN_REPEAT
52 const struct button_mapping
*plugin_contexts
[]
53 = {generic_directions
, generic_actions
,
54 #if defined(HAVE_REMOTE_LCD)
58 #define NB_ACTION_CONTEXTS \
59 sizeof(plugin_contexts)/sizeof(struct button_mapping*)
64 int current_r
,current_g
,current_b
;
68 /******************************* Globals ***********************************/
70 static const struct plugin_api
* rb
; /* global api struct pointer */
73 * Compute a new random step to make the point bounce the borders of the screen
76 int get_new_step(int step
)
79 return -(MIN_STEP_RANGE
+ rb
->rand() % (MAX_STEP_RANGE
-MIN_STEP_RANGE
));
81 return (MIN_STEP_RANGE
+ rb
->rand() % (MAX_STEP_RANGE
-MIN_STEP_RANGE
));
100 struct point points
[NB_POINTS
];
104 * Generates a random polygon (which fits the screen size though)
106 void polygon_init(struct polygon
* polygon
, struct screen
* display
)
109 for(i
=0;i
<NB_POINTS
;++i
)
111 polygon
->points
[i
].x
=(rb
->rand() % (display
->getwidth()));
112 polygon
->points
[i
].y
=(rb
->rand() % (display
->getheight()));
117 * Draw the given polygon onto the screen
120 void polygon_draw(struct polygon
* polygon
, struct screen
* display
)
123 for(i
=0;i
<NB_POINTS
-1;++i
)
125 display
->drawline(polygon
->points
[i
].x
, polygon
->points
[i
].y
,
126 polygon
->points
[i
+1].x
, polygon
->points
[i
+1].y
);
128 display
->drawline(polygon
->points
[0].x
, polygon
->points
[0].y
,
129 polygon
->points
[NB_POINTS
-1].x
,
130 polygon
->points
[NB_POINTS
-1].y
);
134 * Polygon moving data Stuffs
139 struct point move_steps
[NB_POINTS
];
142 void polygon_move_init(struct polygon_move
* polygon_move
)
145 for(i
=0;i
<NB_POINTS
;++i
)
147 polygon_move
->move_steps
[i
].x
=get_new_step(-1);
148 /* -1 because we want a positive random step */
149 polygon_move
->move_steps
[i
].y
=get_new_step(-1);
154 * Update the given polygon's position according to the given informations in
155 * polygon_move (polygon_move may be updated)
157 void polygon_update(struct polygon
*polygon
, struct screen
* display
, struct polygon_move
*polygon_move
)
160 for(i
=0;i
<NB_POINTS
;++i
)
162 x
=polygon
->points
[i
].x
;
163 step
=polygon_move
->move_steps
[i
].x
;
168 polygon_move
->move_steps
[i
].x
=get_new_step(step
);
170 else if(x
>=display
->getwidth())
172 x
=display
->getwidth()-1;
173 polygon_move
->move_steps
[i
].x
=get_new_step(step
);
175 polygon
->points
[i
].x
=x
;
177 y
=polygon
->points
[i
].y
;
178 step
=polygon_move
->move_steps
[i
].y
;
183 polygon_move
->move_steps
[i
].y
=get_new_step(step
);
185 else if(y
>=display
->getheight())
187 y
=display
->getheight()-1;
188 polygon_move
->move_steps
[i
].y
=get_new_step(step
);
190 polygon
->points
[i
].y
=y
;
195 * Polygon fifo Stuffs
203 struct polygon tab
[MAX_POLYGONS
];
206 void fifo_init(struct polygon_fifo
* fifo
)
213 void fifo_push(struct polygon_fifo
* fifo
, struct polygon
* polygon
)
215 if(fifo
->nb_items
>=MAX_POLYGONS
)
220 * Workaround for gcc (which uses memcpy internally) to avoid link error
221 * fifo->tab[fifo->fifo_head]=polygon
223 rb
->memcpy(&(fifo
->tab
[fifo
->fifo_head
]), polygon
, sizeof(struct polygon
));
225 if(fifo
->fifo_head
>=MAX_POLYGONS
)
229 struct polygon
* fifo_pop(struct polygon_fifo
* fifo
)
232 if(fifo
->nb_items
==0)
235 index
=fifo
->fifo_tail
;
237 if(fifo
->fifo_tail
>=MAX_POLYGONS
)
239 return(&(fifo
->tab
[index
]));
246 void polygons_draw(struct polygon_fifo
* polygons
, struct screen
* display
)
249 for(i
=0, j
=polygons
->fifo_tail
;i
<polygons
->nb_items
;++i
, ++j
)
253 polygon_draw(&(polygons
->tab
[j
]), display
);
257 void cleanup(void *parameter
)
261 backlight_use_settings(rb
);
262 #ifdef HAVE_REMOTE_LCD
263 remote_backlight_use_settings(rb
);
267 #ifdef HAVE_LCD_COLOR
268 void color_randomize(struct line_color
* color
)
270 color
->r
= rb
->rand()%255;
271 color
->g
= rb
->rand()%255;
272 color
->b
= rb
->rand()%255;
275 void color_init(struct line_color
* color
)
277 color_randomize(color
);
278 color
->current_r
=color
->r
;
279 color
->current_g
=color
->g
;
280 color
->current_b
=color
->b
;
283 void color_change(struct line_color
* color
)
285 if(color
->current_r
<color
->r
)
287 else if(color
->current_r
>color
->r
)
289 if(color
->current_g
<color
->g
)
291 else if(color
->current_g
>color
->g
)
293 if(color
->current_b
<color
->b
)
295 else if(color
->current_b
>color
->b
)
298 if(color
->current_r
==color
->r
&&
299 color
->current_g
==color
->g
&&
300 color
->current_b
==color
->b
)
301 color_randomize(color
);
304 #define COLOR_RGBPACK(color) \
305 LCD_RGBPACK((color)->current_r, (color)->current_g, (color)->current_b)
307 void color_apply(struct line_color
* color
, struct screen
* display
)
309 if (display
->is_color
){
311 SCREEN_COLOR_TO_NATIVE(display
,COLOR_RGBPACK(color
));
312 display
->set_foreground(foreground
);
321 int plugin_main(void)
324 int sleep_time
=DEFAULT_WAIT_TIME
;
325 int nb_wanted_polygons
=DEFAULT_NB_POLYGONS
;
327 struct polygon_fifo polygons
[NB_SCREENS
];
328 struct polygon_move move
[NB_SCREENS
]; /* This describes the movement of the leading
329 polygon, the others just follow */
330 struct polygon leading_polygon
[NB_SCREENS
];
333 #ifdef HAVE_LCD_COLOR
334 struct screen
*display
= rb
->screens
[i
];
335 if (display
->is_color
)
336 display
->set_background(LCD_BLACK
);
338 fifo_init(&polygons
[i
]);
339 polygon_move_init(&move
[i
]);
340 polygon_init(&leading_polygon
[i
], rb
->screens
[i
]);
343 #ifdef HAVE_LCD_COLOR
344 struct line_color color
;
352 struct screen
* display
=rb
->screens
[i
];
353 if(polygons
[i
].nb_items
>nb_wanted_polygons
)
354 { /* We have too many polygons, we must drop some of them */
355 fifo_pop(&polygons
[i
]);
357 if(nb_wanted_polygons
==polygons
[i
].nb_items
)
358 { /* We have the good number of polygons, we can safely drop
359 the last one to add the new one later */
360 fifo_pop(&polygons
[i
]);
362 fifo_push(&polygons
[i
], &leading_polygon
[i
]);
365 * Then we update the leading polygon for the next round acording to
366 * current move (the move may be altered in case of sreen border
369 polygon_update(&leading_polygon
[i
], display
, &move
[i
]);
371 /* Now the drawing part */
372 #ifdef HAVE_LCD_COLOR
373 color_apply(&color
, display
);
375 display
->clear_display();
376 polygons_draw(&polygons
[i
], display
);
379 #ifdef HAVE_LCD_COLOR
380 color_change(&color
);
383 if (sleep_time
<0)/* full speed */
386 rb
->sleep(sleep_time
);
387 action
= pluginlib_getaction(rb
, TIMEOUT_NOBLOCK
,
388 plugin_contexts
, NB_ACTION_CONTEXTS
);
395 case DEMYSTIFY_ADD_POLYGON
:
396 case DEMYSTIFY_ADD_POLYGON_REPEAT
:
397 if(nb_wanted_polygons
<MAX_POLYGONS
)
398 ++nb_wanted_polygons
;
401 case DEMYSTIFY_REMOVE_POLYGON
:
402 case DEMYSTIFY_REMOVE_POLYGON_REPEAT
:
403 if(nb_wanted_polygons
>MIN_POLYGONS
)
404 --nb_wanted_polygons
;
407 case DEMYSTIFY_INCREASE_SPEED
:
408 case DEMYSTIFY_INCREASE_SPEED_REPEAT
:
413 case DEMYSTIFY_DECREASE_SPEED
:
414 case DEMYSTIFY_DECREASE_SPEED_REPEAT
:
419 if (rb
->default_event_handler_ex(action
, cleanup
, NULL
)
420 == SYS_USB_CONNECTED
)
421 return PLUGIN_USB_CONNECTED
;
427 /*************************** Plugin entry point ****************************/
429 enum plugin_status
plugin_start(const struct plugin_api
* api
, const void* parameter
)
433 rb
= api
; /* copy to global api pointer */
436 rb
->lcd_set_backdrop(NULL
);
438 backlight_force_on(rb
); /* backlight control in lib/helper.c */
439 #ifdef HAVE_REMOTE_LCD
440 remote_backlight_force_on(rb
); /* remote backlight control in lib/helper.c */
447 #endif /* #ifdef HAVE_LCD_BITMAP */