-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaxMatrix2.cpp
227 lines (194 loc) · 6.01 KB
/
MaxMatrix2.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
/* MaxMatrix2
* Version 0.1 OCT 2013
* Copyright 2013 Nikolai Neff
* based upon MaxMatrix by Oscar Kin-Chung Au
*
* Tested with Arduino 1.0.5 and 1.5.4 on Arduino Uno Rev3
*
* This file is part of MaxMatrix2
*
* MaxMatrix2 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, either version 3 of the License, or
* (at your option) any later version.
*
* MaxMatrix2 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 MaxMatrix2. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Arduino.h"
#include "MaxMatrix2.h"
MaxMatrix2::MaxMatrix2(byte _data, byte _load, byte _clock, byte _numDisplays)
{
data = _data;
load = _load;
clock = _clock;
numDisplays = _numDisplays;
transmissionActive = false;
}
void MaxMatrix2::init(byte scanLimit, byte decodeMode, byte intensity, bool displayTest)
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(load, OUTPUT);
/* From the MAX7219/MAX7221 Datasheet:
On initial power-up, all control registers are reset, the
display is blanked, and the MAX7219/MAX7221 enter
shutdown mode. Program the display driver prior to
display use. Otherwise, it will initially be set to scan one
digit, it will not decode data in the data registers, and
the intensity register will be set to its minimum value.
*/
/*It might take a while for these defaults to kick in,
especially if the devices power supply is backed up by a capacitor as it is recommended in the manual.
Therefore, the init function always sends values to all status-registers to make sure no settings from the last power-on cycle remain*/
byte i;
for (i=0; i<numDisplays; i++)
{
if (i!=numDisplays-1)
{
sendData(max7219_reg_scanLimit, scanLimit);
} else {
sendData(max7219_reg_scanLimit, scanLimit, true);
}
}
for (i=0; i<numDisplays; i++)
{
if (i!=numDisplays-1)
{
sendData(max7219_reg_decodeMode, decodeMode);
} else {
sendData(max7219_reg_decodeMode, decodeMode, true);
}
}
for (i=0; i<numDisplays; i++)
{
if (i!=numDisplays-1)
{
sendData(max7219_reg_displayTest, displayTest);
} else {
sendData(max7219_reg_displayTest, displayTest, true);
}
}
for (i=0; i<numDisplays; i++)
{
if (i!=numDisplays-1)
{
sendData(max7219_reg_shutdown, 0x01);
} else {
sendData(max7219_reg_shutdown, 0x01, true);
}
}
for (i=0; i<numDisplays; i++)
{
if (i!=numDisplays-1)
{
sendData(max7219_reg_intensity, intensity);
} else {
sendData(max7219_reg_intensity, intensity, true);
}
}
}
void MaxMatrix2::sendData(byte registerAddr, byte value, bool end) // sends data serially into the shift registers without terminating the transmission afterwards
{
if (transmissionActive == false)
{
digitalWrite(load, LOW); //indicates data transfer start
transmissionActive = true;
}
shiftOut(data, clock, MSBFIRST, registerAddr); //send desired 16 bit of data split up in to parts, as Arduino only supports 8 bit in software-SPI mode
shiftOut(data, clock, MSBFIRST, value);
if (end == true)
{
digitalWrite(load, HIGH); //indicates data transfer end
transmissionActive = false;
}
}
void MaxMatrix2::setIntensity(byte display, byte intensity) //sets the intensity for a specific display
{
sendCommand(display, max7219_reg_intensity, intensity);
}
void MaxMatrix2::setDecodeMode(byte display, byte decodeMode) //sets the decoding for CODE-B Font for specific digits for a specific display
{
sendCommand(display, max7219_reg_decodeMode, decodeMode);
}
void MaxMatrix2::setShutdown(byte display, byte shutdown) //shutdown or wake up specific display
{
sendCommand(display, max7219_reg_shutdown, shutdown);
}
void MaxMatrix2::setTestMode(byte display, byte testMode) //test specific display
{
sendCommand(display, max7219_reg_displayTest, testMode);
}
void MaxMatrix2::clear(byte display)
{
for (byte i=1; i<=8; i++) //output registers are 1 to 8
{
sendCommand(display, i, 0); //set all outputs to 0
}
}
void MaxMatrix2::clearAll()
{
for(byte i=0; i<matrixSize; i++)
{
for(byte n=0; n<numDisplays; n++)
{
if(n!=numDisplays-1)
{
sendData(i+1,0);
} else {
sendData(i+1,0,true);
}
}
}
}
void MaxMatrix2::sendCommand(byte display, byte registerAddr, byte value) // sends a command to a specific Display
{
//for details on the Addressing of specific registers look in the Max 7219 datasheet
digitalWrite(load, LOW); //indicates data transfer start
/*cascaded displays act like a shift register. To access a display in the middle of this chain,
all other displays have to be ignored by sending specific No-OP instructions. */
for (byte i=0; i<(numDisplays-(display+1)); i++) //send NoOps for leading Displays
{
shiftOut(data, clock, MSBFIRST, max7219_reg_noop);
shiftOut(data, clock, MSBFIRST, 0x00);
}
shiftOut(data, clock, MSBFIRST, registerAddr); //send desired 16 bit of data split up in to parts, as Arduino only supports 8 bit in software-SPI mode
shiftOut(data, clock, MSBFIRST, value);
for (byte i=0; i<display; i++) //send NoOps for trailing Displays
{
shiftOut(data, clock, MSBFIRST, max7219_reg_noop);
shiftOut(data, clock, MSBFIRST, 0x00);
}
digitalWrite(load, HIGH); //indicates data transfer end
}
void MaxMatrix2::sendArray(byte display, byte buffer[matrixSize])
{
for (byte i=0; i<matrixSize; i++)
{
sendCommand(display, i+1, buffer[i]);
}
}
void MaxMatrix2::sendASCIIText(String text)
{
//Todo: implement
// int col = 0;
// while (text != 0)
// {
// if (text < 32) //ignore control caracters
// {
// continue;
// }
// char c = *s - 32;
//
// memcpy_P(buffer, CH + 7*c, 7);
// m.writeSprite(col, 0, buffer);
// m.setColumn(col + buffer[0], 0);
// col += buffer[0] + 1;
// s++;
// }
}