-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoDLEDMatrixCascade.cpp
More file actions
178 lines (160 loc) · 4.78 KB
/
TwoDLEDMatrixCascade.cpp
File metadata and controls
178 lines (160 loc) · 4.78 KB
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
#include "TwoDLEDMatrixCascade.h"
byte reverseBits(byte);
TwoDLEDMatrixCascade::TwoDLEDMatrixCascade(uint8_t dataPin, uint8_t clkPin, uint8_t csPin, int numRowDevices, int numColumnDevices) : ledControlRef(dataPin, clkPin, csPin, numRowDevices * numColumnDevices) {
this->numRowDevices = numRowDevices;
this->numColumnDevices = numColumnDevices;
this->maxRow = (numRowDevices * 8) - 1;
this->maxColumn = (numColumnDevices * 8) - 1;
for (int i = 0; i < MAX_DEVICES; i++) {
this->rotations[i] = 0;
}
}
void TwoDLEDMatrixCascade::shutdown(bool status) {
int deviceCount = ledControlRef.getDeviceCount();
for (int i = 0; i < deviceCount; i++) {
ledControlRef.shutdown(i, status);
}
}
void TwoDLEDMatrixCascade::setIntensity(uint8_t intensity) {
int deviceCount = ledControlRef.getDeviceCount();
for (int i = 0; i < deviceCount; i++) {
ledControlRef.setIntensity(i, intensity);
}
}
void TwoDLEDMatrixCascade::clearDisplay() {
int deviceCount = ledControlRef.getDeviceCount();
for (int i = 0; i < deviceCount; i++) {
ledControlRef.clearDisplay(i);
}
}
void TwoDLEDMatrixCascade::setRotation(int deviceNum, uint8_t value) {
rotations[deviceNum] = value;
}
void TwoDLEDMatrixCascade::setLed(int row, int col, boolean state) {
int deviceID = getDeviceIndex(row, col);
row = row % 8;
col = col % 8;
switch (rotations[deviceID] % 4) {
case 0:
ledControlRef.setLed(deviceID, row, col, state);
break;
case 1:
col = 7 - col;
ledControlRef.setLed(deviceID, col, row, state);
break;
case 2:
row = 7 - row;
col = 7 - col;
ledControlRef.setLed(deviceID, row, col, state);
break;
case 3:
row = 7 - row;
ledControlRef.setLed(deviceID, col, row, state);
break;
}
}
void TwoDLEDMatrixCascade::setRow(int row, uint8_t values[]) {
int devices[MAX_COLUMNS];
getDeviceIndexesInaRow(row, devices);
row = row % 8;
for (int i = 0; i < numColumnDevices; i++) {
switch (rotations[devices[i]] % 4) {
case 0:
ledControlRef.setRow(devices[i], row, values[i]);
break;
case 1:
ledControlRef.setColumn(devices[i], row, reverseBits(values[i]));
break;
case 2:
ledControlRef.setRow(devices[i], 7 - row, reverseBits(values[i]));
break;
case 3:
ledControlRef.setColumn(devices[i], 7 - row, values[i]);
break;
}
}
}
void TwoDLEDMatrixCascade::setColumn(int col, uint8_t values[]) {
int devices[MAX_ROWS];
getDeviceIndexesInaColumn(col, devices);
col = col % 8;
for (int i = 0; i < numRowDevices; i++) {
switch (rotations[devices[i]] % 4) {
case 0:
ledControlRef.setColumn(devices[i], col, values[i]);
break;
case 1:
ledControlRef.setRow(devices[i], 7-col, values[i]);
break;
case 2:
ledControlRef.setColumn(devices[i], 7 - col, reverseBits(values[i]));
break;
case 3:
ledControlRef.setRow(devices[i], col, reverseBits(values[i]));
break;
}
}
}
int TwoDLEDMatrixCascade::getDeviceIndex(int row, int col) {
if (row > maxRow || col > maxColumn)
return -1;
int rowBlock = row / 8;
//because of spiral wiring
//even indexed row of devices are numbered from left to right
//while odd indexed row of devices are numbered right to left
if (rowBlock % 2 == 0) {
return col / 8 + (rowBlock * numColumnDevices);
} else {
return (maxColumn - col) / 8 + (rowBlock * numColumnDevices);
}
}
void TwoDLEDMatrixCascade::getDeviceIndexesInaRow(int row, int devices[]) {
if (row > maxRow)
return;
int rowBlock = row / 8;
int firstDevice;
//because of spiral wiring
//even indexed row of devices are numbered from left to right
//while odd indexed row of devices are numbered right to left
if (rowBlock % 2 == 0) {
firstDevice = rowBlock * numColumnDevices;
for (int i = 0; i < numColumnDevices; i++) {
devices[i] = firstDevice++;
}
} else {
int firstDevice = ((rowBlock * numColumnDevices) + numColumnDevices) - 1;
for (int i = 0; i < numColumnDevices; i++) {
devices[i] = firstDevice--;
}
}
}
void TwoDLEDMatrixCascade::getDeviceIndexesInaColumn(int col, int devices[]) {
if (col > maxColumn)
return;
int columnBlock = col / 8;
int firstDevice;
for (int i = 0; i < numRowDevices; i++) {
if (i % 2 == 0) {
firstDevice = i * numColumnDevices;
devices[i] = firstDevice + columnBlock;
} else {
firstDevice = ((i * numColumnDevices) + numColumnDevices) - 1;
devices[i] = firstDevice - columnBlock;
}
}
}
byte reverseBits(byte num)
{
byte count = 8 - 1;
byte reverse_num = num;
num >>= 1;
while (num)
{
reverse_num <<= 1;
reverse_num |= num & 1;
num >>= 1;
count--;
}
reverse_num <<= count;
return reverse_num;
}