doc: add a man page for am7xxx_mode_switch
[libam7xxx.git] / examples / picoproj.c
1 /* picoproj - test program for libam7xxx
2  *
3  * Copyright (C) 2012  Antonio Ospite <ospite@studenti.unina.it>
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 /**
20  * @example examples/picoproj.c
21  * A minimal example to show how to use libam7xxx to display a static image.
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #include "am7xxx.h"
33
34 static void usage(char *name)
35 {
36         printf("usage: %s [OPTIONS]\n\n", name);
37         printf("OPTIONS:\n");
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);
56 }
57
58 int main(int argc, char *argv[])
59 {
60         int ret;
61         int exit_code = EXIT_SUCCESS;
62         int opt;
63
64         char filename[FILENAME_MAX] = {0};
65         FILE *image_fp;
66         struct stat st;
67         am7xxx_context *ctx;
68         am7xxx_device *dev;
69         int log_level = AM7XXX_LOG_INFO;
70         int device_index = 0;
71         am7xxx_power_mode power_mode = AM7XXX_POWER_LOW;
72         am7xxx_zoom_mode zoom = AM7XXX_ZOOM_ORIGINAL;
73         int format = AM7XXX_IMAGE_FORMAT_JPEG;
74         int width = 800;
75         int height = 480;
76         unsigned char *image;
77         unsigned int size;
78         am7xxx_device_info device_info;
79
80         while ((opt = getopt(argc, argv, "d:f:F:l:p:z:W:H:h")) != -1) {
81                 switch (opt) {
82                 case 'd':
83                         device_index = atoi(optarg);
84                         if (device_index < 0) {
85                                 fprintf(stderr, "Unsupported device index\n");
86                                 exit(EXIT_FAILURE);
87                         }
88                         break;
89                 case 'f':
90                         if (filename[0] != '\0')
91                                 fprintf(stderr, "Warning: image file already specified\n");
92                         strncpy(filename, optarg, FILENAME_MAX);
93                         break;
94                 case 'F':
95                         format = atoi(optarg);
96                         switch(format) {
97                         case AM7XXX_IMAGE_FORMAT_JPEG:
98                                 fprintf(stdout, "JPEG format\n");
99                                 break;
100                         case AM7XXX_IMAGE_FORMAT_NV12:
101                                 fprintf(stdout, "NV12 format\n");
102                                 break;
103                         default:
104                                 fprintf(stderr, "Unsupported format\n");
105                                 exit(EXIT_FAILURE);
106                         }
107                         break;
108                 case 'l':
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;
113                         }
114                         break;
115                 case 'p':
116                         power_mode = atoi(optarg);
117                         switch(power_mode) {
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);
124                                 break;
125                         default:
126                                 fprintf(stderr, "Invalid power mode value, must be between %d and %d\n",
127                                         AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
128                                 exit(EXIT_FAILURE);
129                         }
130                         break;
131                 case 'z':
132                         zoom = atoi(optarg);
133                         switch(zoom) {
134                         case AM7XXX_ZOOM_ORIGINAL:
135                         case AM7XXX_ZOOM_H:
136                         case AM7XXX_ZOOM_H_V:
137                         case AM7XXX_ZOOM_TEST:
138                                 fprintf(stdout, "Zoom: %d\n", zoom);
139                                 break;
140                         default:
141                                 fprintf(stderr, "Invalid zoom mode value, must be between %d and %d\n",
142                                         AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TEST);
143                                 exit(EXIT_FAILURE);
144                         }
145                         break;
146                 case 'W':
147                         width = atoi(optarg);
148                         if (width < 0) {
149                                 fprintf(stderr, "Unsupported width\n");
150                                 exit(EXIT_FAILURE);
151                         }
152                         break;
153                 case 'H':
154                         height = atoi(optarg);
155                         if (height < 0) {
156                                 fprintf(stderr, "Unsupported height\n");
157                                 exit(EXIT_FAILURE);
158                         }
159                         break;
160                 case 'h':
161                         usage(argv[0]);
162                         exit(EXIT_SUCCESS);
163                         break;
164                 default: /* '?' */
165                         usage(argv[0]);
166                         exit(EXIT_FAILURE);
167                 }
168         }
169
170         if (filename[0] == '\0') {
171                 fprintf(stderr, "An image file MUST be specified.\n");
172                 exit_code = EXIT_FAILURE;
173                 goto out;
174         }
175
176         image_fp = fopen(filename, "rb");
177         if (image_fp == NULL) {
178                 perror("fopen");
179                 exit_code = EXIT_FAILURE;
180                 goto out;
181         }
182         if (fstat(fileno(image_fp), &st) < 0) {
183                 perror("fstat");
184                 exit_code = EXIT_FAILURE;
185                 goto out_close_image_fp;
186         }
187         size = st.st_size;
188
189         image = malloc(size * sizeof(unsigned char));
190         if (image == NULL) {
191                 perror("malloc");
192                 exit_code = EXIT_FAILURE;
193                 goto out_close_image_fp;
194         }
195
196         ret = fread(image, size, 1, image_fp);
197         if (ret != 1) {
198                 if (feof(image_fp))
199                         fprintf(stderr, "Unexpected end of file.\n");
200                 else if (ferror(image_fp))
201                         perror("fread");
202                 else
203                         fprintf(stderr, "Unexpected error condition.\n");
204
205                 goto out_free_image;
206         }
207
208         ret = am7xxx_init(&ctx);
209         if (ret < 0) {
210                 perror("am7xxx_init");
211                 exit_code = EXIT_FAILURE;
212                 goto out_free_image;
213         }
214
215         am7xxx_set_log_level(ctx, log_level);
216
217         ret = am7xxx_open_device(ctx, &dev, 0);
218         if (ret < 0) {
219                 perror("am7xxx_open_device");
220                 exit_code = EXIT_FAILURE;
221                 goto cleanup;
222         }
223
224
225         ret = am7xxx_close_device(dev);
226         if (ret < 0) {
227                 perror("am7xxx_close_device");
228                 exit_code = EXIT_FAILURE;
229                 goto cleanup;
230         }
231
232         ret = am7xxx_open_device(ctx, &dev, device_index);
233         if (ret < 0) {
234                 perror("am7xxx_open_device");
235                 exit_code = EXIT_FAILURE;
236                 goto cleanup;
237         }
238
239         ret = am7xxx_get_device_info(dev, &device_info);
240         if (ret < 0) {
241                 perror("am7xxx_get_device_info");
242                 exit_code = EXIT_FAILURE;
243                 goto cleanup;
244         }
245         printf("Native resolution: %dx%d\n",
246                device_info.native_width, device_info.native_height);
247
248         ret = am7xxx_set_zoom_mode(dev, zoom);
249         if (ret < 0) {
250                 perror("am7xxx_set_zoom_mode");
251                 exit_code = EXIT_FAILURE;
252                 goto cleanup;
253         }
254
255         ret = am7xxx_set_power_mode(dev, power_mode);
256         if (ret < 0) {
257                 perror("am7xxx_set_power_mode");
258                 exit_code = EXIT_FAILURE;
259                 goto cleanup;
260         }
261
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");
265                 goto cleanup;
266         }
267
268
269         if ((unsigned int)width > device_info.native_width ||
270             (unsigned int)height > device_info.native_height)
271                 fprintf(stderr, "WARNING: image not fitting the native resolution, it may be displayed wrongly!\n");
272
273         ret = am7xxx_send_image(dev, format, width, height, image, size);
274         if (ret < 0) {
275                 perror("am7xxx_send_image");
276                 exit_code = EXIT_FAILURE;
277                 goto cleanup;
278         }
279
280         exit_code = EXIT_SUCCESS;
281
282 cleanup:
283         am7xxx_shutdown(ctx);
284
285 out_free_image:
286         free(image);
287
288 out_close_image_fp:
289         ret = fclose(image_fp);
290         if (ret == EOF)
291                 perror("fclose");
292
293 out:
294         exit(exit_code);
295 }