Rev 1160 | Rev 1380 | 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 | ------------ |
||
1379 | giacomo | 21 | CVS : $Id: ball.c,v 1.4 2004-04-17 17:16:46 giacomo Exp $ |
1085 | pj | 22 | |
23 | File: $File$ |
||
1379 | giacomo | 24 | Revision: $Revision: 1.4 $ |
25 | Last update: $Date: 2004-04-17 17:16:46 $ |
||
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 | |||
1379 | giacomo | 128 | task_nopreempt(); |
1085 | pj | 129 | grx_disc(ox, oy, R, 0); |
1379 | giacomo | 130 | task_preempt(); |
131 | |||
1160 | pj | 132 | ox = x; |
133 | oy = BALL_Y - y; |
||
1085 | pj | 134 | |
1160 | pj | 135 | if (ballexit && i!=0xFFFF) { |
136 | npc--; |
||
137 | return 0; |
||
138 | } |
||
1085 | pj | 139 | |
1379 | giacomo | 140 | task_nopreempt(); |
1085 | pj | 141 | grx_disc(ox, oy, R, i); |
1379 | giacomo | 142 | task_preempt(); |
1085 | pj | 143 | |
1160 | pj | 144 | my_delay(); |
145 | |||
1085 | pj | 146 | ty += dt; |
147 | tx += dt; |
||
148 | task_endcycle(); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | void killball(KEY_EVT *k) |
||
153 | { |
||
154 | ballexit = 1; |
||
155 | } |
||
156 | |||
157 | void ballfun(KEY_EVT *k) |
||
158 | { |
||
159 | SOFT_TASK_MODEL mp; |
||
160 | int r,g,b; |
||
161 | PID pid; |
||
162 | char palla_str[]="palla "; |
||
163 | |||
164 | if (npc == BALL_MAX_P) return; |
||
165 | |||
166 | ballexit = 0; |
||
167 | |||
168 | r = 64 + myrand(190); |
||
169 | g = 64 + myrand(190); |
||
170 | b = 64 + myrand(190); |
||
171 | |||
172 | itoa(npc,palla_str+5); |
||
173 | |||
174 | soft_task_default_model(mp); |
||
1379 | giacomo | 175 | soft_task_def_level(mp,2); |
1085 | pj | 176 | soft_task_def_ctrl_jet(mp); |
177 | soft_task_def_arg(mp, (void *)rgb16(r,g,b)); |
||
178 | soft_task_def_group(mp, BALL_GROUP); |
||
179 | soft_task_def_met(mp, WCET_BALL); |
||
180 | soft_task_def_period(mp,PERIOD_BALL); |
||
181 | soft_task_def_usemath(mp); |
||
182 | pid = task_create(palla_str, palla, &mp, NULL); |
||
183 | |||
184 | if (pid != NIL) { |
||
185 | task_activate(pid); |
||
186 | npc++; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | void hardball() |
||
191 | { |
||
192 | HARD_TASK_MODEL mp; |
||
193 | int r,g,b; |
||
194 | PID pid; |
||
195 | |||
196 | r = 255; |
||
197 | g = 255; |
||
198 | b = 255; |
||
199 | |||
200 | hard_task_default_model(mp); |
||
201 | hard_task_def_ctrl_jet(mp); |
||
202 | hard_task_def_arg(mp, (void *)rgb16(r,g,b)); |
||
1158 | pj | 203 | hard_task_def_wcet(mp, WCET_HARD_BALL); |
1085 | pj | 204 | hard_task_def_mit(mp,PERIOD_BALL); |
205 | hard_task_def_usemath(mp); |
||
206 | pid = task_create("pallaEDF", palla, &mp, NULL); |
||
207 | if (pid == NIL) { |
||
1158 | pj | 208 | sys_shutdown_message("Could not create task <pallaEDF>"); |
1379 | giacomo | 209 | task_activate(shutdown_task_PID); |
210 | return; |
||
1085 | pj | 211 | } |
212 | else |
||
213 | task_activate(pid); |
||
214 | } |
||
215 | |||
216 | |||
217 | /*--------------------------------------------------------------*/ |
||
1160 | pj | 218 | /* MAIN process */ |
1085 | pj | 219 | /*--------------------------------------------------------------*/ |
220 | |||
221 | void scenario_ball() |
||
222 | { |
||
223 | grx_text("Noise", 0, 45 /*BALL_Y-BALL_HEIGHT-15*/, rgb16(0,0,255), black); |
||
224 | grx_line(0,55,383,55,red); |
||
225 | grx_rect(BALL_XMIN-R-1, BALL_Y-BALL_HEIGHT-R-1, |
||
1160 | pj | 226 | BALL_XMAX+R+1, BALL_Y+R+1, rgb16(0,200,0)); |
1085 | pj | 227 | } |
228 | |||
229 | void init_ball(void) |
||
230 | { |
||
231 | KEY_EVT k; |
||
232 | |||
233 | hardball(); |
||
234 | |||
235 | k.flag = 0; |
||
236 | k.scan = KEY_SPC; |
||
237 | k.ascii = ' '; |
||
1379 | giacomo | 238 | k.status = KEY_PRESSED; |
239 | keyb_hook(k,ballfun,FALSE); |
||
1085 | pj | 240 | |
241 | k.flag = 0; |
||
242 | k.scan = KEY_BKS; |
||
243 | k.ascii = ' '; |
||
1379 | giacomo | 244 | k.status = KEY_PRESSED; |
245 | keyb_hook(k,killball,FALSE); |
||
1085 | pj | 246 | } |
247 | |||
248 | /*--------------------------------------------------------------*/ |