Add bouncing logo demo.
[bertos.git] / app / demo / demo.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Bernardo Innocenti <bernie@develer.com>
11  *
12  * \brief Windowing system test.
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.2  2006/03/27 04:49:50  bernie
18  *#* Add bouncing logo demo.
19  *#*
20  *#* Revision 1.1  2006/03/22 09:52:13  bernie
21  *#* Add demo application.
22  *#*
23  *#* Revision 1.1  2006/01/23 23:14:29  bernie
24  *#* Implement simple, but impressive windowing system.
25  *#*
26  *#*/
27
28 #include <emul/emul.h>
29 #include <kern/proc.h>
30 #include <drv/timer.h>
31 #include <drv/buzzer.h>
32 #include <drv/lcd_gfx.h>
33 #include <drv/kbd.h>
34 #include <gfx/gfx.h>
35 #include <gfx/win.h>
36 #include <gfx/text.h>
37 #include <gfx/font.h>
38 #include <icons/logo.h>
39 #include <mware/menu.h>
40 #include <cfg/macros.h>
41
42
43 /* SETTINGS SUBMENU ***************************/
44
45 static struct MenuItem settings_items[] =
46 {
47         { (const_iptr_t)"settings_0", 0, (MenuHook)0,  (iptr_t)0        },
48         { (const_iptr_t)"settings_1", 0, (MenuHook)0,  (iptr_t)0        },
49         { (const_iptr_t)"settings_2", 0, (MenuHook)0, (iptr_t)0      },
50         { (const_iptr_t)"settings_3", 0, (MenuHook)0,  (iptr_t)0   },
51         { (const_iptr_t)"settings_4", 0, (MenuHook)0,  (iptr_t)0 },
52         { (const_iptr_t)"settings_5", MIF_TOGGLE, (MenuHook)0, (iptr_t)0             },
53         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
54 };
55 static struct Menu settings_menu = { settings_items, "Settings Menu", MF_STICKY, &lcd_bitmap, 0 };
56
57 /*** DISPLAY MENU ****************************/
58
59 static struct MenuItem display_items[] =
60 {
61         { (const_iptr_t)"display_0", 0, (MenuHook)0,           (iptr_t)0 },
62         { (const_iptr_t)"display_1", 0, (MenuHook)0, (iptr_t)0     },
63         { (const_iptr_t)"display_2", 0, (MenuHook)0,   (iptr_t)0     },
64         { (const_iptr_t)"display_3", 0, (MenuHook)0,  (iptr_t)0 },
65         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
66 };
67 static struct Menu display_menu = { display_items, "Display Menu", MF_STICKY, &lcd_bitmap, 0 };
68
69
70 /*** SETUP MENU ******************************/
71
72 static struct MenuItem setup_items[] =
73 {
74         { (const_iptr_t)"Setup 0", 0, (MenuHook)NULL, (iptr_t)0    },
75         { (const_iptr_t)"Setup 1", 0, (MenuHook)NULL, (iptr_t)0    },
76         { (const_iptr_t)"Setup 2", 0, (MenuHook)NULL, (iptr_t)0    },
77         { (const_iptr_t)"Setup 3", 0, (MenuHook)NULL, (iptr_t)0    },
78         { (const_iptr_t)0, 0, NULL, NULL }
79 };
80 static struct Menu setup_menu = { setup_items, "Setup Menu", MF_STICKY, &lcd_bitmap, 0 };
81
82
83 /*** MAIN MENU *******************************/
84
85 static struct MenuItem main_items[] =
86 {
87         { (const_iptr_t)"Settings", 0, (MenuHook)menu_handle, (iptr_t)&settings_menu },
88         { (const_iptr_t)"Display", 0, (MenuHook)menu_handle, (iptr_t)&display_menu  },
89         { (const_iptr_t)"Setup",   0, (MenuHook)menu_handle, (iptr_t)&setup_menu    },
90         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
91 };
92 static struct Menu main_menu = { main_items, "Main Menu", MF_STICKY, &lcd_bitmap, 0 };
93
94 static void magic(struct Bitmap *bitmap, coord_t x, coord_t y)
95 {
96         static const coord_t coords[] = { 120, 34, 90, 90, 30, 90, 0, 34, 60, 0, 90, 90, 0, 34, 120, 34, 30, 90, 60, 0 };
97         unsigned int i;
98
99         gfx_moveTo(bitmap, coords[countof(coords)-2]/2 + x, coords[countof(coords)-1]/3 + y);
100         for (i = 0; i < countof(coords); i += 2)
101                 gfx_lineTo(bitmap, coords[i]/2 + x, coords[i+1]/3 + y);
102 }
103
104 Window root_win;
105
106 void schedule(void)
107 {
108 //      win_compose(&root_win);
109         lcd_blit_bitmap(root_win.bitmap);
110         emul_idle();
111         usleep(10000);
112 }
113
114 void hello_world(void)
115 {
116         gfx_bitmapClear(&lcd_bitmap);
117         extern const Font font_10x20;
118         extern const Font font_helvB10;
119         extern const Font font_courB08;
120         extern const Font font_courB14;
121         extern const Font font_mono17;
122         extern const Font font_luBS14;
123         extern const Font font_ncenB18;
124         gfx_setFont(&lcd_bitmap, &font_luBS14);
125
126         text_xprintf(&lcd_bitmap, 1, 0, STYLEF_BOLD | TEXT_FILL | TEXT_CENTER, "Hello, world!");
127         for (int i = 0; i < 1000; ++i)
128         {
129                 lcd_blit_bitmap(&lcd_bitmap);
130                 emul_idle();
131         }
132 }
133
134 /**
135  * Show the splash screen
136  */
137 void bouncing_logo(void)
138 {
139         const long SPEED_SCALE = 1000;
140         const long GRAVITY_ACCEL = 10;
141         const long BOUNCE_ELASTICITY = 2;
142         long h = (long)(-project_grl_logo.height) * SPEED_SCALE;
143         long speed = 1000;
144
145         /* Repeat until logo stands still on the bottom edge */
146         while (!((speed == 0) && (h == 0)))
147         {
148                 /* Move */
149                 h += speed;
150
151                 /* Gravity acceleration */
152                 speed += GRAVITY_ACCEL;
153
154                 if (h > 0 && speed > 0)
155                 {
156                         /* Bounce */
157                         speed = - (speed / BOUNCE_ELASTICITY);
158
159                 }
160
161                 /* Update graphics */
162                 gfx_bitmapClear(&lcd_bitmap);
163                 gfx_blitImage(&lcd_bitmap,
164                         (lcd_bitmap.width - project_grl_logo.width) / 2,
165                         h / SPEED_SCALE,
166                         &project_grl_logo);
167                 lcd_blit_bitmap(&lcd_bitmap);
168
169                 timer_delay(10);
170         }
171 }
172
173 int main(int argc, char *argv[])
174 {
175         emul_init(&argc, argv);
176         timer_init();
177         buz_init();
178         kbd_init();
179         lcd_init();
180         proc_init();
181
182         hello_world();
183         bouncing_logo();
184
185         const coord_t small_left = 45, small_top = 30, small_width = 50, small_height = 30;
186         const coord_t large_left = -10, large_top = 10, large_width = 85, large_height = 41;
187
188         Window small_win, large_win;
189         Bitmap small_bm, large_bm;
190         uint8_t small_raster[RASTER_SIZE(small_width, small_height)];
191         uint8_t large_raster[RASTER_SIZE(large_width, large_height)];
192
193         win_create(&root_win,  &lcd_bitmap);
194
195         gfx_bitmapInit(&large_bm, large_raster, large_width, large_height);
196         win_create(&large_win, &large_bm);
197         win_open(&large_win, &root_win);
198         win_move(&large_win, large_left, large_top);
199
200         gfx_bitmapInit(&small_bm, small_raster, small_width, small_height);
201         win_create(&small_win, &small_bm);
202         win_open(&small_win, &root_win);
203         win_move(&small_win, small_left, small_top);
204
205
206         coord_t x = 0, y = LCD_WIDTH / 2;
207         coord_t xdir = +1, ydir = -1;
208         coord_t xdir_large = +1;
209         coord_t ydir_small = +1;
210         int raise_counter = 0;
211         int i;
212         Bitmap *bm;
213
214         for(;;)
215         {
216                 /* Background animation */
217                 bm = &lcd_bitmap;
218                 gfx_bitmapClear(bm);
219 /*              gfx_setClipRect(bm, 0, 0, bm->width, bm->height);
220                 gfx_rectDraw(bm, 10, 10, bm->width-10, bm->height-10);
221                 gfx_setClipRect(bm, 11, 11, bm->width-11, bm->height-11);
222 */              magic(bm, x, y);
223                 x += xdir;
224                 y += ydir;
225                 if (x >= bm->width)  xdir = -1;
226                 if (x <= -50)        xdir = +1;
227                 if (y >= bm->height) ydir = -1;
228                 if (y <= -50)        ydir = +1;
229
230                 menu_handle(&main_menu);
231
232                 /* Large window animation */
233                 bm = large_win.bitmap;
234                 gfx_bitmapClear(bm);
235                 for (i = 0; i < bm->height / 2; i += 2)
236                         gfx_rectDraw(bm, 0 + i, 0 + i, bm->width - i, bm->height - i);
237
238
239                 /* Small window animation */
240                 bm = small_win.bitmap;
241                 gfx_bitmapClear(bm);
242                 gfx_rectDraw(bm, 0, 0, bm->width, bm->height);
243                 gfx_line(bm, 0, 0, bm->width, bm->height);
244                 gfx_line(bm, bm->width, 0, 0, bm->height);
245
246                 /* Move windows around */
247                 win_move(&large_win, large_win.geom.xmin + xdir_large, large_top);
248                 if (large_win.geom.xmin < -20) xdir_large = +1;
249                 if (large_win.geom.xmin > RECT_WIDTH(&root_win.geom) - 5) xdir_large = -1;
250
251                 win_move(&small_win, small_left, small_win.geom.ymin + ydir_small);
252                 if (small_win.geom.ymin < -20) ydir_small = +1;
253                 if (small_win.geom.ymin > RECT_HEIGHT(&root_win.geom) - 5) ydir_small = -1;
254
255                 ++raise_counter;
256                 if (raise_counter % 997 == 0)
257                         win_raise(&small_win);
258                 else if (raise_counter % 731 == 0)
259                         win_raise(&large_win);
260
261                 win_compose(&root_win);
262                 lcd_blit_bitmap(root_win.bitmap);
263                 emul_idle();
264                 usleep(10000);
265         }
266
267         emul_cleanup();
268         return 0;
269 }