demo.c

Go to the documentation of this file.
00001 
00038 #include <cfg/macros.h>
00039 
00040 #include <emul/emul.h>
00041 
00042 #include <kern/irq.h>
00043 #include <kern/proc.h>
00044 #include <kern/monitor.h>
00045 #include <kern/msg.h>
00046 
00047 #include <drv/timer.h>
00048 #include <drv/buzzer.h>
00049 #include <drv/lcd_gfx.h>
00050 #include <drv/kbd.h>
00051 
00052 #include <gfx/gfx.h>
00053 #include <gfx/win.h>
00054 #include <gfx/text.h>
00055 #include <gfx/font.h>
00056 
00057 #include <gui/menu.h>
00058 #include <icons/logo.h>
00059 
00066 void schedule(void)
00067 {
00068     lcd_blitBitmap(&lcd_bitmap);
00069     emul_idle();
00070 }
00071 
00075 static void magic(struct Bitmap *bitmap, coord_t x, coord_t y)
00076 {
00077     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 };
00078     unsigned int i;
00079 
00080     gfx_moveTo(bitmap, coords[countof(coords)-2]/2 + x, coords[countof(coords)-1]/3 + y);
00081     for (i = 0; i < countof(coords); i += 2)
00082         gfx_lineTo(bitmap, coords[i]/2 + x, coords[i+1]/3 + y);
00083 }
00084 
00085 static void hello_world(Bitmap *bm)
00086 {
00087     extern const Font font_ncenB18;
00088     const Font *old_font = bm->font;
00089 
00090     gfx_bitmapClear(bm);
00091 
00092     /* Set big font */
00093     gfx_setFont(bm, &font_ncenB18);
00094 
00095     text_xprintf(bm, 1, 0, STYLEF_BOLD | TEXT_FILL | TEXT_CENTER,
00096             "Hello, world!");
00097 
00098     lcd_blitBitmap(bm);
00099     timer_delay(1000);
00100 
00101     /* Restore old font */
00102     gfx_setFont(bm, old_font);
00103 }
00104 
00108 static void bouncing_logo(Bitmap *bm)
00109 {
00110     const long SPEED_SCALE = 1000;
00111     const long GRAVITY_ACCEL = 10;
00112     const long BOUNCE_ELASTICITY = 2;
00113     long h = (long)(-bertos_logo.height) * SPEED_SCALE;
00114     long speed = 1000;
00115 
00116     /* Repeat until logo stands still on the bottom edge */
00117     while (!((speed == 0) && (h == 0)))
00118     {
00119         /* Move */
00120         h += speed;
00121 
00122         /* Gravity acceleration */
00123         speed += GRAVITY_ACCEL;
00124 
00125         if (h > 0 && speed > 0)
00126         {
00127             /* Bounce */
00128             speed = - (speed / BOUNCE_ELASTICITY);
00129 
00130         }
00131 
00132         /* Update graphics */
00133         gfx_bitmapClear(bm);
00134         gfx_blitImage(bm,
00135             (bm->width - bertos_logo.width) / 2,
00136             h / SPEED_SCALE,
00137             &bertos_logo);
00138         lcd_blitBitmap(bm);
00139 
00140         timer_delay(10);
00141     }
00142 }
00143 
00144 void win_demo(Bitmap *bm)
00145 {
00146     const coord_t small_left = 45, small_top = 30, small_width = 50, small_height = 30;
00147     const coord_t large_left = -10, large_top = 10, large_width = 85, large_height = 41;
00148 
00149     Window root_win, small_win, large_win;
00150     Bitmap small_bm, large_bm;
00151     uint8_t small_raster[RAST_SIZE(small_width, small_height)];
00152     uint8_t large_raster[RAST_SIZE(large_width, large_height)];
00153 
00154     win_create(&root_win, bm);
00155 
00156     gfx_bitmapInit(&large_bm, large_raster, large_width, large_height);
00157     win_create(&large_win, &large_bm);
00158     win_open(&large_win, &root_win);
00159     win_move(&large_win, large_left, large_top);
00160 
00161     gfx_bitmapInit(&small_bm, small_raster, small_width, small_height);
00162     win_create(&small_win, &small_bm);
00163     win_open(&small_win, &root_win);
00164     win_move(&small_win, small_left, small_top);
00165 
00166 
00167     coord_t x = 0, y = LCD_WIDTH / 2;
00168     coord_t xdir = +1, ydir = -1;
00169     coord_t xdir_large = +1;
00170     coord_t ydir_small = +1;
00171     int raise_counter = 0;
00172     int i;
00173 
00174     for(;;)
00175     {
00176         /* Background animation */
00177         bm = root_win.bitmap;
00178         gfx_bitmapClear(bm);
00179 //      gfx_setClipRect(bm, 0, 0, bm->width, bm->height);
00180 //      gfx_rectDraw(bm, 10, 10, bm->width-10, bm->height-10);
00181 //      gfx_setClipRect(bm, 11, 11, bm->width-11, bm->height-11);
00182         magic(bm, x, y);
00183         x += xdir;
00184         y += ydir;
00185         if (x >= bm->width)  xdir = -1;
00186         if (x <= -50)        xdir = +1;
00187         if (y >= bm->height) ydir = -1;
00188         if (y <= -50)        ydir = +1;
00189 
00190         /* Large window animation */
00191         bm = large_win.bitmap;
00192         gfx_bitmapClear(bm);
00193         for (i = 0; i < bm->height / 2; i += 2)
00194             gfx_rectDraw(bm, 0 + i, 0 + i, bm->width - i, bm->height - i);
00195 
00196 
00197         /* Small window animation */
00198         bm = small_win.bitmap;
00199         gfx_bitmapClear(bm);
00200         gfx_rectDraw(bm, 0, 0, bm->width, bm->height);
00201         gfx_line(bm, 0, 0, bm->width, bm->height);
00202         gfx_line(bm, bm->width, 0, 0, bm->height);
00203 
00204         /* Move windows around */
00205         win_move(&large_win, large_win.geom.xmin + xdir_large, large_top);
00206         if (large_win.geom.xmin < -20) xdir_large = +1;
00207         if (large_win.geom.xmin > RECT_WIDTH(&root_win.geom) - 5) xdir_large = -1;
00208 
00209         win_move(&small_win, small_left, small_win.geom.ymin + ydir_small);
00210         if (small_win.geom.ymin < -20) ydir_small = +1;
00211         if (small_win.geom.ymin > RECT_HEIGHT(&root_win.geom) - 5) ydir_small = -1;
00212 
00213         ++raise_counter;
00214         if (raise_counter % 997 == 0)
00215             win_raise(&small_win);
00216         else if (raise_counter % 731 == 0)
00217             win_raise(&large_win);
00218 
00219         win_compose(&root_win);
00220 
00221         /* Also does LCD refresh, etc. */
00222         if (kbd_peek())
00223             break;
00224     }
00225 }
00226 
00227 void proc_demo(void)
00228 {
00229     proc_testRun();
00230 }
00231 
00232 void timer_demo(void)
00233 {
00234     timer_testRun();
00235     timer_testTearDown();
00236 }
00237 
00238 
00239 /* SETTINGS SUBMENU */
00240 
00241 static struct MenuItem settings_items[] =
00242 {
00243     { (const_iptr_t)"System",     0, (MenuHook)0,  (iptr_t)0 },
00244     { (const_iptr_t)"Language",   0, (MenuHook)0,  (iptr_t)0 },
00245     { (const_iptr_t)"Networking", 0, (MenuHook)0,  (iptr_t)0 },
00246     { (const_iptr_t)"Date & Time",0, (MenuHook)0,  (iptr_t)0 },
00247     { (const_iptr_t)"Power Saving", MIF_TOGGLE, (MenuHook)0, (iptr_t)0 },
00248     { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
00249 };
00250 static struct Menu settings_menu = { settings_items, "Settings Menu", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0 };
00251 
00252 
00253 /* MX SUBMENU */
00254 
00255 static struct MenuItem mx_items[] =
00256 {
00257     { (const_iptr_t)"Mouse",                  MIF_CHECKIT | MIF_EXCLUDE_1 | MIF_EXCLUDE_2, (MenuHook)0,  (iptr_t)0 },
00258     { (const_iptr_t)"Keyboard", MIF_CHECKED | MIF_CHECKIT | MIF_EXCLUDE_0 | MIF_EXCLUDE_2, (MenuHook)0,  (iptr_t)0 },
00259     { (const_iptr_t)"Joystick", MIF_CHECKIT | MIF_EXCLUDE_0 | MIF_EXCLUDE_1, (MenuHook)0,  (iptr_t)0 },
00260     { (const_iptr_t)"Autosave", MIF_CHECKED | MIF_CHECKIT | MIF_TOGGLE, (MenuHook)0,  (iptr_t)0 },
00261     { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
00262 };
00263 
00264 static struct Menu mx_menu = { mx_items, (const_iptr_t)0, MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0 };
00265 
00266 
00267 /* DISPLAY SUBMENU */
00268 
00269 static struct MenuItem display_items[] =
00270 {
00271     { (const_iptr_t)"Background", 0, (MenuHook)0, (iptr_t)0 },
00272     { (const_iptr_t)"Colors",     0, (MenuHook)0, (iptr_t)0 },
00273     { (const_iptr_t)"Style",      0, (MenuHook)0, (iptr_t)0 },
00274     { (const_iptr_t)"Icon Theme", 0, (MenuHook)0, (iptr_t)0 },
00275     { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
00276 };
00277 static struct Menu display_menu = { display_items, "Display Menu", MF_SAVESEL, &lcd_bitmap, 0 };
00278 
00279 
00280 /* MAIN MENU */
00281 
00282 static struct MenuItem main_items[] =
00283 {
00284     { (const_iptr_t)"Win Fly",     0, (MenuHook)win_demo,     (iptr_t)&lcd_bitmap    },
00285     { (const_iptr_t)"Bounce!",     0, (MenuHook)bouncing_logo,(iptr_t)&lcd_bitmap    },
00286     { (const_iptr_t)"Hello World", 0, (MenuHook)hello_world,  (iptr_t)&lcd_bitmap    },
00287     { (const_iptr_t)"Scheduling",  0, (MenuHook)proc_demo,    (iptr_t)&lcd_bitmap    },
00288     { (const_iptr_t)"Timer Test",  0, (MenuHook)timer_demo,   (iptr_t)&lcd_bitmap    },
00289     { (const_iptr_t)"Menu MX",     0, (MenuHook)menu_handle,  (iptr_t)&mx_menu       },
00290     { (const_iptr_t)"Display",     0, (MenuHook)menu_handle,  (iptr_t)&display_menu  },
00291     { (const_iptr_t)"Settings",    0, (MenuHook)menu_handle,  (iptr_t)&settings_menu },
00292     { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
00293 };
00294 static struct Menu main_menu = { main_items, "Main Menu", MF_STICKY, &lcd_bitmap, 0 };
00295 
00296 
00297 static cpu_stack_t monitor_stack[CONFIG_KERN_MINSTACKSIZE / sizeof(cpu_stack_t)];
00298 
00299 int main(int argc, char *argv[])
00300 {
00301     emul_init(&argc, argv);
00302 
00303     #if CONFIG_KERN_PREEMPT
00304         irq_init();
00305     #endif
00306     timer_init();
00307     buz_init();
00308     kbd_init();
00309     lcd_init();
00310     proc_init();
00311     monitor_start(sizeof(monitor_stack), monitor_stack);
00312 
00313     menu_handle(&main_menu);
00314 
00315     timer_cleanup();
00316     emul_cleanup();
00317     return 0;
00318 }