diff options
author | Marcus Meissner <marcus@jet.franken.de> | 2008-10-10 19:08:33 +0000 |
---|---|---|
committer | Marcus Meissner <marcus@jet.franken.de> | 2008-10-10 19:08:33 +0000 |
commit | 314fcced1c96bf60ff035398c0e24a1c7cd1d693 (patch) | |
tree | 19e93c880ad92b17dd5152188a5ecb34ef67ee9f /examples | |
parent | 9d1cf608c67b718345acf0b69b7375b08712314e (diff) | |
download | libgphoto2-314fcced1c96bf60ff035398c0e24a1c7cd1d693.tar.gz |
New file config.c for configuration helpers.
Use it for lunkwill-canon-capture.c
Owner setting and reporting (sample-owner)
Very basic hello world program now in sample-autodetect,
multi camrea program in sample-multi-detect.
README updated
git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@11372 67ed7778-7388-44ab-90cf-0a291f65f57c
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 8 | ||||
-rw-r--r-- | examples/README | 27 | ||||
-rw-r--r-- | examples/lunkwill-canon-capture.c | 87 | ||||
-rw-r--r-- | examples/sample-autodetect.c | 69 | ||||
-rw-r--r-- | examples/samples.h | 4 |
5 files changed, 84 insertions, 111 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am index a5268f19c..be50d1050 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -21,10 +21,12 @@ INSTALL_TESTS_ENVIRONMENT = env \ CLEANFILES = $(check_SCRIPTS) -noinst_PROGRAMS += sample-autodetect lunkwill-canon-capture -sample_autodetect_SOURCES = sample-autodetect.c samples.h autodetect.c context.c +noinst_PROGRAMS += sample-autodetect sample-multi-detect lunkwill-canon-capture sample-owner -lunkwill_canon_capture_SOURCES = lunkwill-canon-capture.c +sample_autodetect_SOURCES = sample-autodetect.c samples.h context.c config.c +sample_owner_SOURCES = sample-owner.c samples.h context.c config.c +sample_multi_detect_SOURCES = sample-multi-detect.c samples.h autodetect.c context.c config.c +lunkwill_canon_capture_SOURCES = lunkwill-canon-capture.c context.c samples.h config.c LDADD = $(top_builddir)/libgphoto2/libgphoto2.la \ $(top_builddir)/libgphoto2_port/libgphoto2_port/libgphoto2_port.la \ diff --git a/examples/README b/examples/README index 1b85fabd7..936a199f8 100644 --- a/examples/README +++ b/examples/README @@ -3,9 +3,34 @@ Sample snippets for programmatic camera usage. This directory contains sample snippets for your C / C++ program accessing cameras for various purposes. +Files with helper routines: + autodetect.c: Auto detecting multiple cameras and initializing + specific cameras. + + config.c: Querying and setting configuration values. + + context.c: Handling of GPContext for error reporting and so on. + + samples.h: Header file for above helper files. + +Files with samples: + sample-autodetect.c: Simple camera autodetection and simple API + calls. + + sample-multi-detect.c: Same as above, but autodetect and use all + cameras. + + sample-owner.c: Gets or Sets the Camera Owner. Demonstrates string + variable configuration handling. + + lunkwill-canon-capture.c: A simple capture program that captures + a single image, retrieves it to disk, + and removes it from the camera. + + If you find a common thing to do missing, please report them (or do them yourselves and send patches). -Please do not keep it for yourself. +Please do not keep good sample code for yourself. Marcus Meissner <marcus@jet.franken.de> diff --git a/examples/lunkwill-canon-capture.c b/examples/lunkwill-canon-capture.c index 4e215050d..55cdaee01 100644 --- a/examples/lunkwill-canon-capture.c +++ b/examples/lunkwill-canon-capture.c @@ -20,69 +20,14 @@ #include <string.h> #include <gphoto2/gphoto2.h> +#include "samples.h" + static void errordumper(GPLogLevel level, const char *domain, const char *format, va_list args, void *data) { - - vfprintf(stdout, format, args); fprintf(stdout, "\n"); } -static void enable_capture(Camera *canon, GPContext *canoncontext) { - int retval; - CameraWidget *rootconfig; /* okay, not really */ - CameraWidget *actualrootconfig; - CameraWidget *child; - CameraWidget *capture = child; - const char *widgetinfo; - const char *widgetlabel; - int widgetid; - CameraWidgetType widgettype; - const int one=1; - - printf("Get root config.\n"); - - retval = gp_camera_get_config(canon, &rootconfig, canoncontext); - actualrootconfig = rootconfig; - printf(" Retval: %d\n", retval); - - printf("Get main config.\n"); - retval = gp_widget_get_child_by_name(rootconfig, "main", &child); - printf(" Retval: %d\n", retval); - - printf("Get settings config.\n"); - rootconfig = child; - retval = gp_widget_get_child_by_name(rootconfig, "settings", &child); - printf(" Retval: %d\n", retval); - - printf("Get capture config.\n"); - rootconfig = child; - retval = gp_widget_get_child_by_name(rootconfig, "capture", &child); - printf(" Retval: %d\n", retval); - - - gp_widget_get_name(capture, &widgetinfo); - printf("config name: %s\n", widgetinfo ); - - gp_widget_get_label(capture, &widgetlabel); - printf("config label: %s\n", widgetlabel); - - gp_widget_get_id(capture, &widgetid); - printf("config id: %d\n", widgetid); - - gp_widget_get_type(capture, &widgettype); - printf("config type: %d == %d \n", widgettype, GP_WIDGET_TOGGLE); - - printf("Set value.\n"); - - retval = gp_widget_set_value(capture, &one); - printf(" Retval: %d\n", retval); - - printf("Enabling capture.\n"); - retval = gp_camera_set_config(canon, actualrootconfig, canoncontext); - printf(" Retval: %d\n", retval); -} - /* This seems to have no effect on where images go void set_capturetarget(Camera *canon, GPContext *canoncontext) { int retval; @@ -145,10 +90,8 @@ void set_capturetarget(Camera *canon, GPContext *canoncontext) { static void capture_to_file(Camera *canon, GPContext *canoncontext, char *fn) { - int retval; + int fd, retval; CameraFile *canonfile; - const char *filedata; - unsigned long int filesize; CameraFilePath camera_file_path; printf("Capturing.\n"); @@ -162,19 +105,13 @@ capture_to_file(Camera *canon, GPContext *canoncontext, char *fn) { printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name); - retval = gp_file_new(&canonfile); + fd = open(fn, O_CREAT | O_WRONLY, 0644); + retval = gp_file_new_from_fd(&canonfile, fd); printf(" Retval: %d\n", retval); retval = gp_camera_file_get(canon, camera_file_path.folder, camera_file_path.name, GP_FILE_TYPE_NORMAL, canonfile, canoncontext); printf(" Retval: %d\n", retval); - retval = gp_file_get_data_and_size(canonfile, &filedata, &filesize); - printf(" Retval: %d\n", retval); - - int fd = open(fn, O_CREAT | O_WRONLY, 0644); - write(fd, filedata, filesize); - close(fd); - printf("Deleting.\n"); retval = gp_camera_file_delete(canon, camera_file_path.folder, camera_file_path.name, canoncontext); @@ -185,11 +122,11 @@ capture_to_file(Camera *canon, GPContext *canoncontext, char *fn) { int main(int argc, char **argv) { - Camera *canon; - int retval; + Camera *canon; + int retval; + GPContext *canoncontext = sample_create_context(); gp_log_add_func(GP_LOG_ERROR, errordumper, NULL); - GPContext *canoncontext = gp_context_new(); gp_camera_new(&canon); /* When I set GP_LOG_DEBUG instead of GP_LOG_ERROR above, I noticed that the @@ -199,9 +136,11 @@ main(int argc, char **argv) { */ printf("Camera init. Takes about 10 seconds.\n"); retval = gp_camera_init(canon, canoncontext); - printf(" Retval: %d\n", retval); - - enable_capture(canon, canoncontext); + if (retval != GP_OK) { + printf(" Retval: %d\n", retval); + exit (1); + } + canon_enable_capture(canon, TRUE, canoncontext); /*set_capturetarget(canon, canoncontext);*/ capture_to_file(canon, canoncontext, "foo.jpg"); gp_camera_exit(canon, canoncontext); diff --git a/examples/sample-autodetect.c b/examples/sample-autodetect.c index a2f53abf9..a349737b6 100644 --- a/examples/sample-autodetect.c +++ b/examples/sample-autodetect.c @@ -1,5 +1,6 @@ #include <stdio.h> #include <string.h> +#include <stdlib.h> #include <gphoto2/gphoto2-camera.h> @@ -7,46 +8,48 @@ /* Sample autodetection program. * - * This program can autodetect multiple cameras and then calls a - * simple function in each of them (summary). + * This program can autodetect a single camera and then calls a + * simple function in it (summary). */ int main(int argc, char **argv) { - CameraList *list; - Camera **cams; - int ret, i, count; - const char *name, *value; + Camera *camera; + int ret; + char *owner; GPContext *context; + CameraText text; context = sample_create_context (); /* see context.c */ + gp_camera_new (&camera); + + /* This call will autodetect cameras, take the + * first one from the list and use it. It will ignore + * any others... See the *multi* examples on how to + * detect and use more than the first one. + */ + ret = gp_camera_init (camera, context); + if (ret < GP_OK) { + printf("No camera auto detected.\n"); + gp_camera_free (camera); + return 0; + } + + /* Simple query the camera summary text */ + ret = gp_camera_get_summary (camera, &text, context); + if (ret < GP_OK) { + printf("Camera failed retrieving summary.\n"); + gp_camera_free (camera); + return 0; + } + printf("Summary:\n%s\n", text.text); - /* Detect all the cameras that can be autodetected... */ - ret = gp_list_new (&list); - if (ret < GP_OK) return 1; - count = sample_autodetect (list, context); - - /* Now open all cameras we autodected for usage */ - printf("Number of cameras: %d\n", count); - cams = calloc (sizeof (Camera*),count); - for (i = 0; i < count; i++) { - gp_list_get_name (list, i, &name); - gp_list_get_value (list, i, &value); - ret = sample_open_camera (&cams[i], name, value); - if (ret < GP_OK) fprintf(stderr,"Camera %s on port %s failed to open\n", name, value); - } - /* Now call a simple function in each of those cameras. */ - for (i = 0; i < count; i++) { - CameraText text; - ret = gp_camera_get_summary (cams[i], &text, context); - if (ret < GP_OK) { - fprintf (stderr, "Failed to get summary.\n"); - continue; - } - - gp_list_get_name (list, i, &name); - gp_list_get_value (list, i, &value); - printf("%-30s %-16s\n", name, value); - printf("Summary:\n%s\n", text.text); + /* Simple query of a string configuration variable. */ + ret = get_config_value_string (camera, "owner", &owner, context); + if (ret >= GP_OK) { + printf("Owner: %s\n", owner); + free (owner); } + gp_camera_exit (camera, context); + gp_camera_free (camera); return 0; } diff --git a/examples/samples.h b/examples/samples.h index 8d80881b5..7827e9a13 100644 --- a/examples/samples.h +++ b/examples/samples.h @@ -5,4 +5,8 @@ extern int sample_autodetect (CameraList *list, GPContext *context); extern int sample_open_camera (Camera ** camera, const char *model, const char *port); extern GPContext* sample_create_context(void); + +extern int get_config_value_string (Camera *, const char *, char **, GPContext *); +extern int set_config_value_string (Camera *, const char *, const char *, GPContext *); +int canon_enable_capture (Camera *camera, int onoff, GPContext *context); #endif |