Subversion Repositories shark

Rev

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

Rev Author Line No. Line
1174 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
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
12
 *
13
 * http://www.sssup.it
14
 * http://retis.sssup.it
15
 * http://shark.sssup.it
16
 */
17
 
18
/*
19
 * Copyright (C) 2003 Giacomo Guidi
20
 *
21
 * This program is free software; you can redistribute it and/or modify
22
 * it under the terms of the GNU General Public License as published by
23
 * the Free Software Foundation; either version 2 of the License, or
24
 * (at your option) any later version.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 * GNU General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU General Public License
32
 * along with this program; if not, write to the Free Software
33
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
34
 */
35
 
36
#include "kernel/kern.h"
1424 giacomo 37
#include "stdlib.h"
1432 giacomo 38
#include "unistd.h"
1174 giacomo 39
 
1423 giacomo 40
#include "drivers/shark_keyb26.h"
41
#include "drivers/shark_videodev26.h"
1424 giacomo 42
#include "drivers/shark_fb26.h"
1423 giacomo 43
 
1474 giacomo 44
#include "tracer.h"
45
 
1174 giacomo 46
#define WIDTH 640
47
#define HEIGHT 480
48
#define BYTES_PP 2
49
 
1443 giacomo 50
//#define COLOR
1480 giacomo 51
#define MAX_SPEED
1443 giacomo 52
 
1444 giacomo 53
#define FRAME_GRABBER_NUMBER 0
54
 
1480 giacomo 55
#define FG_PERIOD 40000 //ONLY IF MAX_SPEED IS DISABLED
1174 giacomo 56
#define FG_WCET 30000
57
#define FG_W 320                
1474 giacomo 58
#define FG_H 240
1174 giacomo 59
 
1487 giacomo 60
unsigned int frame_number = 0;
61
 
1423 giacomo 62
void program_end(void *arg)
63
{
1174 giacomo 64
 
1423 giacomo 65
  sys_end();
66
 
67
}
1174 giacomo 68
 
1424 giacomo 69
void elaborate_image(void *imageptr);
70
 
1470 giacomo 71
void start_frame_grabber(PID elaborate_task_PID, int channel, struct video_buffer *fbuf) {
1174 giacomo 72
 
1423 giacomo 73
  struct video_window win;
74
  struct video_channel chan;
1443 giacomo 75
  struct video_tuner tuner;
1470 giacomo 76
  struct video_picture vpic;
1474 giacomo 77
  int res;
1174 giacomo 78
 
1432 giacomo 79
  /* Init videodev driver */
1444 giacomo 80
  VIDEODEV26_open(FRAME_GRABBER_NUMBER);
1432 giacomo 81
 
1444 giacomo 82
  /* Select the input channel */
1474 giacomo 83
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCGCHAN,(unsigned long)&chan);
84
  //cprintf("res = %d\n",res);
1443 giacomo 85
 
1470 giacomo 86
  chan.channel = channel;
1443 giacomo 87
  chan.type = VIDEO_VC_TUNER;
1432 giacomo 88
  chan.norm = VIDEO_TYPE_CAMERA;
1423 giacomo 89
 
1474 giacomo 90
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSCHAN,(unsigned long)&chan);
91
  //cprintf("res = %d\n",res);
1432 giacomo 92
 
1444 giacomo 93
  /* Enable the tuner */
1443 giacomo 94
 
1474 giacomo 95
  tuner.tuner = 0;
1443 giacomo 96
  tuner.mode = VIDEO_MODE_PAL;
97
 
1474 giacomo 98
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSTUNER,(unsigned long)&tuner);
99
  //cprintf("res = %d\n",res);
1443 giacomo 100
 
1427 giacomo 101
  /* Select palette and depth */
1474 giacomo 102
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCGPICT,(unsigned long)&vpic);
103
  //cprintf("res = %d\n",res);  
104
 
1443 giacomo 105
  #ifdef COLOR
106
        vpic.palette = VIDEO_PALETTE_RGB24;
107
        vpic.depth = 24;
108
  #else
109
        vpic.palette = VIDEO_PALETTE_GREY;
110
        vpic.depth = 8;
111
  #endif
112
 
113
  vpic.brightness = 35000;
114
  vpic.hue = 32000;
115
  vpic.contrast = 32000;
116
  vpic.colour = 32000;                          
1424 giacomo 117
 
1474 giacomo 118
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSPICT,(unsigned long)&vpic);
119
  //cprintf("res = %d\n",res);
1424 giacomo 120
 
1474 giacomo 121
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCGWIN,(unsigned long)&win);
122
  //cprintf("res = %d\n",res);
123
 
1423 giacomo 124
  win.x = 0;
125
  win.y = 0;
1432 giacomo 126
  win.width = FG_W;
127
  win.height = FG_H;
1424 giacomo 128
 
1474 giacomo 129
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSWIN,(unsigned long)&win);
130
  //cprintf("res = %d\n",res);
131
 
1471 giacomo 132
  /* Set the buffer */
1474 giacomo 133
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSFBUF,(unsigned long)(fbuf));
134
  //cprintf("res = %d\n",res);
1471 giacomo 135
 
1470 giacomo 136
  /* IMPORTANT: Set the aperiodic elaboration task
137
   * This is a SHARK change on VIDIOCSYNC. When the
138
   * new frame is ready, the task elaborate_task_PID
139
   * is activated. Elabortate_task must be aperiodic !!
140
   * To link the task to BTTV use this function: */
141
 
142
  VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSYNC,(unsigned long)(elaborate_task_PID));
143
 
144
}
145
 
1471 giacomo 146
/* Check if the frame grabber is ready */
1473 giacomo 147
volatile int ready_to_grab = 1;
1470 giacomo 148
 
1485 giacomo 149
void grab_command(void *arg) {
150
 
151
  SYS_FLAGS f;
152
 
153
  f = kern_fsave();
154
 
155
  int on = 1;
156
  VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCCAPTURE,(unsigned long)&on);
157
 
158
  kern_frestore(f);
159
 
160
}
161
 
1470 giacomo 162
/* Elaboration task, it is called when the frame
163
   grabber buffer is ready */
164
TASK elaborate_task(void *arg) {
165
 
166
  struct video_buffer *fbuf = (struct video_buffer *)(arg);
1487 giacomo 167
  char txt[100];
1470 giacomo 168
 
1423 giacomo 169
  while(1) {
1443 giacomo 170
 
1470 giacomo 171
    elaborate_image(fbuf->base);
1472 giacomo 172
    //cprintf("E");
173
 
174
    //Text version
175
    //printf_xy(1,20,WHITE,"%08x",
1487 giacomo 176
    //    *(unsigned int *)(fbuf->base+50*320*3+50*3));
1443 giacomo 177
 
1487 giacomo 178
    frame_number++;
179
    sprintf(txt,"Elapsed Time = %u us Frame = %u",(int)(sys_gettime(NULL)),frame_number);
180
    grx_text(txt, 10, 250, rgb16(255,255,255), 0);    
181
 
1471 giacomo 182
    ready_to_grab = 1;
183
 
1480 giacomo 184
    #ifdef MAX_SPEED
1485 giacomo 185
      grab_command(NULL);
1480 giacomo 186
    #endif
187
 
1432 giacomo 188
    task_testcancel();
1424 giacomo 189
    task_endcycle();
190
 
1423 giacomo 191
  }
192
 
1424 giacomo 193
  return NULL;
194
 
1174 giacomo 195
}
196
 
1470 giacomo 197
/* Send the grab command */
198
TASK grab_task(void *arg) {
199
 
200
  while(1) {
1471 giacomo 201
 
202
    if (ready_to_grab) {
1473 giacomo 203
 
1471 giacomo 204
      /* Grab */
1485 giacomo 205
      grab_command(NULL);
1470 giacomo 206
 
1472 giacomo 207
      //cprintf("G");
208
 
1471 giacomo 209
      ready_to_grab = 0;
210
 
211
    } else {
212
 
1472 giacomo 213
      //cprintf("S");
1471 giacomo 214
      /* Frame skipped */
215
 
216
    }
217
 
1470 giacomo 218
    task_testcancel();
219
    task_endcycle();
220
 
221
  }
222
 
223
  return NULL;
224
 
225
}
226
 
1424 giacomo 227
extern void *video_memory;
228
 
1174 giacomo 229
void elaborate_image(void *imageptr)
230
{
1424 giacomo 231
 
1174 giacomo 232
  WORD x,y;
233
  BYTE *col;
234
 
1443 giacomo 235
  #ifdef COLOR
236
 
1174 giacomo 237
    for(y = 0; y < FG_H; y++)
238
      for(x = 0; x < FG_W; x++) {
239
 
1443 giacomo 240
        col = (BYTE *)(imageptr + y * FG_W * 3 + x * 3);
241
        *(WORD *)(video_memory + y*(WIDTH*2) + (x*2)) = (WORD)rgb16(*(BYTE *)(col+2),*(BYTE *)(col+1),*(BYTE *)(col+0));
1174 giacomo 242
 
243
    }
244
 
1443 giacomo 245
  #else
246
 
247
    for(y = 0; y < FG_H; y++)
248
      for(x = 0; x < FG_W; x++) {
249
 
250
        col = (BYTE *)(imageptr + y * FG_W + x);
251
        *(WORD *)(video_memory + y*(WIDTH*2) + (x*2)) = (WORD)rgb16(*(BYTE *)(col),*(BYTE *)(col),*(BYTE *)(col));
252
 
253
    }
254
 
255
  #endif
256
 
1174 giacomo 257
}
258
 
259
int main(int argc, char **argv)
260
{
261
 
1470 giacomo 262
  HARD_TASK_MODEL gt;
1485 giacomo 263
  SOFT_TASK_MODEL et;
1470 giacomo 264
  PID grab_task_pid,elaborate_task_pid;
1174 giacomo 265
 
1470 giacomo 266
  struct video_buffer fbuf;
267
  int channel = 0;
268
 
1444 giacomo 269
  if (argc < 2) {
1445 giacomo 270
        sys_shutdown_message("ERROR: Enter the input channel [ex> %s 0]\n",argv[0]);
1444 giacomo 271
        sys_end();
272
  }
1432 giacomo 273
 
1470 giacomo 274
  channel = atoi(argv[1]);
275
 
1485 giacomo 276
  soft_task_default_model(et);
1487 giacomo 277
  soft_task_def_period(et,FG_PERIOD);
1485 giacomo 278
  soft_task_def_arg(et,(void *)(&fbuf));
279
  soft_task_def_met(et,FG_WCET);
280
  soft_task_def_aperiodic(et);
281
  soft_task_def_ctrl_jet(et);
1470 giacomo 282
 
283
  hard_task_default_model(gt);
1480 giacomo 284
  hard_task_def_mit(gt,FG_PERIOD);
1473 giacomo 285
  hard_task_def_wcet(gt,5000);
1470 giacomo 286
  hard_task_def_ctrl_jet(gt);
287
 
288
  grab_task_pid = task_create("GrabTask",grab_task,&gt,NULL);
1423 giacomo 289
  if (grab_task_pid == NIL) {
1444 giacomo 290
        sys_shutdown_message("ERROR: Cannot create grab task\n");
1423 giacomo 291
        sys_end();
292
  }
1174 giacomo 293
 
1470 giacomo 294
  elaborate_task_pid = task_create("ElaborateTask",elaborate_task,&et,NULL);
295
  if (grab_task_pid == NIL) {
296
        sys_shutdown_message("ERROR: Cannot create elaborate task\n");
297
        sys_end();
298
  }
299
 
300
  #ifdef COLOR
301
 
302
        fbuf.base = malloc(FG_W*FG_H*3);
303
        fbuf.height = FG_H;
304
        fbuf.width = FG_W;
305
        fbuf.bytesperline = FG_W*3;
306
        fbuf.depth = 24;
307
 
308
  #else
309
 
310
        fbuf.base = malloc(FG_W*FG_H);
311
        fbuf.height = FG_H;
312
        fbuf.width = FG_W;
313
        fbuf.bytesperline = FG_W;
314
        fbuf.depth = 8;
315
 
316
  #endif
317
 
318
  start_frame_grabber(elaborate_task_pid,channel,&fbuf);
319
 
1480 giacomo 320
  #ifndef MAX_SPEED
321
    /* Activate periodic grabbing */
322
    task_activate(grab_task_pid);
323
  #else
324
    /* Grab */
1485 giacomo 325
    grab_command(NULL);
1480 giacomo 326
  #endif
1174 giacomo 327
 
1189 giacomo 328
  while(keyb_getch(BLOCK) != ESC);
329
 
1480 giacomo 330
  #ifndef MAX_SPEED
331
    task_kill(grab_task_pid);
332
  #endif
1432 giacomo 333
 
334
  sleep(1);
335
 
1423 giacomo 336
  sys_end();
1189 giacomo 337
 
1174 giacomo 338
  return 0;
339
 
340
}