#include #include #include #include #include #include #include #include #include int vid_fd = -1; int pp_fd = -1; FRAMEGRABBER *fg = NULL; void cleanup (void); void handler (int sig) { cleanup(); exit(0); } void press_button (void); int main () { int res; time_t t; int i; int mode; int fd; int jumps; float freq=67.25; unsigned char data; FRAME *fr = NULL; pp_fd = open ("/dev/parports/0", O_RDWR); res = ioctl (pp_fd, PPCLAIM, NULL); if (pp_fd <0 || res != 0) { perror ("Couldn't open or claim parallel port: "); exit (0); } atexit (cleanup); signal (SIGINT, handler); fg = fg_open ("/dev/v4l/video0"); if (!fg) { perror ("Couldn't open video dev: "); exit(0); } fg_set_source (fg, FG_SOURCE_TV); fg_set_channel (fg, freq); fg->window.width = fg->caps.minwidth; fg->window.height = fg->caps.minheight; fr = fg_new_compatible_frame(fg); mode = 0; ioctl (pp_fd, PPDATADIR, &mode); data = 0xff; ioctl (pp_fd, PPWDATA, &data); fg_grab_frame (fg, fr); frame_save (fr, "img.png"); for (jumps=0;;) { int i; int j; static int hit=0; unsigned int color=0; unsigned int colsum=0; unsigned int *p; unsigned int red =0; unsigned int green =0; unsigned int blue =0; int pixels, intensity; fg_grab_frame (fg, fr); p = (unsigned int *) fr->data; for (i=0; iwidth; i++) { for (j=0; jheight; j++) { color = p[i + j * fr->height]; red += ((color & 0xff0000) >> 16); green += ((color & 0xff00) >> 8); blue += (color & 0xff); } } pixels = fr->width * fr->height; red /= pixels; green /= pixels; blue /= pixels; colsum = (red << 16); colsum |= (green << 8); colsum |= blue; intensity = (red + green + blue )/ 3; // printf ("intensity = %d\n", intensity); if (intensity > 100) { if (hit == 1) { if (intensity >= 135) { continue; } } jumps++; hit ++; printf ("Pressing button! (%d)\n", jumps); press_button (); } else { hit = 0; } } frame_save (fr, "img.pgm"); } void cleanup (void) { ioctl (pp_fd, PPRELEASE, NULL); } void press_button(void) { unsigned char data; data = 0xf0; usleep (10000); ioctl (pp_fd, PPWDATA, &data); usleep (50000); data = 0xff; ioctl (pp_fd, PPWDATA, &data); sleep (1); }