-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMD_MAX72xx_buf.cpp
373 lines (306 loc) · 8.69 KB
/
MD_MAX72xx_buf.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
MD_MAX72xx - Library for using a MAX7219/7221 LED matrix controller
See header file for comments
This file contains methods that act on display buffers.
Copyright (C) 2012-13 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 "MD_MAX72xx.h"
#include "MD_MAX72xx_lib.h"
/**
* \file
* \brief Implements buffer related methods
*/
bool MD_MAX72XX::clear(uint8_t buf)
{
if (buf > LAST_BUFFER)
return(false);
memset(_matrix[buf].dig, 0, sizeof(_matrix[buf].dig));
_matrix[buf].changed = ALL_CHANGED;
if (_updateEnabled) flushBuffer(buf);
return(true);
}
uint8_t MD_MAX72XX::bitReverse(uint8_t b)
// Reverse the order of bits within a byte.
// Returns the reversed byte value.
{
b = ((b & 0xf0) >> 4) | ((b & 0x0f) << 4);
b = ((b & 0xcc) >> 2) | ((b & 0x33) << 2);
b = ((b & 0xaa) >> 1) | ((b & 0x55) << 1);
return(b);
}
#if HW_DIG_ROWS
bool MD_MAX72XX::copyColumn(uint8_t buf, uint8_t cSrc, uint8_t cDest)
#else
bool MD_MAX72XX::copyRow(uint8_t buf, uint8_t cSrc, uint8_t cDest)
#endif
// Src and Dest are in pixel coordinates.
// if we are just copying rows there is no need to repackage any data
{
uint8_t maskSrc = 1 << HW_COL(cSrc); // which column of bits is the column data
#if HW_DIG_ROWS
PRINT("\ncopyCol: (", buf);
#else
PRINT("\ncopyRow: (", buf);
#endif
PRINT(", ", cSrc);
PRINT(", ", cDest);
PRINTS(") ");
if ((buf > LAST_BUFFER) || (cSrc >= COL_SIZE) || (cDest >= COL_SIZE))
return(false);
for (uint8_t i=0; i<ROW_SIZE; i++)
{
if (_matrix[buf].dig[i] & maskSrc)
bitSet(_matrix[buf].dig[i], HW_COL(cDest));
else
bitClear(_matrix[buf].dig[i], HW_COL(cDest));
}
_matrix[buf].changed = ALL_CHANGED;
if (_updateEnabled) flushBuffer(buf);
return(true);
}
#if HW_DIG_ROWS
uint8_t MD_MAX72XX::getColumn(uint8_t buf, uint8_t c)
#else
uint8_t MD_MAX72XX::getRow(uint8_t buf, uint8_t c)
#endif
// c is in pixel coordinates and the return value must be in pixel coordinate order
{
uint8_t mask = 1 << HW_COL(c); // which column of bits is the column data
uint8_t value = 0; // assembles data to be returned to caller
#if HW_DIG_ROWS
PRINT("\ngetCol: (", buf);
#else
PRINT("\ngetRow: (", buf);
#endif
PRINT(", ", c);
PRINTS(") ");
if ((buf > LAST_BUFFER) || (c >= COL_SIZE))
return(0);
PRINTX("mask 0x", mask);
// for each digit data, pull out the column/row bit and place
// it in value. The loop creates the data in pixel coordinate order as it goes.
for (uint8_t i=0; i<ROW_SIZE; i++)
{
if (_matrix[buf].dig[HW_ROW(i)] & mask)
bitSet(value, i);
}
PRINTX(" value 0x", value);
return(value);
}
#if HW_DIG_ROWS
bool MD_MAX72XX::setColumn(uint8_t buf, uint8_t c, uint8_t value)
#else
bool MD_MAX72XX::setRow(uint8_t buf, uint8_t c, uint8_t value)
#endif
// c and value are in pixel coordinate order
{
#if HW_DIG_ROWS
PRINT("\nsetCol: (", buf);
#else
PRINT("\nsetRow: (", buf);
#endif
PRINT(", ", c);
PRINTX(") 0x", value);
if ((buf > LAST_BUFFER) || (c >= COL_SIZE))
return(false);
for (uint8_t i=0; i<ROW_SIZE; i++)
{
if (value & (1 << i)) // mask off next column value passed in and set it in the dig buffer
bitSet(_matrix[buf].dig[HW_ROW(i)], HW_COL(c));
else
bitClear(_matrix[buf].dig[HW_ROW(i)], HW_COL(c));
}
_matrix[buf].changed = ALL_CHANGED;
if (_updateEnabled) flushBuffer(buf);
return(true);
}
#if HW_DIG_ROWS
bool MD_MAX72XX::copyRow(uint8_t buf, uint8_t rSrc, uint8_t rDest)
#else
bool MD_MAX72XX::copyColumn(uint8_t buf, uint8_t rSrc, uint8_t rDest)
#endif
// Src and Dest are in pixel coordinates.
// if we are just copying digits there is no need to repackage any data
{
#if HW_DIG_ROWS
PRINT("\ncopyRow: (", buf);
#else
PRINT("\ncopyColumn: (", buf);
#endif
PRINT(", ", rSrc);
PRINT(", ", rDest);
PRINTS(") ");
if ((buf > LAST_BUFFER) || (rSrc >= ROW_SIZE) || (rDest >= ROW_SIZE))
return(false);
_matrix[buf].dig[HW_ROW(rDest)] = _matrix[buf].dig[HW_ROW(rSrc)];
bitSet(_matrix[buf].changed, HW_ROW(rDest));
if (_updateEnabled) flushBuffer(buf);
return(true);
}
#if HW_DIG_ROWS
uint8_t MD_MAX72XX::getRow(uint8_t buf, uint8_t r)
#else
uint8_t MD_MAX72XX::getColumn(uint8_t buf, uint8_t r)
#endif
// r is in pixel coordinates for this buffer
// returned value is in pixel coordinates
{
#if HW_DIG_ROWS
PRINT("\ngetRow: (", buf);
#else
PRINT("\ngetCol: (", buf);
#endif
PRINT(", ", r);
PRINTS(") ");
if ((buf > LAST_BUFFER) || (r >= ROW_SIZE))
return(0);
uint8_t value = HW_REV_COLS ? bitReverse(_matrix[buf].dig[HW_ROW(r)]) : _matrix[buf].dig[HW_ROW(r)];
PRINTX("0x", value);
return(value);
}
#if HW_DIG_ROWS
bool MD_MAX72XX::setRow(uint8_t buf, uint8_t r, uint8_t value)
#else
bool MD_MAX72XX::setColumn(uint8_t buf, uint8_t r, uint8_t value)
#endif
// r and value are in pixel coordinates
{
#if HW_DIG_ROWS
PRINT("\nsetRow: (", buf);
#else
PRINT("\nsetCol: (", buf);
#endif
PRINT(", ", r);
PRINTX(") 0x", value);
if ((buf > LAST_BUFFER) || (r >= ROW_SIZE))
return(false);
_matrix[buf].dig[HW_ROW(r)] = HW_REV_COLS ? bitReverse(value) : value;
bitSet(_matrix[buf].changed, HW_ROW(r));
if (_updateEnabled) flushBuffer(buf);
return(true);
}
bool MD_MAX72XX::transform(uint8_t buf, transformType_t ttype)
{
if (buf > LAST_BUFFER)
return(false);
if (!transformBuffer(buf, ttype))
return(false);
if (_updateEnabled) flushBuffer(buf);
return(true);
}
bool MD_MAX72XX::transformBuffer(uint8_t buf, transformType_t ttype)
{
uint8_t t[ROW_SIZE];
switch (ttype)
{
//--------------
case TSL: // Transform Shift Left one pixel element
#if HW_DIG_ROWS
for (uint8_t i=0; i<ROW_SIZE; i++)
#if HW_REV_COLS
_matrix[buf].dig[i] >>= 1;
#else
_matrix[buf].dig[i] <<= 1;
#endif
#else
#warning HW_DIG_ROWS=0
for (uint8_t i=ROW_SIZE; i>0; --i)
_matrix[buf].dig[i] = _matrix[buf].dig[i-1];
#endif
break;
//--------------
case TSR: // Transform Shift Right one pixel element
#if HW_DIG_ROWS
for (uint8_t i=0; i<ROW_SIZE; i++)
#if HW_REV_COLS
_matrix[buf].dig[i] <<= 1;
#else
_matrix[buf].dig[i] >>= 1;
#endif
#else
for (uint8_t i=0; i<ROW_SIZE-1; i++)
_matrix[buf].dig[i] = _matrix[buf].dig[i+1];
#endif
break;
//--------------
case TSU: // Transform Shift Up one pixel element
if (_wrapAround) // save the first row or a zero row
t[0] = getRow(buf, 0);
else
t[0] = 0;
#if HW_DIG_ROWS
for (uint8_t i=0; i<ROW_SIZE-1; i++)
copyRow(buf, i+1, i);
#else
for (int8_t i=ROW_SIZE-1; i>=0; i--)
_matrix[buf].dig[i] <<= 1;
#endif
setRow(buf, ROW_SIZE-1, t[0]);
break;
//--------------
case TSD: // Transform Shift Down one pixel element
if (_wrapAround) // save the last row or a zero row
t[0] = getRow(buf, ROW_SIZE-1);
else
t[0] = 0;
#if HW_DIG_ROWS
for (uint8_t i=ROW_SIZE; i>0; --i)
copyRow(buf, i-1, i);
#else
for (uint8_t i=0; i<ROW_SIZE; i++)
_matrix[buf].dig[i] >>= 1;
#endif
setRow(buf, 0, t[0]);
break;
//--------------
#if HW_DIG_ROWS
case TFLR: // Transform Flip Left to Right
#else
case TFUD: // Transform Flip Up to Down
#endif
for (uint8_t i=0; i<ROW_SIZE; i++)
_matrix[buf].dig[i] = bitReverse(_matrix[buf].dig[i]);
break;
//--------------
#if HW_DIG_ROWS
case TFUD: // Transform Flip Up to Down
#else
case TFLR: // Transform Flip Left to Right
#endif
for (uint8_t i=0; i<ROW_SIZE/2; i++)
{
uint8_t t = _matrix[buf].dig[i];
_matrix[buf].dig[i] = _matrix[buf].dig[ROW_SIZE-i-1];
_matrix[buf].dig[ROW_SIZE-i-1] = t;
}
break;
//--------------
case TRC: // Transform Rotate Clockwise
for (uint8_t i=0; i<ROW_SIZE; i++)
t[i] = getColumn(buf, COL_SIZE-1-i);
for (uint8_t i=0; i<ROW_SIZE; i++)
setRow(buf, i, t[i]);
break;
//--------------
case TINV: // Transform INVert
for (uint8_t i=0; i<ROW_SIZE; i++)
_matrix[buf].dig[i] = ~_matrix[buf].dig[i];
break;
default:
return(false);
}
_matrix[buf].changed = ALL_CHANGED;
return(true);
}