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