121 lines
2.8 KiB
C++
121 lines
2.8 KiB
C++
#include "TimerOne.h"
|
|
|
|
#define TIME_PER_LAYER_IN_US 700
|
|
#define BUS_START_PIN 22
|
|
#define CLOCK_START_PIN 30
|
|
#define SW_START_PIN 38
|
|
|
|
class Cube {
|
|
private:
|
|
static int layer_count;
|
|
static int brightness_count;
|
|
|
|
public:
|
|
// Every int represents a row of 8 LED status.
|
|
static int LED_status[8][8];
|
|
|
|
static void display() {
|
|
// Serial.println("Here");
|
|
if (brightness_count >= 2) {
|
|
digitalWrite(SW_START_PIN + layer_count, LOW);
|
|
layer_count = (layer_count + 1) % 8;
|
|
}
|
|
|
|
brightness_count = (brightness_count + 1) % 3;
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
for (int j = 0; j < 8; j++) {
|
|
// In LED_status:
|
|
// 0 = off
|
|
// 4 = brightest
|
|
digitalWrite(BUS_START_PIN + j,
|
|
((LED_status[layer_count][i] >> (j * 2)) & 3) >=
|
|
(3 - brightness_count));
|
|
}
|
|
digitalWrite(CLOCK_START_PIN + i, HIGH);
|
|
digitalWrite(CLOCK_START_PIN + i, LOW);
|
|
}
|
|
|
|
digitalWrite(SW_START_PIN + layer_count, HIGH);
|
|
}
|
|
|
|
static void set_status(int x, int y, int z, int brightness) {
|
|
if (x >= 8 || x < 0 || y >= 8 || y < 0 || z >= 8 || z < 0) {
|
|
return;
|
|
}
|
|
brightness %= 4;
|
|
LED_status[z][x] =
|
|
(LED_status[z][x] & (~(3 << (y * 2)))) | (brightness << (y * 2));
|
|
}
|
|
|
|
static int get_status(int x, int y, int z) {
|
|
return LED_status[z][x] >> (y * 2) & 3;
|
|
}
|
|
|
|
static void clear() {
|
|
for (int i = 0; i < 8; i++) {
|
|
for (int j = 0; j < 8; j++) {
|
|
LED_status[i][j] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void draw_line(int x, int y, int z, int length, int direction, int brightness) {
|
|
if (x >=8 || x < 0 || y >=8 || y < 0 || z >= 8 || z < 0) {
|
|
return;
|
|
}
|
|
if (direction >= 3 || direction < 0 || length <= 0 || brightness >= 4 || brightness < 0) {
|
|
return;
|
|
}
|
|
// 0: x
|
|
// 1: y
|
|
// 2: z
|
|
switch (direction) {
|
|
case 0:
|
|
for (int i = 0; i < length; i++) {
|
|
set_status(x + i, y, z, brightness);
|
|
}
|
|
break;
|
|
case 1:
|
|
for (int i = 0; i < length; i++) {
|
|
set_status(x, y + i, z, brightness);
|
|
}
|
|
break;
|
|
case 2:
|
|
for (int i = 0; i < length; i++) {
|
|
set_status(x, y, z + i, brightness);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
int Cube::layer_count = 0;
|
|
int Cube::brightness_count = 0;
|
|
int Cube::LED_status[8][8] = {0};
|
|
int bright = 3;
|
|
|
|
void setup() {
|
|
for (int i = 22; i < 46; i++) {
|
|
pinMode(i, OUTPUT);
|
|
}
|
|
|
|
Timer1.initialize();
|
|
Timer1.setPeriod(TIME_PER_LAYER_IN_US);
|
|
Timer1.attachInterrupt(Cube::display);
|
|
|
|
Serial.begin(115200);
|
|
}
|
|
|
|
void loop() {
|
|
|
|
for (int i = 7; i >= 0; i--) {
|
|
for (int j = 0; j < 8; j++) {
|
|
for (int k = 0; k < 8; k++) {
|
|
Cube::set_status(i, j, k, bright);
|
|
delay(5);
|
|
}
|
|
}
|
|
}
|
|
bright = (bright + 1) % 4;
|
|
} |