-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLEDCubeDisplay.ino
executable file
·204 lines (179 loc) · 10.5 KB
/
LEDCubeDisplay.ino
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
//###############################################################################
//# LEDCube - Display Driver #
//###############################################################################
//# Copyright 2015 - 2016 Dirk Heisswolf #
//# This file is part of the LEDCube project. #
//# #
//# LEDCube is free software: you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation, either version 3 of the License, or #
//# (at your option) any later version. #
//# #
//# LEDCube 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 General Public License for more details. #
//# #
//# You should have received a copy of the GNU General Public License #
//# along with LEDCube. If not, see <http://www.gnu.org/licenses/>. #
//###############################################################################
//# Description: #
//# #
//# Y #
//# / C3---C7---C11--C15 Anode connections: #
//# --X / / / / | C0-C15: Q outputs of 74HC595s #
//# | C2---C6---C10--C14 @ #
//# Z / / / / |/| 74HC595 control: #
//# C1---C5---C9---C13 @ @ DS (serial data input): 3 (PD3) #
//# / / / / |/|/| OE (output enable, active low): 2 (PD2) #
//# L0 C0---C4---C8---C12 @ @ @ ST (storage clock): 1 (PD1) #
//# | | | | /|/|/ SH (shift clock): 0 (PD0) #
//# L1 C0---C4---C8---C12 @ @ #
//# | | | | /|/ Cathode connections: #
//# L2 C0---C4---C8---C12 @ L0: 4 (PD4) #
//# | | | | / L1: 5 (PD5) #
//# L3 C0---C4---C8---C12 L2: 6 (PD6) #
//# L3: 7 (PD7) #
//# #
//# LED state format (unsigned 64-bit integer): #
//# #
//# C15 C14 C13 C12 #
//# +-----------+-----------+-----------+-----------+ #
//# |L3 L2 L1 L0|L3 L2 L1 L0|L3 L2 L1 L0|L3 L2 L1 L0| #
//# +-----------+-----------+-----------+-----------+ #
//# 63 60 59 56 55 52 51 48 #
//# C11 C10 C9 C8 #
//# +-----------+-----------+-----------+-----------+ #
//# |L3 L2 L1 L0|L3 L2 L1 L0|L3 L2 L1 L0|L3 L2 L1 L0| #
//# +-----------+-----------+-----------+-----------+ #
//# 47 44 43 40 39 36 35 32 #
//# C7 C6 C5 C4 #
//# +-----------+-----------+-----------+-----------+ #
//# |L3 L2 L1 L0|L3 L2 L1 L0|L3 L2 L1 L0|L3 L2 L1 L0| #
//# +-----------+-----------+-----------+-----------+ #
//# 31 28 27 24 23 20 19 16 #
//# C3 C2 C1 C0 #
//# +-----------+-----------+-----------+-----------+ #
//# |L3 L2 L1 L0|L3 L2 L1 L0|L3 L2 L1 L0|L3 L2 L1 L0| #
//# +-----------+-----------+-----------+-----------+ #
//# 15 12 11 8 7 4 3 0 #
//# #
//###############################################################################
//# Version History: #
//# November 30, 2015 #
//# - Initial release #
//###############################################################################
#include <avr/sleep.h>
#include <avr/power.h>
#include "LEDCube.h"
// General definitions
//====================
//Framerate
#define SUBFRAMES 60/FRAMERATE //subframes per frame
//FIFI depth
#define FIFODEPTH 16 //number of FIFO entries
// Variables
//==========
//FIFO
ledState fifo[FIFODEPTH]; //FIFO buffer
unsigned int fifoIn = 0; //index of next free cell in FIFO buffer
unsigned int fifoOut = 0; //index of oldest entry in FIFO buffer
// FIFO is empty if (fifoIn == fifoOut)
//Display state
byte columnCount = 0; //current column (display inactive if (column < 0)
byte subframeCount = SUBFRAMES; //current subframe
//Temporary buffers
ledState currentFrame = 0xFEDCBA9876543210; //current frame
byte nextPortD = 0; //column buffer
// Setup routine
//==============
void dispSetup() {
//Initialize on-board LED (pin 13)
pinMode(13, OUTPUT); //enable LED output
digitalWrite(13, LOW); //turn LED off
//Ports and shift registers
PORTD = L3|L2|L1|L0| OE; //disable shift register outputs
DDRD = L3|L2|L1|L0|DS|OE|ST|SH; //set entire port to output
for (int i=0; i < 16; i++) { //shift in 16 zeros
PORTD = L3|L2|L1|L0| OE|SH; //toggle shift clock
PORTD = L3|L2|L1|L0| OE;
}
PORTD = L3|L2|L1|L0|DS| ST; //toggle storage clock, enable outputs, and assert shift input
PORTD = L3|L2|L1|L0|DS| SH; //toggle shift clock
PORTD = L3|L2|L1|L0;
//Timer 2
TCCR2A = (1 << WGM21); //CTC mode, no pin toggling
TCCR2B = (1 << CS22) | //set timer clock to 15.625kHz
(1 << CS21) |
(1 << CS20);
OCR2A = 15625/(FRAMERATE*SUBFRAMES); //set interrupt frequency
TIMSK2 = (1 << OCIE2A); //enable output compare A interrupt
//Power modes
power_timer2_enable(); //make sure timer2 is powered up
set_sleep_mode(SLEEP_MODE_IDLE); //use idle mode
sleep_enable(); //enable sleep instruction
}
// Queue frame
//============
//Queue single frame
void dispQueueFrame(ledState frame) {
//Copy new frame into FIFO buffer
fifo[fifoIn] = frame;
//Start of atomic sequence
noInterrupts(); //disable interrupts
//Wait until there is room in the queue
while(((fifoOut-fifoIn)%FIFODEPTH) == 1) {//repeat as long as fifo is empty
WAIT_FOR_INTERRUPT(); //wait for anything to happen
noInterrupts(); //disable interrupts
}
//Advance in-index
fifoIn = (fifoIn+1)%FIFODEPTH; //increment and wrap index
//End of atomic sequence
interrupts(); //enable interrupts
}
//Queue frame multiple (n) times
void dispQueueFrames(ledState frame, byte n) {
while (n--) {
dispQueueFrame(frame);
}
}
// Interrupt Service Routine
//==========================
ISR(TIMER2_COMPA_vect){ //timer2 output compare A interrupt
digitalWrite(13, HIGH); //turn LED onduring the execution of the ISR
//Local Variables
byte tmpOut;
//Drive precalculated column pattern
tmpOut = PORTD; //shift column
tmpOut = (tmpOut & 0xF8) | SH; // determine shift register inputs first
PORTD = tmpOut; // then dreive all outputs at once
PORTD = nextPortD; //drive precalculated column pattern
//Switch to next column
columnCount++; //increment column counter
if (columnCount < COLUMNS) { //check for end of subframe
//Determine next column pattern in sub-frame
nextPortD = ((~(currentFrame >> (4*(columnCount-1))) & 0xF0) | ST);//update column buffer
} else {
//Subframe is complete
columnCount = 0; //reset column counter
subframeCount--; //decrement subframe count
//Update frame
if (fifoIn != fifoOut) { //check if a new frame is in the queue
// if (subframeCount == 1) { //check for next to last subframe
// //Fade frames on next to last subframe
// currentFrame |= fifo[fifoOut]; //merge current and next frame
// } else
if (subframeCount == 0) { //check for last subframe
//Switch to next frame
subframeCount = SUBFRAMES; //reset subframe counter
currentFrame = fifo[fifoOut]; //get next frame
fifoOut = ((fifoOut+1)%FIFODEPTH); //remove frame from FIFO
}
}
//Determine next column pattern in sub-frame
nextPortD = ((~(currentFrame << 4) & 0xF0) | ST);//update column buffer
//assert shift register input
PORTD |= DS; //assert DS
}
digitalWrite(13, LOW); //turn LED off
}