Subversion Repositories shark

Rev

Rev 1485 | Rev 1487 | 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
 
1423 giacomo 60
void program_end(void *arg)
61
{
1174 giacomo 62
 
1423 giacomo 63
  sys_end();
64
 
65
}
1174 giacomo 66
 
1424 giacomo 67
void elaborate_image(void *imageptr);
68
 
1470 giacomo 69
void start_frame_grabber(PID elaborate_task_PID, int channel, struct video_buffer *fbuf) {
1174 giacomo 70
 
1423 giacomo 71
  struct video_window win;
72
  struct video_channel chan;
1443 giacomo 73
  struct video_tuner tuner;
1470 giacomo 74
  struct video_picture vpic;
1474 giacomo 75
  int res;
1174 giacomo 76
 
1432 giacomo 77
  /* Init videodev driver */
1444 giacomo 78
  VIDEODEV26_open(FRAME_GRABBER_NUMBER);
1432 giacomo 79
 
1444 giacomo 80
  /* Select the input channel */
1474 giacomo 81
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCGCHAN,(unsigned long)&chan);
82
  //cprintf("res = %d\n",res);
1443 giacomo 83
 
1470 giacomo 84
  chan.channel = channel;
1443 giacomo 85
  chan.type = VIDEO_VC_TUNER;
1432 giacomo 86
  chan.norm = VIDEO_TYPE_CAMERA;
1423 giacomo 87
 
1474 giacomo 88
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSCHAN,(unsigned long)&chan);
89
  //cprintf("res = %d\n",res);
1432 giacomo 90
 
1444 giacomo 91
  /* Enable the tuner */
1443 giacomo 92
 
1474 giacomo 93
  tuner.tuner = 0;
1443 giacomo 94
  tuner.mode = VIDEO_MODE_PAL;
95
 
1474 giacomo 96
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSTUNER,(unsigned long)&tuner);
97
  //cprintf("res = %d\n",res);
1443 giacomo 98
 
1427 giacomo 99
  /* Select palette and depth */
1474 giacomo 100
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCGPICT,(unsigned long)&vpic);
101
  //cprintf("res = %d\n",res);  
102
 
1443 giacomo 103
  #ifdef COLOR
104
        vpic.palette = VIDEO_PALETTE_RGB24;
105
        vpic.depth = 24;
106
  #else
107
        vpic.palette = VIDEO_PALETTE_GREY;
108
        vpic.depth = 8;
109
  #endif
110
 
111
  vpic.brightness = 35000;
112
  vpic.hue = 32000;
113
  vpic.contrast = 32000;
114
  vpic.colour = 32000;                          
1424 giacomo 115
 
1474 giacomo 116
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSPICT,(unsigned long)&vpic);
117
  //cprintf("res = %d\n",res);
1424 giacomo 118
 
1474 giacomo 119
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCGWIN,(unsigned long)&win);
120
  //cprintf("res = %d\n",res);
121
 
1423 giacomo 122
  win.x = 0;
123
  win.y = 0;
1432 giacomo 124
  win.width = FG_W;
125
  win.height = FG_H;
1424 giacomo 126
 
1474 giacomo 127
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSWIN,(unsigned long)&win);
128
  //cprintf("res = %d\n",res);
129
 
1471 giacomo 130
  /* Set the buffer */
1474 giacomo 131
  res = VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSFBUF,(unsigned long)(fbuf));
132
  //cprintf("res = %d\n",res);
1471 giacomo 133
 
1470 giacomo 134
  /* IMPORTANT: Set the aperiodic elaboration task
135
   * This is a SHARK change on VIDIOCSYNC. When the
136
   * new frame is ready, the task elaborate_task_PID
137
   * is activated. Elabortate_task must be aperiodic !!
138
   * To link the task to BTTV use this function: */
139
 
140
  VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSYNC,(unsigned long)(elaborate_task_PID));
141
 
142
}
143
 
1471 giacomo 144
/* Check if the frame grabber is ready */
1473 giacomo 145
volatile int ready_to_grab = 1;
1470 giacomo 146
 
1485 giacomo 147
void grab_command(void *arg) {
148
 
149
  SYS_FLAGS f;
150
 
151
  f = kern_fsave();
152
 
153
  int on = 1;
154
  VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCCAPTURE,(unsigned long)&on);
155
 
156
  kern_frestore(f);
157
 
158
}
159
 
1470 giacomo 160
/* Elaboration task, it is called when the frame
161
   grabber buffer is ready */
162
TASK elaborate_task(void *arg) {
163
 
164
  struct video_buffer *fbuf = (struct video_buffer *)(arg);
165
 
1423 giacomo 166
  while(1) {
1443 giacomo 167
 
1470 giacomo 168
    elaborate_image(fbuf->base);
1472 giacomo 169
    //cprintf("E");
170
 
171
    //Text version
172
    //printf_xy(1,20,WHITE,"%08x",
173
    //    *(unsigned int *)(fbuf->base+50*320*3+50*3));    
1443 giacomo 174
 
1471 giacomo 175
    ready_to_grab = 1;
176
 
1480 giacomo 177
    #ifdef MAX_SPEED
1485 giacomo 178
      grab_command(NULL);
1480 giacomo 179
    #endif
180
 
1432 giacomo 181
    task_testcancel();
1424 giacomo 182
    task_endcycle();
183
 
1423 giacomo 184
  }
185
 
1424 giacomo 186
  return NULL;
187
 
1174 giacomo 188
}
189
 
1470 giacomo 190
/* Send the grab command */
191
TASK grab_task(void *arg) {
192
 
193
  while(1) {
1471 giacomo 194
 
195
    if (ready_to_grab) {
1473 giacomo 196
 
1471 giacomo 197
      /* Grab */
1485 giacomo 198
      grab_command(NULL);
1470 giacomo 199
 
1472 giacomo 200
      //cprintf("G");
201
 
1471 giacomo 202
      ready_to_grab = 0;
203
 
204
    } else {
205
 
1472 giacomo 206
      //cprintf("S");
1471 giacomo 207
      /* Frame skipped */
208
 
209
    }
210
 
1470 giacomo 211
    task_testcancel();
212
    task_endcycle();
213
 
214
  }
215
 
216
  return NULL;
217
 
218
}
219
 
1424 giacomo 220
extern void *video_memory;
221
 
1174 giacomo 222
void elaborate_image(void *imageptr)
223
{
1424 giacomo 224
 
1174 giacomo 225
  WORD x,y;
226
  BYTE *col;
227
 
1443 giacomo 228
  #ifdef COLOR
229
 
1174 giacomo 230
    for(y = 0; y < FG_H; y++)
231
      for(x = 0; x < FG_W; x++) {
232
 
1443 giacomo 233
        col = (BYTE *)(imageptr + y * FG_W * 3 + x * 3);
234
        *(WORD *)(video_memory + y*(WIDTH*2) + (x*2)) = (WORD)rgb16(*(BYTE *)(col+2),*(BYTE *)(col+1),*(BYTE *)(col+0));
1174 giacomo 235
 
236
    }
237
 
1443 giacomo 238
  #else
239
 
240
    for(y = 0; y < FG_H; y++)
241
      for(x = 0; x < FG_W; x++) {
242
 
243
        col = (BYTE *)(imageptr + y * FG_W + x);
244
        *(WORD *)(video_memory + y*(WIDTH*2) + (x*2)) = (WORD)rgb16(*(BYTE *)(col),*(BYTE *)(col),*(BYTE *)(col));
245
 
246
    }
247
 
248
  #endif
249
 
1174 giacomo 250
}
251
 
252
int main(int argc, char **argv)
253
{
254
 
1470 giacomo 255
  HARD_TASK_MODEL gt;
1485 giacomo 256
  SOFT_TASK_MODEL et;
1470 giacomo 257
  PID grab_task_pid,elaborate_task_pid;
1174 giacomo 258
 
1470 giacomo 259
  struct video_buffer fbuf;
260
  int channel = 0;
261
 
1444 giacomo 262
  if (argc < 2) {
1445 giacomo 263
        sys_shutdown_message("ERROR: Enter the input channel [ex> %s 0]\n",argv[0]);
1444 giacomo 264
        sys_end();
265
  }
1432 giacomo 266
 
1470 giacomo 267
  channel = atoi(argv[1]);
268
 
1485 giacomo 269
  soft_task_default_model(et);
1486 giacomo 270
  #ifndef MAX_SPEED
271
    soft_task_def_period(et,FG_PERIOD);
272
  #else
273
    /* The period must be less than the frame rate (around 40000 us) */
274
    soft_task_def_period(et,35000);
275
  #endif
1485 giacomo 276
  soft_task_def_arg(et,(void *)(&fbuf));
277
  soft_task_def_met(et,FG_WCET);
278
  soft_task_def_aperiodic(et);
279
  soft_task_def_ctrl_jet(et);
1470 giacomo 280
 
281
  hard_task_default_model(gt);
1480 giacomo 282
  hard_task_def_mit(gt,FG_PERIOD);
1473 giacomo 283
  hard_task_def_wcet(gt,5000);
1470 giacomo 284
  hard_task_def_ctrl_jet(gt);
285
 
286
  grab_task_pid = task_create("GrabTask",grab_task,&gt,NULL);
1423 giacomo 287
  if (grab_task_pid == NIL) {
1444 giacomo 288
        sys_shutdown_message("ERROR: Cannot create grab task\n");
1423 giacomo 289
        sys_end();
290
  }
1174 giacomo 291
 
1470 giacomo 292
  elaborate_task_pid = task_create("ElaborateTask",elaborate_task,&et,NULL);
293
  if (grab_task_pid == NIL) {
294
        sys_shutdown_message("ERROR: Cannot create elaborate task\n");
295
        sys_end();
296
  }
297
 
298
  #ifdef COLOR
299
 
300
        fbuf.base = malloc(FG_W*FG_H*3);
301
        fbuf.height = FG_H;
302
        fbuf.width = FG_W;
303
        fbuf.bytesperline = FG_W*3;
304
        fbuf.depth = 24;
305
 
306
  #else
307
 
308
        fbuf.base = malloc(FG_W*FG_H);
309
        fbuf.height = FG_H;
310
        fbuf.width = FG_W;
311
        fbuf.bytesperline = FG_W;
312
        fbuf.depth = 8;
313
 
314
  #endif
315
 
316
  start_frame_grabber(elaborate_task_pid,channel,&fbuf);
317
 
1480 giacomo 318
  #ifndef MAX_SPEED
319
    /* Activate periodic grabbing */
320
    task_activate(grab_task_pid);
321
  #else
322
    /* Grab */
1485 giacomo 323
    grab_command(NULL);
1480 giacomo 324
  #endif
1174 giacomo 325
 
1189 giacomo 326
  while(keyb_getch(BLOCK) != ESC);
327
 
1480 giacomo 328
  #ifndef MAX_SPEED
329
    task_kill(grab_task_pid);
330
  #endif
1432 giacomo 331
 
332
  sleep(1);
333
 
1423 giacomo 334
  sys_end();
1189 giacomo 335
 
1174 giacomo 336
  return 0;
337
 
338
}