Subversion Repositories shark

Rev

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