-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.cc
200 lines (172 loc) · 6.09 KB
/
clock.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
// Copyright (C) 2017 Ben Ptacek <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://gnu.org/licenses/gpl-2.0.txt>
//
// LED Smart Clock
#include "led-matrix.h"
#include "graphics.h"
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
using namespace rgb_matrix;
volatile bool interrupt_received = false;
static void InterruptHandler(int signo) {
interrupt_received = true;
}
static int usage(const char *progname) {
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-d <time-format> : Default '%%H:%%M'. See strftime()\n"
"\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-S <spacing> : Spacing pixels between letters (Default: 0)\n"
"\t-C <r,g,b> : Color. Default 255,255,0\n"
"\t-B <r,g,b> : Background-Color. Default 0,0,0\n"
"\t-O <r,g,b> : Outline-Color, e.g. to increase contrast.\n"
);
return 1;
}
static bool parseColor(Color *c, const char *str) {
return sscanf(str, "%hhu,%hhu,%hhu", &c->r, &c->g, &c->b) == 3;
}
static bool FullSaturation(const Color &c) {
return (c.r == 0 || c.r == 255)
&& (c.g == 0 || c.g == 255)
&& (c.b == 0 || c.b == 255);
}
int main(int argc, char *argv[]) {
RGBMatrix::Options matrix_options;
rgb_matrix::RuntimeOptions runtime_opt;
if (!rgb_matrix::ParseOptionsFromFlags(&argc, &argv,
&matrix_options, &runtime_opt)) {
return usage(argv[0]);
}
const char *time_format = "%H:%M";
Color color(255, 255, 0);
Color bg_color(0, 0, 0);
Color outline_color(0,0,0);
bool with_outline = false;
const char *bdf_font_file = NULL;
int x_orig = 0;
int y_orig = 0;
int brightness = 100;
int letter_spacing = 0;
int opt;
while ((opt = getopt(argc, argv, "x:y:f:C:B:O:b:S:d:")) != -1) {
switch (opt) {
case 'd': time_format = strdup(optarg); break;
case 'b': brightness = atoi(optarg); break;
case 'x': x_orig = atoi(optarg); break;
case 'y': y_orig = atoi(optarg); break;
case 'f': bdf_font_file = strdup(optarg); break;
case 'S': letter_spacing = atoi(optarg); break;
case 'C':
if (!parseColor(&color, optarg)) {
fprintf(stderr, "Invalid color spec: %s\n", optarg);
return usage(argv[0]);
}
break;
case 'B':
if (!parseColor(&bg_color, optarg)) {
fprintf(stderr, "Invalid background color spec: %s\n", optarg);
return usage(argv[0]);
}
break;
case 'O':
if (!parseColor(&outline_color, optarg)) {
fprintf(stderr, "Invalid outline color spec: %s\n", optarg);
return usage(argv[0]);
}
with_outline = true;
break;
default:
return usage(argv[0]);
}
}
if (bdf_font_file == NULL) {
fprintf(stderr, "Need to specify BDF font-file with -f\n");
return usage(argv[0]);
}
/*
* Load font. This needs to be a filename with a bdf bitmap font.
*/
rgb_matrix::Font font;
if (!font.LoadFont(bdf_font_file)) {
fprintf(stderr, "Couldn't load font '%s'\n", bdf_font_file);
return 1;
}
rgb_matrix::Font *outline_font = NULL;
if (with_outline) {
outline_font = font.CreateOutlineFont();
}
if (brightness < 1 || brightness > 100) {
fprintf(stderr, "Brightness is outside usable range.\n");
return 1;
}
RGBMatrix *matrix = rgb_matrix::CreateMatrixFromOptions(matrix_options,
runtime_opt);
if (matrix == NULL)
return 1;
matrix->SetBrightness(brightness);
const bool all_extreme_colors = (brightness == 100)
&& FullSaturation(color)
&& FullSaturation(bg_color)
&& FullSaturation(outline_color);
if (all_extreme_colors)
matrix->SetPWMBits(1);
const int x = x_orig;
int y = y_orig;
FrameCanvas *offscreen = matrix->CreateFrameCanvas();
char text_buffer[256];
struct timespec next_time;
next_time.tv_sec = time(NULL);
next_time.tv_nsec = 0;
struct tm tm;
signal(SIGTERM, InterruptHandler);
signal(SIGINT, InterruptHandler);
while (!interrupt_received) {
localtime_r(&next_time.tv_sec, &tm);
strftime(text_buffer, sizeof(text_buffer), time_format, &tm);
offscreen->Fill(bg_color.r, bg_color.g, bg_color.b);
if (outline_font) {
rgb_matrix::DrawText(offscreen, *outline_font,
x - 1, y + font.baseline(),
outline_color, NULL, text_buffer,
letter_spacing - 2);
}
rgb_matrix::DrawText(offscreen, font, x, y + font.baseline(),
color, NULL, text_buffer,
letter_spacing);
// Wait until we're ready to show it.
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next_time, NULL);
// Atomic swap with double buffer
offscreen = matrix->SwapOnVSync(offscreen);
next_time.tv_sec += 1;
}
// Finished. Shut down the RGB matrix.
matrix->Clear();
delete matrix;
write(STDOUT_FILENO, "\n", 1); // Create a fresh new line after ^C on screen
return 0;
}