forked from nhatuan84/esp32-led-matrix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLedMatrix.cpp
240 lines (219 loc) · 6.66 KB
/
LedMatrix.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <SPI.h>
#include "LedMatrix.h"
#include "cp437font.h"
/**
* Heavily influenced by the code and the blog posts from https://github.com/nickgammon/MAX7219_Dot_Matrix
*/
LedMatrix::LedMatrix(byte numberOfDevices, byte slaveSelectPin) {
myNumberOfDevices = numberOfDevices;
mySlaveSelectPin = slaveSelectPin;
cols = new byte[numberOfDevices * 8];
}
/**
* numberOfDevices: how many modules are daisy changed togehter
* slaveSelectPin: which pin is controlling the CS/SS pin of the first module?
*/
void LedMatrix::init() {
pinMode(mySlaveSelectPin, OUTPUT);
SPI.begin ();
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV128);
for(byte device = 0; device < myNumberOfDevices; device++) {
sendByte (device, MAX7219_REG_SCANLIMIT, 7); // show all 8 digits
sendByte (device, MAX7219_REG_DECODEMODE, 0); // using an led matrix (not digits)
sendByte (device, MAX7219_REG_DISPLAYTEST, 0); // no display test
sendByte (device, MAX7219_REG_INTENSITY, 0); // character intensity: range: 0 to 15
sendByte (device, MAX7219_REG_SHUTDOWN, 1); // not in shutdown mode (ie. start it up)
}
}
void LedMatrix::sendByte (const byte device, const byte reg, const byte data) {
digitalWrite(mySlaveSelectPin,LOW);
for (int i=0; i<myNumberOfDevices; i++)
{
SPI.transfer ((i==device)?reg:(byte)0);
SPI.transfer ((i==device)?data:(byte)0);
}
digitalWrite (mySlaveSelectPin, HIGH);
}
void LedMatrix::sendByte (const byte reg, const byte data) {
digitalWrite(mySlaveSelectPin,LOW);
for (int i=0; i<myNumberOfDevices; i++)
{
SPI.transfer (reg);
SPI.transfer (data);
}
digitalWrite (mySlaveSelectPin, HIGH);
}
void LedMatrix::setIntensity(const byte intensity) {
sendByte(MAX7219_REG_INTENSITY, intensity);
}
void LedMatrix::setTextAlignment(byte textAlignment) {
myTextAlignment = textAlignment;
calculateTextAlignmentOffset();
}
void LedMatrix::calculateTextAlignmentOffset() {
switch(myTextAlignment) {
case TEXT_ALIGN_LEFT:
myTextAlignmentOffset = 0;
break;
case TEXT_ALIGN_LEFT_END:
myTextAlignmentOffset = myNumberOfDevices * 8;
break;
case TEXT_ALIGN_RIGHT:
myTextAlignmentOffset = myTextLength - myNumberOfDevices * 8;
break;
case TEXT_ALIGN_RIGHT_END:
myTextAlignmentOffset = - myTextLength;
break;
case TEXT_ALIGN_CENTER:
myTextAlignmentOffset = (myNumberOfDevices * 8 - myTextLength) / 2;
break;
}
}
void LedMatrix::clear() {
for (byte col = 0; col < myNumberOfDevices * 8; col++) {
cols[col] = 0;
}
}
void LedMatrix::commit() {
if (myDisplayOrientation) {
for (byte dcol=0; dcol < 8; dcol++) {
digitalWrite(mySlaveSelectPin,LOW);
for (int dev=0; dev < myNumberOfDevices; dev++) {
byte b = 0;
for (byte fcol=0; fcol < 8; fcol++)
if (cols[dev*8+fcol]&(1<<dcol))
b |= (128>>fcol);
SPI.transfer(dcol+1);
SPI.transfer(b);
}
digitalWrite(mySlaveSelectPin,HIGH);
}
}
else {
for (byte col=0; col < 8; col++) {
digitalWrite(mySlaveSelectPin,LOW);
for (int dev=0; dev < myNumberOfDevices; dev++) {
SPI.transfer(col+1);
SPI.transfer(cols[dev*8+col]);
}
digitalWrite(mySlaveSelectPin,HIGH);
}
}
}
void LedMatrix::setText(String text) {
myText = text;
myTextOffset = 0;
myTextLength = 0;
for (int i = 0; i < myText.length(); i++) myTextLength += cp437_width[(byte)myText.charAt(i)];
calculateTextAlignmentOffset();
}
int LedMatrix::getTextLength() {
return myTextLength;
}
void LedMatrix::setNextText(String nextText) {
myNextText = nextText;
}
void LedMatrix::scrollTextRight() {
myTextOffset = (myTextOffset + 1) % (myTextLength - 5);
}
void LedMatrix::scrollTextLeft() {
myTextOffset = (myTextOffset - 1) % (myTextLength + myNumberOfDevices * 8);
if (myTextOffset == 0 && myNextText.length() > 0) {
myText = myNextText;
myNextText = "";
myTextLength = 0;
for (int i = 0; i < myText.length(); i++) myTextLength += cp437_width[(byte)myText.charAt(i)];
calculateTextAlignmentOffset();
}
}
void LedMatrix::oscillateText() {
int maxColumns = myTextLength;
int maxDisplayColumns = myNumberOfDevices * 8;
if (maxDisplayColumns > maxColumns) {
return;
}
if (myTextOffset - maxDisplayColumns == -maxColumns) {
increment = 1;
}
if (myTextOffset == 0) {
increment = -1;
}
myTextOffset += increment;
}
void LedMatrix::setAlternateDisplayOrientation(byte x) {
myDisplayOrientation = x;
}
void LedMatrix::drawText() {
byte escape = 0;
byte letter;
uint32_t code;
int width;
int pos0 = myTextOffset + myTextAlignmentOffset;
for (int i = 0; i < myText.length(); i++) {
letter = (byte)myText.charAt(i);
if (escape > 0)
{
code = (code << 8) + letter;
escape--;
}
else if (letter == 226)
{
code = letter;
escape = 2;
}
else if (letter == 194 || letter == 195)
{
code = letter;
escape = 1;
}
else code = letter;
if (escape > 0) continue;
switch (code)
{
case 0xc280:
case 0xe282ac: letter = 238; // Epsilon = Euro
break;
case 0xc384: letter = 142; // Ä
break;
case 0xc396: letter = 153; // Ö
break;
case 0xc39c: letter = 154; // Ü
break;
case 0xc39f: letter = 225; // ß
break;
case 0xc2a0: letter = 32; // Blank
break;
case 0xc3a4: letter = 132; // ä
break;
case 0xc2b0: letter = 248; // °
break;
case 0xc3b6: letter = 148; // ö
break;
case 0xc3bc: letter = 129; // ü
break;
case 0xe28093: letter = 196; // long -
break;
default: if (code < 256) letter = code;
else letter = 32;
break;
}
width = pgm_read_byte (&cp437_width[letter]);
for (byte col = 0; col < width; col++) {
int position = pos0 + col;
if (position >= 0 && position < myNumberOfDevices * 8 && col < 8) {
setColumn(position, pgm_read_byte (&cp437_font [letter] [col]));
}
}
pos0 += width;
}
}
void LedMatrix::setColumn(int column, byte value) {
if (column < 0 || column >= myNumberOfDevices * 8) {
return;
}
cols[column] = value;
}
void LedMatrix::setPixel(byte x, byte y) {
bitWrite(cols[x], y, true);
}