Subversion Repositories shark

Rev

Rev 1123 | Rev 1382 | 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
 ------------
1377 giacomo 21
 CVS :        $Id: fly.c,v 1.4 2004-04-17 11:36:13 giacomo Exp $
1085 pj 22
 
23
 File:        $File$
1377 giacomo 24
 Revision:    $Revision: 1.4 $
25
 Last update: $Date: 2004-04-17 11:36:13 $
1085 pj 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 <stdlib.h>
54
#include <math.h>
55
 
1377 giacomo 56
#include <drivers/shark_linuxc26.h>
57
#include <drivers/shark_pci26.h>
58
#include <drivers/shark_fb26.h>
59
#include <drivers/shark_input26.h>
60
#include <drivers/shark_keyb26.h>
61
 
62
#define FRAME_BUFFER_DEVICE 0
63
 
1085 pj 64
#define YMENU    10             /* menu level                   */
65
#define XMIN     50
66
#define XMAX     600
67
#define YMIN     100
68
#define YMAX     450
69
#define VEL      5              /* linear velocity (def. = 5)   */
70
#define ANG      30             /* angolo massimo sterzata (30) */
71
#define D        3              /* raggio mosca                 */
1377 giacomo 72
#undef ESC
1085 pj 73
#define ESC      27             /* ASCII code of ESCAPE key     */
74
#define MAX_P    35             /* max number of flies          */
75
#define FLYGROUP 1
76
 
77
double  tick = 1.0;             /* system tick = 1 ms           */
78
int     fly_period = 40000;     /* task period                  */
79
int     fly_wcet = 1000;        /* task wcet                    */
80
PID     pid;
81
 
1377 giacomo 82
PID shutdown_task_PID = -1;
83
 
84
int device_drivers_init() {
85
 
86
        KEYB_PARMS kparms = BASE_KEYB;
87
 
88
        LINUXC26_register_module();
89
 
90
        PCI26_init();
91
 
92
        keyb_def_ctrlC(kparms, NULL);
93
 
94
        INPUT26_init();
95
 
96
        KEYB26_init(&kparms);
97
 
98
        FB26_init();
99
 
100
        FB26_open(FRAME_BUFFER_DEVICE);
101
 
102
        FB26_use_grx(FRAME_BUFFER_DEVICE);
103
 
104
        FB26_setmode(FRAME_BUFFER_DEVICE,"640x480-16");
105
 
106
        return 0;
107
 
108
}
109
 
110
int device_drivers_close() {
111
 
112
        FB26_close(FRAME_BUFFER_DEVICE);
113
 
114
        KEYB26_close();
115
 
116
        INPUT26_close();
117
 
118
        return 0;
119
 
120
}
121
 
122
TASK shutdown_task_body(void *arg) {
123
 
124
        device_drivers_close();
125
 
126
        sys_shutdown_message("-- S.Ha.R.K. Closed --\n");
127
 
128
        sys_end();
129
 
130
        return NULL;
131
 
132
}
133
 
134
void set_shutdown_task() {
135
 
136
        NRT_TASK_MODEL nrt;
137
 
138
        nrt_task_default_model(nrt);
139
 
140
        shutdown_task_PID = task_create("Shutdown Task",shutdown_task_body,&nrt,NULL);
141
        if (shutdown_task_PID == NIL) {
142
                sys_shutdown_message("Error: Cannot create shutdown task\n");
143
                sys_end();
144
        }
145
 
146
}
147
 
1085 pj 148
/*--------------------------------------------------------------*/
149
 
150
void    draw_fly(int x, int y, int c)
151
{
152
        grx_disc(x, y, D, c);
153
}
154
 
155
/******************************************************************/
156
 
157
TASK    fly(void *arg)
158
{
159
int     x, y;
160
int     ox, oy;
161
int     dx, dy, da;
162
int     teta, col;
163
int     outx, outy;
164
double  r;
165
int     i = (int)arg;
166
 
167
        x = ox = (XMIN+XMAX)/2;
168
        y = oy = (YMIN+YMAX)/2;
169
        teta = 0;
170
        col = 2 + i;                    /* colore fly           */
171
 
172
        while (1) {
173
 
174
                da = rand()%(2*ANG) - ANG;      /*  da = [-ANG,ANG] */
175
                teta += da;
176
 
177
                if (teta > 360) teta -= 360;
178
                if (teta < 0) teta += 360;
179
                r = (double)teta * PI / 180.;
180
 
181
                dx = (float)(VEL * cos(r));
182
                dy = (float)(VEL * sin(r));
183
                x += dx;
184
                y += dy;
185
 
186
                outx = (x >= XMAX) || (x <= XMIN);
187
                outy = (y >= YMAX) || (y <= YMIN);
188
 
189
                if (outx || outy) {
190
                        x = x - dx;
191
                        y = y - dy;
192
                        if (outx) teta = 180 - teta;
193
                        if (outy) teta = -teta;
194
                        if (teta > 360) teta -= 360;
195
                        if (teta < 0) teta += 360;
196
                        r = (double)teta * PI / 180.;
197
 
198
                        dx = (float)(VEL * cos(r));
199
                        dy = (float)(VEL * sin(r));
200
 
201
                        x += dx;
202
                        y += dy;
203
                }
204
 
205
                draw_fly(ox, oy, 0);
206
                draw_fly(x, y, col);
207
                ox = x; oy = y;
208
 
209
                task_endcycle();
210
        }
211
}
212
 
213
/****************************** MAIN ******************************/
214
 
215
int main(int argc, char **argv)
216
{
217
    HARD_TASK_MODEL m;
218
 
219
    char c;             /* character from keyboard      */
220
    int  i = 0;         /* number of tasks created      */
221
    TIME seme;          /* used to init the random seed */
222
 
1377 giacomo 223
    set_shutdown_task();
1085 pj 224
 
1377 giacomo 225
    device_drivers_init();
1085 pj 226
 
227
    /* The scenario */
228
    grx_rect(XMIN-D-1, YMIN-D-1, XMAX+D+1, YMAX+D+1, 14);
229
    grx_text("Simulation of Random Flies", XMIN, YMENU+10, 13, 0);
230
    grx_text("SPACE create a fly"        , XMIN, YMENU+20, 12, 0);
231
    grx_text("ESC   exit to DOS"         , XMIN, YMENU+30, 12, 0);
232
 
233
    /* The program waits a space to create a fly */
234
    c = keyb_getch(BLOCK);
235
 
236
    /* randomize!!!! */
237
    seme = sys_gettime(NULL);
238
    srand(seme);
239
 
240
    do {
241
        if ((c == ' ') && (i < MAX_P)) {
242
            hard_task_default_model(m);
243
            hard_task_def_ctrl_jet (m);
244
            hard_task_def_arg      (m, (void *)i);
245
            hard_task_def_wcet     (m, fly_wcet);
246
            hard_task_def_mit      (m, fly_period);
247
            hard_task_def_group    (m, FLYGROUP);
248
            hard_task_def_usemath  (m);
249
            pid = task_create("fly", fly, &m, NULL);
250
            if (pid == NIL) {
1377 giacomo 251
              sys_shutdown_message("Could not create task <fly>");
252
              task_activate(shutdown_task_PID);
253
              return 0;
1085 pj 254
            }
255
            task_activate(pid);
256
            i++;
257
        }
258
        c = keyb_getch(BLOCK);
259
 
260
    } while (c != ESC);
261
 
1377 giacomo 262
    task_activate(shutdown_task_PID);
1085 pj 263
 
264
    return 0;
1377 giacomo 265
 
1085 pj 266
}
267
 
268
/*--------------------------------------------------------------*/