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