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