Skip to content

Commit d14a34f

Browse files
committed
o Proivde a good example and install a signal handler.
1 parent 10d39be commit d14a34f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

examples-api-use/clock.cc

+12-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@
99
#include "graphics.h"
1010

1111
#include <getopt.h>
12+
#include <signal.h>
1213
#include <stdio.h>
1314
#include <stdlib.h>
1415
#include <string.h>
15-
#include <unistd.h>
1616
#include <time.h>
17+
#include <unistd.h>
1718

1819
using namespace rgb_matrix;
1920

21+
volatile bool interrupt_received = false;
22+
static void InterruptHandler(int signo) {
23+
interrupt_received = true;
24+
}
25+
2026
static int usage(const char *progname) {
2127
fprintf(stderr, "usage: %s [options]\n", progname);
2228
fprintf(stderr, "Reads text from stdin and displays it. "
@@ -149,7 +155,10 @@ int main(int argc, char *argv[]) {
149155
next_time.tv_nsec = 0;
150156
struct tm tm;
151157

152-
for (;;) {
158+
signal(SIGTERM, InterruptHandler);
159+
signal(SIGINT, InterruptHandler);
160+
161+
while (!interrupt_received) {
153162
localtime_r(&next_time.tv_sec, &tm);
154163
strftime(text_buffer, sizeof(text_buffer), time_format, &tm);
155164
offscreen->Fill(bg_color.r, bg_color.g, bg_color.b);
@@ -176,5 +185,6 @@ int main(int argc, char *argv[]) {
176185
matrix->Clear();
177186
delete matrix;
178187

188+
write(STDOUT_FILENO, "\n", 1); // Create a fresh new line after ^C on screen
179189
return 0;
180190
}

0 commit comments

Comments
 (0)