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