Subversion Repositories shark

Rev

Rev 1472 | Rev 1474 | 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 */
1473 giacomo 134
volatile 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);
1472 giacomo 145
    //cprintf("E");
146
 
147
    //Text version
148
    //printf_xy(1,20,WHITE,"%08x",
149
    //    *(unsigned int *)(fbuf->base+50*320*3+50*3));    
1443 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) {
1473 giacomo 170
 
171
      task_nopreempt();
172
 
1471 giacomo 173
      /* Grab */
174
      on = 1;
175
      VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCCAPTURE,(unsigned long)&on);    
1470 giacomo 176
 
1473 giacomo 177
      task_preempt();
178
 
1472 giacomo 179
      //cprintf("G");
180
 
1471 giacomo 181
      ready_to_grab = 0;
182
 
183
    } else {
184
 
1472 giacomo 185
      //cprintf("S");
1471 giacomo 186
      /* Frame skipped */
187
 
188
    }
189
 
1470 giacomo 190
    task_testcancel();
191
    task_endcycle();
192
 
193
  }
194
 
195
  return NULL;
196
 
197
}
198
 
1424 giacomo 199
extern void *video_memory;
200
 
1174 giacomo 201
void elaborate_image(void *imageptr)
202
{
1424 giacomo 203
 
1174 giacomo 204
  WORD x,y;
205
  BYTE *col;
206
 
1443 giacomo 207
  #ifdef COLOR
208
 
1174 giacomo 209
    for(y = 0; y < FG_H; y++)
210
      for(x = 0; x < FG_W; x++) {
211
 
1443 giacomo 212
        col = (BYTE *)(imageptr + y * FG_W * 3 + x * 3);
213
        *(WORD *)(video_memory + y*(WIDTH*2) + (x*2)) = (WORD)rgb16(*(BYTE *)(col+2),*(BYTE *)(col+1),*(BYTE *)(col+0));
1174 giacomo 214
 
215
    }
216
 
1443 giacomo 217
  #else
218
 
219
    for(y = 0; y < FG_H; y++)
220
      for(x = 0; x < FG_W; x++) {
221
 
222
        col = (BYTE *)(imageptr + y * FG_W + x);
223
        *(WORD *)(video_memory + y*(WIDTH*2) + (x*2)) = (WORD)rgb16(*(BYTE *)(col),*(BYTE *)(col),*(BYTE *)(col));
224
 
225
    }
226
 
227
  #endif
228
 
1174 giacomo 229
}
230
 
231
int main(int argc, char **argv)
232
{
233
 
1470 giacomo 234
  HARD_TASK_MODEL gt;
235
  SOFT_TASK_MODEL et;
236
  PID grab_task_pid,elaborate_task_pid;
1174 giacomo 237
 
1470 giacomo 238
  struct video_buffer fbuf;
239
  int channel = 0;
240
 
1444 giacomo 241
  if (argc < 2) {
1445 giacomo 242
        sys_shutdown_message("ERROR: Enter the input channel [ex> %s 0]\n",argv[0]);
1444 giacomo 243
        sys_end();
244
  }
1432 giacomo 245
 
1470 giacomo 246
  channel = atoi(argv[1]);
247
 
248
  soft_task_default_model(et);
249
  soft_task_def_period(et,40000);
250
  soft_task_def_arg(et,(void *)(&fbuf));
1472 giacomo 251
  soft_task_def_met(et,20000);
1470 giacomo 252
  soft_task_def_aperiodic(et);
253
  soft_task_def_ctrl_jet(et);
254
 
255
  hard_task_default_model(gt);
256
  hard_task_def_mit(gt,40000);
1473 giacomo 257
  hard_task_def_wcet(gt,5000);
1470 giacomo 258
  hard_task_def_ctrl_jet(gt);
259
 
260
  grab_task_pid = task_create("GrabTask",grab_task,&gt,NULL);
1423 giacomo 261
  if (grab_task_pid == NIL) {
1444 giacomo 262
        sys_shutdown_message("ERROR: Cannot create grab task\n");
1423 giacomo 263
        sys_end();
264
  }
1174 giacomo 265
 
1470 giacomo 266
  elaborate_task_pid = task_create("ElaborateTask",elaborate_task,&et,NULL);
267
  if (grab_task_pid == NIL) {
268
        sys_shutdown_message("ERROR: Cannot create elaborate task\n");
269
        sys_end();
270
  }
271
 
272
  #ifdef COLOR
273
 
274
        fbuf.base = malloc(FG_W*FG_H*3);
275
        fbuf.height = FG_H;
276
        fbuf.width = FG_W;
277
        fbuf.bytesperline = FG_W*3;
278
        fbuf.depth = 24;
279
 
280
  #else
281
 
282
        fbuf.base = malloc(FG_W*FG_H);
283
        fbuf.height = FG_H;
284
        fbuf.width = FG_W;
285
        fbuf.bytesperline = FG_W;
286
        fbuf.depth = 8;
287
 
288
  #endif
289
 
290
  start_frame_grabber(elaborate_task_pid,channel,&fbuf);
291
 
1471 giacomo 292
  /* Activate periodic grabbing */
1423 giacomo 293
  task_activate(grab_task_pid);
1174 giacomo 294
 
1189 giacomo 295
  while(keyb_getch(BLOCK) != ESC);
296
 
1432 giacomo 297
  task_kill(grab_task_pid);
298
 
299
  sleep(1);
300
 
1423 giacomo 301
  sys_end();
1189 giacomo 302
 
1174 giacomo 303
  return 0;
304
 
305
}