Subversion Repositories shark

Rev

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