54cbedf5df5c8b772072b4dc43fc192f172d08d4
[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-f <filename>\t\tthe image file to upload\n");
39         printf("\t-F <format>\t\tthe image format to use (default is JPEG)\n");
40         printf("\t\t\t\tSUPPORTED FORMATS:\n");
41         printf("\t\t\t\t\t1 - JPEG\n");
42         printf("\t\t\t\t\t2 - NV12\n");
43         printf("\t-l <log level>\t\tthe verbosity level of libam7xxx output (0-5)\n");
44         printf("\t-p <power mode>\t\tthe power mode of device, between %d (off) and %d (turbo)\n",
45                AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
46         printf("\t\t\t\tWARNING: Level 2 and greater require the master AND\n");
47         printf("\t\t\t\t         the slave connector to be plugged in.\n");
48         printf("\t-z <zoom mode>\t\tthe display zoom mode, between %d (original) and %d (test)\n",
49                AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TEST);
50         printf("\t-W <image width>\tthe width of the image to upload\n");
51         printf("\t-H <image height>\tthe height of the image to upload\n");
52         printf("\t-h \t\t\tthis help message\n");
53         printf("\n\nEXAMPLE OF USE:\n");
54         printf("\t%s -f file.jpg -F 1 -l 5 -W 800 -H 480\n", name);
55 }
56
57 int main(int argc, char *argv[])
58 {
59         int ret;
60         int exit_code = EXIT_SUCCESS;
61         int opt;
62
63         char filename[FILENAME_MAX] = {0};
64         FILE *image_fp;
65         struct stat st;
66         am7xxx_context *ctx;
67         am7xxx_device *dev;
68         int log_level = AM7XXX_LOG_INFO;
69         am7xxx_power_mode power_mode = AM7XXX_POWER_LOW;
70         am7xxx_zoom_mode zoom = AM7XXX_ZOOM_ORIGINAL;
71         int format = AM7XXX_IMAGE_FORMAT_JPEG;
72         int width = 800;
73         int height = 480;
74         unsigned char *image;
75         unsigned int size;
76         am7xxx_device_info device_info;
77
78         while ((opt = getopt(argc, argv, "f:F:l:p:z:W:H:h")) != -1) {
79                 switch (opt) {
80                 case 'f':
81                         if (filename[0] != '\0')
82                                 fprintf(stderr, "Warning: image file already specified\n");
83                         strncpy(filename, optarg, FILENAME_MAX);
84                         break;
85                 case 'F':
86                         format = atoi(optarg);
87                         switch(format) {
88                         case AM7XXX_IMAGE_FORMAT_JPEG:
89                                 fprintf(stdout, "JPEG format\n");
90                                 break;
91                         case AM7XXX_IMAGE_FORMAT_NV12:
92                                 fprintf(stdout, "NV12 format\n");
93                                 break;
94                         default:
95                                 fprintf(stderr, "Unsupported format\n");
96                                 exit(EXIT_FAILURE);
97                         }
98                         break;
99                 case 'l':
100                         log_level = atoi(optarg);
101                         if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
102                                 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
103                                 log_level = AM7XXX_LOG_ERROR;
104                         }
105                         break;
106                 case 'p':
107                         power_mode = atoi(optarg);
108                         switch(power_mode) {
109                         case AM7XXX_POWER_OFF:
110                         case AM7XXX_POWER_LOW:
111                         case AM7XXX_POWER_MIDDLE:
112                         case AM7XXX_POWER_HIGH:
113                         case AM7XXX_POWER_TURBO:
114                                 fprintf(stdout, "Power mode: %d\n", power_mode);
115                                 break;
116                         default:
117                                 fprintf(stderr, "Invalid power mode value, must be between %d and %d\n",
118                                         AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
119                                 exit(EXIT_FAILURE);
120                         }
121                         break;
122                 case 'z':
123                         zoom = atoi(optarg);
124                         switch(zoom) {
125                         case AM7XXX_ZOOM_ORIGINAL:
126                         case AM7XXX_ZOOM_H:
127                         case AM7XXX_ZOOM_H_V:
128                         case AM7XXX_ZOOM_TEST:
129                                 fprintf(stdout, "Zoom: %d\n", zoom);
130                                 break;
131                         default:
132                                 fprintf(stderr, "Invalid zoom mode value, must be between %d and %d\n",
133                                         AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TEST);
134                                 exit(EXIT_FAILURE);
135                         }
136                         break;
137                 case 'W':
138                         width = atoi(optarg);
139                         if (width < 0) {
140                                 fprintf(stderr, "Unsupported width\n");
141                                 exit(EXIT_FAILURE);
142                         }
143                         break;
144                 case 'H':
145                         height = atoi(optarg);
146                         if (height < 0) {
147                                 fprintf(stderr, "Unsupported height\n");
148                                 exit(EXIT_FAILURE);
149                         }
150                         break;
151                 case 'h':
152                         usage(argv[0]);
153                         exit(EXIT_SUCCESS);
154                         break;
155                 default: /* '?' */
156                         usage(argv[0]);
157                         exit(EXIT_FAILURE);
158                 }
159         }
160
161         if (filename[0] == '\0') {
162                 fprintf(stderr, "An image file MUST be specified.\n");
163                 exit_code = EXIT_FAILURE;
164                 goto out;
165         }
166
167         image_fp = fopen(filename, "rb");
168         if (image_fp == NULL) {
169                 perror("fopen");
170                 exit_code = EXIT_FAILURE;
171                 goto out;
172         }
173         if (fstat(fileno(image_fp), &st) < 0) {
174                 perror("fstat");
175                 exit_code = EXIT_FAILURE;
176                 goto out_close_image_fp;
177         }
178         size = st.st_size;
179
180         image = malloc(size * sizeof(unsigned char));
181         if (image == NULL) {
182                 perror("malloc");
183                 exit_code = EXIT_FAILURE;
184                 goto out_close_image_fp;
185         }
186
187         ret = fread(image, size, 1, image_fp);
188         if (ret != 1) {
189                 if (feof(image_fp))
190                         fprintf(stderr, "Unexpected end of file.\n");
191                 else if (ferror(image_fp))
192                         perror("fread");
193                 else
194                         fprintf(stderr, "Unexpected error condition.\n");
195
196                 goto out_free_image;
197         }
198
199         ret = am7xxx_init(&ctx);
200         if (ret < 0) {
201                 perror("am7xxx_init");
202                 exit_code = EXIT_FAILURE;
203                 goto out_free_image;
204         }
205
206         am7xxx_set_log_level(ctx, log_level);
207
208         ret = am7xxx_open_device(ctx, &dev, 0);
209         if (ret < 0) {
210                 perror("am7xxx_open_device");
211                 exit_code = EXIT_FAILURE;
212                 goto cleanup;
213         }
214
215
216         ret = am7xxx_close_device(dev);
217         if (ret < 0) {
218                 perror("am7xxx_close_device");
219                 exit_code = EXIT_FAILURE;
220                 goto cleanup;
221         }
222
223         ret = am7xxx_open_device(ctx, &dev, 0);
224         if (ret < 0) {
225                 perror("am7xxx_open_device");
226                 exit_code = EXIT_FAILURE;
227                 goto cleanup;
228         }
229
230         ret = am7xxx_get_device_info(dev, &device_info);
231         if (ret < 0) {
232                 perror("am7xxx_get_device_info");
233                 exit_code = EXIT_FAILURE;
234                 goto cleanup;
235         }
236         printf("Native resolution: %dx%d\n",
237                device_info.native_width, device_info.native_height);
238
239         ret = am7xxx_set_zoom_mode(dev, zoom);
240         if (ret < 0) {
241                 perror("am7xxx_set_zoom_mode");
242                 exit_code = EXIT_FAILURE;
243                 goto cleanup;
244         }
245
246         ret = am7xxx_set_power_mode(dev, power_mode);
247         if (ret < 0) {
248                 perror("am7xxx_set_power_mode");
249                 exit_code = EXIT_FAILURE;
250                 goto cleanup;
251         }
252
253         /* When setting AM7XXX_ZOOM_TEST don't display the actual image */
254         if (zoom == AM7XXX_ZOOM_TEST)
255                 goto cleanup;
256
257
258         if ((unsigned int)width > device_info.native_width ||
259             (unsigned int)height > device_info.native_height)
260                 fprintf(stderr, "WARNING: image not fitting the native resolution, it may be displayed wrongly!\n");
261
262         ret = am7xxx_send_image(dev, format, width, height, image, size);
263         if (ret < 0) {
264                 perror("am7xxx_send_image");
265                 exit_code = EXIT_FAILURE;
266                 goto cleanup;
267         }
268
269         exit_code = EXIT_SUCCESS;
270
271 cleanup:
272         am7xxx_shutdown(ctx);
273
274 out_free_image:
275         free(image);
276
277 out_close_image_fp:
278         ret = fclose(image_fp);
279         if (ret == EOF)
280                 perror("fclose");
281
282 out:
283         exit(exit_code);
284 }