Subversion Repositories shark

Rev

Details | 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: ball.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
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 JUMPING BALLS                     */
50
/*--------------------------------------------------------------*/
51
 
52
#include "demo.h"
53
#include <kernel/func.h>
54
#include <stdlib.h>
55
 
56
#define R       8               /* dimension of a ball          */
57
#define G       9.8             /* acceleration of gravity      */
58
 
59
static int ballexit = 0;
60
static int      npc = 0;        /* number of tasks created      */
61
 
62
/*--------------------------------------------------------------*/
63
/*      Periodic task for ball simulation                       */
64
/*--------------------------------------------------------------*/
65
 
66
TASK    palla(int i)
67
{
68
int     x, y;           /* coordinate grafiche pallina  */
69
int     ox, oy;         /* vecchia posizione pallina    */
70
int     x0, y0;         /* posizione iniziale X pallina */
71
float   vx, vy;         /* velocit… della pallina       */
72
float   vy0;            /* velocita' pallina al primo rimbalzo */
73
float   ty, tx;         /* variabile temporale          */
74
float   dt;             /* incremento temporale         */
75
 
76
    y = oy = y0 = BALL_HEIGHT;
77
    x = ox = x0 = BALL_XMIN;
78
 
79
    vy0= sqrt(2. * G * (float)BALL_HEIGHT);
80
    vy = 0;
81
    vx = BALL_VELX + myrand(9);
82
    tx = 0;
83
    ty = 0;
84
    dt = ((float)PERIOD_BALL)/100000;
85
 
86
    while (1) {
87
        y = y0 + vy*ty - .5*G*ty*ty;
88
        x = x0 + vx * tx;
89
 
90
        if (y < 0) {
91
                y = 0;
92
 
93
                if (vy == 0.0)
94
                  vy = vy0;
95
                else if (vy < BALL_VYMIN)
96
                  vy = vy0 * (1.0 - myrand(50)/100.0);
97
                else
98
                  vy = 0.9 * vy;
99
 
100
                ty = 0.0;
101
                y0 = 0;
102
        }
103
 
104
        if (x > BALL_XMAX) {
105
                tx = 0.0;
106
                x0 = BALL_XMAX;
107
                vx = -vx;
108
                x = x0 + vx * tx;
109
        }
110
 
111
        if (x < BALL_XMIN) {
112
                tx = 0.0;
113
                x0 = BALL_XMIN;
114
                vx = -vx;
115
                x = x0 + vx * tx;
116
        }
117
 
118
        mutex_lock(&mutex);
119
        grx_disc(ox, oy, R, 0);
120
        ox = x;
121
        oy = BALL_Y - y;
122
        mutex_unlock(&mutex);
123
 
124
        if (ballexit && i!=0xFFFF) {
125
          npc--;
126
          return 0;
127
        }
128
 
129
        mutex_lock(&mutex);
130
        grx_disc(ox, oy, R, i);
131
        mutex_unlock(&mutex);
132
 
133
        {
134
          int xxx;
135
          for (xxx=0; xxx<10000; xxx++);
136
        }
137
        ty += dt;
138
        tx += dt;
139
        task_endcycle();
140
    }
141
}
142
 
143
void killball(KEY_EVT *k)
144
{
145
  ballexit = 1;
146
}
147
 
148
void ballfun(KEY_EVT *k)
149
{
150
  SOFT_TASK_MODEL mp;
151
  int r,g,b;
152
  PID pid;
153
  char palla_str[]="palla  ";
154
 
155
  if (npc == BALL_MAX_P) return;
156
 
157
  ballexit = 0;
158
 
159
  r = 64 + myrand(190);
160
  g = 64 + myrand(190);
161
  b = 64 + myrand(190);
162
 
163
  itoa(npc,palla_str+5);
164
 
165
  soft_task_default_model(mp);
166
  soft_task_def_level(mp,1);
167
  soft_task_def_ctrl_jet(mp);
168
  soft_task_def_arg(mp, (void *)rgb16(r,g,b));
169
  soft_task_def_group(mp, BALL_GROUP);
170
  soft_task_def_met(mp, WCET_BALL);
171
  soft_task_def_period(mp,PERIOD_BALL);
172
  soft_task_def_usemath(mp);
173
  pid = task_create(palla_str, palla, &mp, NULL);
174
 
175
  if (pid != NIL) {
176
    task_activate(pid);
177
    npc++;
178
  }
179
}
180
 
181
void hardball()
182
{
183
  HARD_TASK_MODEL mp;
184
  int r,g,b;
185
  PID pid;
186
 
187
  r = 255;
188
  g = 255;
189
  b = 255;
190
 
191
  hard_task_default_model(mp);
192
  hard_task_def_ctrl_jet(mp);
193
  hard_task_def_arg(mp, (void *)rgb16(r,g,b));
194
  hard_task_def_wcet(mp, 380);
195
  hard_task_def_mit(mp,PERIOD_BALL);
196
  hard_task_def_usemath(mp);
197
  pid = task_create("pallaEDF", palla, &mp, NULL);
198
  if (pid == NIL) {
199
        grx_close();
200
        perror("Could not create task <pallaEDF>");
201
        sys_end();
202
  }
203
  else
204
    task_activate(pid);
205
}
206
 
207
 
208
/*--------------------------------------------------------------*/
209
/*                      MAIN process                            */
210
/*--------------------------------------------------------------*/
211
 
212
void scenario_ball()
213
{
214
  grx_text("Noise", 0, 45 /*BALL_Y-BALL_HEIGHT-15*/, rgb16(0,0,255), black);
215
  grx_line(0,55,383,55,red);
216
//grx_line(0,BALL_Y-BALL_HEIGHT-6,383,BALL_Y-BALL_HEIGHT-6,red);
217
 
218
  grx_rect(BALL_XMIN-R-1, BALL_Y-BALL_HEIGHT-R-1,
219
           BALL_XMAX+R+1, BALL_Y+R+1, rgb16(0,200,0));
220
}
221
 
222
void init_ball(void)
223
{
224
    KEY_EVT k;
225
 
226
    hardball();
227
 
228
    k.flag = 0;
229
    k.scan = KEY_SPC;
230
    k.ascii = ' ';
231
    keyb_hook(k,ballfun);
232
 
233
    k.flag = 0;
234
    k.scan = KEY_BKS;
235
    k.ascii = ' ';
236
    keyb_hook(k,killball);
237
}
238
 
239
/*--------------------------------------------------------------*/