Subversion Repositories shark

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/**
 ------------
 CVS :        $Id: fly.c,v 1.1.1.1 2002-09-02 09:37:41 pj Exp $

 File:        $File$
 Revision:    $Revision: 1.1.1.1 $
 Last update: $Date: 2002-09-02 09:37:41 $
 ------------
**/


/*
 * Copyright (C) 2000 Paolo Gai and Giorgio Buttazzo
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


/*--------------------------------------------------------------*/
/*              SIMULATION OF RANDOM FLIES                      */
/*--------------------------------------------------------------*/

#include <kernel/kern.h>
#include <drivers/glib.h>
#include <drivers/keyb.h>
#include <semaphore.h>
#include <stdlib.h>
#include <math.h>

#define YMENU    10             /* menu level                   */
#define XMIN     50
#define XMAX     600
#define YMIN     100
#define YMAX     450
#define VEL      5              /* linear velocity (def. = 5)   */
#define ANG      30             /* angolo massimo sterzata (30) */
#define D        3              /* raggio mosca                 */
#define ESC      27             /* ASCII code of ESCAPE key     */
#define MAX_P    35             /* max number of flies          */
#define FLYGROUP 1

double  tick = 1.0;             /* system tick = 1 ms           */
int     fly_period = 40000;     /* task period                  */
int     fly_wcet = 1000;        /* task wcet                    */
PID     pid;
sem_t   mutex;

/*--------------------------------------------------------------*/

void    draw_fly(int x, int y, int c)
{
        sem_wait(&mutex);
        grx_disc(x, y, D, c);
        sem_post(&mutex);
}

/******************************************************************/

TASK    fly(void *arg)
{
int     x, y;
int     ox, oy;
int     dx, dy, da;
int     teta, col;
int     outx, outy;
double  r;
int     i = (int)arg;

        x = ox = (XMIN+XMAX)/2;
        y = oy = (YMIN+YMAX)/2;
        teta = 0;
        col = 2 + i;                    /* colore fly           */

        while (1) {

                da = rand()%(2*ANG) - ANG;      /*  da = [-ANG,ANG] */
                teta += da;

                if (teta > 360) teta -= 360;
                if (teta < 0) teta += 360;
                r = (double)teta * PI / 180.;

                dx = (float)(VEL * cos(r));
                dy = (float)(VEL * sin(r));
                x += dx;
                y += dy;

                outx = (x >= XMAX) || (x <= XMIN);
                outy = (y >= YMAX) || (y <= YMIN);

                if (outx || outy) {
                        x = x - dx;
                        y = y - dy;
                        if (outx) teta = 180 - teta;
                        if (outy) teta = -teta;
                        if (teta > 360) teta -= 360;
                        if (teta < 0) teta += 360;
                        r = (double)teta * PI / 180.;

                        dx = (float)(VEL * cos(r));
                        dy = (float)(VEL * sin(r));

                        x += dx;
                        y += dy;
                }

                draw_fly(ox, oy, 0);
                draw_fly(x, y, col);
                ox = x; oy = y;

                task_endcycle();
        }
}

/****************************************************************/

/* This function is called when the system exits */
void byebye(void *arg)
{
  grx_close();
  kern_printf("Bye Bye!\n");
}

/****************************** MAIN ******************************/

int main(int argc, char **argv)
{
    HARD_TASK_MODEL m;

    char c;             /* character from keyboard      */
    int  i = 0;         /* number of tasks created      */
    TIME seme;          /* used to init the random seed */

    /* Set the exception handler */
    set_exchandler_grx();

    /* Set the closing function */
    sys_atrunlevel(byebye, NULL, RUNLEVEL_BEFORE_EXIT);

    /* graphic card Initialization */
    if (grx_init() < 1) {
       sys_abort(1);
    }
   
    if (grx_open(640, 480, 8) < 0) {
        kern_printf("GRX Err\n");
        sys_abort(1);
    }
    kern_printf("Video card ok!\n");

    /* The scenario */
    grx_rect(XMIN-D-1, YMIN-D-1, XMAX+D+1, YMAX+D+1, 14);
    grx_text("Simulation of Random Flies", XMIN, YMENU+10, 13, 0);
    grx_text("SPACE create a fly"        , XMIN, YMENU+20, 12, 0);
    grx_text("ESC   exit to DOS"         , XMIN, YMENU+30, 12, 0);

    /* The program waits a space to create a fly */
    c = keyb_getch(BLOCK);

    /* randomize!!!! */
    seme = sys_gettime(NULL);
    srand(seme);

    do {
        if ((c == ' ') && (i < MAX_P)) {
            hard_task_default_model(m);
            hard_task_def_ctrl_jet (m);
            hard_task_def_arg      (m, (void *)i);
            hard_task_def_wcet     (m, fly_wcet);
            hard_task_def_mit      (m, fly_period);
            hard_task_def_group    (m, FLYGROUP);
            hard_task_def_usemath  (m);
            pid = task_create("fly", fly, &m, NULL);
            if (pid == NIL) {
              grx_close();
              perror("Could not create task <fly>");
              sys_abort(1);
            }
            task_activate(pid);
            i++;
        }
        c = keyb_getch(BLOCK);

    } while (c != ESC);

    sys_end();

    return 0;
}

/*--------------------------------------------------------------*/