forked from hzeller/rpi-rgb-led-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input-example.cc
66 lines (55 loc) · 1.94 KB
/
input-example.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
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
// Small example how to use the input bits
//
// This code is public domain
// (but note, that the led-matrix library this depends on is GPL v2)
#include "led-matrix.h"
#include <unistd.h>
#include <math.h>
#include <stdio.h>
#include <signal.h>
using rgb_matrix::GPIO;
using rgb_matrix::RGBMatrix;
using rgb_matrix::Canvas;
volatile bool interrupt_received = false;
static void InterruptHandler(int signo) {
interrupt_received = true;
}
int main(int argc, char *argv[]) {
RGBMatrix::Options defaults;
defaults.hardware_mapping = "regular"; // or e.g. "adafruit-hat"
defaults.rows = 32;
defaults.chain_length = 1;
defaults.parallel = 1;
RGBMatrix *matrix = rgb_matrix::CreateMatrixFromFlags(&argc, &argv,
&defaults);
if (matrix == NULL)
return 1;
// It is always good to set up a signal handler to cleanly exit when we
// receive a CTRL-C for instance.
signal(SIGTERM, InterruptHandler);
signal(SIGINT, InterruptHandler);
// Let's request all input bits and see which are actually available.
// This will differ depending on which hardware mapping you use and how
// many parallel chains you have.
const uint32_t available_inputs = matrix->gpio()->RequestInputs(0xffffffff);
fprintf(stderr, "Available GPIO-bits: ");
for (int b = 0; b < 32; ++b) {
if (available_inputs & (1<<b))
fprintf(stderr, "%d ", b);
}
fprintf(stderr, "\n");
while (!interrupt_received) {
// Block and wait until any input bit changed or 100ms passed
uint32_t inputs = matrix->AwaitInputChange(100);
// Minimal output: let's show the bits with LEDs in the first row
for (int b = 0; b < 32; ++b) {
uint8_t col = (inputs & (1<<b)) ? 255 : 0;
matrix->SetPixel(32-b, 0, col, col, col);
}
}
fprintf(stderr, "Exiting.\n");
matrix->Clear();
delete matrix;
return 0;
}