-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMaxMatrix.cpp
222 lines (195 loc) · 4.21 KB
/
MaxMatrix.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
/*
* MaxMatrix
* Version 1.0 Feb 2013
* Copyright 2013 Oscar Kin-Chung Au
*/
#include "Arduino.h"
#include "MaxMatrix.h"
MaxMatrix::MaxMatrix(byte _data, byte _load, byte _clock, byte _num)
{
data = _data;
load = _load;
clock = _clock;
num = _num;
for (int i=0; i<80; i++)
buffer[i] = 0;
}
void MaxMatrix::init()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(load, OUTPUT);
digitalWrite(clock, HIGH);
setCommand(max7219_reg_scanLimit, 0x07);
setCommand(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
setCommand(max7219_reg_shutdown, 0x01); // not in shutdown mode
setCommand(max7219_reg_displayTest, 0x00); // no display test
// empty registers, turn all LEDs off
clearMatrix();
setIntensity(0x0f); // the first 0x0f is the value you can set
}
void MaxMatrix::setIntensity(byte intensity)
{
setCommand(max7219_reg_intensity, intensity);
}
void MaxMatrix::clearMatrix()
{
for (int i=0; i<8; i++)
setColumnAll(i,0);
for (int i=0; i<80; i++)
buffer[i] = 0;
}
void MaxMatrix::setCommand(byte command, byte value)
{
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
shiftOut(data, clock, MSBFIRST, command);
shiftOut(data, clock, MSBFIRST, value);
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}
void MaxMatrix::setColumn(byte col, byte value)
{
int n = col / 8;
int c = col % 8;
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
if (i == n)
{
shiftOut(data, clock, MSBFIRST, c + 1);
shiftOut(data, clock, MSBFIRST, value);
}
else
{
shiftOut(data, clock, MSBFIRST, 0);
shiftOut(data, clock, MSBFIRST, 0);
}
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
buffer[col] = value;
}
void MaxMatrix::setColumnAll(byte col, byte value)
{
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
shiftOut(data, clock, MSBFIRST, col + 1);
shiftOut(data, clock, MSBFIRST, value);
buffer[col * i] = value;
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}
void MaxMatrix::setDot(byte col, byte row, byte value)
{
bitWrite(buffer[col], row, value);
int n = col / 8;
int c = col % 8;
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
if (i == n)
{
shiftOut(data, clock, MSBFIRST, c + 1);
shiftOut(data, clock, MSBFIRST, buffer[col]);
}
else
{
shiftOut(data, clock, MSBFIRST, 0);
shiftOut(data, clock, MSBFIRST, 0);
}
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}
/* void MaxMatrix::writeSprite(int x, int y, const byte* sprite)
{
int w = sprite[0];
int h = sprite[1];
if (h == 8 && y == 0)
for (int i=0; i<w; i++)
{
int c = x + i;
if (c>=0 && c<80)
setColumn(c, sprite[i+2]);
}
else
for (int i=0; i<w; i++)
for (int j=0; j<h; j++)
{
int c = x + i;
int r = y + j;
if (c>=0 && c<80 && r>=0 && r<8)
setDot(c, r, bitRead(sprite[i+2], j));
}
}
void MaxMatrix::reload()
{
for (int i=0; i<8; i++)
{
int col = i;
digitalWrite(load, LOW);
for (int j=0; j<num; j++)
{
shiftOut(data, clock, MSBFIRST, i + 1);
shiftOut(data, clock, MSBFIRST, buffer[col]);
col += 8;
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}
}
void MaxMatrix::shiftLeft(bool rotate, bool fill_zero)
{
byte old = buffer[0];
int i;
for (i=0; i<80; i++)
buffer[i] = buffer[i+1];
if (rotate) buffer[num*8-1] = old;
else if (fill_zero) buffer[num*8-1] = 0;
reload();
}
void MaxMatrix::shiftRight(bool rotate, bool fill_zero)
{
int last = num*8-1;
byte old = buffer[last];
int i;
for (i=79; i>0; i--)
buffer[i] = buffer[i-1];
if (rotate) buffer[0] = old;
else if (fill_zero) buffer[0] = 0;
reload();
}
void MaxMatrix::shiftUp(bool rotate)
{
for (int i=0; i<num*8; i++)
{
bool b = buffer[i] & 1;
buffer[i] >>= 1;
if (rotate) bitWrite(buffer[i], 7, b);
}
reload();
}
void MaxMatrix::shiftDown(bool rotate)
{
for (int i=0; i<num*8; i++)
{
bool b = buffer[i] & 128;
buffer[i] <<= 1;
if (rotate) bitWrite(buffer[i], 0, b);
}
reload();
}
*/
// rutina para Zowi, para meter sus caritas en la matriz de 8
void MaxMatrix::writeFull(unsigned long value) {
for (int r=0; r<5;r++){
for (int c=0; c<6; c++){
setDot(6-c,7-r,(1L & (value >> r*6+c)));
}
}
}