Rev 1480 | Rev 1485 | 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 | |
147 | /* Elaboration task, it is called when the frame |
||
148 | grabber buffer is ready */ |
||
149 | TASK elaborate_task(void *arg) { |
||
150 | |||
151 | struct video_buffer *fbuf = (struct video_buffer *)(arg); |
||
1480 | giacomo | 152 | #ifdef MAX_SPEED |
153 | int on; |
||
154 | #endif |
||
1470 | giacomo | 155 | |
1423 | giacomo | 156 | while(1) { |
1443 | giacomo | 157 | |
1470 | giacomo | 158 | elaborate_image(fbuf->base); |
1472 | giacomo | 159 | //cprintf("E"); |
160 | |||
161 | //Text version |
||
162 | //printf_xy(1,20,WHITE,"%08x", |
||
163 | // *(unsigned int *)(fbuf->base+50*320*3+50*3)); |
||
1443 | giacomo | 164 | |
1471 | giacomo | 165 | ready_to_grab = 1; |
166 | |||
1480 | giacomo | 167 | #ifdef MAX_SPEED |
168 | on = 1; |
||
169 | VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCCAPTURE,(unsigned long)&on); |
||
170 | #endif |
||
171 | |||
1432 | giacomo | 172 | task_testcancel(); |
1424 | giacomo | 173 | task_endcycle(); |
174 | |||
1423 | giacomo | 175 | } |
176 | |||
1424 | giacomo | 177 | return NULL; |
178 | |||
1174 | giacomo | 179 | } |
180 | |||
1470 | giacomo | 181 | /* Send the grab command */ |
182 | TASK grab_task(void *arg) { |
||
183 | |||
184 | int on; |
||
185 | |||
186 | while(1) { |
||
1471 | giacomo | 187 | |
188 | if (ready_to_grab) { |
||
1473 | giacomo | 189 | |
190 | task_nopreempt(); |
||
191 | |||
1471 | giacomo | 192 | /* Grab */ |
193 | on = 1; |
||
194 | VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCCAPTURE,(unsigned long)&on); |
||
1470 | giacomo | 195 | |
1473 | giacomo | 196 | task_preempt(); |
197 | |||
1472 | giacomo | 198 | //cprintf("G"); |
199 | |||
1471 | giacomo | 200 | ready_to_grab = 0; |
201 | |||
202 | } else { |
||
203 | |||
1472 | giacomo | 204 | //cprintf("S"); |
1471 | giacomo | 205 | /* Frame skipped */ |
206 | |||
207 | } |
||
208 | |||
1470 | giacomo | 209 | task_testcancel(); |
210 | task_endcycle(); |
||
211 | |||
212 | } |
||
213 | |||
214 | return NULL; |
||
215 | |||
216 | } |
||
217 | |||
1424 | giacomo | 218 | extern void *video_memory; |
219 | |||
1174 | giacomo | 220 | void elaborate_image(void *imageptr) |
221 | { |
||
1424 | giacomo | 222 | |
1174 | giacomo | 223 | WORD x,y; |
224 | BYTE *col; |
||
225 | |||
1443 | giacomo | 226 | #ifdef COLOR |
227 | |||
1174 | giacomo | 228 | for(y = 0; y < FG_H; y++) |
229 | for(x = 0; x < FG_W; x++) { |
||
230 | |||
1443 | giacomo | 231 | col = (BYTE *)(imageptr + y * FG_W * 3 + x * 3); |
232 | *(WORD *)(video_memory + y*(WIDTH*2) + (x*2)) = (WORD)rgb16(*(BYTE *)(col+2),*(BYTE *)(col+1),*(BYTE *)(col+0)); |
||
1174 | giacomo | 233 | |
234 | } |
||
235 | |||
1443 | giacomo | 236 | #else |
237 | |||
238 | for(y = 0; y < FG_H; y++) |
||
239 | for(x = 0; x < FG_W; x++) { |
||
240 | |||
241 | col = (BYTE *)(imageptr + y * FG_W + x); |
||
242 | *(WORD *)(video_memory + y*(WIDTH*2) + (x*2)) = (WORD)rgb16(*(BYTE *)(col),*(BYTE *)(col),*(BYTE *)(col)); |
||
243 | |||
244 | } |
||
245 | |||
246 | #endif |
||
247 | |||
1174 | giacomo | 248 | } |
249 | |||
250 | int main(int argc, char **argv) |
||
251 | { |
||
252 | |||
1470 | giacomo | 253 | HARD_TASK_MODEL gt; |
1484 | giacomo | 254 | HARD_TASK_MODEL et; |
1470 | giacomo | 255 | PID grab_task_pid,elaborate_task_pid; |
1174 | giacomo | 256 | |
1470 | giacomo | 257 | struct video_buffer fbuf; |
258 | int channel = 0; |
||
1480 | giacomo | 259 | #ifdef MAX_SPEED |
260 | int on; |
||
261 | #endif |
||
1470 | giacomo | 262 | |
1444 | giacomo | 263 | if (argc < 2) { |
1445 | giacomo | 264 | sys_shutdown_message("ERROR: Enter the input channel [ex> %s 0]\n",argv[0]); |
1444 | giacomo | 265 | sys_end(); |
266 | } |
||
1432 | giacomo | 267 | |
1470 | giacomo | 268 | channel = atoi(argv[1]); |
269 | |||
1484 | giacomo | 270 | hard_task_default_model(et); |
271 | hard_task_def_mit(et,FG_PERIOD); |
||
272 | hard_task_def_arg(et,(void *)(&fbuf)); |
||
273 | hard_task_def_wcet(et,FG_WCET); |
||
274 | hard_task_def_aperiodic(et); |
||
275 | hard_task_def_ctrl_jet(et); |
||
1470 | giacomo | 276 | |
277 | hard_task_default_model(gt); |
||
1480 | giacomo | 278 | hard_task_def_mit(gt,FG_PERIOD); |
1473 | giacomo | 279 | hard_task_def_wcet(gt,5000); |
1470 | giacomo | 280 | hard_task_def_ctrl_jet(gt); |
281 | |||
282 | grab_task_pid = task_create("GrabTask",grab_task,>,NULL); |
||
1423 | giacomo | 283 | if (grab_task_pid == NIL) { |
1444 | giacomo | 284 | sys_shutdown_message("ERROR: Cannot create grab task\n"); |
1423 | giacomo | 285 | sys_end(); |
286 | } |
||
1174 | giacomo | 287 | |
1470 | giacomo | 288 | elaborate_task_pid = task_create("ElaborateTask",elaborate_task,&et,NULL); |
289 | if (grab_task_pid == NIL) { |
||
290 | sys_shutdown_message("ERROR: Cannot create elaborate task\n"); |
||
291 | sys_end(); |
||
292 | } |
||
293 | |||
294 | #ifdef COLOR |
||
295 | |||
296 | fbuf.base = malloc(FG_W*FG_H*3); |
||
297 | fbuf.height = FG_H; |
||
298 | fbuf.width = FG_W; |
||
299 | fbuf.bytesperline = FG_W*3; |
||
300 | fbuf.depth = 24; |
||
301 | |||
302 | #else |
||
303 | |||
304 | fbuf.base = malloc(FG_W*FG_H); |
||
305 | fbuf.height = FG_H; |
||
306 | fbuf.width = FG_W; |
||
307 | fbuf.bytesperline = FG_W; |
||
308 | fbuf.depth = 8; |
||
309 | |||
310 | #endif |
||
311 | |||
312 | start_frame_grabber(elaborate_task_pid,channel,&fbuf); |
||
313 | |||
1480 | giacomo | 314 | #ifndef MAX_SPEED |
315 | /* Activate periodic grabbing */ |
||
316 | task_activate(grab_task_pid); |
||
317 | #else |
||
318 | /* Grab */ |
||
319 | on = 1; |
||
320 | VIDEODEV26_ioctl(FRAME_GRABBER_NUMBER,VIDIOCCAPTURE,(unsigned long)&on); |
||
321 | #endif |
||
1174 | giacomo | 322 | |
1189 | giacomo | 323 | while(keyb_getch(BLOCK) != ESC); |
324 | |||
1480 | giacomo | 325 | #ifndef MAX_SPEED |
326 | task_kill(grab_task_pid); |
||
327 | #endif |
||
1432 | giacomo | 328 | |
329 | sleep(1); |
||
330 | |||
1423 | giacomo | 331 | sys_end(); |
1189 | giacomo | 332 | |
1174 | giacomo | 333 | return 0; |
334 | |||
335 | } |