rpi-rgb-led-matrix/examples-api-use/text-example.cc

125 lines
3.4 KiB
C++
Raw Permalink Normal View History

2014-09-11 07:20:34 +00:00
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
// Small example how write text.
2014-09-12 03:03:56 +00:00
//
// This code is public domain
// (but note, that the led-matrix library this depends on is GPL v2)
2014-09-11 07:20:34 +00:00
#include "led-matrix.h"
#include "graphics.h"
2014-09-12 01:02:16 +00:00
#include <getopt.h>
2014-09-11 07:20:34 +00:00
#include <stdio.h>
2014-09-12 01:02:16 +00:00
#include <stdlib.h>
2014-09-11 07:20:34 +00:00
#include <string.h>
#include <unistd.h>
using namespace rgb_matrix;
static int usage(const char *progname) {
2014-09-12 01:02:16 +00:00
fprintf(stderr, "usage: %s [options]\n", progname);
fprintf(stderr, "Reads text from stdin and displays it. "
"Empty string: clear screen\n");
fprintf(stderr, "Options:\n");
rgb_matrix::PrintMatrixFlags(stderr);
fprintf(stderr,
"\t-f <font-file> : Use given font.\n"
"\t-b <brightness> : Sets brightness percent. Default: 100.\n"
"\t-x <x-origin> : X-Origin of displaying text (Default: 0)\n"
"\t-y <y-origin> : Y-Origin of displaying text (Default: 0)\n"
"\t-C <r,g,b> : Color. Default 255,255,0\n");
2014-09-11 07:20:34 +00:00
return 1;
}
2014-09-12 01:02:16 +00:00
static bool parseColor(Color *c, const char *str) {
return sscanf(str, "%hhu,%hhu,%hhu", &c->r, &c->g, &c->b) == 3;
}
2014-09-11 07:20:34 +00:00
int main(int argc, char *argv[]) {
RGBMatrix *canvas = rgb_matrix::CreateMatrixFromFlags(&argc, &argv);
2014-09-12 01:02:16 +00:00
Color color(255, 255, 0);
const char *bdf_font_file = NULL;
int x_orig = 0;
int y_orig = -1;
int brightness = 100;
2014-09-12 01:02:16 +00:00
int opt;
while ((opt = getopt(argc, argv, "x:y:f:C:b:")) != -1) {
2014-09-12 01:02:16 +00:00
switch (opt) {
case 'b': brightness = atoi(optarg); break;
2014-09-12 01:02:16 +00:00
case 'x': x_orig = atoi(optarg); break;
case 'y': y_orig = atoi(optarg); break;
case 'f': bdf_font_file = strdup(optarg); break;
case 'C':
if (!parseColor(&color, optarg)) {
fprintf(stderr, "Invalid color spec.\n");
return usage(argv[0]);
}
break;
default:
return usage(argv[0]);
2014-09-12 01:02:16 +00:00
}
}
if (bdf_font_file == NULL) {
fprintf(stderr, "Need to specify BDF font-file with -f\n");
2014-09-11 07:20:34 +00:00
return usage(argv[0]);
}
if (canvas == NULL)
return 1;
2014-09-11 07:20:34 +00:00
/*
* Load font. This needs to be a filename with a bdf bitmap font.
*/
rgb_matrix::Font font;
2014-09-12 01:02:16 +00:00
if (!font.LoadFont(bdf_font_file)) {
fprintf(stderr, "Couldn't load font '%s'\n", bdf_font_file);
2014-09-11 07:20:34 +00:00
return usage(argv[0]);
}
if (brightness < 1 || brightness > 100) {
fprintf(stderr, "Brightness is outside usable range.\n");
return 1;
}
canvas->SetBrightness(brightness);
bool all_extreme_colors = brightness == 100;
all_extreme_colors &= color.r == 0 || color.r == 255;
all_extreme_colors &= color.g == 0 || color.g == 255;
all_extreme_colors &= color.b == 0 || color.b == 255;
if (all_extreme_colors)
canvas->SetPWMBits(1);
2014-09-11 07:20:34 +00:00
2014-09-12 01:02:16 +00:00
const int x = x_orig;
int y = y_orig;
2014-09-11 07:20:34 +00:00
if (isatty(STDIN_FILENO)) {
// Only give a message if we are interactive. If connected via pipe, be quiet
printf("Enter lines. Full screen or empty line clears screen.\n"
"Supports UTF-8. CTRL-D for exit.\n");
}
2014-09-11 07:20:34 +00:00
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
const size_t last = strlen(line);
if (last > 0) line[last - 1] = '\0'; // remove newline.
bool line_empty = strlen(line) == 0;
if ((y + font.height() > canvas->height()) || line_empty) {
2014-09-11 07:20:34 +00:00
canvas->Clear();
2014-09-12 01:02:16 +00:00
y = y_orig;
2014-09-11 07:20:34 +00:00
}
if (line_empty)
continue;
2014-09-11 07:20:34 +00:00
rgb_matrix::DrawText(canvas, font, x, y + font.baseline(), color, line);
y += font.height();
}
// Finished. Shut down the RGB matrix.
canvas->Clear();
delete canvas;
return 0;
}