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