-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplitFlapDisplay.cpp
91 lines (74 loc) · 2.61 KB
/
SplitFlapDisplay.cpp
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
// Copyright (c) 2020 Stefano Guglielmetti
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SplitFlapDisplay.h"
SplitFlapDisplay::SplitFlapDisplay(int lettersNumber, int letterPins[],
int hallPins[], int flapDelay) {
// here the pointer this->lettersNumber is used to disambiguate from the
// lettersNumber parameter
this->lettersNumber = lettersNumber;
// same as above
this->flapDelay = flapDelay;
// initialize a new SplitFlapLetter object for each letter of the display
for (int i = 0; i < lettersNumber; i++) {
letters[i] = SplitFlapLetter(letterPins[i], hallPins[i]);
}
init();
}
void SplitFlapDisplay::init() {
previousMillis = 0; // reset the internal non blocking delay timer
reset(); // as soon as the display is initialized, resets all the letters
}
// the display is ready if all the letters are ready
bool SplitFlapDisplay::isReady() {
for (int i = 0; i < lettersNumber; i++) {
if (!letters[i].isReady()) return false;
}
return true;
}
// reset all the letters
void SplitFlapDisplay::reset() {
for (int i = 0; i < lettersNumber; i++) {
letters[i].reset();
}
}
// print a single character on all the letters
// i.e. display.print('A');
void SplitFlapDisplay::print(char letter) {
if (!isReady()) return;
for (int i = 0; i < lettersNumber; i++) {
letters[i].print(letter);
}
}
// print an array of characters
// i.e. display.print("JEKO RULEZ");
void SplitFlapDisplay::print(char* letter) {
if (!isReady()) return;
for (int i = 0; i < lettersNumber; i++) {
letters[i].print(letter[i]);
}
}
// This must be invoked at each Arduino loop iteration, it will use a non
// blocking delay to send a refresh signal to all the letters each flapDelay
// milliseconds, so they will run (almost) simultaneously. We also achieve the
// nice to have side effect of non blocking the host Arduino script.
void SplitFlapDisplay::refresh() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= flapDelay) {
previousMillis = currentMillis;
for (int i = 0; i < lettersNumber; i++) {
letters[i].refresh();
}
}
}
// This method will brutally flap all the letters, it shouldn't really be there
// and the corresponding SplitFlapLetter::flap() method should be private
// I've added it for demo purposes (see the split_flap_song example)
void SplitFlapDisplay::flap() {
for (int i = 0; i < lettersNumber; i++) {
letters[i].flap();
}
}
// Same as above, but only flaps the specified letter
void SplitFlapDisplay::flap(int letter) { letters[letter].flap(); }