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