Add demo application.
[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.1  2006/03/22 09:52:13  bernie
18  *#* Add demo application.
19  *#*
20  *#* Revision 1.1  2006/01/23 23:14:29  bernie
21  *#* Implement simple, but impressive windowing system.
22  *#*
23  *#*/
24
25 #include <emul/emul.h>
26 #include <kern/proc.h>
27 #include <drv/timer.h>
28 #include <drv/buzzer.h>
29 #include <drv/lcd_gfx.h>
30 #include <drv/kbd.h>
31 #include <gfx/gfx.h>
32 #include <gfx/win.h>
33 #include <gfx/text.h>
34 #include <gfx/font.h>
35 #include <icons/artwork.h>
36 #include <mware/menu.h>
37 #include <cfg/macros.h>
38
39
40 /* SETTINGS SUBMENU ***************************/
41
42 static struct MenuItem settings_items[] =
43 {
44         { (const_iptr_t)"settings_0", 0, (MenuHook)0,  (iptr_t)0        },
45         { (const_iptr_t)"settings_1", 0, (MenuHook)0,  (iptr_t)0        },
46         { (const_iptr_t)"settings_2", 0, (MenuHook)0, (iptr_t)0      },
47         { (const_iptr_t)"settings_3", 0, (MenuHook)0,  (iptr_t)0   },
48         { (const_iptr_t)"settings_4", 0, (MenuHook)0,  (iptr_t)0 },
49         { (const_iptr_t)"settings_5", MIF_TOGGLE, (MenuHook)0, (iptr_t)0             },
50         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
51 };
52 static struct Menu settings_menu = { settings_items, "Settings Menu", MF_STICKY, &lcd_bitmap, 0 };
53
54 /*** DISPLAY MENU ****************************/
55
56 static struct MenuItem display_items[] =
57 {
58         { (const_iptr_t)"display_0", 0, (MenuHook)0,           (iptr_t)0 },
59         { (const_iptr_t)"display_1", 0, (MenuHook)0, (iptr_t)0     },
60         { (const_iptr_t)"display_2", 0, (MenuHook)0,   (iptr_t)0     },
61         { (const_iptr_t)"display_3", 0, (MenuHook)0,  (iptr_t)0 },
62         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
63 };
64 static struct Menu display_menu = { display_items, "Display Menu", MF_STICKY, &lcd_bitmap, 0 };
65
66
67 /*** SETUP MENU ******************************/
68
69 static struct MenuItem setup_items[] =
70 {
71         { (const_iptr_t)"Setup 0", 0, (MenuHook)NULL, (iptr_t)0    },
72         { (const_iptr_t)"Setup 1", 0, (MenuHook)NULL, (iptr_t)0    },
73         { (const_iptr_t)"Setup 2", 0, (MenuHook)NULL, (iptr_t)0    },
74         { (const_iptr_t)"Setup 3", 0, (MenuHook)NULL, (iptr_t)0    },
75         { (const_iptr_t)0, 0, NULL, NULL }
76 };
77 static struct Menu setup_menu = { setup_items, "Setup Menu", MF_STICKY, &lcd_bitmap, 0 };
78
79
80 /*** MAIN MENU *******************************/
81
82 static struct MenuItem main_items[] =
83 {
84         { (const_iptr_t)"Settings", 0, (MenuHook)menu_handle, (iptr_t)&settings_menu },
85         { (const_iptr_t)"Display", 0, (MenuHook)menu_handle, (iptr_t)&display_menu  },
86         { (const_iptr_t)"Setup",   0, (MenuHook)menu_handle, (iptr_t)&setup_menu    },
87         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
88 };
89 static struct Menu main_menu = { main_items, "Main Menu", MF_STICKY, &lcd_bitmap, 0 };
90
91 static void magic(struct Bitmap *bitmap, coord_t x, coord_t y)
92 {
93         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 };
94         unsigned int i;
95
96         gfx_moveTo(bitmap, coords[countof(coords)-2]/2 + x, coords[countof(coords)-1]/3 + y);
97         for (i = 0; i < countof(coords); i += 2)
98                 gfx_lineTo(bitmap, coords[i]/2 + x, coords[i+1]/3 + y);
99 }
100
101 Window root_win;
102
103 void schedule(void)
104 {
105 //      win_compose(&root_win);
106         lcd_blit_bitmap(root_win.bitmap);
107         emul_idle();
108         usleep(10000);
109 }
110
111 int main(int argc, char *argv[])
112 {
113         emul_init(&argc, argv);
114         timer_init();
115         buz_init();
116         kbd_init();
117         lcd_init();
118         proc_init();
119
120         gfx_bitmapClear(&lcd_bitmap);
121         extern const Font font_10x20;
122         extern const Font font_helvB10;
123         extern const Font font_courB08;
124         extern const Font font_courB14;
125         extern const Font font_mono17;
126         extern const Font font_luBS14;
127         extern const Font font_ncenB18;
128         gfx_setFont(&lcd_bitmap, &font_luBS14);
129
130         text_xprintf(&lcd_bitmap, 1, 0, STYLEF_BOLD | TEXT_FILL | TEXT_CENTER, "Hello, world!");
131         for (int i = 0; i < 1000; ++i)
132         {
133                 lcd_blit_bitmap(&lcd_bitmap);
134                 emul_idle();
135         }
136         gfx_blitRaster(&lcd_bitmap, 0, 0, customer_pw_logo, 122, 32, 4);
137         lcd_blit_bitmap(&lcd_bitmap);
138         emul_idle();
139         sleep(1);
140         //timer_delay(1000);
141
142         const coord_t small_left = 45, small_top = 30, small_width = 50, small_height = 30;
143         const coord_t large_left = -10, large_top = 10, large_width = 85, large_height = 41;
144
145         Window small_win, large_win;
146         Bitmap small_bm, large_bm;
147         uint8_t small_raster[RASTER_SIZE(small_width, small_height)];
148         uint8_t large_raster[RASTER_SIZE(large_width, large_height)];
149
150         win_create(&root_win,  &lcd_bitmap);
151
152         gfx_bitmapInit(&large_bm, large_raster, large_width, large_height);
153         win_create(&large_win, &large_bm);
154         win_open(&large_win, &root_win);
155         win_move(&large_win, large_left, large_top);
156
157         gfx_bitmapInit(&small_bm, small_raster, small_width, small_height);
158         win_create(&small_win, &small_bm);
159         win_open(&small_win, &root_win);
160         win_move(&small_win, small_left, small_top);
161
162
163         coord_t x = 0, y = LCD_WIDTH / 2;
164         coord_t xdir = +1, ydir = -1;
165         coord_t xdir_large = +1;
166         coord_t ydir_small = +1;
167         int raise_counter = 0;
168         int i;
169         Bitmap *bm;
170
171         for(;;)
172         {
173                 /* Background animation */
174                 bm = &lcd_bitmap;
175                 gfx_bitmapClear(bm);
176 /*              gfx_setClipRect(bm, 0, 0, bm->width, bm->height);
177                 gfx_rectDraw(bm, 10, 10, bm->width-10, bm->height-10);
178                 gfx_setClipRect(bm, 11, 11, bm->width-11, bm->height-11);
179 */              magic(bm, x, y);
180                 x += xdir;
181                 y += ydir;
182                 if (x >= bm->width)  xdir = -1;
183                 if (x <= -50)        xdir = +1;
184                 if (y >= bm->height) ydir = -1;
185                 if (y <= -50)        ydir = +1;
186
187                 menu_handle(&main_menu);
188
189                 /* Large window animation */
190                 bm = large_win.bitmap;
191                 gfx_bitmapClear(bm);
192                 for (i = 0; i < bm->height / 2; i += 2)
193                         gfx_rectDraw(bm, 0 + i, 0 + i, bm->width - i, bm->height - i);
194
195
196                 /* Small window animation */
197                 bm = small_win.bitmap;
198                 gfx_bitmapClear(bm);
199                 gfx_rectDraw(bm, 0, 0, bm->width, bm->height);
200                 gfx_line(bm, 0, 0, bm->width, bm->height);
201                 gfx_line(bm, bm->width, 0, 0, bm->height);
202
203                 /* Move windows around */
204                 win_move(&large_win, large_win.geom.xmin + xdir_large, large_top);
205                 if (large_win.geom.xmin < -20) xdir_large = +1;
206                 if (large_win.geom.xmin > RECT_WIDTH(&root_win.geom) - 5) xdir_large = -1;
207
208                 win_move(&small_win, small_left, small_win.geom.ymin + ydir_small);
209                 if (small_win.geom.ymin < -20) ydir_small = +1;
210                 if (small_win.geom.ymin > RECT_HEIGHT(&root_win.geom) - 5) ydir_small = -1;
211
212                 ++raise_counter;
213                 if (raise_counter % 997 == 0)
214                         win_raise(&small_win);
215                 else if (raise_counter % 731 == 0)
216                         win_raise(&large_win);
217
218                 win_compose(&root_win);
219                 lcd_blit_bitmap(root_win.bitmap);
220                 emul_idle();
221                 usleep(10000);
222         }
223
224         emul_cleanup();
225         return 0;
226 }