Subversion Repositories shark

Rev

Rev 1445 | Rev 1471 | 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
 
1470 giacomo 120
  /* IMPORTANT: Set the aperiodic elaboration task
121
   * This is a SHARK change on VIDIOCSYNC. When the
122
   * new frame is ready, the task elaborate_task_PID
123
   * is activated. Elabortate_task must be aperiodic !!
124
   * To link the task to BTTV use this function: */
125
 
126
  VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSYNC,(unsigned long)(elaborate_task_PID));
127
 
128
}
129
 
130
 
131
/* Elaboration task, it is called when the frame
132
   grabber buffer is ready */
133
TASK elaborate_task(void *arg) {
134
 
135
  struct video_buffer *fbuf = (struct video_buffer *)(arg);
136
 
1423 giacomo 137
  while(1) {
1443 giacomo 138
 
1470 giacomo 139
    elaborate_image(fbuf->base);
1443 giacomo 140
 
1470 giacomo 141
    /* Text version
1443 giacomo 142
    printf_xy(1,20,WHITE,"%08x",
1470 giacomo 143
        *(unsigned int *)(fbuf->base+50*320*3+50*3));
1428 giacomo 144
    */
1423 giacomo 145
 
1432 giacomo 146
    task_testcancel();
1424 giacomo 147
    task_endcycle();
148
 
1423 giacomo 149
  }
150
 
1424 giacomo 151
  return NULL;
152
 
1174 giacomo 153
}
154
 
1470 giacomo 155
/* Send the grab command */
156
TASK grab_task(void *arg) {
157
 
158
  struct video_buffer *fbuf = (struct video_buffer *)(arg);
159
  int on;
160
 
161
  while(1) {
162
 
163
    VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCSFBUF,(unsigned long)(fbuf));    
164
 
165
    /* Grab */
166
    on = 1;
167
    VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCCAPTURE,(unsigned long)&on);    
168
 
169
    task_testcancel();
170
    task_endcycle();
171
 
172
  }
173
 
174
  return NULL;
175
 
176
}
177
 
1424 giacomo 178
extern void *video_memory;
179
 
1174 giacomo 180
void elaborate_image(void *imageptr)
181
{
1424 giacomo 182
 
1174 giacomo 183
  WORD x,y;
184
  BYTE *col;
185
 
1443 giacomo 186
  #ifdef COLOR
187
 
1174 giacomo 188
    for(y = 0; y < FG_H; y++)
189
      for(x = 0; x < FG_W; x++) {
190
 
1443 giacomo 191
        col = (BYTE *)(imageptr + y * FG_W * 3 + x * 3);
192
        *(WORD *)(video_memory + y*(WIDTH*2) + (x*2)) = (WORD)rgb16(*(BYTE *)(col+2),*(BYTE *)(col+1),*(BYTE *)(col+0));
1174 giacomo 193
 
194
    }
195
 
1443 giacomo 196
  #else
197
 
198
    for(y = 0; y < FG_H; y++)
199
      for(x = 0; x < FG_W; x++) {
200
 
201
        col = (BYTE *)(imageptr + y * FG_W + x);
202
        *(WORD *)(video_memory + y*(WIDTH*2) + (x*2)) = (WORD)rgb16(*(BYTE *)(col),*(BYTE *)(col),*(BYTE *)(col));
203
 
204
    }
205
 
206
  #endif
207
 
1174 giacomo 208
}
209
 
210
int main(int argc, char **argv)
211
{
212
 
1470 giacomo 213
  HARD_TASK_MODEL gt;
214
  SOFT_TASK_MODEL et;
215
  PID grab_task_pid,elaborate_task_pid;
1174 giacomo 216
 
1470 giacomo 217
  struct video_buffer fbuf;
218
  int channel = 0;
219
 
1444 giacomo 220
  if (argc < 2) {
1445 giacomo 221
        sys_shutdown_message("ERROR: Enter the input channel [ex> %s 0]\n",argv[0]);
1444 giacomo 222
        sys_end();
223
  }
1432 giacomo 224
 
1470 giacomo 225
  channel = atoi(argv[1]);
226
 
227
  soft_task_default_model(et);
228
  soft_task_def_period(et,40000);
229
  soft_task_def_arg(et,(void *)(&fbuf));
230
  soft_task_def_met(et,30000);
231
  soft_task_def_aperiodic(et);
232
  soft_task_def_ctrl_jet(et);
233
 
234
  hard_task_default_model(gt);
235
  hard_task_def_mit(gt,40000);
236
  hard_task_def_arg(gt,(void *)(&fbuf));
237
  hard_task_def_wcet(gt,3000);
238
  hard_task_def_ctrl_jet(gt);
239
 
240
  grab_task_pid = task_create("GrabTask",grab_task,&gt,NULL);
1423 giacomo 241
  if (grab_task_pid == NIL) {
1444 giacomo 242
        sys_shutdown_message("ERROR: Cannot create grab task\n");
1423 giacomo 243
        sys_end();
244
  }
1174 giacomo 245
 
1470 giacomo 246
  elaborate_task_pid = task_create("ElaborateTask",elaborate_task,&et,NULL);
247
  if (grab_task_pid == NIL) {
248
        sys_shutdown_message("ERROR: Cannot create elaborate task\n");
249
        sys_end();
250
  }
251
 
252
  #ifdef COLOR
253
 
254
        fbuf.base = malloc(FG_W*FG_H*3);
255
        fbuf.height = FG_H;
256
        fbuf.width = FG_W;
257
        fbuf.bytesperline = FG_W*3;
258
        fbuf.depth = 24;
259
 
260
  #else
261
 
262
        fbuf.base = malloc(FG_W*FG_H);
263
        fbuf.height = FG_H;
264
        fbuf.width = FG_W;
265
        fbuf.bytesperline = FG_W;
266
        fbuf.depth = 8;
267
 
268
  #endif
269
 
270
  start_frame_grabber(elaborate_task_pid,channel,&fbuf);
271
 
1423 giacomo 272
  task_activate(grab_task_pid);
1174 giacomo 273
 
1189 giacomo 274
  while(keyb_getch(BLOCK) != ESC);
275
 
1432 giacomo 276
  task_kill(grab_task_pid);
277
 
278
  sleep(1);
279
 
1423 giacomo 280
  sys_end();
1189 giacomo 281
 
1174 giacomo 282
  return 0;
283
 
284
}