-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplexer.h
104 lines (84 loc) · 2.85 KB
/
multiplexer.h
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
class Multiplexer {
public:
static void setMatrix(bool[][6][6], bool);
static bool nextLayer();
private:
static bool matrix[6][6][6];
static bool newMatrix[6][6][6];
static short currentLayer, lastLayer;
static short outputData[6];
static void constructData(int);
static void sendToCube();
};
bool Multiplexer::matrix[6][6][6] = {{{0}}};
bool Multiplexer::newMatrix[6][6][6] = {{{0}}};
short Multiplexer::currentLayer = 0, Multiplexer::lastLayer = 0;
short Multiplexer::outputData[6] = {0};
void Multiplexer::setMatrix(bool inMatrix[][6][6], bool mode) {
// Mode 0 takes a brand-new matrix and queues it to be shown
if (!mode)
memcpy(&newMatrix[0][0][0], &inMatrix[0][0][0], (6*6*6)*sizeof(bool));
// Mode 1 sets the display matrix to some new matrix (presumably newMatrix)
else
memcpy(&matrix[0][0][0], &inMatrix[0][0][0], (6*6*6)*sizeof(bool));
return;
}
bool Multiplexer::nextLayer() {
// Build the data array for the current layer
constructData(currentLayer);
if (DEBUG) {
Serial.print("Layer ");
Serial.print(currentLayer);
Serial.print(": ");
for (int num : outputData) {
Serial.print(num);
Serial.print(" ");
}
Serial.println("");
}
// Send the data to the shift registers and turn on the current layer
sendToCube();
// Update the state variables and return true if we're returning to the bottom
lastLayer = currentLayer;
if (currentLayer == 5) {
currentLayer = 0;
setMatrix(newMatrix, 1);
return true;
}
else {
currentLayer++;
return false;
}
}
void Multiplexer::constructData(int layer) {
// Place the bits of data into the data array based on the pin configuration setting
bool temp_data[48] = {0};
for (int r = 0; r < 6; r++) {
for (int c = 0; c < 6; c++) {
temp_data[PIN_CONFIGURATION[r][c] - 1] = matrix[layer][r][c];
}
}
// Convert the binary array we just created into an array of integers
for (int sr = 0, arr_pos = 0; sr < 6; sr++) {
// For each byte int the private data array, convert it to an int and save it to the output array
int out_num = 0;
for (int b = 7; b >= 0; b--) {
out_num += temp_data[arr_pos] * 1 << b;
arr_pos++;
}
outputData[sr] = out_num;
}
return;
}
void Multiplexer::sendToCube() {
// Send the the data to the shift registers
digitalWrite(ShiftLatchPin, LOW);
for (int i = 5; i >= 0; i--) {
shiftOut(ShiftDataPin, ShiftClockPin, LSBFIRST, outputData[i]);
}
// Update the layer transistors and display the output of the shift registers
digitalWrite(LAYER_PINS[lastLayer], LOW);
digitalWrite(ShiftLatchPin, HIGH);
digitalWrite(LAYER_PINS[currentLayer], HIGH);
return;
}