Take 2 at 'Consolidate all fixed point math routines in one library' (FS#10400) by...
[kugel-rb/myfork.git] / apps / plugins / cube.c
blobc770214700b527906f25515cf20637a21a2b7634
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Damien Teney
11 * modified to use int instead of float math by Andreas Zwirtes
12 * heavily extended by Jens Arnold
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 ***************************************************************************/
23 #include "plugin.h"
24 #include "lib/grey.h"
25 #include "lib/playergfx.h"
26 #include "lib/xlcd.h"
27 #include "lib/fixedpoint.h"
29 PLUGIN_HEADER
31 /* Loops that the values are displayed */
32 #define DISP_TIME 30
34 /* variable button definitions */
35 #if CONFIG_KEYPAD == RECORDER_PAD
36 #define CUBE_QUIT BUTTON_OFF
37 #define CUBE_NEXT BUTTON_RIGHT
38 #define CUBE_PREV BUTTON_LEFT
39 #define CUBE_INC BUTTON_UP
40 #define CUBE_DEC BUTTON_DOWN
41 #define CUBE_MODE BUTTON_F1
42 #define CUBE_PAUSE BUTTON_PLAY
43 #define CUBE_HIGHSPEED BUTTON_ON
45 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
46 #define CUBE_QUIT BUTTON_OFF
47 #define CUBE_NEXT BUTTON_RIGHT
48 #define CUBE_PREV BUTTON_LEFT
49 #define CUBE_INC BUTTON_UP
50 #define CUBE_DEC BUTTON_DOWN
51 #define CUBE_MODE BUTTON_F1
52 #define CUBE_PAUSE BUTTON_SELECT
53 #define CUBE_HIGHSPEED BUTTON_ON
55 #elif CONFIG_KEYPAD == PLAYER_PAD
56 #define CUBE_QUIT BUTTON_STOP
57 #define CUBE_INC BUTTON_RIGHT
58 #define CUBE_DEC BUTTON_LEFT
59 #define CUBE_NEXT (BUTTON_ON | BUTTON_RIGHT)
60 #define CUBE_PREV (BUTTON_ON | BUTTON_LEFT)
61 #define CUBE_MODE BUTTON_MENU
62 #define CUBE_PAUSE BUTTON_PLAY
63 #define CUBE_HIGHSPEED_PRE BUTTON_ON
64 #define CUBE_HIGHSPEED (BUTTON_ON | BUTTON_REL)
66 #elif CONFIG_KEYPAD == ONDIO_PAD
67 #define CUBE_QUIT BUTTON_OFF
68 #define CUBE_NEXT BUTTON_RIGHT
69 #define CUBE_PREV BUTTON_LEFT
70 #define CUBE_INC BUTTON_UP
71 #define CUBE_DEC BUTTON_DOWN
72 #define CUBE_MODE_PRE BUTTON_MENU
73 #define CUBE_MODE (BUTTON_MENU | BUTTON_REL)
74 #define CUBE_PAUSE (BUTTON_MENU | BUTTON_LEFT)
75 #define CUBE_HIGHSPEED (BUTTON_MENU | BUTTON_RIGHT)
77 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
78 (CONFIG_KEYPAD == IRIVER_H300_PAD)
79 #define CUBE_QUIT BUTTON_OFF
80 #define CUBE_NEXT BUTTON_RIGHT
81 #define CUBE_PREV BUTTON_LEFT
82 #define CUBE_INC BUTTON_UP
83 #define CUBE_DEC BUTTON_DOWN
84 #define CUBE_MODE BUTTON_MODE
85 #define CUBE_PAUSE BUTTON_ON
86 #define CUBE_HIGHSPEED BUTTON_SELECT
88 #define CUBE_RC_QUIT BUTTON_RC_STOP
90 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
91 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
92 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
93 #define CUBE_QUIT (BUTTON_SELECT | BUTTON_MENU)
94 #define CUBE_NEXT BUTTON_RIGHT
95 #define CUBE_PREV BUTTON_LEFT
96 #define CUBE_INC BUTTON_SCROLL_FWD
97 #define CUBE_DEC BUTTON_SCROLL_BACK
98 #define CUBE_MODE BUTTON_MENU
99 #define CUBE_PAUSE BUTTON_PLAY
100 #define CUBE_HIGHSPEED_PRE BUTTON_SELECT
101 #define CUBE_HIGHSPEED (BUTTON_SELECT | BUTTON_REL)
103 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
104 #define CUBE_QUIT BUTTON_PLAY
105 #define CUBE_NEXT BUTTON_RIGHT
106 #define CUBE_PREV BUTTON_LEFT
107 #define CUBE_INC BUTTON_UP
108 #define CUBE_DEC BUTTON_DOWN
109 #define CUBE_MODE BUTTON_MODE
110 #define CUBE_PAUSE BUTTON_SELECT
111 #define CUBE_HIGHSPEED BUTTON_EQ
113 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
114 #define CUBE_QUIT BUTTON_POWER
115 #define CUBE_NEXT BUTTON_RIGHT
116 #define CUBE_PREV BUTTON_LEFT
117 #define CUBE_INC BUTTON_UP
118 #define CUBE_DEC BUTTON_DOWN
119 #define CUBE_MODE BUTTON_REC
120 #define CUBE_PAUSE BUTTON_PLAY
121 #define CUBE_HIGHSPEED BUTTON_SELECT
123 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
124 #define CUBE_QUIT BUTTON_POWER
125 #define CUBE_NEXT BUTTON_RIGHT
126 #define CUBE_PREV BUTTON_LEFT
127 #define CUBE_INC BUTTON_UP
128 #define CUBE_DEC BUTTON_DOWN
129 #define CUBE_MODE BUTTON_MENU
130 #define CUBE_PAUSE BUTTON_SELECT
131 #define CUBE_HIGHSPEED BUTTON_A
133 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
134 #define CUBE_QUIT BUTTON_POWER
135 #define CUBE_NEXT BUTTON_RIGHT
136 #define CUBE_PREV BUTTON_LEFT
137 #define CUBE_INC BUTTON_SCROLL_FWD
138 #define CUBE_DEC BUTTON_SCROLL_BACK
139 #define CUBE_MODE BUTTON_DOWN
140 #define CUBE_PAUSE BUTTON_UP
141 #define CUBE_HIGHSPEED BUTTON_SELECT
143 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
144 #define CUBE_QUIT (BUTTON_HOME|BUTTON_REPEAT)
145 #define CUBE_NEXT BUTTON_RIGHT
146 #define CUBE_PREV BUTTON_LEFT
147 #define CUBE_INC BUTTON_SCROLL_FWD
148 #define CUBE_DEC BUTTON_SCROLL_BACK
149 #define CUBE_MODE BUTTON_DOWN
150 #define CUBE_PAUSE BUTTON_UP
151 #define CUBE_HIGHSPEED BUTTON_SELECT
153 #elif (CONFIG_KEYPAD == SANSA_C200_PAD) || \
154 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
155 (CONFIG_KEYPAD == SANSA_M200_PAD)
156 #define CUBE_QUIT BUTTON_POWER
157 #define CUBE_NEXT BUTTON_RIGHT
158 #define CUBE_PREV BUTTON_LEFT
159 #define CUBE_INC BUTTON_VOL_UP
160 #define CUBE_DEC BUTTON_VOL_DOWN
161 #define CUBE_MODE BUTTON_DOWN
162 #define CUBE_PAUSE BUTTON_UP
163 #define CUBE_HIGHSPEED BUTTON_SELECT
166 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
167 #define CUBE_QUIT BUTTON_POWER
168 #define CUBE_NEXT BUTTON_RIGHT
169 #define CUBE_PREV BUTTON_LEFT
170 #define CUBE_INC BUTTON_SCROLL_UP
171 #define CUBE_DEC BUTTON_SCROLL_DOWN
172 #define CUBE_MODE BUTTON_REW
173 #define CUBE_PAUSE BUTTON_PLAY
174 #define CUBE_HIGHSPEED BUTTON_FF
176 #elif CONFIG_KEYPAD == MROBE500_PAD
177 #define CUBE_QUIT BUTTON_POWER
179 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
180 #define CUBE_QUIT BUTTON_BACK
181 #define CUBE_NEXT BUTTON_RIGHT
182 #define CUBE_PREV BUTTON_LEFT
183 #define CUBE_INC BUTTON_UP
184 #define CUBE_DEC BUTTON_DOWN
185 #define CUBE_MODE BUTTON_MENU
186 #define CUBE_PAUSE BUTTON_PLAY
187 #define CUBE_HIGHSPEED BUTTON_SELECT
189 #elif (CONFIG_KEYPAD == MROBE100_PAD)
190 #define CUBE_QUIT BUTTON_POWER
191 #define CUBE_NEXT BUTTON_RIGHT
192 #define CUBE_PREV BUTTON_LEFT
193 #define CUBE_INC BUTTON_UP
194 #define CUBE_DEC BUTTON_DOWN
195 #define CUBE_MODE BUTTON_MENU
196 #define CUBE_PAUSE BUTTON_PLAY
197 #define CUBE_HIGHSPEED BUTTON_SELECT
199 #elif (CONFIG_KEYPAD == IAUDIO_M3_PAD)
200 #define CUBE_QUIT BUTTON_RC_REC
201 #define CUBE_NEXT BUTTON_RC_FF
202 #define CUBE_PREV BUTTON_RC_REW
203 #define CUBE_INC BUTTON_RC_VOL_UP
204 #define CUBE_DEC BUTTON_RC_VOL_DOWN
205 #define CUBE_MODE BUTTON_RC_MODE
206 #define CUBE_PAUSE BUTTON_RC_PLAY
207 #define CUBE_HIGHSPEED BUTTON_RC_MENU
209 #elif CONFIG_KEYPAD == COWOND2_PAD
210 #define CUBE_QUIT BUTTON_POWER
212 #elif (CONFIG_KEYPAD == IAUDIO67_PAD)
213 #define CUBE_QUIT BUTTON_POWER
214 #define CUBE_NEXT BUTTON_RIGHT
215 #define CUBE_PREV BUTTON_LEFT
216 #define CUBE_INC BUTTON_VOLUP
217 #define CUBE_DEC BUTTON_VOLDOWN
218 #define CUBE_MODE BUTTON_MENU
219 #define CUBE_PAUSE BUTTON_PLAY
220 #define CUBE_HIGHSPEED BUTTON_STOP
222 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
223 #define CUBE_QUIT BUTTON_BACK
224 #define CUBE_NEXT BUTTON_RIGHT
225 #define CUBE_PREV BUTTON_LEFT
226 #define CUBE_INC BUTTON_UP
227 #define CUBE_DEC BUTTON_DOWN
228 #define CUBE_MODE BUTTON_MENU
229 #define CUBE_PAUSE BUTTON_PLAY
230 #define CUBE_HIGHSPEED BUTTON_SELECT
232 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
233 #define CUBE_QUIT BUTTON_POWER
234 #define CUBE_NEXT BUTTON_RIGHT
235 #define CUBE_PREV BUTTON_LEFT
236 #define CUBE_INC BUTTON_UP
237 #define CUBE_DEC BUTTON_DOWN
238 #define CUBE_MODE BUTTON_MENU
239 #define CUBE_PAUSE BUTTON_SELECT
240 #define CUBE_HIGHSPEED BUTTON_VIEW
242 #elif CONFIG_KEYPAD == ONDAVX747_PAD
243 #define CUBE_QUIT BUTTON_POWER
245 #else
246 #error No keymap defined!
247 #endif
249 #ifdef HAVE_TOUCHSCREEN
250 #ifndef CUBE_QUIT
251 #define CUBE_QUIT BUTTON_TOPLEFT
252 #endif
253 #ifndef CUBE_NEXT
254 #define CUBE_NEXT BUTTON_MIDRIGHT
255 #endif
256 #ifndef CUBE_PREV
257 #define CUBE_PREV BUTTON_MIDLEFT
258 #endif
259 #ifndef CUBE_INC
260 #define CUBE_INC BUTTON_TOPMIDDLE
261 #endif
262 #ifndef CUBE_DEC
263 #define CUBE_DEC BUTTON_BOTTOMMIDDLE
264 #endif
265 #ifndef CUBE_MODE
266 #define CUBE_MODE BUTTON_TOPRIGHT
267 #endif
268 #ifndef CUBE_PAUSE
269 #define CUBE_PAUSE BUTTON_CENTER
270 #endif
271 #ifndef CUBE_HIGHSPEED
272 #define CUBE_HIGHSPEED BUTTON_BOTTOMRIGHT
273 #endif
274 #endif
277 #ifdef HAVE_LCD_BITMAP
279 #define DIST (10 * MIN(LCD_HEIGHT, LCD_WIDTH) / 16)
280 static int x_off = LCD_WIDTH/2;
281 static int y_off = LCD_HEIGHT/2;
283 #if LCD_DEPTH == 1
284 #define USE_GSLIB
285 GREY_INFO_STRUCT
286 struct my_lcd {
287 void (*update)(void);
288 void (*clear_display)(void);
289 void (*drawline)(int x1, int y1, int x2, int y2);
290 void (*putsxy)(int x, int y, const unsigned char *string);
293 static struct my_lcd greyfuncs = {
294 grey_update, grey_clear_display, grey_drawline, grey_putsxy
296 static struct my_lcd lcdfuncs; /* initialised at runtime */
297 static struct my_lcd *mylcd = &greyfuncs;
299 #define MYLCD(fn) mylcd->fn
300 #define MY_FILLTRIANGLE(x1, y1, x2, y2, x3, y3) grey_filltriangle(x1, y1, x2, y2, x3, y3)
301 #define MY_SET_FOREGROUND(fg) grey_set_foreground(fg)
302 #define MY_GET_FOREGROUND() grey_get_foreground()
304 #else
305 #define MYLCD(fn) rb->lcd_ ## fn
306 #define MY_FILLTRIANGLE(x1, y1, x2, y2, x3, y3) xlcd_filltriangle(x1, y1, x2, y2, x3, y3)
307 #define MY_SET_FOREGROUND(fg) rb->lcd_set_foreground(fg)
308 #define MY_GET_FOREGROUND() rb->lcd_get_foreground()
309 #endif
311 #if CONFIG_LCD == LCD_SSD1815
312 #define ASPECT 320 /* = 1.25 (fixed point 24.8) */
313 #else
314 #define ASPECT 256 /* = 1.00 */
315 #endif
317 #else /* !LCD_BITMAP */
319 #define MYLCD(fn) pgfx_ ## fn
320 #define DIST 9
321 static int x_off = 10;
322 static int y_off = 7;
323 #define ASPECT 300 /* = 1.175 */
325 #endif /* !LCD_BITMAP */
327 struct point_3D {
328 long x, y, z;
331 struct point_2D {
332 long x, y;
335 struct line {
336 int start, end;
339 struct face {
340 int corner[4];
341 int line[4];
344 /* initial, unrotated cube corners */
345 static const struct point_3D sommet[8] =
347 {-DIST, -DIST, -DIST},
348 { DIST, -DIST, -DIST},
349 { DIST, DIST, -DIST},
350 {-DIST, DIST, -DIST},
351 {-DIST, -DIST, DIST},
352 { DIST, -DIST, DIST},
353 { DIST, DIST, DIST},
354 {-DIST, DIST, DIST}
357 /* The 12 lines forming the edges */
358 static const struct line lines[12] =
360 {0, 1}, {1, 2}, {2, 3}, {3, 0},
361 {4, 7}, {7, 6}, {6, 5}, {5, 4},
362 {0, 4}, {1, 5}, {2, 6}, {3, 7}
365 static bool lines_drawn[12];
367 /* The 6 faces of the cube; points are in clockwise order when viewed
368 from the outside */
369 static const struct face faces[6] =
371 {{0, 1, 2, 3}, {0, 1, 2, 3}},
372 {{4, 7, 6, 5}, {4, 5, 6, 7}},
373 {{0, 4, 5, 1}, {8, 7, 9, 0}},
374 {{2, 6, 7, 3}, {10, 5, 11, 2}},
375 {{0, 3, 7, 4}, {3, 11, 4, 8}},
376 {{1, 5, 6, 2}, {9, 6, 10, 1}}
379 #if LCD_DEPTH > 1 || defined(USE_GSLIB)
380 static const unsigned face_colors[6] =
382 #ifdef HAVE_LCD_COLOR
383 LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(0, 255, 0),
384 LCD_RGBPACK(0, 255, 0), LCD_RGBPACK(0, 0, 255), LCD_RGBPACK(0, 0, 255)
385 #elif defined(USE_GSLIB)
386 #ifdef MROBE_100
387 GREY_LIGHTGRAY, GREY_LIGHTGRAY, GREY_DARKGRAY,
388 GREY_DARKGRAY, GREY_WHITE, GREY_WHITE
389 #else
390 GREY_LIGHTGRAY, GREY_LIGHTGRAY, GREY_DARKGRAY,
391 GREY_DARKGRAY, GREY_BLACK, GREY_BLACK
392 #endif
393 #else
394 LCD_LIGHTGRAY, LCD_LIGHTGRAY, LCD_DARKGRAY,
395 LCD_DARKGRAY, LCD_BLACK, LCD_BLACK
396 #endif
398 #endif
400 enum {
401 #if LCD_DEPTH > 1 || defined(USE_GSLIB)
402 SOLID,
403 #endif
404 HIDDEN_LINES,
405 WIREFRAME,
406 NUM_MODES
409 static int mode = 0;
411 struct counter {
412 const char *label;
413 short speed;
414 short angle;
417 static struct counter axes[] = {
418 {"x-axis", 1, 0},
419 {"y-axis", 3, 0},
420 {"z-axis", 2, 0}
423 static struct point_3D point3D[8];
424 static struct point_2D point2D[8];
425 static long matrice[3][3];
427 static const int nb_points = 8;
428 static long z_off = 600;
430 static void cube_rotate(int xa, int ya, int za)
432 int i;
433 /* Just to prevent unnecessary lookups */
434 long sxa, cxa, sya, cya, sza, cza;
436 sxa = fp14_sin(xa);
437 cxa = fp14_cos(xa);
438 sya = fp14_sin(ya);
439 cya = fp14_cos(ya);
440 sza = fp14_sin(za);
441 cza = fp14_cos(za);
443 /* calculate overall translation matrix */
444 matrice[0][0] = (cza * cya) >> 14;
445 matrice[1][0] = (sza * cya) >> 14;
446 matrice[2][0] = -sya;
448 matrice[0][1] = (((cza * sya) >> 14) * sxa - sza * cxa) >> 14;
449 matrice[1][1] = (((sza * sya) >> 14) * sxa + cxa * cza) >> 14;
450 matrice[2][1] = (sxa * cya) >> 14;
452 matrice[0][2] = (((cza * sya) >> 14) * cxa + sza * sxa) >> 14;
453 matrice[1][2] = (((sza * sya) >> 14) * cxa - cza * sxa) >> 14;
454 matrice[2][2] = (cxa * cya) >> 14;
456 /* apply translation matrix to all points */
457 for (i = 0; i < nb_points; i++)
459 point3D[i].x = matrice[0][0] * sommet[i].x + matrice[1][0] * sommet[i].y
460 + matrice[2][0] * sommet[i].z;
462 point3D[i].y = matrice[0][1] * sommet[i].x + matrice[1][1] * sommet[i].y
463 + matrice[2][1] * sommet[i].z;
465 point3D[i].z = matrice[0][2] * sommet[i].x + matrice[1][2] * sommet[i].y
466 + matrice[2][2] * sommet[i].z;
470 static void cube_viewport(void)
472 int i;
474 /* Do viewport transformation for all points */
475 for (i = 0; i < nb_points; i++)
477 #if ASPECT != 256
478 point2D[i].x = (point3D[i].x * ASPECT) / (point3D[i].z + (z_off << 14))
479 + x_off;
480 #else
481 point2D[i].x = (point3D[i].x << 8) / (point3D[i].z + (z_off << 14))
482 + x_off;
483 #endif
484 point2D[i].y = (point3D[i].y << 8) / (point3D[i].z + (z_off << 14))
485 + y_off;
489 static void cube_draw(void)
491 int i, j, line;
492 #if LCD_DEPTH > 1 || defined(USE_GSLIB)
493 unsigned old_foreground;
494 #endif
496 switch (mode)
498 #if LCD_DEPTH > 1 || defined(USE_GSLIB)
499 case SOLID:
501 old_foreground = MY_GET_FOREGROUND();
502 for (i = 0; i < 6; i++)
504 /* backface culling; if the shape winds counter-clockwise, we are
505 * looking at the backface, and the (simplified) cross product
506 * is < 0. Do not draw it. */
507 if (0 >= (point2D[faces[i].corner[1]].x - point2D[faces[i].corner[0]].x)
508 * (point2D[faces[i].corner[2]].y - point2D[faces[i].corner[1]].y)
509 - (point2D[faces[i].corner[1]].y - point2D[faces[i].corner[0]].y)
510 * (point2D[faces[i].corner[2]].x - point2D[faces[i].corner[1]].x))
511 continue;
513 MY_SET_FOREGROUND(face_colors[i]);
514 MY_FILLTRIANGLE(point2D[faces[i].corner[0]].x,
515 point2D[faces[i].corner[0]].y,
516 point2D[faces[i].corner[1]].x,
517 point2D[faces[i].corner[1]].y,
518 point2D[faces[i].corner[2]].x,
519 point2D[faces[i].corner[2]].y);
520 MY_FILLTRIANGLE(point2D[faces[i].corner[0]].x,
521 point2D[faces[i].corner[0]].y,
522 point2D[faces[i].corner[2]].x,
523 point2D[faces[i].corner[2]].y,
524 point2D[faces[i].corner[3]].x,
525 point2D[faces[i].corner[3]].y);
528 MY_SET_FOREGROUND(old_foreground);
529 break;
530 #endif /* (LCD_DEPTH > 1) || GSLIB */
532 case HIDDEN_LINES:
534 rb->memset(lines_drawn, 0, sizeof(lines_drawn));
535 for (i = 0; i < 6; i++)
537 /* backface culling; if the shape winds counter-clockwise, we are
538 * looking at the backface, and the (simplified) cross product
539 * is < 0. Do not draw it. */
540 if (0 >= (point2D[faces[i].corner[1]].x - point2D[faces[i].corner[0]].x)
541 * (point2D[faces[i].corner[2]].y - point2D[faces[i].corner[1]].y)
542 - (point2D[faces[i].corner[1]].y - point2D[faces[i].corner[0]].y)
543 * (point2D[faces[i].corner[2]].x - point2D[faces[i].corner[1]].x))
544 continue;
546 for (j = 0; j < 4; j++)
548 line = faces[i].line[j];
549 if (!lines_drawn[line])
551 lines_drawn[line] = true;
552 MYLCD(drawline)(point2D[lines[line].start].x,
553 point2D[lines[line].start].y,
554 point2D[lines[line].end].x,
555 point2D[lines[line].end].y);
559 break;
561 case WIREFRAME:
563 for (i = 0; i < 12; i++)
564 MYLCD(drawline)(point2D[lines[i].start].x,
565 point2D[lines[i].start].y,
566 point2D[lines[i].end].x,
567 point2D[lines[i].end].y);
568 break;
572 void cleanup(void *parameter)
574 (void)parameter;
576 #ifdef USE_GSLIB
577 grey_release();
578 #elif defined HAVE_LCD_CHARCELLS
579 pgfx_release();
580 #endif
583 enum plugin_status plugin_start(const void* parameter)
585 char buffer[30];
586 int t_disp = 0;
587 #ifdef USE_GSLIB
588 unsigned char *gbuf;
589 size_t gbuf_size = 0;
590 bool mode_switch = true;
591 #endif
593 int button;
594 int lastbutton = BUTTON_NONE;
595 int curr = 0;
596 bool highspeed = false;
597 bool paused = false;
598 bool redraw = true;
599 bool exit = false;
601 (void)(parameter);
603 #ifdef HAVE_LCD_BITMAP
604 #if defined(USE_GSLIB)
605 gbuf = (unsigned char *)rb->plugin_get_buffer(&gbuf_size);
606 if (!grey_init(gbuf, gbuf_size, GREY_BUFFERED,
607 LCD_WIDTH, LCD_HEIGHT, NULL))
609 rb->splash(HZ, "Couldn't init greyscale display");
610 return PLUGIN_ERROR;
612 /* init lcd_ function pointers */
613 lcdfuncs.update = rb->lcd_update;
614 lcdfuncs.clear_display = rb->lcd_clear_display;
615 lcdfuncs.drawline = rb->lcd_drawline;
616 lcdfuncs.putsxy = rb->lcd_putsxy;
618 #ifdef MROBE_100
619 grey_set_background(GREY_BLACK);
620 #endif
622 grey_setfont(FONT_SYSFIXED);
623 #endif
624 rb->lcd_setfont(FONT_SYSFIXED);
625 #else /* LCD_CHARCELLS */
626 if (!pgfx_init(4, 2))
628 rb->splash(HZ*2, "Old LCD :(");
629 return PLUGIN_OK;
631 pgfx_display(0, 0);
632 #endif
634 while(!exit)
636 if (redraw)
638 MYLCD(clear_display)();
639 cube_rotate(axes[0].angle, axes[1].angle, axes[2].angle);
640 cube_viewport();
641 cube_draw();
642 redraw = false;
645 #ifdef HAVE_LCD_BITMAP
646 if (t_disp > 0)
648 t_disp--;
649 rb->snprintf(buffer, sizeof(buffer), "%s: %d %s",
650 axes[curr].label,
651 paused ? axes[curr].angle : axes[curr].speed,
652 highspeed ? "(hs)" : "");
653 MYLCD(putsxy)(0, LCD_HEIGHT-8, buffer);
654 if (t_disp == 0)
655 redraw = true;
657 #else
658 if (t_disp > 0)
660 if (t_disp == DISP_TIME)
662 rb->lcd_puts(5, 0, axes[curr].label);
663 rb->snprintf(buffer, sizeof(buffer), "%d %c",
664 paused ? axes[curr].angle : axes[curr].speed,
665 highspeed ? 'H' : ' ');
666 rb->lcd_puts(5, 1, buffer);
668 t_disp--;
669 if (t_disp == 0)
671 rb->lcd_clear_display();
672 pgfx_display(0, 0);
675 #endif
676 #ifdef USE_GSLIB
677 if (mode_switch)
679 grey_show(mode == SOLID);
680 mode_switch = false;
682 #endif
683 MYLCD(update)();
685 if (!paused)
687 int i;
689 for (i = 0; i < 3; i++)
691 axes[i].angle += axes[i].speed;
692 if (axes[i].angle > 359)
693 axes[i].angle -= 360;
694 else if (axes[i].angle < 0)
695 axes[i].angle += 360;
697 redraw = true;
699 if (highspeed)
700 rb->yield();
701 else
702 rb->sleep(HZ/25);
703 button = rb->button_get(false);
705 else
707 button = rb->button_get_w_tmo(HZ/25);
710 switch (button)
712 case CUBE_INC:
713 case CUBE_INC|BUTTON_REPEAT:
714 if (!paused)
716 if (axes[curr].speed < 10)
717 axes[curr].speed++;
719 else
721 if (++axes[curr].angle > 359)
722 axes[curr].angle -= 360;
724 t_disp = DISP_TIME;
725 redraw = true;
726 break;
728 case CUBE_DEC:
729 case CUBE_DEC|BUTTON_REPEAT:
730 if (!paused)
732 if (axes[curr].speed > -10)
733 axes[curr].speed--;
735 else
737 if (--axes[curr].angle < 0)
738 axes[curr].angle += 360;
740 t_disp = DISP_TIME;
741 redraw = true;
742 break;
744 case CUBE_NEXT:
745 if (++curr > 2)
746 curr = 0;
747 t_disp = DISP_TIME;
748 break;
750 case CUBE_PREV:
751 if (--curr < 0)
752 curr = 2;
753 t_disp = DISP_TIME;
754 break;
756 case CUBE_MODE:
757 #ifdef CUBE_MODE_PRE
758 if (lastbutton != CUBE_MODE_PRE)
759 break;
760 #endif
761 if (++mode >= NUM_MODES)
762 mode = 0;
763 #ifdef USE_GSLIB
764 mylcd = (mode == SOLID) ? &greyfuncs : &lcdfuncs;
765 mode_switch = true;
766 #endif
767 redraw = true;
768 break;
770 case CUBE_PAUSE:
771 #ifdef CUBE_PAUSE_PRE
772 if (lastbutton != CUBE_PAUSE_PRE)
773 break;
774 #endif
775 paused = !paused;
776 break;
778 case CUBE_HIGHSPEED:
779 #ifdef CUBE_HIGHSPEED_PRE
780 if (lastbutton != CUBE_HIGHSPEED_PRE)
781 break;
782 #endif
783 highspeed = !highspeed;
784 t_disp = DISP_TIME;
785 break;
787 #ifdef CUBE_RC_QUIT
788 case CUBE_RC_QUIT:
789 #endif
790 case CUBE_QUIT:
791 exit = true;
792 break;
794 default:
795 if (rb->default_event_handler_ex(button, cleanup, NULL)
796 == SYS_USB_CONNECTED)
797 return PLUGIN_USB_CONNECTED;
798 break;
800 if (button != BUTTON_NONE)
801 lastbutton = button;
804 #ifdef USE_GSLIB
805 grey_release();
806 #elif defined(HAVE_LCD_CHARCELLS)
807 pgfx_release();
808 #endif
809 return PLUGIN_OK;