void close_dc20(int fd) { /* * Put the camera back to 9600 baud */ init_pck[2] = speeds[0].pkt_code[0]; init_pck[3] = speeds[0].pkt_code[1]; if (send_pck(fd, init_pck) == -1) { if (!quiet) fprintf(stderr, "%s: close_dc20: error: could not set attributes\n", __progname); } /* Restore original device settings. */ if (tcsetattr(fd, TCSANOW, &tty_orig) == -1 && !quiet) { perror("tcsetattr"); fprintf(stderr, "%s: close_dc20: error: could not set attributes\n", __progname); } if (close(fd) == -1 && !quiet) { perror("close"); fprintf(stderr, "%s: close_dc20: error: could not close device\n", __progname); } }
static int change_res (int fd, unsigned char res) { char f[] = "change_res"; DBG (127, "%s called\n", f); if (res != 0 && res != 1) { DBG (3, "%s: error: unsupported resolution\n", f); return -1; } /* cameras resolution semantics are opposite of ours */ res = !res; DBG (127, "%s: setting res to %d\n", f, res); res_pck[2] = res; if (send_pck (fd, res_pck) == -1) { DBG (4, "%s: error: send_pck returned -1\n", f); } if (end_of_data (fd) == -1) { DBG (4, "%s: error: end_of_data returned -1\n", f); } return 0; }
Dc20Info *get_info(int fd) { static Dc20Info result; unsigned char buf[256]; if (send_pck(fd, info_pck) == -1) { if (!quiet) fprintf(stderr, "%s: get_info: error: send_pck returned -1\n", __progname); return NULL; } if (verbose) printf("%s: get_info: read info packet\n", __progname); if (read_data(fd, buf, 256) == -1) { if (!quiet) fprintf(stderr, "%s: get_info: error: read_data returned -1\n", __progname); return NULL; } if (end_of_data(fd) == -1) { if (!quiet) fprintf(stderr, "%s: get_info: error: end_of_data returned -1\n", __progname); return NULL; } result.model = buf[1]; result.ver_major = buf[2]; result.ver_minor = buf[3]; result.pic_taken = buf[8]<<8|buf[9]; #ifdef DC25 /* Not sure where the previous line came from. All the * information I have says that even on the DC20 the number of * standard res pics is in byte 17 and the number of high res pics * is in byte 19. This is definitely true on my DC25. */ result.pic_taken = buf[17]+buf[19]; #endif result.pic_left = buf[10]<<8|buf[11]; #ifdef DC25 /* Not sure where the previous line came from. All the * information I have says that even on the DC20 the number of * standard res pics left is in byte 23 and the number of high res * pics left is in byte 21. It seems to me that the conservative * approach is to report the number of high res pics left. */ result.pic_left = buf[21]; #endif result.flags.low_res = buf[23]; #ifdef DC25 /* Not sure where the previous line came from. All the * information I have says that even on the DC20 the low_res * byte is 11. */ result.flags.low_res = buf[11]; #endif result.flags.low_batt = buf[29]; return &result; }
static SANE_Status snap_pic (int fd) { char f[] = "snap_pic"; /* make sure camera is set to our settings state */ if (change_res (Camera.fd, dc210_opt_lowres) == -1) { DBG (1, "%s: Failed to set resolution\n", f); return SANE_STATUS_INVAL; } /* take the picture */ if (send_pck (fd, shoot_pck) == -1) { DBG (4, "%s: error: send_pck returned -1\n", f); return SANE_STATUS_INVAL; } else { if (end_of_data (Camera.fd) == -1) { DBG (2, "%s: error: end_of_data returned -1\n", f); return SANE_STATUS_INVAL; } } Camera.pic_taken++; Camera.pic_left--; Camera.current_picture_number = Camera.pic_taken; image_range.max++; sod[DC210_OPT_IMAGE_NUMBER].cap &= ~SANE_CAP_INACTIVE; /* add this one to the Pictures array */ if ((Camera.Pictures = (PictureInfo *) realloc (Camera.Pictures, Camera.pic_taken * sizeof (PictureInfo))) == NULL) { DBG (4, "%s: error: allocate memory for pictures array\n", f); return SANE_STATUS_INVAL; } if (get_picture_info (Camera.Pictures + Camera.pic_taken, Camera.pic_taken) == -1) { DBG (1, "%s: Failed to get new picture info\n", f); /* XXX - I guess we should try to erase the image here */ return SANE_STATUS_INVAL; } return SANE_STATUS_GOOD; }
static int get_picture_info (PictureInfo * pic, int p) { char f[] = "get_picture_info"; static char buffer[256]; DBG (4, "%s: info for pic #%d\n", f, p); pic_info_pck[3] = (unsigned char) p; if (send_pck (Camera.fd, pic_info_pck) == -1) { DBG (4, "%s: error: send_pck returned -1\n", f); return -1; } if (read_data (Camera.fd, (unsigned char *) buffer, 256) == -1) { DBG (2, "%s: error: read_data returned -1\n", f); return -1; } if (end_of_data (Camera.fd) == -1) { DBG (2, "%s: error: end_of_data returned -1\n", f); return -1; } if (buffer[3] == 0) { pic->low_res = SANE_TRUE; } else if (buffer[3] == 1) { pic->low_res = SANE_FALSE; } else { DBG (2, "%s: error: unknown resolution code %u\n", f, buffer[3]); return -1; } pic->size = (buffer[8] & 0xFF) << 24; pic->size |= (buffer[9] & 0xFF) << 16; pic->size |= (buffer[10] & 0xFF) << 8; pic->size |= (buffer[11] & 0xFF); return 0; }
int main() { int i; for (i=1;i<32;i++) buffer [i] = i; for (i=1;i<32;i++) { delay(5000000); send_pck (0,0,buffer,30,0x00); wait_for_sending_pck(); delay(50000000); send_pck (0,0,buffer,31,0x00); wait_for_sending_pck(); } while(1) { gpio_o_wr(0,1); delay(500000); gpio_o_wr(0,0); delay(500000); }//while return 0; }
static int erase (int fd) { if (send_pck (fd, erase_pck) == -1) { DBG (3, "erase: error: send_pck returned -1\n"); return -1; } if (end_of_data (fd) == -1) { DBG (3, "erase: error: end_of_data returned -1\n"); return -1; } return 0; }
int get_pic(int fd, int which, unsigned char *pic, int low_res) { unsigned char buf[1024]; int n = (low_res) ? 61 : 122; int i; pic_pck[3] = (unsigned char)which; if (send_pck(fd, pic_pck) == -1) { if (!quiet) fprintf(stderr, "%s: get_pic: error: send_pck returned -1\n", __progname); return -1; } printf("Get image #%d: ", which); hash_init(); for (i = 0; i < n; i++) { hash_mark(i, n - 1, 40); if (read_data(fd, buf, 1024) == -1) { if (!quiet) fprintf(stderr, "%s: get_pic: error: read_data returned -1\n", __progname); return -1; } #ifdef DC25 /* * If this is the first row, byte 4 tells us if this is * a low or hi-res image */ if ( i == 0 ) { if ( buf[4] == 0 ) { n=122; } else { n=61; } } #endif memcpy((char *)&pic[i*1024], buf, 1024); } printf("\n"); return end_of_data(fd); }
int get_info (DC210 * camera) { char f[] = "get_info"; unsigned char buf[256]; if (send_pck (camera->fd, info_pck) == -1) { DBG (2, "%s: error: send_pck returned -1\n", f); return -1; } DBG (9, "%s: read info packet\n", f); if (read_data (camera->fd, buf, 256) == -1) { DBG (2, "%s: error: read_data returned -1\n", f); return -1; } if (end_of_data (camera->fd) == -1) { DBG (2, "%s: error: end_of_data returned -1\n", f); return -1; } camera->model = buf[1]; camera->ver_major = buf[2]; camera->ver_minor = buf[3]; camera->pic_taken = buf[56] << 8 | buf[57]; camera->pic_left = buf[72] << 8 | buf[73]; camera->flags.low_res = buf[22]; camera->flags.low_batt = buf[8]; return 0; }
static int init_dc210 (DC210 * camera) { struct termios tty_new; int speed_index; for (speed_index = 0; speed_index < NELEMS (speeds); speed_index++) { if (speeds[speed_index].baud == camera->baud) { init_pck[2] = speeds[speed_index].pkt_code[0]; init_pck[3] = speeds[speed_index].pkt_code[1]; break; } } if (init_pck[2] == 0) { DBG (2, "unsupported baud rate.\n"); return -1; } /* Open device file. */ if ((camera->fd = open (camera->tty_name, O_RDWR)) == -1) { DBG (2, "init_dc210: error: could not open %s for read/write\n", camera->tty_name); return -1; } /* Save old device information to restore when we are done. */ if (tcgetattr (camera->fd, &tty_orig) == -1) { DBG (2, "init_dc210: error: could not get attributes\n"); return -1; } memcpy ((char *) &tty_new, (char *) &tty_orig, sizeof (struct termios)); /* We need the device to be raw. 8 bits even parity on 9600 baud to start. */ #ifdef HAVE_CFMAKERAW cfmakeraw (&tty_new); #else /* Modified to set the port REALLY as required. Code inspired by the gPhoto2 serial port setup */ /* input control settings */ tty_new.c_iflag &= ~(IGNBRK | IGNCR | INLCR | ICRNL | IUCLC | IXANY | IXON | IXOFF | INPCK | ISTRIP); tty_new.c_iflag |= (BRKINT | IGNPAR); /* output control settings */ tty_new.c_oflag &= ~OPOST; /* hardware control settings */ tty_new.c_cflag = (tty_new.c_cflag & ~CSIZE) | CS8; tty_new.c_cflag &= ~(PARENB | PARODD | CSTOPB); # if defined(__sgi) tty_new.c_cflag &= ~CNEW_RTSCTS; # else /* OS/2 doesn't have CRTSCTS - will this work for them? */ # ifdef CRTSCTS tty_new.c_cflag &= ~CRTSCTS; # endif # endif tty_new.c_cflag |= CLOCAL | CREAD; #endif /* line discipline settings */ tty_new.c_lflag &= ~(ICANON | ISIG | ECHO | ECHONL | ECHOE | ECHOK | IEXTEN); tty_new.c_cc[VMIN] = 0; tty_new.c_cc[VTIME] = 5; cfsetospeed (&tty_new, B9600); cfsetispeed (&tty_new, B9600); if (tcsetattr (camera->fd, TCSANOW, &tty_new) == -1) { DBG (2, "init_dc210: error: could not set attributes\n"); return -1; } /* send a break to get it back to a known state */ /* Used to supply a non-zero argument to tcsendbreak(), TCSBRK, * and TCSBRKP, but that is system dependent. e.g. on irix a non-zero * value does a drain instead of a break. A zero value is universally * used to send a break. */ #ifdef HAVE_TCSENDBREAK tcsendbreak (camera->fd, 0); # if defined(__sgi) tcdrain (camera->fd); # endif # elif defined(TCSBRKP) ioctl (camera->fd, TCSBRKP, 0); # elif defined(TCSBRK) ioctl (camera->fd, TCSBRK, 0); #endif /* and wait for it to recover from the break */ #ifdef HAVE_USLEEP usleep (breakpause); #else sleep (1); #endif if (send_pck (camera->fd, init_pck) == -1) { /* * The camera always powers up at 9600, so we try * that first. However, it may be already set to * a different speed. Try the entries in the table: */ for (speed_index = NELEMS (speeds) - 1; speed_index > 0; speed_index--) { int x; DBG (3, "init_dc210: changing speed to %d\n", (int) speeds[speed_index].baud); cfsetospeed (&tty_new, speeds[speed_index].baud); cfsetispeed (&tty_new, speeds[speed_index].baud); if (tcsetattr (camera->fd, TCSANOW, &tty_new) == -1) { DBG (2, "init_dc210: error: could not set attributes\n"); return -1; } for (x = 0; x < 3; x++) if (send_pck (camera->fd, init_pck) != -1) break; } if (speed_index == 0) { tcsetattr (camera->fd, TCSANOW, &tty_orig); DBG (2, "init_dc210: error: no suitable baud rate\n"); return -1; } } /* Set speed to requested speed. */ cfsetospeed (&tty_new, Camera.baud); cfsetispeed (&tty_new, Camera.baud); if (tcsetattr (camera->fd, TCSANOW, &tty_new) == -1) { DBG (2, "init_dc210: error: could not set attributes\n"); return -1; } return camera->fd; }
SANE_Status sane_start (SANE_Handle handle) { DBG (127, "sane_start called\n"); if (handle != MAGIC || !is_open || (Camera.current_picture_number == 0 && dc210_opt_snap == SANE_FALSE)) return SANE_STATUS_INVAL; /* Unknown handle ... */ if (Camera.scanning) return SANE_STATUS_EOF; if (dc210_opt_snap) { /* * Don't allow picture unless there is room in the * camera. */ if (Camera.pic_left == 0) { DBG (3, "No room to store new picture\n"); return SANE_STATUS_INVAL; } if (snap_pic (Camera.fd) != SANE_STATUS_GOOD) { DBG (1, "Failed to snap new picture\n"); return SANE_STATUS_INVAL; } } if (dc210_opt_thumbnails) { thumb_pck[3] = (unsigned char) Camera.current_picture_number - 1; thumb_pck[4] = 1; if (send_pck (Camera.fd, thumb_pck) == -1) { DBG (4, "sane_start: error: send_pck returned -1\n"); return SANE_STATUS_INVAL; } parms.bytes_per_line = 96 * 3; parms.pixels_per_line = 96; parms.lines = 72; bytes_in_buffer = 0; bytes_read_from_buffer = 0; } else { my_src_ptr src; struct jpeg_error_mgr jerr; int row_stride; pic_pck[3] = (unsigned char) Camera.current_picture_number - 1; if (send_pck (Camera.fd, pic_pck) == -1) { DBG (4, "sane_start: error: send_pck returned -1\n"); return SANE_STATUS_INVAL; } cinfo.err = jpeg_std_error (&jerr); jpeg_create_decompress (&cinfo); cinfo.src = (struct jpeg_source_mgr *) (*cinfo.mem->alloc_small) ((j_common_ptr) & cinfo, JPOOL_PERMANENT, sizeof (my_source_mgr)); src = (my_src_ptr) cinfo.src; src->buffer = (JOCTET *) (*cinfo.mem->alloc_small) ((j_common_ptr) & cinfo, JPOOL_PERMANENT, 1024 * sizeof (JOCTET)); src->pub.init_source = sanei_jpeg_init_source; src->pub.fill_input_buffer = sanei_jpeg_fill_input_buffer; src->pub.skip_input_data = sanei_jpeg_skip_input_data; src->pub.resync_to_restart = jpeg_resync_to_restart; /* default */ src->pub.term_source = sanei_jpeg_term_source; src->pub.bytes_in_buffer = 0; src->pub.next_input_byte = NULL; (void) jpeg_read_header (&cinfo, TRUE); dest_mgr = sanei_jpeg_jinit_write_ppm (&cinfo); (void) jpeg_start_decompress (&cinfo); row_stride = cinfo.output_width * cinfo.output_components; } Camera.scanning = SANE_TRUE; /* don't overlap scan requests */ total_bytes_read = 0; return SANE_STATUS_GOOD; }
int init_dc20(char *device, speed_t speed) { struct termios tty_new; int tfd, speed_index; fprintf(stderr, "port was %s and speed was %d \n", device, speed); for (speed_index = 0; speed_index < NUM_OF_SPEEDS; speed_index++) { if (speeds[speed_index].baud == speed) { init_pck[2] = speeds[speed_index].pkt_code[0]; init_pck[3] = speeds[speed_index].pkt_code[1]; break; } } fprintf (stderr, "int2 is %x and int 3 is %x \n", init_pck[2], init_pck[3]); if (init_pck[2] == 0) { if (!quiet) fprintf(stderr, "%s: init_dc20: error: unsupported baud rate.\n", __progname); return -1; } /* Open device file. */ if ((tfd = open(device, O_RDWR)) == -1) { if (!quiet) { perror("open"); fprintf(stderr, "%s: init_dc20: error: could not open %s for read/write\n", __progname, device); } return -1; } /* Save old device information to restore when we are done. */ if (tcgetattr(tfd, &tty_orig) == -1) { if (!quiet) { perror("tcgetattr"); fprintf(stderr, "%s: init_dc20: error: could not get attributes\n", __progname); } return -1; } memcpy((char *)&tty_new, (char *)&tty_orig, sizeof(struct termios)); /* We need the device to be raw. 8 bits even parity on 9600 baud to start. */ cfmakeraw(&tty_new); tty_new.c_oflag &= ~CSTOPB; tty_new.c_cflag |= PARENB; tty_new.c_cflag &= ~PARODD; tty_new.c_cc[VMIN] = 0; tty_new.c_cc[VTIME] = 10; cfsetospeed(&tty_new, B9600); cfsetispeed(&tty_new, B9600); if (tcsetattr(tfd, TCSANOW, &tty_new) == -1) { if (!quiet) { perror("tcsetattr"); fprintf(stderr, "%s: init_dc20: error: could not set attributes\n", __progname); } return -1; } if (send_pck(tfd, init_pck) == -1) { /* * Ok, we tried at 9600, but the machine may be at a different speed * Let's try our speed. */ for (speed_index = NUM_OF_SPEEDS-1; speed_index > 0; speed_index--) { if (verbose) printf("%s: init_dc20: changing speed to %d\n", __progname, (int)speeds[speed_index].baud); cfsetospeed(&tty_new, speeds[speed_index].baud); cfsetispeed(&tty_new, speeds[speed_index].baud); if (tcsetattr(tfd, TCSANOW, &tty_new) == -1) { if (!quiet) { perror("tcsetattr"); fprintf(stderr, "%s: init_dc20: error: could not set attributes\n", __progname); } return -1; } if (send_pck(tfd, init_pck) != -1) break; } if (speed_index == 0) { tcsetattr(tfd, TCSANOW, &tty_orig); if (!quiet) fprintf(stderr, "%s: init_dc20: error: no suitable baud rate\n", __progname); return -1; } } /* Set speed to requested speed. Also, make a long timeout (we need this for erase and shoot operations) */ tty_new.c_cc[VTIME] = 150; cfsetospeed(&tty_new, speed); cfsetispeed(&tty_new, speed); if (tcsetattr(tfd, TCSANOW, &tty_new) == -1) { if (!quiet) { perror("tcsetattr"); fprintf(stderr, "%s: init_dc20: error: could not set attributes\n", __progname); } return -1; } fprintf(stderr, "About to return from init_dc20!"); return tfd; }