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