8af2fbc93c120960a19cc9c31b1bb139355165eb
[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 show a static image.
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32
33 #include "am7xxx.h"
34
35 static void usage(char *name)
36 {
37         printf("usage: %s [OPTIONS]\n\n", name);
38         printf("OPTIONS:\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-W <image width>\tthe width of the image to upload\n");
46         printf("\t-H <image height>\tthe height of the image to upload\n");
47         printf("\t-h \t\t\tthis help message\n");
48         printf("\n\nEXAMPLE OF USE:\n");
49         printf("\t%s -f file.jpg -F 1 -l 5 -W 800 -H 480\n", name);
50 }
51
52 int main(int argc, char *argv[])
53 {
54         int ret;
55         int exit_code = EXIT_SUCCESS;
56         int opt;
57
58         char filename[FILENAME_MAX] = {0};
59         int image_fd;
60         struct stat st;
61         am7xxx_context *ctx;
62         am7xxx_device *dev;
63         int log_level = AM7XXX_LOG_INFO;
64         int format = AM7XXX_IMAGE_FORMAT_JPEG;
65         int width = 800;
66         int height = 480;
67         unsigned char *image;
68         unsigned int size;
69         am7xxx_device_info device_info;
70
71         while ((opt = getopt(argc, argv, "f:F:l:W:H:h")) != -1) {
72                 switch (opt) {
73                 case 'f':
74                         strncpy(filename, optarg, FILENAME_MAX);
75                         break;
76                 case 'F':
77                         format = atoi(optarg);
78                         switch(format) {
79                         case AM7XXX_IMAGE_FORMAT_JPEG:
80                                 fprintf(stdout, "JPEG format\n");
81                                 break;
82                         case AM7XXX_IMAGE_FORMAT_NV12:
83                                 fprintf(stdout, "NV12 format\n");
84                                 break;
85                         default:
86                                 fprintf(stderr, "Unsupported format\n");
87                                 exit(EXIT_FAILURE);
88                         }
89                         break;
90                 case 'l':
91                         log_level = atoi(optarg);
92                         if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
93                                 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
94                                 log_level = AM7XXX_LOG_ERROR;
95                         }
96                         break;
97                 case 'W':
98                         width = atoi(optarg);
99                         if (width < 0) {
100                                 fprintf(stderr, "Unsupported width\n");
101                                 exit(EXIT_FAILURE);
102                         }
103                         break;
104                 case 'H':
105                         height = atoi(optarg);
106                         if (height < 0) {
107                                 fprintf(stderr, "Unsupported height\n");
108                                 exit(EXIT_FAILURE);
109                         }
110                         break;
111                 case 'h':
112                         usage(argv[0]);
113                         exit(EXIT_SUCCESS);
114                         break;
115                 default: /* '?' */
116                         usage(argv[0]);
117                         exit(EXIT_FAILURE);
118                 }
119         }
120
121         if (filename[0] == '\0') {
122                 fprintf(stderr, "An image file MUST be specified.\n");
123                 exit_code = EXIT_FAILURE;
124                 goto out;
125         }
126
127         image_fd = open(filename, O_RDONLY);
128         if (image_fd < 0) {
129                 perror("open");
130                 exit_code = EXIT_FAILURE;
131                 goto out;
132         }
133         if (fstat(image_fd, &st) < 0) {
134                 perror("fstat");
135                 exit_code = EXIT_FAILURE;
136                 goto out_close_image_fd;
137         }
138         size = st.st_size;
139
140         image = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, image_fd, 0);
141         if (image == NULL) {
142                 perror("mmap");
143                 exit_code = EXIT_FAILURE;
144                 goto out_close_image_fd;
145         }
146
147         ret = am7xxx_init(&ctx);
148         if (ret < 0) {
149                 perror("am7xxx_init");
150                 exit_code = EXIT_FAILURE;
151                 goto out_munmap;
152         }
153
154         am7xxx_set_log_level(ctx, log_level);
155
156         ret = am7xxx_open_device(ctx, &dev, 0);
157         if (ret < 0) {
158                 perror("am7xxx_open_device");
159                 exit_code = EXIT_FAILURE;
160                 goto cleanup;
161         }
162
163
164         ret = am7xxx_close_device(dev);
165         if (ret < 0) {
166                 perror("am7xxx_close_device");
167                 exit_code = EXIT_FAILURE;
168                 goto cleanup;
169         }
170
171         ret = am7xxx_open_device(ctx, &dev, 0);
172         if (ret < 0) {
173                 perror("am7xxx_open_device");
174                 exit_code = EXIT_FAILURE;
175                 goto cleanup;
176         }
177
178         ret = am7xxx_get_device_info(dev, &device_info);
179         if (ret < 0) {
180                 perror("am7xxx_get_info");
181                 exit_code = EXIT_FAILURE;
182                 goto cleanup;
183         }
184         printf("Native resolution: %dx%d\n",
185                device_info.native_width, device_info.native_height);
186
187         ret = am7xxx_set_power_mode(dev, AM7XXX_POWER_LOW);
188         if (ret < 0) {
189                 perror("am7xxx_set_power_mode");
190                 exit_code = EXIT_FAILURE;
191                 goto cleanup;
192         }
193
194         ret = am7xxx_send_image(dev, format, width, height, image, size);
195         if (ret < 0) {
196                 perror("am7xxx_send_image");
197                 exit_code = EXIT_FAILURE;
198                 goto cleanup;
199         }
200
201         exit_code = EXIT_SUCCESS;
202
203 cleanup:
204         am7xxx_shutdown(ctx);
205
206 out_munmap:
207         ret = munmap(image, size);
208         if (ret < 0)
209                 perror("munmap");
210
211 out_close_image_fd:
212         ret = close(image_fd);
213         if (ret < 0)
214                 perror("close");
215
216 out:
217         exit(exit_code);
218 }