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