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