1 /* picoproj - test program for libam7xxx
3 * Copyright (C) 2012 Antonio Ospite <ospite@studenti.unina.it>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @example examples/picoproj.c
21 * A minimal example to show how to use libam7xxx to display a static image.
27 #include <sys/types.h>
34 static void usage(char *name)
36 printf("usage: %s [OPTIONS]\n\n", name);
38 printf("\t-d <index>\t\tthe device index (default is 0)\n");
39 printf("\t-f <filename>\t\tthe image file to upload\n");
40 printf("\t-F <format>\t\tthe image format to use (default is JPEG)\n");
41 printf("\t\t\t\tSUPPORTED FORMATS:\n");
42 printf("\t\t\t\t\t1 - JPEG\n");
43 printf("\t\t\t\t\t2 - NV12\n");
44 printf("\t-l <log level>\t\tthe verbosity level of libam7xxx output (0-5)\n");
45 printf("\t-p <power mode>\t\tthe power mode of device, between %d (off) and %d (turbo)\n",
46 AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
47 printf("\t\t\t\tWARNING: Level 2 and greater require the master AND\n");
48 printf("\t\t\t\t the slave connector to be plugged in.\n");
49 printf("\t-z <zoom mode>\t\tthe display zoom mode, between %d (original) and %d (test)\n",
50 AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TEST);
51 printf("\t-W <image width>\tthe width of the image to upload\n");
52 printf("\t-H <image height>\tthe height of the image to upload\n");
53 printf("\t-h \t\t\tthis help message\n");
54 printf("\n\nEXAMPLE OF USE:\n");
55 printf("\t%s -f file.jpg -F 1 -l 5 -W 800 -H 480\n", name);
58 int main(int argc, char *argv[])
61 int exit_code = EXIT_SUCCESS;
64 char filename[FILENAME_MAX] = {0};
69 int log_level = AM7XXX_LOG_INFO;
71 am7xxx_power_mode power_mode = AM7XXX_POWER_LOW;
72 am7xxx_zoom_mode zoom = AM7XXX_ZOOM_ORIGINAL;
73 int format = AM7XXX_IMAGE_FORMAT_JPEG;
78 am7xxx_device_info device_info;
80 while ((opt = getopt(argc, argv, "d:f:F:l:p:z:W:H:h")) != -1) {
83 device_index = atoi(optarg);
84 if (device_index < 0) {
85 fprintf(stderr, "Unsupported device index\n");
90 if (filename[0] != '\0')
91 fprintf(stderr, "Warning: image file already specified\n");
92 strncpy(filename, optarg, FILENAME_MAX);
95 format = atoi(optarg);
97 case AM7XXX_IMAGE_FORMAT_JPEG:
98 fprintf(stdout, "JPEG format\n");
100 case AM7XXX_IMAGE_FORMAT_NV12:
101 fprintf(stdout, "NV12 format\n");
104 fprintf(stderr, "Unsupported format\n");
109 log_level = atoi(optarg);
110 if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
111 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
112 log_level = AM7XXX_LOG_ERROR;
116 power_mode = atoi(optarg);
118 case AM7XXX_POWER_OFF:
119 case AM7XXX_POWER_LOW:
120 case AM7XXX_POWER_MIDDLE:
121 case AM7XXX_POWER_HIGH:
122 case AM7XXX_POWER_TURBO:
123 fprintf(stdout, "Power mode: %d\n", power_mode);
126 fprintf(stderr, "Invalid power mode value, must be between %d and %d\n",
127 AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
134 case AM7XXX_ZOOM_ORIGINAL:
136 case AM7XXX_ZOOM_H_V:
137 case AM7XXX_ZOOM_TEST:
138 fprintf(stdout, "Zoom: %d\n", zoom);
141 fprintf(stderr, "Invalid zoom mode value, must be between %d and %d\n",
142 AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TEST);
147 width = atoi(optarg);
149 fprintf(stderr, "Unsupported width\n");
154 height = atoi(optarg);
156 fprintf(stderr, "Unsupported height\n");
170 if (filename[0] == '\0') {
171 fprintf(stderr, "An image file MUST be specified with the -f option.\n\n");
173 exit_code = EXIT_FAILURE;
177 image_fp = fopen(filename, "rb");
178 if (image_fp == NULL) {
180 exit_code = EXIT_FAILURE;
183 if (fstat(fileno(image_fp), &st) < 0) {
185 exit_code = EXIT_FAILURE;
186 goto out_close_image_fp;
190 image = malloc(size * sizeof(unsigned char));
193 exit_code = EXIT_FAILURE;
194 goto out_close_image_fp;
197 ret = fread(image, size, 1, image_fp);
200 fprintf(stderr, "Unexpected end of file.\n");
201 else if (ferror(image_fp))
204 fprintf(stderr, "Unexpected error condition.\n");
209 ret = am7xxx_init(&ctx);
211 perror("am7xxx_init");
212 exit_code = EXIT_FAILURE;
216 am7xxx_set_log_level(ctx, log_level);
218 ret = am7xxx_open_device(ctx, &dev, 0);
220 perror("am7xxx_open_device");
221 exit_code = EXIT_FAILURE;
225 ret = am7xxx_close_device(dev);
227 perror("am7xxx_close_device");
228 exit_code = EXIT_FAILURE;
232 ret = am7xxx_open_device(ctx, &dev, device_index);
234 perror("am7xxx_open_device");
235 exit_code = EXIT_FAILURE;
239 ret = am7xxx_get_device_info(dev, &device_info);
241 perror("am7xxx_get_device_info");
242 exit_code = EXIT_FAILURE;
245 printf("Native resolution: %dx%d\n",
246 device_info.native_width, device_info.native_height);
248 ret = am7xxx_set_zoom_mode(dev, zoom);
250 perror("am7xxx_set_zoom_mode");
251 exit_code = EXIT_FAILURE;
255 ret = am7xxx_set_power_mode(dev, power_mode);
257 perror("am7xxx_set_power_mode");
258 exit_code = EXIT_FAILURE;
262 /* When setting AM7XXX_ZOOM_TEST don't display the actual image */
263 if (zoom == AM7XXX_ZOOM_TEST) {
264 printf("AM7XXX_ZOOM_TEST requested, not sending actual image.\n");
268 if ((unsigned int)width > device_info.native_width ||
269 (unsigned int)height > device_info.native_height)
271 "WARNING: image is %dx%d, not fitting the native resolution, it may be displayed wrongly!\n",
274 ret = am7xxx_send_image(dev, format, width, height, image, size);
276 perror("am7xxx_send_image");
277 exit_code = EXIT_FAILURE;
281 exit_code = EXIT_SUCCESS;
284 am7xxx_shutdown(ctx);
290 ret = fclose(image_fp);