-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMD_MAX72xx.cpp
297 lines (250 loc) · 7.43 KB
/
MD_MAX72xx.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
MD_MAX72xx - Library for using a MAX7219/7221 LED matrix controller
See header file for comments
This file contains class and hardware related methods.
Copyright (C) 2012-14 Marco Colli. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
#include <SPI.h>
#include "MD_MAX72xx.h"
#include "MD_MAX72xx_lib.h"
/**
* \file
* \brief Implements class definition and general methods
*/
MD_MAX72XX::MD_MAX72XX(uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t numDevices):
_dataPin(dataPin), _clkPin(clkPin), _csPin(csPin), _maxDevices(numDevices),
_updateEnabled(true), _hardwareSPI(false)
{
}
MD_MAX72XX::MD_MAX72XX(uint8_t csPin, uint8_t numDevices):
_dataPin(0), _clkPin(0), _csPin(csPin), _maxDevices(numDevices),
_updateEnabled(true), _hardwareSPI(true)
{
}
void MD_MAX72XX::begin(void)
{
// initialize the AVR hardware
if (_hardwareSPI)
{
PRINTS("\nHardware SPI");
SPI.begin();
// Old mode of operations!
//SPI.setDataMode(SPI_MODE0);
//SPI.setBitOrder(MSBFIRST);
//SPI.setClockDivider(SPI_CLOCK_DIV2);
}
else
{
PRINTS("\nBitBang SPI")
pinMode(_dataPin, OUTPUT);
pinMode(_clkPin, OUTPUT);
}
// initialise our preferred CS pin (could be same as SS)
digitalWrite(_csPin, HIGH);
pinMode(_csPin, OUTPUT);
// object memory and internals
setShiftDataInCallback(NULL);
setShiftDataOutCallback(NULL);
_matrix = (deviceInfo_t *)malloc(sizeof(deviceInfo_t) * _maxDevices);
_spiData = (uint8_t *)malloc(SPI_DATA_SIZE);
#if USE_LOCAL_FONT
#if USE_INDEX_FONT
_fontIndex = (uint16_t *)malloc(sizeof(uint16_t) * ASCII_INDEX_SIZE);
#else
_fontIndex = NULL;
#endif
setFont(NULL);
#endif // INCLUDE_LOCAL_FONT
// Initialize the display devices. On initial power-up
// - all control registers are reset,
// - scan limit is set to one digit (row/col or LED),
// - Decoding mode is off,
// - intensity is set to the minimum,
// - the display is blanked, and
// - the MAX7219/MAX7221 is shut down.
// The devices need to be set to our library defaults prior using the
// display modules.
control(TEST, OFF); // no test
control(SCANLIMIT, ROW_SIZE-1); // scan limit is set to max on startup
control(INTENSITY, MAX_INTENSITY/2); // set intensity to a reasonable value
control(DECODE, OFF); // make sure that no decoding happens (warm boot potential issue)
clear();
control(SHUTDOWN, OFF); // take the modules out of shutdown mode
}
MD_MAX72XX::~MD_MAX72XX(void)
{
if (_hardwareSPI) SPI.end(); // reset SPI mode
free(_matrix);
free(_spiData);
#if USE_LOCAL_FONT && USE_FONT_INDEX
if (_fontIndex != NULL) free(_fontIndex);
#endif
}
void MD_MAX72XX::controlHardware(uint8_t dev, controlRequest_t mode, int value)
// control command is for the devices, translate internal request to device bytes
// into the transmission buffer
{
uint8_t opcode = OP_NOOP;
uint8_t param = 0;
// work out data to write
switch (mode)
{
case SHUTDOWN:
opcode = OP_SHUTDOWN;
param = (value == OFF ? 1 : 0);
break;
case SCANLIMIT:
opcode = OP_SCANLIMIT;
param = (value > MAX_SCANLIMIT ? MAX_SCANLIMIT : value);
break;
case INTENSITY:
opcode = OP_INTENSITY;
param = (value > MAX_INTENSITY ? MAX_INTENSITY : value);
break;
case DECODE:
opcode = OP_DECODEMODE;
param = (value == OFF ? 0 : 0xff);
break;
case TEST:
opcode = OP_DISPLAYTEST;
param = (value == OFF ? 0 : 1);
break;
default:
return;
}
// put our device data into the buffer
_spiData[SPI_OFFSET(dev, 0)] = opcode;
_spiData[SPI_OFFSET(dev, 1)] = param;
}
void MD_MAX72XX::controlLibrary(controlRequest_t mode, int value)
// control command was internal, set required parameters
{
switch (mode)
{
case UPDATE:
_updateEnabled = (value == ON);
if (_updateEnabled) flushBufferAll();
break;
case WRAPAROUND:
_wrapAround = (value == ON);
break;
}
}
bool MD_MAX72XX::control(uint8_t startDev, uint8_t endDev, controlRequest_t mode, int value)
{
if (endDev < startDev) return(false);
if (mode < UPDATE) // device based control
{
spiClearBuffer();
for (uint8_t i = startDev; i <= endDev; i++)
controlHardware(i, mode, value);
spiSend();
}
else // internal control function, doesn't relate to specific device
{
controlLibrary(mode, value);
}
return(true);
}
bool MD_MAX72XX::control(uint8_t buf, controlRequest_t mode, int value)
// dev is zero based and needs adjustment if used
{
if (buf > LAST_BUFFER) return(false);
if (mode < UPDATE) // device based control
{
spiClearBuffer();
controlHardware(buf, mode, value);
spiSend();
}
else // internal control function, doesn't relate to specific device
{
controlLibrary(mode, value);
}
return(true);
}
void MD_MAX72XX::flushBufferAll()
// Only one data byte is sent to a device, so if there are many changes, it is more
// efficient to send a data byte all devices at the same time, substantially cutting
// the number of communication messages required.
{
for (uint8_t i=0; i<ROW_SIZE; i++) // all data rows
{
bool bChange = false; // set to true if we detected a change
spiClearBuffer();
for (uint8_t dev = FIRST_BUFFER; dev <= LAST_BUFFER; dev++) // all devices
{
if (bitRead(_matrix[dev].changed, i))
{
// put our device data into the buffer
_spiData[SPI_OFFSET(dev, 0)] = OP_DIGIT0+i;
_spiData[SPI_OFFSET(dev, 1)] = _matrix[dev].dig[i];
bChange = true;
}
}
if (bChange) spiSend();
}
// mark everything as cleared
for (uint8_t dev = FIRST_BUFFER; dev <= LAST_BUFFER; dev++)
_matrix[dev].changed = ALL_CLEAR;
}
void MD_MAX72XX::flushBuffer(uint8_t buf)
// Use this function when the changes are limited to one device only.
// Address passed is a buffer address
{
PRINT("\nflushBuf: ", buf);
PRINTS(" r");
if (buf > LAST_BUFFER)
return;
for (uint8_t i = 0; i < ROW_SIZE; i++)
{
if (bitRead(_matrix[buf].changed, i))
{
PRINT("", i);
spiClearBuffer();
// put our device data into the buffer
_spiData[SPI_OFFSET(buf, 0)] = OP_DIGIT0+i;
_spiData[SPI_OFFSET(buf, 1)] = _matrix[buf].dig[i];
spiSend();
}
}
_matrix[buf].changed = ALL_CLEAR;
}
void MD_MAX72XX::spiClearBuffer(void)
// Clear out the spi data array
{
memset(_spiData, OP_NOOP, SPI_DATA_SIZE);
}
void MD_MAX72XX::spiSend()
{
// initialise the SPI transaction
if (_hardwareSPI)
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
digitalWrite(_csPin, LOW);
// shift out the data
if (_hardwareSPI)
{
for (int i = 0; i < SPI_DATA_SIZE; i++)
SPI.transfer(_spiData[i]);
}
else
{
for (int i = 0; i < SPI_DATA_SIZE; i++)
shiftOut(_dataPin, _clkPin, MSBFIRST, _spiData[i]);
}
// end the SPI transaction
digitalWrite(_csPin, HIGH);
if (_hardwareSPI)
SPI.endTransaction();
}