Subversion Repositories shark

Rev

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