1 /* picoproj - test program for libam7xxx
 
   3  * Copyright (C) 2012  Antonio Ospite <ospite@studenti.unina.it>
 
   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.
 
  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.
 
  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/>.
 
  20  * @example examples/picoproj.c
 
  21  * A minimal example to show how to use libam7xxx to display a static image.
 
  28 #include <sys/types.h>
 
  35 static void usage(char *name)
 
  37         printf("usage: %s [OPTIONS]\n\n", name);
 
  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);
 
  54 int main(int argc, char *argv[])
 
  57         int exit_code = EXIT_SUCCESS;
 
  60         char filename[FILENAME_MAX] = {0};
 
  65         int log_level = AM7XXX_LOG_INFO;
 
  66         am7xxx_power_mode power_mode = AM7XXX_POWER_LOW;
 
  67         int format = AM7XXX_IMAGE_FORMAT_JPEG;
 
  72         am7xxx_device_info device_info;
 
  74         while ((opt = getopt(argc, argv, "f:F:l:p:W:H:h")) != -1) {
 
  77                         if (filename[0] != '\0')
 
  78                                 fprintf(stderr, "Warning: image file already specified\n");
 
  79                         strncpy(filename, optarg, FILENAME_MAX);
 
  82                         format = atoi(optarg);
 
  84                         case AM7XXX_IMAGE_FORMAT_JPEG:
 
  85                                 fprintf(stdout, "JPEG format\n");
 
  87                         case AM7XXX_IMAGE_FORMAT_NV12:
 
  88                                 fprintf(stdout, "NV12 format\n");
 
  91                                 fprintf(stderr, "Unsupported format\n");
 
  96                         log_level = atoi(optarg);
 
  97                         if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
 
  98                                 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
 
  99                                 log_level = AM7XXX_LOG_ERROR;
 
 103                         power_mode = atoi(optarg);
 
 105                         case AM7XXX_POWER_OFF:
 
 106                         case AM7XXX_POWER_LOW:
 
 107                         case AM7XXX_POWER_MIDDLE:
 
 108                         case AM7XXX_POWER_HIGH:
 
 109                         case AM7XXX_POWER_TURBO:
 
 110                                 fprintf(stdout, "Power mode: %x\n", power_mode);
 
 113                                 fprintf(stderr, "Invalid power mode value, must be between %x and %x\n", AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
 
 118                         width = atoi(optarg);
 
 120                                 fprintf(stderr, "Unsupported width\n");
 
 125                         height = atoi(optarg);
 
 127                                 fprintf(stderr, "Unsupported height\n");
 
 141         if (filename[0] == '\0') {
 
 142                 fprintf(stderr, "An image file MUST be specified.\n");
 
 143                 exit_code = EXIT_FAILURE;
 
 147         image_fd = open(filename, O_RDONLY);
 
 150                 exit_code = EXIT_FAILURE;
 
 153         if (fstat(image_fd, &st) < 0) {
 
 155                 exit_code = EXIT_FAILURE;
 
 156                 goto out_close_image_fd;
 
 160         image = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, image_fd, 0);
 
 163                 exit_code = EXIT_FAILURE;
 
 164                 goto out_close_image_fd;
 
 167         ret = am7xxx_init(&ctx);
 
 169                 perror("am7xxx_init");
 
 170                 exit_code = EXIT_FAILURE;
 
 174         am7xxx_set_log_level(ctx, log_level);
 
 176         ret = am7xxx_open_device(ctx, &dev, 0);
 
 178                 perror("am7xxx_open_device");
 
 179                 exit_code = EXIT_FAILURE;
 
 184         ret = am7xxx_close_device(dev);
 
 186                 perror("am7xxx_close_device");
 
 187                 exit_code = EXIT_FAILURE;
 
 191         ret = am7xxx_open_device(ctx, &dev, 0);
 
 193                 perror("am7xxx_open_device");
 
 194                 exit_code = EXIT_FAILURE;
 
 198         ret = am7xxx_get_device_info(dev, &device_info);
 
 200                 perror("am7xxx_get_info");
 
 201                 exit_code = EXIT_FAILURE;
 
 204         printf("Native resolution: %dx%d\n",
 
 205                device_info.native_width, device_info.native_height);
 
 207         ret = am7xxx_set_power_mode(dev, power_mode);
 
 209                 perror("am7xxx_set_power_mode");
 
 210                 exit_code = EXIT_FAILURE;
 
 214         if ((unsigned int)width > device_info.native_width ||
 
 215             (unsigned int)height > device_info.native_height)
 
 216                 fprintf(stderr, "WARNING: image not fitting the native resolution, it may be displayed wrongly!\n");
 
 218         ret = am7xxx_send_image(dev, format, width, height, image, size);
 
 220                 perror("am7xxx_send_image");
 
 221                 exit_code = EXIT_FAILURE;
 
 225         exit_code = EXIT_SUCCESS;
 
 228         am7xxx_shutdown(ctx);
 
 231         ret = munmap(image, size);
 
 236         ret = close(image_fd);