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