Subversion Repositories shark

Rev

Rev 1085 | Rev 1098 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1085 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
21
 CVS :        $Id: fly.c,v 1.1.1.1 2002-09-02 09:37:41 pj Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1.1.1 $
25
 Last update: $Date: 2002-09-02 09:37:41 $
26
 ------------
27
**/
28
 
29
/*
30
 * Copyright (C) 2000 Paolo Gai and Giorgio Buttazzo
31
 *
32
 * This program is free software; you can redistribute it and/or modify
33
 * it under the terms of the GNU General Public License as published by
34
 * the Free Software Foundation; either version 2 of the License, or
35
 * (at your option) any later version.
36
 *
37
 * This program is distributed in the hope that it will be useful,
38
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40
 * GNU General Public License for more details.
41
 *
42
 * You should have received a copy of the GNU General Public License
43
 * along with this program; if not, write to the Free Software
44
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
45
 *
46
 */
47
 
48
/*--------------------------------------------------------------*/
49
/*              SIMULATION OF RANDOM FLIES                      */
50
/*--------------------------------------------------------------*/
51
 
52
#include <kernel/kern.h>
53
#include <drivers/glib.h>
54
#include <drivers/keyb.h>
55
#include <semaphore.h>
56
#include <stdlib.h>
57
#include <math.h>
58
 
59
#define YMENU    10             /* menu level                   */
60
#define XMIN     50
61
#define XMAX     600
62
#define YMIN     100
63
#define YMAX     450
64
#define VEL      5              /* linear velocity (def. = 5)   */
65
#define ANG      30             /* angolo massimo sterzata (30) */
66
#define D        3              /* raggio mosca                 */
67
#define ESC      27             /* ASCII code of ESCAPE key     */
68
#define MAX_P    35             /* max number of flies          */
69
#define FLYGROUP 1
70
 
71
double  tick = 1.0;             /* system tick = 1 ms           */
72
int     fly_period = 40000;     /* task period                  */
73
int     fly_wcet = 1000;        /* task wcet                    */
74
PID     pid;
75
sem_t   mutex;
76
 
77
/*--------------------------------------------------------------*/
78
 
79
void    draw_fly(int x, int y, int c)
80
{
81
        sem_wait(&mutex);
82
        grx_disc(x, y, D, c);
83
        sem_post(&mutex);
84
}
85
 
86
/******************************************************************/
87
 
88
TASK    fly(void *arg)
89
{
90
int     x, y;
91
int     ox, oy;
92
int     dx, dy, da;
93
int     teta, col;
94
int     outx, outy;
95
double  r;
96
int     i = (int)arg;
97
 
98
        x = ox = (XMIN+XMAX)/2;
99
        y = oy = (YMIN+YMAX)/2;
100
        teta = 0;
101
        col = 2 + i;                    /* colore fly           */
102
 
103
        while (1) {
104
 
105
                da = rand()%(2*ANG) - ANG;      /*  da = [-ANG,ANG] */
106
                teta += da;
107
 
108
                if (teta > 360) teta -= 360;
109
                if (teta < 0) teta += 360;
110
                r = (double)teta * PI / 180.;
111
 
112
                dx = (float)(VEL * cos(r));
113
                dy = (float)(VEL * sin(r));
114
                x += dx;
115
                y += dy;
116
 
117
                outx = (x >= XMAX) || (x <= XMIN);
118
                outy = (y >= YMAX) || (y <= YMIN);
119
 
120
                if (outx || outy) {
121
                        x = x - dx;
122
                        y = y - dy;
123
                        if (outx) teta = 180 - teta;
124
                        if (outy) teta = -teta;
125
                        if (teta > 360) teta -= 360;
126
                        if (teta < 0) teta += 360;
127
                        r = (double)teta * PI / 180.;
128
 
129
                        dx = (float)(VEL * cos(r));
130
                        dy = (float)(VEL * sin(r));
131
 
132
                        x += dx;
133
                        y += dy;
134
                }
135
 
136
                draw_fly(ox, oy, 0);
137
                draw_fly(x, y, col);
138
                ox = x; oy = y;
139
 
140
                task_endcycle();
141
        }
142
}
143
 
144
/****************************************************************/
145
 
146
/* This function is called when the system exits */
147
void byebye(void *arg)
148
{
149
  grx_close();
150
  kern_printf("Bye Bye!\n");
151
}
152
 
153
/****************************** MAIN ******************************/
154
 
155
int main(int argc, char **argv)
156
{
157
    HARD_TASK_MODEL m;
158
 
159
    char c;             /* character from keyboard      */
160
    int  i = 0;         /* number of tasks created      */
161
    TIME seme;          /* used to init the random seed */
162
 
163
    /* Set the exception handler */
164
    set_exchandler_grx();
165
 
166
    /* Set the closing function */
167
    sys_atrunlevel(byebye, NULL, RUNLEVEL_BEFORE_EXIT);
168
 
169
    /* graphic card Initialization */
170
    if (grx_init() < 1) {
171
       sys_abort(1);
172
    }
173
 
174
    if (grx_open(640, 480, 8) < 0) {
175
        kern_printf("GRX Err\n");
176
        sys_abort(1);
177
    }
178
    kern_printf("Video card ok!\n");
179
 
180
    /* The scenario */
181
    grx_rect(XMIN-D-1, YMIN-D-1, XMAX+D+1, YMAX+D+1, 14);
182
    grx_text("Simulation of Random Flies", XMIN, YMENU+10, 13, 0);
183
    grx_text("SPACE create a fly"        , XMIN, YMENU+20, 12, 0);
184
    grx_text("ESC   exit to DOS"         , XMIN, YMENU+30, 12, 0);
185
 
186
    /* The program waits a space to create a fly */
187
    c = keyb_getch(BLOCK);
188
 
189
    /* randomize!!!! */
190
    seme = sys_gettime(NULL);
191
    srand(seme);
192
 
193
    do {
194
        if ((c == ' ') && (i < MAX_P)) {
195
            hard_task_default_model(m);
196
            hard_task_def_ctrl_jet (m);
197
            hard_task_def_arg      (m, (void *)i);
198
            hard_task_def_wcet     (m, fly_wcet);
199
            hard_task_def_mit      (m, fly_period);
200
            hard_task_def_group    (m, FLYGROUP);
201
            hard_task_def_usemath  (m);
202
            pid = task_create("fly", fly, &m, NULL);
203
            if (pid == NIL) {
204
              grx_close();
205
              perror("Could not create task <fly>");
206
              sys_abort(1);
207
            }
208
            task_activate(pid);
209
            i++;
210
        }
211
        c = keyb_getch(BLOCK);
212
 
213
    } while (c != ESC);
214
 
215
    sys_end();
216
 
217
    return 0;
218
}
219
 
220
/*--------------------------------------------------------------*/