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