graphlcd-base/tools/showtext/showtext.c
author root@rika
Wed, 06 Feb 2008 17:32:55 +0000
changeset 4 df6a40031aa5
permissions -rw-r--r--
added graphlcd-base
root@4
     1
/*
root@4
     2
 * GraphLCD tool showtext
root@4
     3
 *
root@4
     4
 * showtext.c  -  a tool to show a text on a LCD
root@4
     5
 *
root@4
     6
 * This file is released under the GNU General Public License. Refer
root@4
     7
 * to the COPYING file distributed with this package.
root@4
     8
 *
root@4
     9
 * (c) 2004 Andreas Regel <andreas.regel AT powarman.de>
root@4
    10
 */
root@4
    11
root@4
    12
#include <stdio.h>
root@4
    13
#include <string.h>
root@4
    14
#include <stdlib.h>
root@4
    15
#include <getopt.h>
root@4
    16
#include <unistd.h>
root@4
    17
root@4
    18
#include <string>
root@4
    19
root@4
    20
#include <glcdgraphics/bitmap.h>
root@4
    21
#include <glcdgraphics/font.h>
root@4
    22
#include <glcddrivers/config.h>
root@4
    23
#include <glcddrivers/driver.h>
root@4
    24
#include <glcddrivers/drivers.h>
root@4
    25
root@4
    26
static const char *prgname = "showtext";
root@4
    27
static const char *version = "0.0.3";
root@4
    28
root@4
    29
static const char * kDefaultConfigFile = "/etc/graphlcd.conf";
root@4
    30
root@4
    31
static const int kFontFNT = 0;
root@4
    32
static const int kFontFT2 = 1;
root@4
    33
root@4
    34
void usage()
root@4
    35
{
root@4
    36
	fprintf(stdout, "\n");
root@4
    37
	fprintf(stdout, "%s v%s\n", prgname, version);
root@4
    38
	fprintf(stdout, "%s is a tool to show a text on a LCD.\n", prgname);
root@4
    39
	fprintf(stdout, "\n");
root@4
    40
	fprintf(stdout, "  Usage: %s [-c CONFIGFILE] [-d DISPLAY] [-f FONT] [-x XPOS] [-y YPOS] [-uib] text [more text]\n\n", prgname);
root@4
    41
	fprintf(stdout, "  -c  --config      specifies the location of the config file\n");
root@4
    42
	fprintf(stdout, "                    (default: /etc/graphlcd.conf)\n");
root@4
    43
	fprintf(stdout, "  -d  --display     specifies the output display (default is the first one)\n");
root@4
    44
	fprintf(stdout, "  -f  --font        specifies the font that is used for the text:\n");
root@4
    45
	fprintf(stdout, "                    fnt:/path/to/font.fnt for GraphLCD font format\n");
root@4
    46
	fprintf(stdout, "                    ft2:/path/to/font.ttf:size for FreeType2 supported fonts\n");
root@4
    47
	fprintf(stdout, "  -e  --encoding    specifies the encoding that is used for the text\n");
root@4
    48
	fprintf(stdout, "                    (p.e. iso8859-1)\n");
root@4
    49
	fprintf(stdout, "  -x  --xpos        specifies the x-position where the text starts\n");
root@4
    50
	fprintf(stdout, "  -y  --ypos        specifies the y-position where the text starts\n");
root@4
    51
	fprintf(stdout, "  -u  --upsidedown  rotates the output by 180 degrees (default: no)\n");
root@4
    52
	fprintf(stdout, "  -i  --invert      inverts the output (default: no)\n");
root@4
    53
	fprintf(stdout, "  -b  --brightness  set brightness for display if driver support it [%%]\n");
root@4
    54
	fprintf(stdout, "                    (default: config file value)\n");
root@4
    55
	fprintf(stdout, "\n" );
root@4
    56
	fprintf(stdout, "  example: %s -c /etc/graphlcd.conf -f fnt:f17.fnt \"Line1\" \"Line2\"\n", prgname);
root@4
    57
	fprintf(stdout, "\n" );
root@4
    58
}
root@4
    59
root@4
    60
int main(int argc, char *argv[])
root@4
    61
{
root@4
    62
	static struct option long_options[] =
root@4
    63
	{
root@4
    64
		{"config",     required_argument, NULL, 'c'},
root@4
    65
		{"display",    required_argument, NULL, 'd'},
root@4
    66
		{"font",       required_argument, NULL, 'f'},
root@4
    67
		{"encoding",   required_argument, NULL, 'e'},
root@4
    68
		{"upsidedown",       no_argument, NULL, 'u'},
root@4
    69
		{"invert",           no_argument, NULL, 'i'},
root@4
    70
		{"brightness", required_argument, NULL, 'b'},
root@4
    71
		{"xpos",       required_argument, NULL, 'x'},
root@4
    72
		{"ypos",       required_argument, NULL, 'y'},
root@4
    73
		{NULL}
root@4
    74
	};
root@4
    75
root@4
    76
	std::string configName = "";
root@4
    77
	std::string displayName = "";
root@4
    78
	std::string fontName;
root@4
    79
	std::string fontFileName = "";
root@4
    80
	std::string encoding = "";
root@4
    81
	int fontType;
root@4
    82
	int fontSize = 16;
root@4
    83
	bool upsideDown = false;
root@4
    84
	bool invert = false;
root@4
    85
	int brightness = -1;
root@4
    86
	int x = 0;
root@4
    87
	int y = 0;
root@4
    88
	unsigned int displayNumber = 0;
root@4
    89
root@4
    90
	int c, option_index = 0;
root@4
    91
	while ((c = getopt_long(argc, argv, "c:d:f:e:uib:x:y:", long_options, &option_index)) != -1)
root@4
    92
	{
root@4
    93
		switch (c)
root@4
    94
		{
root@4
    95
			case 'c':
root@4
    96
				configName = optarg;
root@4
    97
				break;
root@4
    98
root@4
    99
			case 'd':
root@4
   100
				displayName = optarg;
root@4
   101
				break;
root@4
   102
root@4
   103
			case 'f':
root@4
   104
				fontName = optarg;
root@4
   105
				break;
root@4
   106
root@4
   107
			case 'e':
root@4
   108
				encoding = optarg;
root@4
   109
				break;
root@4
   110
root@4
   111
			case 'u':
root@4
   112
				upsideDown = true;
root@4
   113
				break;
root@4
   114
root@4
   115
			case 'i':
root@4
   116
				invert = true;
root@4
   117
				break;
root@4
   118
root@4
   119
			case 'b':
root@4
   120
				brightness = atoi(optarg);
root@4
   121
				if (brightness < 0) brightness = 0;
root@4
   122
				if (brightness > 100) brightness = 100;
root@4
   123
				break;
root@4
   124
root@4
   125
			case 'x':
root@4
   126
				x = atoi(optarg);
root@4
   127
				break;
root@4
   128
root@4
   129
			case 'y':
root@4
   130
				y = atoi(optarg);
root@4
   131
				break;
root@4
   132
root@4
   133
			default:
root@4
   134
				usage();
root@4
   135
				return 1;
root@4
   136
		}
root@4
   137
	}
root@4
   138
root@4
   139
	if (configName.length() == 0)
root@4
   140
	{
root@4
   141
		configName = kDefaultConfigFile;
root@4
   142
		fprintf(stdout, "WARNING: No config file specified, using default (%s).\n", configName.c_str());
root@4
   143
	}
root@4
   144
root@4
   145
	if (GLCD::Config.Load(configName) == false)
root@4
   146
	{
root@4
   147
		fprintf(stderr, "Error loading config file!\n");
root@4
   148
		return 2;
root@4
   149
	}
root@4
   150
	if (GLCD::Config.driverConfigs.size() > 0)
root@4
   151
	{
root@4
   152
		if (displayName.length() > 0)
root@4
   153
		{
root@4
   154
			for (displayNumber = 0; displayNumber < GLCD::Config.driverConfigs.size(); displayNumber++)
root@4
   155
			{
root@4
   156
				if (GLCD::Config.driverConfigs[displayNumber].name == displayName)
root@4
   157
					break;
root@4
   158
			}
root@4
   159
			if (displayNumber == GLCD::Config.driverConfigs.size())
root@4
   160
			{
root@4
   161
				fprintf(stderr, "ERROR: Specified display %s not found in config file!\n", displayName.c_str());
root@4
   162
				return 3;
root@4
   163
			}
root@4
   164
		}
root@4
   165
		else
root@4
   166
		{
root@4
   167
			fprintf(stdout, "WARNING: No display specified, using first one.\n");
root@4
   168
			displayNumber = 0;
root@4
   169
		}
root@4
   170
	}
root@4
   171
	else
root@4
   172
	{
root@4
   173
		fprintf(stderr, "ERROR: No displays specified in config file!\n");
root@4
   174
		return 4;
root@4
   175
	}
root@4
   176
root@4
   177
	if (encoding.length() == 0)
root@4
   178
	{
root@4
   179
		encoding = "iso8859-1";
root@4
   180
		fprintf(stdout, "WARNING: No encoding specified, using default (%s).\n", encoding.c_str());
root@4
   181
	}
root@4
   182
root@4
   183
	if (fontName.length() == 0)
root@4
   184
	{
root@4
   185
		fprintf(stderr, "Error: No font specified!\n");
root@4
   186
		return 5;
root@4
   187
	}
root@4
   188
	if (fontName.find("fnt:") == 0)
root@4
   189
	{
root@4
   190
		fontType = kFontFNT;
root@4
   191
		fontFileName = fontName.substr(4);
root@4
   192
	}
root@4
   193
	else if (fontName.find("ft2:") == 0)
root@4
   194
	{
root@4
   195
		fontType = kFontFT2;
root@4
   196
		std::string::size_type pos = fontName.find(":", 4);
root@4
   197
		if (pos == std::string::npos)
root@4
   198
		{
root@4
   199
			fontSize = 16;
root@4
   200
			fprintf(stdout, "WARNING: No font size specified, using default (%d).\n", fontSize);
root@4
   201
			fontFileName = fontName.substr(4);
root@4
   202
		}
root@4
   203
		else
root@4
   204
		{
root@4
   205
			std::string tmp = fontName.substr(pos + 1);
root@4
   206
			fontSize = atoi(tmp.c_str());
root@4
   207
			fontFileName = fontName.substr(4, pos - 4);
root@4
   208
		}
root@4
   209
	}
root@4
   210
	else
root@4
   211
	{
root@4
   212
		fprintf(stderr, "Error: Unknown font type!\n");
root@4
   213
		return 6;
root@4
   214
	}
root@4
   215
	
root@4
   216
	if (optind == argc)
root@4
   217
	{
root@4
   218
		usage();
root@4
   219
		fprintf(stderr, "ERROR: You have to specify a text\n");
root@4
   220
		return 7;
root@4
   221
	}
root@4
   222
root@4
   223
	GLCD::Config.driverConfigs[displayNumber].upsideDown ^= upsideDown;
root@4
   224
	GLCD::Config.driverConfigs[displayNumber].invert ^= invert;
root@4
   225
	if (brightness != -1)
root@4
   226
		GLCD::Config.driverConfigs[displayNumber].brightness = brightness;
root@4
   227
root@4
   228
	GLCD::cFont font;
root@4
   229
	if (fontType == kFontFNT)
root@4
   230
	{
root@4
   231
		if (font.LoadFNT(fontFileName) == false)
root@4
   232
		{
root@4
   233
			fprintf(stderr, "ERROR: Failed loading font file %s\n", fontFileName.c_str());
root@4
   234
			return 8;
root@4
   235
		}
root@4
   236
	}
root@4
   237
	else
root@4
   238
	{
root@4
   239
		if (font.LoadFT2(fontFileName, encoding, fontSize) == false)
root@4
   240
		{
root@4
   241
			fprintf(stderr, "ERROR: Failed loading font file %s\n", fontFileName.c_str());
root@4
   242
			return 8;
root@4
   243
		}
root@4
   244
	}
root@4
   245
	GLCD::cDriver * lcd = GLCD::CreateDriver(GLCD::Config.driverConfigs[displayNumber].id, &GLCD::Config.driverConfigs[displayNumber]);
root@4
   246
	if (!lcd)
root@4
   247
	{
root@4
   248
		fprintf(stderr, "ERROR: Failed creating display object %s\n", displayName.c_str());
root@4
   249
		return 9;
root@4
   250
	}
root@4
   251
	if (lcd->Init() != 0)
root@4
   252
	{
root@4
   253
		fprintf(stderr, "ERROR: Failed initializing display %s\n", displayName.c_str());
root@4
   254
		delete lcd;
root@4
   255
		return 10;
root@4
   256
	}
root@4
   257
root@4
   258
	GLCD::cBitmap * bitmap = new GLCD::cBitmap(lcd->Width(), lcd->Height());
root@4
   259
	int optText;
root@4
   260
	std::string text;
root@4
   261
root@4
   262
	bitmap->Clear();
root@4
   263
	optText = optind;
root@4
   264
	while (optText < argc)
root@4
   265
	{
root@4
   266
		text = argv[optText];
root@4
   267
		optText++;
root@4
   268
root@4
   269
		bitmap->DrawText(x, y, bitmap->Width() - 1, text, &font);
root@4
   270
		y += font.LineHeight();
root@4
   271
	}
root@4
   272
	lcd->SetScreen(bitmap->Data(), bitmap->Width(), bitmap->Height(), bitmap->LineSize());
root@4
   273
	lcd->Refresh(true);
root@4
   274
root@4
   275
	lcd->DeInit();
root@4
   276
	delete lcd;
root@4
   277
root@4
   278
	return 0;
root@4
   279
}