int main(int argc, char *argv[]) { maple_device_t *dreameye; dreameye_state_t *state; uint8 *buf; int size, err; FILE *fp; int img_count, i; char fn[64]; kos_blockdev_t sd_dev; uint8 partition_type; /* We're not using these, obviously... */ (void)argc; (void)argv; /* Comment this out if you'd rather that debug output went to dcload. Of course, you'll need to be using dcload-ip, but you should have already known that. ;-) */ dbgio_dev_select("fb"); printf("KallistiOS Dreameye Image Dump program\n"); printf("Attempting to find a connected Dreameye device...\n"); dreameye = maple_enum_type(0, MAPLE_FUNC_CAMERA); if(!dreameye) { printf("Couldn't find any attached devices, bailing out.\n"); exit(EXIT_FAILURE); } state = (dreameye_state_t *)maple_dev_status(dreameye); printf("Attempting to grab the number of saved images...\n"); dreameye_get_image_count(dreameye, 1); printf("Image Count is %s -- (%d)\n", state->image_count_valid ? "valid" : "invalid", state->image_count); img_count = state->image_count; /* Initialize the low-level SD card stuff. */ if(sd_init()) { printf("Could not initialize the SD card. Please make sure that you " "have an SD card adapter plugged in and an SD card inserted.\n"); exit(EXIT_FAILURE); } /* Grab the block device for the first partition on the SD card. Note that you must have the SD card formatted with an MBR partitioning scheme. */ if(sd_blockdev_for_partition(0, &sd_dev, &partition_type)) { printf("Could not find the first partition on the SD card!\n"); exit(EXIT_FAILURE); } /* Check to see if the MBR says that we have a Linux partition. */ if(partition_type != 0x83) { printf("MBR indicates a non-ext2 filesystem. Will try to mount " "anyway\n"); } /* Initialize fs_ext2 and attempt to mount the device. */ if(fs_ext2_init()) { printf("Could not initialize fs_ext2!\n"); exit(EXIT_FAILURE); } if(fs_ext2_mount("/sd", &sd_dev, FS_EXT2_MOUNT_READWRITE)) { printf("Could not mount SD card as ext2fs. Please make sure the card " "has been properly formatted.\n"); exit(EXIT_FAILURE); } /* Try to make a "dreameye" directory on the root of the card and move to the new directory. */ if(mkdir("/sd/dreameye", 0777)) { printf("Cannot create a dreameye directory: %s\n", strerror(errno)); exit(EXIT_FAILURE); } if(chdir("/sd/dreameye")) { printf("Cannot set current working directory: %s\n", strerror(errno)); exit(EXIT_FAILURE); } for(i = 0; i < img_count; ++i) { printf("Reading image %d...\n", i + 1); err = dreameye_get_image(dreameye, i + 2, &buf, &size); if(err != MAPLE_EOK) { printf("Error was: %d\n", err); free(buf); continue; } printf("Image received successfully, size %d bytes\n", size); sprintf(fn, "image%02d.jpg", i + 1); if(!(fp = fopen(fn, "wb"))) { printf("Cannot open /sd/dreameye/%s: %s\n", fn, strerror(errno)); free(buf); continue; } if(fwrite(buf, 1, size, fp) != (size_t)size) { printf("Cannot write image to file: %s\n", strerror(errno)); free(buf); fclose(fp); continue; } fclose(fp); free(buf); } /* Clean up the filesystem and everything else */ fs_ext2_unmount("/sd"); fs_ext2_shutdown(); sd_shutdown(); printf("Complete!\n"); return 0; }
int builtin_dreameye_cmd(int argc, char *argv[]) { if(argc < 2) { ds_printf("Usage: %s options args...\n" "Options: \n" " -g, --grab -Grab images from dreameye and save to file\n" " -e, --erase -Erase images from dreameye\n" " -c, --count -Get images count\n" " -p, --param -Get param\n\n" "Arguments: \n" " -f, --file -File for save image\n" " -d, --dir -Directory for save all images\n" " -n, --num -Image number for grab or erase (doesn't set it for all)\n\n" "Example: %s -g -n 2 -f /sd/photo.jpg\n" " %s -g -d /sd", argv[0], argv[0], argv[0]); return CMD_NO_ARG; } /* Arguments */ int grab = 0, count = 0, erase = 0, param = 0; char *file = NULL, *dir = NULL; int num = -1; /* Buffers */ char fn[MAX_FN_LEN]; uint8 *buf; int size, err, i; /* Device state */ maple_device_t *dreameye; dreameye_state_t *state; struct cfg_option options[] = { {"grab", 'g', NULL, CFG_BOOL, (void *) &grab, 0}, {"count", 'c', NULL, CFG_BOOL, (void *) &count, 0}, {"erase", 'e', NULL, CFG_BOOL, (void *) &erase, 0}, {"param", 'p', NULL, CFG_BOOL, (void *) ¶m, 0}, {"file", 'f', NULL, CFG_STR, (void *) &file, 0}, {"num", 'n', NULL, CFG_INT, (void *) &num, 0}, {"dir", 'd', NULL, CFG_STR, (void *) &dir, 0}, CFG_END_OF_LIST }; CMD_DEFAULT_ARGS_PARSER(options); dreameye = maple_enum_type(0, MAPLE_FUNC_CAMERA); if(!dreameye) { ds_printf("DS_ERROR: Couldn't find any attached devices, bailing out.\n"); return CMD_ERROR; } state = (dreameye_state_t *)maple_dev_status(dreameye); if(param) { uint16 val; while(++num < 0x20) { dreameye_get_param(dreameye, DREAMEYE_COND_REG_JANGGU, num & 0xff, &val); ds_printf("0x%02x = 0x%02x\n", num & 0xff, val & 0xff); } return CMD_OK; } /* Get count option */ if(count) { ds_printf("DS_PROCESS: Attempting to grab the number of saved images...\n"); dreameye_get_image_count(dreameye, 1); if(state->image_count_valid && state->image_count > 0) { ds_printf("DS_INFO: Count of images: %d\n", state->image_count); } else { ds_printf("DS_INFO: No images avaible.\n"); } } /* Grap data option */ if(grab) { ds_printf("DS_PROCESS: Attempting to grab the %s...\n", (num > -1 ? "image" : "all images")); if(num < 0) { dreameye_get_image_count(dreameye, 1); if(!state->image_count_valid || !state->image_count) { ds_printf("DS_ERROR: No images avaible.\n"); return CMD_ERROR; } for(i = 2; i < state->image_count + 2; i++) { err = dreameye_get_image(dreameye, i, &buf, &size); if(err != MAPLE_EOK) { ds_printf("DS_ERROR: No image data at index: %d\n", i); break; } sprintf(fn, "%s/photo_%d.jpg", dir, i); ds_printf("DS_OK: Image received successfully. Writing %d bytes to %s\n", size, fn); if(save_image(fn, buf, size) < 0) { ds_printf("DS_ERROR: Couldn't save image to %s\n", fn); } free(buf); } } else { if(num < 2) { err = dreameye_get_video_frame(dreameye, num, &buf, &size); } else { err = dreameye_get_image(dreameye, num, &buf, &size); } if(err != MAPLE_EOK) { ds_printf("DS_ERROR: No image data at index: %d\n", num); return CMD_ERROR; } ds_printf("DS_OK: Image received successfully. Writing %d bytes to %s\n", size, file); if(save_image(file, buf, size) < 0) { ds_printf("DS_ERROR: Couldn't save image to %s\n", file); free(buf); return CMD_ERROR; } free(buf); } ds_printf("DS_OK: Complete.\n"); return CMD_OK; } /* Erase option */ if(erase) { if(num < 2 || num > 33) { ds_printf("DS_ERROR: Wrong image index: %d\n", num); return CMD_ERROR; } ds_printf("DS_PROCESS: Erasing %s...\n", (num > 1 ? "image" : "all images")); err = dreameye_erase_image(dreameye, (num > 1 ? num : 0xFF), 1); if(err != MAPLE_EOK) { if(num > -1) { ds_printf("DS_ERROR: Couldn't erase image at index: %d\n", num); } else { ds_printf("DS_ERROR: Couldn't erase all images\n"); } return CMD_ERROR; } ds_printf("DS_OK: Complete.\n"); return CMD_OK; } return (!count ? CMD_NO_ARG : CMD_OK); }