graphlcd-base/tools/showpic/showpic.c
changeset 4 df6a40031aa5
child 30 7fd00015f62f
equal deleted inserted replaced
3:d0e62fc47285 4:df6a40031aa5
       
     1 /*
       
     2  * GraphLCD tool showpic
       
     3  *
       
     4  * showpic.c  -  a tool to show image files in GLCD format on a LCD
       
     5  *
       
     6  * based on graphlcd plugin 0.1.1 for the Video Disc Recorder
       
     7  *  (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online.de>
       
     8  *  
       
     9  * This file is released under the GNU General Public License. Refer
       
    10  * to the COPYING file distributed with this package.
       
    11  *
       
    12  * (c) 2004 Andreas Regel <andreas.regel AT powarman.de>
       
    13  */
       
    14 
       
    15 #include <stdio.h>
       
    16 #include <string.h>
       
    17 #include <stdlib.h>
       
    18 #include <getopt.h>
       
    19 #include <dlfcn.h>
       
    20 #include <unistd.h>
       
    21 #include <syslog.h>
       
    22 #include <signal.h>
       
    23 
       
    24 #include <string>
       
    25 
       
    26 #include <glcdgraphics/bitmap.h>
       
    27 #include <glcdgraphics/glcd.h>
       
    28 #include <glcdgraphics/image.h>
       
    29 #include <glcddrivers/config.h>
       
    30 #include <glcddrivers/driver.h>
       
    31 #include <glcddrivers/drivers.h>
       
    32 
       
    33 static const char *prgname = "showpic";
       
    34 static const char *version = "0.1.2";
       
    35 
       
    36 static const int kDefaultSleepMs = 100;
       
    37 static const char * kDefaultConfigFile = "/etc/graphlcd.conf";
       
    38 
       
    39 static volatile bool stopProgramm = false;
       
    40 
       
    41 static void sighandler(int signal)
       
    42 {
       
    43 	switch (signal)
       
    44 	{
       
    45 		case SIGINT:
       
    46 		case SIGQUIT:
       
    47 		case SIGTERM:
       
    48 			stopProgramm = true;
       
    49 	}
       
    50 }
       
    51 
       
    52 void usage()
       
    53 {
       
    54 	fprintf(stdout, "\n");
       
    55 	fprintf(stdout, "%s v%s\n", prgname, version);
       
    56 	fprintf(stdout, "%s is a tool to show an image on a LCD.\n", prgname);
       
    57 	fprintf(stdout, "The image must be in a special format (*.glcd).\n");
       
    58 	fprintf(stdout, "You can create such images with the convpic tool.\n\n");
       
    59 	fprintf(stdout, "  Usage: %s [-c CONFIGFILE] [-d DISPLAY] [-s SLEEP] [-uie] file [more files]\n\n", prgname);
       
    60 	fprintf(stdout, "  -c  --config      specifies the location of the config file\n");
       
    61 	fprintf(stdout, "                    (default: /etc/graphlcd.conf)\n");
       
    62 	fprintf(stdout, "  -d  --display     specifies the output display (default is the first one)\n");
       
    63 	fprintf(stdout, "  -u  --upsidedown  rotates the output by 180 degrees (default: no)\n");
       
    64 	fprintf(stdout, "  -i  --invert      inverts the output (default: no)\n");
       
    65 	fprintf(stdout, "  -e  --endless     show all images in endless loop (default: no)\n");
       
    66 	fprintf(stdout, "  -s  --sleep       set sleeptime between two images [ms] (default: %d ms)\n", kDefaultSleepMs);
       
    67 	fprintf(stdout, "  -b  --brightness  set brightness for display if driver support it [%%]\n");
       
    68 	fprintf(stdout, "                    (default: config file value)\n");
       
    69 	fprintf(stdout, "\n" );
       
    70 	fprintf(stdout, "  examples: %s -c /etc/graphlcd.conf vdr-logo.glcd\n", prgname);
       
    71 	fprintf(stdout, "            %s -c /etc/graphlcd.conf -d LCD_T6963 -u -i vdr-logo.glcd\n", prgname);
       
    72 	fprintf(stdout, "\n" );
       
    73 }
       
    74 
       
    75 int main(int argc, char *argv[])
       
    76 {
       
    77 	static struct option long_options[] =
       
    78 	{
       
    79 		{"config",     required_argument, NULL, 'c'},
       
    80 		{"display",    required_argument, NULL, 'd'},
       
    81 		{"sleep",      required_argument, NULL, 's'},
       
    82 		{"endless",          no_argument, NULL, 'e'},
       
    83 		{"upsidedown",       no_argument, NULL, 'u'},
       
    84 		{"invert",           no_argument, NULL, 'i'},
       
    85 		{"brightness", required_argument, NULL, 'b'},
       
    86 		{NULL}
       
    87 	};
       
    88 
       
    89 	std::string configName = "";
       
    90 	std::string displayName = "";
       
    91 	bool upsideDown = false;
       
    92 	bool invert = false;
       
    93 	int brightness = -1;
       
    94 	bool delay = false;
       
    95 	int sleepMs = 100;
       
    96 	bool endless = false;
       
    97 	unsigned int displayNumber = 0;
       
    98 
       
    99 	int c, option_index = 0;
       
   100 	while ((c = getopt_long(argc, argv, "c:d:s:euib:", long_options, &option_index)) != -1)
       
   101 	{
       
   102 		switch(c)
       
   103 		{
       
   104 			case 'c':
       
   105 				configName = optarg;
       
   106 				break;
       
   107 
       
   108 			case 'd':
       
   109 				displayName = optarg;
       
   110 				break;
       
   111 
       
   112 			case 'u':
       
   113 				upsideDown = true;
       
   114 				break;
       
   115 
       
   116 			case 'i':
       
   117 				invert = true;
       
   118 				break;
       
   119 
       
   120 			case 's':
       
   121 				sleepMs = atoi(optarg);
       
   122 				delay = true;
       
   123 				break;
       
   124 
       
   125 			case 'e':
       
   126 				endless = true;
       
   127 				break;
       
   128 
       
   129 			case 'b':
       
   130 				brightness = atoi(optarg);
       
   131 				if (brightness < 0) brightness = 0;
       
   132 				if (brightness > 100) brightness = 100;
       
   133 				break;
       
   134 
       
   135 			default:
       
   136 				usage();
       
   137 				return 1;
       
   138 		}
       
   139 	}
       
   140 
       
   141 	if (configName.length() == 0)
       
   142 	{
       
   143 		configName = kDefaultConfigFile;
       
   144 		syslog(LOG_INFO, "Error: No config file specified, using default (%s).\n", configName.c_str());
       
   145 	}
       
   146 
       
   147 	if (GLCD::Config.Load(configName) == false)
       
   148 	{
       
   149 		fprintf(stdout, "Error loading config file!\n");
       
   150 		return 2;
       
   151 	}
       
   152 	if (GLCD::Config.driverConfigs.size() > 0)
       
   153 	{
       
   154 		if (displayName.length() > 0)
       
   155 		{
       
   156 			for (displayNumber = 0; displayNumber < GLCD::Config.driverConfigs.size(); displayNumber++)
       
   157 			{
       
   158 				if (GLCD::Config.driverConfigs[displayNumber].name == displayName)
       
   159 					break;
       
   160 			}
       
   161 			if (displayNumber == GLCD::Config.driverConfigs.size())
       
   162 			{
       
   163 				fprintf(stdout, "ERROR: Specified display %s not found in config file!\n", displayName.c_str());
       
   164 				return 3;
       
   165 			}
       
   166 		}
       
   167 		else
       
   168 		{
       
   169 			fprintf(stdout, "WARNING: No display specified, using first one.\n");
       
   170 			displayNumber = 0;
       
   171 		}
       
   172 	}
       
   173 	else
       
   174 	{
       
   175 		fprintf(stdout, "ERROR: No displays specified in config file!\n");
       
   176 		return 4;
       
   177 	}
       
   178 
       
   179 	if (optind == argc)
       
   180 	{
       
   181 		usage();
       
   182 		fprintf(stderr, "ERROR: You have to specify the image\n");
       
   183 		return 5;
       
   184 	}
       
   185 
       
   186 	GLCD::Config.driverConfigs[displayNumber].upsideDown ^= upsideDown;
       
   187 	GLCD::Config.driverConfigs[displayNumber].invert ^= invert;
       
   188 	if (brightness != -1)
       
   189 		GLCD::Config.driverConfigs[displayNumber].brightness = brightness;
       
   190 	GLCD::cDriver * lcd = GLCD::CreateDriver(GLCD::Config.driverConfigs[displayNumber].id, &GLCD::Config.driverConfigs[displayNumber]);
       
   191 	if (!lcd)
       
   192 	{
       
   193 		fprintf(stderr, "ERROR: Failed creating display object %s\n", displayName.c_str());
       
   194 		return 6;
       
   195 	}
       
   196 	if (lcd->Init() != 0)
       
   197 	{
       
   198 		fprintf(stderr, "ERROR: Failed initializing display %s\n", displayName.c_str());
       
   199 		delete lcd;
       
   200 		return 7;
       
   201 	}
       
   202   lcd->SetBrightness(GLCD::Config.driverConfigs[displayNumber].brightness);
       
   203 
       
   204 	signal(SIGINT, sighandler);
       
   205 	signal(SIGQUIT, sighandler);
       
   206 	signal(SIGTERM, sighandler);
       
   207 	signal(SIGHUP, sighandler);
       
   208 
       
   209 	const GLCD::cBitmap * bitmap;
       
   210 	GLCD::cImage image;
       
   211 	GLCD::cGLCDFile glcd;
       
   212 	int optFile;
       
   213 	std::string picFile;
       
   214 
       
   215 	optFile = optind;
       
   216 	while (optFile < argc && !stopProgramm)
       
   217 	{
       
   218 		picFile = argv[optFile++];
       
   219 		if (glcd.Load(image, picFile) == false)
       
   220 		{
       
   221 			fprintf(stderr, "ERROR: Failed loading file %s\n", picFile.c_str());
       
   222 			return 8;
       
   223 		}
       
   224 
       
   225 		if (delay)
       
   226 			image.SetDelay(sleepMs);
       
   227 
       
   228 		while ((bitmap = image.GetBitmap()) != NULL && !stopProgramm)
       
   229 		{
       
   230 			lcd->SetScreen(bitmap->Data(), bitmap->Width(), bitmap->Height(), bitmap->LineSize());
       
   231 			lcd->Refresh(true);
       
   232 
       
   233 			if (image.Next(0)) // Select next image
       
   234 			{
       
   235 				usleep(image.Delay() * 1000);
       
   236 			}
       
   237 			else if (endless && argc == (optind + 1)) // Endless and one and only image
       
   238 			{
       
   239 				image.First(0);
       
   240 				usleep(image.Delay() * 1000);
       
   241 			}
       
   242 			else 
       
   243 				break;
       
   244 		}
       
   245 
       
   246 		if (optFile < argc || endless)
       
   247 			usleep(sleepMs * 1000);
       
   248 		if (optFile >= argc && endless)
       
   249 			optFile = optind;
       
   250 	}
       
   251 
       
   252 	lcd->DeInit();
       
   253 	delete lcd;
       
   254 
       
   255 	return 0;
       
   256 }