Subversion Repositories shark

Rev

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

Rev Author Line No. Line
1127 giacomo 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
 *   Giacomo Guidi       <giacomo@gandalf.sssup.it>
10
 *
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
 
1314 giacomo 19
#include <drivers/glib.h>
1127 giacomo 20
#include <drivers/keyb.h>
21
 
22
#include <GL/osmesa.h>
23
#include <GL/glut.h>
24
 
25
#include <math.h>
26
#include <stdlib.h>
27
 
28
#include <kernel/log.h>
29
#include <kernel/kern.h>
30
 
31
#ifndef M_PI
32
#define M_PI 3.14159265
33
#endif
34
 
35
#define WIDTH 640
1321 giacomo 36
#define HEIGHT 430
1127 giacomo 37
#define BYTES_PP 2 //BytesPerPixel
38
 
39
OSMesaContext ctx;
40
 
41
unsigned char *rgb_565_buf = NULL; //RGB 16 bpp Buffer
42
unsigned char *video_buf = NULL; //Video Buffer
43
 
44
unsigned long int VMEMLONG = WIDTH * HEIGHT * BYTES_PP / 4; // Used by copy_videomem_16to16
45
unsigned long int RGB565MEM = WIDTH * HEIGHT * BYTES_PP; // Total video mem
46
 
1321 giacomo 47
unsigned long int PERIOD_REFRESH = 45000;
48
unsigned long int PERIOD_DISEGNA = 45000;
1127 giacomo 49
 
50
unsigned long int WCET_REFRESH, WCET_DISEGNA;
51
 
52
TASK refesh(void);
53
TASK disegna(void);
54
 
55
PID refresh_PID, disegna_PID;
56
 
57
static void
58
gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
59
  GLint teeth, GLfloat tooth_depth)
60
{
61
  GLint i;
62
  GLfloat r0, r1, r2;
63
  GLfloat angle, da;
64
  GLfloat u, v, len;
65
 
66
  r0 = inner_radius;
67
  r1 = outer_radius - tooth_depth / 2.0;
68
  r2 = outer_radius + tooth_depth / 2.0;
69
 
70
  da = 2.0 * M_PI / teeth / 4.0;
71
 
72
  glShadeModel(GL_FLAT);
73
 
74
  glNormal3f(0.0, 0.0, 1.0);
75
 
76
  glBegin(GL_QUAD_STRIP);
77
  for (i = 0; i <= teeth; i++) {
78
    angle = i * 2.0 * M_PI / teeth;
79
    glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
80
    glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
81
    if (i < teeth) {
82
      glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
83
      glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
84
    }
85
  }
86
  glEnd();
87
 
88
  glBegin(GL_QUADS);
89
  da = 2.0 * M_PI / teeth / 4.0;
90
  for (i = 0; i < teeth; i++) {
91
    angle = i * 2.0 * M_PI / teeth;
92
 
93
    glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
94
    glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
95
    glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
96
    glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
97
  }
98
  glEnd();
99
 
100
  glNormal3f(0.0, 0.0, -1.0);
101
 
102
  glBegin(GL_QUAD_STRIP);
103
  for (i = 0; i <= teeth; i++) {
104
    angle = i * 2.0 * M_PI / teeth;
105
    glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
106
    glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
107
    if (i < teeth) {
108
      glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
109
      glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
110
    }
111
  }
112
  glEnd();
113
 
114
  glBegin(GL_QUADS);
115
  da = 2.0 * M_PI / teeth / 4.0;
116
  for (i = 0; i < teeth; i++) {
117
    angle = i * 2.0 * M_PI / teeth;
118
 
119
    glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
120
    glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
121
    glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
122
    glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
123
  }
124
  glEnd();
125
 
126
  glBegin(GL_QUAD_STRIP);
127
  for (i = 0; i < teeth; i++) {
128
    angle = i * 2.0 * M_PI / teeth;
129
 
130
    glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
131
    glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
132
    u = r2 * cos(angle + da) - r1 * cos(angle);
133
    v = r2 * sin(angle + da) - r1 * sin(angle);
134
    len = sqrt(u * u + v * v);
135
    u /= len;
136
    v /= len;
137
    glNormal3f(v, -u, 0.0);
138
    glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
139
    glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
140
    glNormal3f(cos(angle), sin(angle), 0.0);
141
    glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
142
    glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
143
    u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
144
    v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
145
    glNormal3f(v, -u, 0.0);
146
    glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
147
    glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
148
    glNormal3f(cos(angle), sin(angle), 0.0);
149
  }
150
 
151
  glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
152
  glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
153
 
154
  glEnd();
155
 
156
  glShadeModel(GL_SMOOTH);
157
 
158
  glBegin(GL_QUAD_STRIP);
159
  for (i = 0; i <= teeth; i++) {
160
    angle = i * 2.0 * M_PI / teeth;
161
    glNormal3f(-cos(angle), -sin(angle), 0.0);
162
    glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
163
    glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
164
  }
165
  glEnd();
166
 
167
}
168
 
169
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
170
static GLint gear1, gear2, gear3;
171
static GLfloat angle = 0.0;
172
 
173
static void draw(void)
174
{
175
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
176
 
177
  glPushMatrix();
178
    glRotatef(view_rotx, 1.0, 0.0, 0.0);
179
    glRotatef(view_roty, 0.0, 1.0, 0.0);
180
    glRotatef(view_rotz, 0.0, 0.0, 1.0);
181
 
182
    glPushMatrix();
183
      glTranslatef(-3.0, -2.0, 0.0);
184
      glRotatef(angle, 0.0, 0.0, 1.0);
185
      glCallList(gear1);
186
    glPopMatrix();
187
 
188
    glPushMatrix();
189
      glTranslatef(3.1, -2.0, 0.0);
190
      glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
191
      glCallList(gear2);
192
    glPopMatrix();
193
 
194
    glPushMatrix();
195
      glTranslatef(-3.1, 4.2, 0.0);
196
      glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
197
      glCallList(gear3);
198
    glPopMatrix();
199
 
200
  glPopMatrix();
201
 
202
  glFinish();
203
 
204
}
205
 
206
static void gl_init()
207
{
208
 
209
  static GLfloat red[4] = {1.0, 0.0, 0.0, 1.0};
210
  static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
211
  static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
212
  static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0};
213
  static GLfloat h = (GLfloat) HEIGHT / (GLfloat) WIDTH;
214
 
215
  //Create the OSMesa Context
216
  ctx = OSMesaCreateContext(OSMESA_RGB_565, NULL);
217
 
218
  //Make Current Context
219
  OSMesaMakeCurrent(ctx, rgb_565_buf, GL_UNSIGNED_SHORT_5_6_5, WIDTH, HEIGHT);
220
 
221
  glLightfv(GL_LIGHT0, GL_POSITION, pos);
222
  glEnable(GL_CULL_FACE);
223
  glEnable(GL_LIGHTING);
224
  glEnable(GL_LIGHT0);
225
  glEnable(GL_DEPTH_TEST);
226
 
227
  /* make the gears */
228
  gear1 = glGenLists(1);
229
  glNewList(gear1, GL_COMPILE);
230
  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
231
  gear(1.0, 4.0, 1.0, 20, 0.7);
232
  glEndList();
233
 
234
  gear2 = glGenLists(1);
235
  glNewList(gear2, GL_COMPILE);
236
  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
237
  gear(0.5, 2.0, 2.0, 10, 0.7);
238
  glEndList();
239
 
240
  gear3 = glGenLists(1);
241
  glNewList(gear3, GL_COMPILE);
242
  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
243
  gear(1.3, 2.0, 0.5, 10, 0.7);
244
  glEndList();
245
 
246
  glEnable(GL_NORMALIZE);
247
 
248
  glViewport(0, 0, (GLint) WIDTH, (GLint) HEIGHT);
249
  glMatrixMode(GL_PROJECTION);
250
  glLoadIdentity();
251
  glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
252
  glMatrixMode(GL_MODELVIEW);
253
  glLoadIdentity();
254
  glTranslatef(0.0, 0.0, -40.0);
255
 
256
}
257
 
1314 giacomo 258
static int screen()
1127 giacomo 259
{
260
 
1314 giacomo 261
  extern DWORD flbaddr;
262
 
263
  /* graphic card Initialization */
264
  if (grx_init() < 1) {
265
     sys_abort(1);
1127 giacomo 266
  }
1314 giacomo 267
 
268
  if (grx_open(640, 480, 16) < 0) {
269
     cprintf("GRX Err\n");
270
     sys_abort(1);
1127 giacomo 271
  }
1314 giacomo 272
 
273
  video_buf = (unsigned char *)flbaddr;
1127 giacomo 274
 
275
  return 0;
276
 
277
}
278
 
279
void program_end(void *arg)
280
{
281
 
282
  OSMesaDestroyContext(ctx);
283
  free(rgb_565_buf);
284
 
1314 giacomo 285
  grx_close();
1127 giacomo 286
 
287
  sys_end();
288
 
289
}
290
 
291
void program_key_end(KEY_EVT *k)
292
{
293
 
294
  sys_end();
295
 
296
}
297
 
298
TASK refresh(void)
299
{
300
 
301
  while(1) {
302
 
1321 giacomo 303
    memcpy((video_buf+40*WIDTH*2), rgb_565_buf, RGB565MEM);
1127 giacomo 304
    task_endcycle();
305
 
306
  }
307
 
308
  sys_end();
309
 
310
}
311
 
312
 
313
TASK disegna(void)
314
{
315
 
316
  char text[100];
317
  TIME disegna_TIME, refresh_TIME;
318
 
319
  while(1) {
320
 
321
    jet_gettable(refresh_PID, &refresh_TIME, 1);
322
    jet_gettable(disegna_PID, &disegna_TIME, 1);
323
 
324
    angle += 1.0;
325
 
326
    draw();
327
 
328
    sprintf(text,"Hard Task Refresh PER:%6d us EX:%6d us",(int)PERIOD_REFRESH,(int)refresh_TIME);
1321 giacomo 329
    grx_text(text,10,5,color16(0,0,255),0);    
1127 giacomo 330
    sprintf(text,"Hard Task Draw    PER:%6d us EX:%6d us",(int)PERIOD_DISEGNA,(int)disegna_TIME);
1321 giacomo 331
    grx_text(text,10,15,color16(0,0,255),0);
1127 giacomo 332
 
333
    task_endcycle();
334
 
335
  }
336
 
337
  sys_end();
338
 
339
}
340
 
341
int main (int argc, char *argv[])
342
{
343
 
344
    HARD_TASK_MODEL ht_refresh, ht_disegna;
345
 
346
    sys_atrunlevel(program_end,NULL, RUNLEVEL_BEFORE_EXIT);
347
 
348
    clear();
349
 
1321 giacomo 350
    WCET_REFRESH =((long int) PERIOD_REFRESH * (0.35));
1155 giacomo 351
    WCET_DISEGNA =((long int) PERIOD_DISEGNA * (0.6));
1127 giacomo 352
 
353
    hard_task_default_model(ht_refresh);
354
    hard_task_def_wcet(ht_refresh,WCET_REFRESH);
355
    hard_task_def_mit(ht_refresh,PERIOD_REFRESH);
356
    hard_task_def_usemath(ht_refresh);
357
    hard_task_def_group(ht_refresh,1);
358
    hard_task_def_ctrl_jet(ht_refresh);
359
 
360
    refresh_PID = task_create("refresh", refresh, &ht_refresh, NULL);
361
    if (refresh_PID == -1) {
362
      sys_end();
363
      exit(4);
364
    }
365
 
366
    hard_task_default_model(ht_disegna);
367
    hard_task_def_mit(ht_disegna,PERIOD_DISEGNA);
368
    hard_task_def_wcet(ht_disegna,WCET_DISEGNA);
369
    hard_task_def_group(ht_disegna,1);
370
    hard_task_def_ctrl_jet(ht_disegna);
371
    hard_task_def_usemath(ht_disegna);
1156 giacomo 372
    hard_task_def_stack(ht_disegna,30000); //VERY IMPORTANT FOR glCallList !!
1127 giacomo 373
 
374
    disegna_PID = task_create("disegna", disegna, &ht_disegna, NULL);
375
    if (disegna_PID == -1) {
376
      sys_end();
377
      exit(4);
378
    }
379
 
380
    {
381
      KEY_EVT k;
382
      k.flag = ALTL_BIT;
383
      k.scan = KEY_C;
384
      k.ascii = 'c';
385
      keyb_hook(k,program_key_end);
386
    }
1155 giacomo 387
 
388
    rgb_565_buf = malloc(RGB565MEM);
389
 
390
    gl_init();
391
 
1314 giacomo 392
    if (screen()) {
1127 giacomo 393
        printk(KERN_INFO "Graphical initialization failed !!\n");
394
        sys_end();
395
    }
396
 
397
    memset(rgb_565_buf, 0, RGB565MEM);
398
 
399
    group_activate(1);
400
 
401
    return 0;
402
 
403
}