forked from freetronics/Cube4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCube.cpp
207 lines (156 loc) · 5.4 KB
/
Cube.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
/*
* File: Cube.cpp
* Version: 0.0
* Author: Andy Gelme (@geekscape) and Marc Alexander (@mtronic)
* License: GPLv3
*
* Low-level Cube drivers for 74154 and MY9262.
*
* ToDo
* ~~~~
* - Clean-up new loadColorPlaneZ() implementation.
* - Test current gain control.
*/
#include "Cube.h"
static volatile bool suspended = false;
byte currentColor = COLOR_PLANE_RED;
byte currentPlaneZ = 0;
rgb_t led[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE]; // [x][y][z].color[]
void loadColorPlaneZ(byte color, byte planeZ);
//long cubeLastTime = 0;
//long cubeTimer1Period = 0;
ISR(TIMER1_OVF_vect) { // 116 microseconds
//digitalWrite(7, digitalRead(7) ^ 1); // Diagnosis via oscilloscope
if(suspended)
return;
if (++ currentColor > COLOR_PLANE_BLUE) {
currentColor = COLOR_PLANE_RED;
currentPlaneZ = (currentPlaneZ + 1) % CUBE_SIZE;
}
loadColorPlaneZ(currentColor, currentPlaneZ);
//long thisTime = micros();
//cubeTimer1Period = thisTime - cubeLastTime;
//cubeLastTime = thisTime;
serialHandler();
}
Cube::Cube() {
}
void Cube::begin(
byte serialPort,
long baudRate) {
serialBegin(serialPort, baudRate);
//pinMode(7, OUTPUT); // Diagnosis via oscilloscope
// Assume these pins are all in consecutive order
for (byte pin = PIN_LED_EN; pin <= PIN_LED_LAT; pin ++) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
digitalWrite(PIN_LED_EN, HIGH); // Disable 74154
pinMode(MOSI, OUTPUT);
pinMode(SCK, OUTPUT);
// MY9262 Write Command
// --------------------
// 15: Data format Mode0: 0
// 14-13: Reserved: 0
// 12: APDM mode: 0
// 11: Counter reset: 0
// 10: Synchronization = manual: 1 The default is "auto" = 0
// 9: Global current HC: 1
// 8-4: Global current data: 01011
// 3-2: Reserved: 00
// 1: Sleep mode = normal: 0
// 0: Reserved: 0
//my9262WriteCommand(0x0d60); // Change synchronization to be "manual"
my9262WriteCommand(0x0ff0); // Highest current
//my9262WriteCommand(0x0800); // Lowest current
all(BLACK);
initializeTimer1(TIMER1_PERIOD);
}
void Cube::initializeTimer1(
long period) { // microseconds
TCCR1A = 0;
TCCR1B = _BV(WGM13);
unsigned char clockSelectBits;
long cycles = (F_CPU / 2000000) * period;
if(cycles < RESOLUTION) clockSelectBits = _BV(CS10);
else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11);
else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11) | _BV(CS10);
else if((cycles >>= 2) < RESOLUTION) clockSelectBits = _BV(CS12);
else if((cycles >>= 2) < RESOLUTION) clockSelectBits = _BV(CS12) | _BV(CS10);
else cycles = RESOLUTION - 1, clockSelectBits = _BV(CS12) | _BV(CS10);
char oldSREG = SREG;
cli();
ICR1 = cycles;
SREG = oldSREG;
TIMSK1 = _BV(TOIE1);
TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
TCCR1B |= clockSelectBits;
}
void Cube::my9262WriteCommand(
unsigned int command) {
digitalWrite(PIN_LED_LAT, LOW);
for (byte b = 0; b < 16; b ++) {
digitalWrite(MOSI, (command & 0x8000) == 0x8000); // MSB first
command <<= 1;
if (b == 6) digitalWrite(PIN_LED_LAT, HIGH); // MY9262 Write Command latch
digitalWrite(SCK, HIGH);
digitalWrite(SCK, LOW);
}
digitalWrite(PIN_LED_LAT, LOW);
}
void loadColorPlaneZ(
byte color,
byte planeZ) {
bool spe_set = (SPCR & (1 << SPE)); // Record the current SPI enable state
SPCR = ((1 << SPE) | (1 << MSTR)); // TODO: Set MSTR in initializeTimer1()
SPSR |= (1 << SPI2X); // TODO: Move to initializeTimer1()
for (byte w = 0; w < 15; w ++) {
SPI.transfer(led[w % CUBE_SIZE][w >> 2][planeZ].color[color]);
SPI.transfer(0x00);
// MY9262 Data latch
PORTD |= (1 << 6); // digitalWrite(PIN_LED_LAT, HIGH);
PORTB |= (1 << 1); // digitalWrite(SCK, HIGH);
PORTB &= ~(1 << 1); // digitalWrite(SCK, LOW);
PORTD &= ~(1 << 6); // digitalWrite(PIN_LED_LAT, LOW);
}
SPCR &= ~(1 << SPE); // SPI.end(), disable SPI so we can bit-bang MOSI
byte value = led[3][3][planeZ].color[color];
for (byte b = 0; b < 8; b ++) { // LSB first
if (value & 0x80) { // digitalWrite(MOSI, (value & 0x80) == 0x80);
PORTB |= (1 << 2);
}
else {
PORTB &= ~(1 << 2);
}
value <<= 1;
PORTB |= (1 << 1); // digitalWrite(SCK, HIGH);
PORTB &= ~(1 << 1); // digitalWrite(SCK, LOW);
}
PORTB &= ~(1 << 2); // digitalWrite(MOSI, LOW);
for (byte b = 8; b < 16; b ++) {
if (b == 14) { // MY9262 Global latch
PORTD |= (1 << 6); // digitalWrite(PIN_LED_LAT, HIGH);
}
PORTB |= (1 << 1); // digitalWrite(SCK, HIGH);
PORTB &= ~(1 << 1); // digitalWrite(SCK, LOW);
}
// Disable 7154, MY9262 latch complete
PORTE |= (1 << 6); // digitalWrite(PIN_LED_EN, HIGH);
PORTD &= ~(1 << 6); // digitalWrite(PIN_LED_LAT, LOW);
byte colorPlaneZSelect = color + (planeZ * 3);
PORTB &= B00001111; // Assume these pins are all in consecutive order
PORTB |= (colorPlaneZSelect << 4);
// Enable 74154
PORTE &= ~(1 << 6); // digitalWrite(PIN_LED_EN, LOW);
// Re-enable SPI hardware if it was enabled
if(spe_set)
SPCR |= (1 << SPE);
}
void Cube::suspend()
{
suspended = true;
}
void Cube::resume()
{
suspended = false;
}