yank 发表于 2016-6-10 16:21:40

8*8*8 LED光立方_我的世界

本帖最后由 yank 于 2016-6-10 16:21 编辑

以下是我毕业设计做的一个光立方,分享给大家一起交流学习。显示的内容并不酷炫啦,不过呢是很用心的。动画表达的是我自己写的一首小诗,我毕设的主题呢就是想体现“工程艺术”(呃呃呃,虽然我的制作并不怎么艺术,不过还是有一颗追求艺术的心啦)。
我录制了视频的,不过好像不可以从本地上传,就给大家分享几张动画显示的图片吧(十二星座 星图)



/*******************************************************
8*8*8 LED光立方控制程序V1.0
接    线:
                        22——29--->D0——D7(锁存数据端)
                        30——37--->CLK(U1-U8锁存使能)
                        38——45--->B0——B7(ULN2803输入端)
控 制 板:
                        Arduino Mega2560
显示内容:
                        动画总共分17个场景,动画显示的主题是一首
                        小诗,17个场景为诗中的意境(诗附在后面)
参考程序:
                        _3D_led(在某一论坛看见的,不过忘记来源了)
特    色:
                        十二星座图(这部分是本人花了很多心思了时间
                        整理的,希望大家能喜欢)

                                                                        by--于江湖(yank)
                                                                        2016.06.03
*********************************************************/

#include "TimerThree.h"
#include <avr/interrupt.h>
#include <string.h>
#define AXIS_X 1
#define AXIS_Y 2
#define AXIS_Z 3
//引脚分配
char pin_D = {22, 23, 24, 25, 26, 27, 28, 29};//锁存器数据端 D0~D7
char pin_CLK = {30, 31, 32, 33, 34, 35, 36, 37};//锁存使能   U1~U8
char pin_B = {38, 39, 40, 41, 42, 43, 44, 45};//共阴极       B0~B7
volatile unsigned char cube;//状态存储数组
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////数组部分///////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//数字部分
unsigned char Num_0 = {{0, 0}, {0, 0}, {60, 0}, {36, 0}, {36, 0}, {36, 0}, {60, 0}, {0, 0}};
unsigned char Num_1 = {{0, 0}, {0, 0}, {8, 0}, {8, 0}, {8, 0}, {8, 0}, {8, 0}, {0, 0}};
unsigned char Num_2 = {{0, 0}, {0, 0}, {60, 0}, {32, 0}, {60, 0}, {4, 0}, {60, 0}, {0, 0}};
unsigned char Num_3 = {{0, 0}, {0, 0}, {60, 0}, {4, 0}, {60, 0}, {4, 0}, {60, 0}, {0, 0}};
unsigned char Num_4 = {{0, 0}, {0, 0}, {4, 0}, {4, 0}, {60, 0}, {36, 0}, {36, 0}, {0, 0}};
unsigned char Num_5 = {{0, 0}, {0, 0}, {60, 0}, {4, 0}, {60, 0}, {32, 0}, {60, 0}, {0, 0}};
unsigned char Num_6 = {{0, 0}, {0, 0}, {60, 0}, {36, 0}, {60, 0}, {32, 0}, {60, 0}, {0, 0}};
unsigned char Num_7 = {{0, 0}, {0, 0}, {4, 0}, {4, 0}, {4, 0}, {4, 0}, {60, 0}, {0, 0}};
unsigned char Num_8 = {{0, 0}, {0, 0}, {60, 0}, {36, 0}, {60, 0}, {36, 0}, {60, 0}, {0, 0}};
unsigned char Num_9 = {{0, 0}, {0, 0}, {60, 0}, {4, 0}, {60, 0}, {36, 0}, {60, 0}, {0, 0}};
//人物部分
unsigned char Tang = {{0}, {0}, {36, 36, 36, 60, 60, 60, 255, 24}, {36, 36, 36, 60, 60, 60, 255, 24}, {0}, {0}, {0}, {0}};
unsigned char Zuo = {{0}, {0}, {36, 36, 36, 0}, {36, 36, 36, 60, 60,}, {0, 0, 0, 60, 60, 0}, {0, 0, 0, 60, 60, 0}, {0, 0, 0, 255, 255, 0}, {0, 0, 0, 24, 24, 0}};
unsigned char Zhan = {{36, 36, 0}, {36, 36, 0}, {36, 36, 0}, {60, 60, 0}, {60, 60, 0}, {60, 60, 0}, {255, 255, 0}, {24, 24, 0}};
//窗帘部分
unsigned char C_begin;
unsigned char C_end;
unsigned char C_yundong;
//下雨部分
unsigned char Y_begin;
unsigned char Y_end;
//数组初始化
void ini_shuzu()
{
for (char i = 0; i < 8; i++)
    for (char k = 0; k < 8; k++)
    {
      C_begin = 255;
      C_end = 255;
    }
for (char k = 0; k < 8; k++)
{
    //下雨部分
    Y_begin = 255;
    Y_end = 255;
    Y_end = 255;
    //窗帘运动
    C_yundong = 231;
    C_yundong = 195;
    C_yundong = 129;
    C_yundong = 129;
    C_yundong = 129;
    C_yundong = 129;
    C_yundong = 129;
    C_yundong = 129;
}
}
//星座部分
unsigned char X_mj = {{8, 0, 0, 0}, {16, 0, 0, 0}, {0, 32, 0, 0}, {0, 4, 0, 0}, {0, 64, 0, 0}, {2, 16, 0, 0}, {0, 100, 0, 0}, {1, 128, 0, 0}};//摩羯
unsigned char X_sp = {{0, 128, 0, 0}, {0, 128, 0, 0}, {0, 224, 0, 0}, {0, 236, 0, 0}, {0, 115, 0, 0}, {4, 4, 0, 0}, {8, 56, 0, 0}, {0, 16, 0, 0}};//水瓶
unsigned char X_sy = {{0, 64, 0, 0}, {0, 96, 0, 0}, {0, 24, 0, 0}, {0, 71, 0, 0}, {0, 3, 0, 0}, {0, 1, 0, 0}, {0, 192, 0, 0}, {0, 128, 0, 0}};//双鱼
unsigned char X_by = {{0, 0, 0, 0}, {0, 0, 64, 0}, {0, 0, 0, 0}, {0, 1, 0, 0}, {0, 2, 0, 0}, {8, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};//白羊
unsigned char X_jn = {{0, 0, 34, 0}, {0, 0, 32, 0}, {0, 0, 20, 0}, {32, 0, 8, 0}, {0, 128, 16, 0}, {0, 0, 4, 0}, {0, 64, 2, 0}, {0, 0, 0, 1}};//金牛
unsigned char X_dz = {{32, 64, 1, 0}, {0, 6, 16, 0}, {0, 8, 160, 0}, {0, 0, 64, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 234, 0}, {80, 0, 16, 0}};//双子
unsigned char X_jx = {{0, 0, 0, 0}, {0, 64, 0, 0}, {0, 0, 4, 0}, {24, 0, 0, 0}, {24, 0, 0, 0}, {0, 32, 0, 0}, {0, 2, 0, 0}, {0, 0, 0, 0}};//巨蟹
unsigned char X_sz = {{0, 0, 32, 0}, {0, 0, 64, 0}, {8, 128, 49, 0}, {0, 64, 32, 0}, {0, 0, 8, 0}, {0, 16, 0, 0}, {0, 0, 20, 0}, {0, 0, 10, 0}};//狮子
unsigned char X_cn = {{0, 0, 0, 0}, {32, 0, 0, 0}, {0, 8, 80, 0}, {0, 0, 5, 0}, {0, 0, 193, 0}, {0, 32, 10, 0}, {0, 0, 192, 0}, {0, 8, 0, 0}};//处女
unsigned char X_tc = {{0, 0, 64, 0}, {0, 0, 32, 0}, {0, 0, 3, 0}, {0, 0, 64, 0}, {0, 0, 0, 0}, {0, 128, 0, 0}, {0, 4, 0, 0}, {0, 32, 0, 0}};//天秤
unsigned char X_tx = {{0, 96, 0, 0}, {0, 80, 0, 0}, {0, 208, 0, 0}, {0, 64, 1, 0}, {0, 9, 2, 0}, {4, 2, 0, 0}, {0, 1, 2, 0}, {0, 1, 0, 0}};//天蝎
unsigned char X_ss = {{0, 4, 0, 0}, {4, 0, 64, 0}, {0, 3, 192, 0}, {0, 42, 0, 0}, {16, 32, 129, 0}, {0, 16, 8, 0}, {0, 0, 32, 0}, {0, 0, 32, 0}};//射手
//太阳部分
unsigned char T_yl = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {7, 7, 7, 0}, {7, 7, 7, 0}, {7, 7, 7, 0}};
unsigned char T_ym = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {3, 3, 0, 0}, {3, 3, 0, 0}};
unsigned char T_ys = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}};
//彩虹部分
unsigned char C_hong = {{3, 0, 0, 0}, {7, 0, 0, 0}, {14, 0, 0, 0}, {28, 0, 0, 0}, {56, 0, 0, 0}, {112, 0, 0, 0}, {224, 0, 0, 0}, {192, 0, 0, 0}};
//山峰部分
unsigned char S_feng = {{255, 0, 0, 0}, {126, 0, 0, 0}, {124, 0, 0, 0}, {120, 0, 0, 0}, {56, 0, 0, 0}, {55, 7, 7, 0}, {55, 7, 7, 0}, {23, 7, 7, 0}};
//月亮部分
unsigned char Y_liang = {{192, 192, 0, 0}, {192, 192, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
//小船部分
unsigned char X_chuan = {{24, 0, 0, 0}, {60, 0, 0, 0}, {126, 0, 0, 0}, {255, 0, 0, 0}, {16, 0, 0, 0}, {28, 0, 0, 0}, {24, 0, 0, 0}, {16, 0, 0, 0}};
//阶梯部分
unsigned char J_ti = {{56, 0, 0, 0}, {8, 8, 8, 0}, {0, 0, 56, 0}, {32, 32, 32, 0}, {56, 0, 0, 0}, {8, 8, 8, 0}, {0, 0, 56, 0}, {32, 32, 32, 0}};

////////////////////////////////////////////////////////////////////////////
////////////////////////////初始化/////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void ini()
{
//窗帘数组
ini_shuzu();
////////引脚初始化/////////////////////////////////
for (int i = 0; i < 8; i++) {
    pinMode(pin_D, OUTPUT);
}
for (int i = 0; i < 8; i++) {
    pinMode(pin_CLK, OUTPUT);
}
for (int i = 0; i < 8; i++) {
    pinMode(pin_B, OUTPUT);
}
///////////////定时器初始化///////////////////////////////
Timer3.initialize(1000);
Timer3.pwm(5, 512);
Timer3.attachInterrupt(out);
}
///////////////////////////////////////////////////////////////////////
//////////////////////定时器中断响应函数//////////////////////////////
/////////////////////////////////////////////////////////////////////
void out()
{
static char layer = 0, temp = 0;
digitalWrite(pin_B, 0);
for (char i = 0; i < 8; i++)
{
    for (char j = 0; j < 8; j++)
    {
      digitalWrite(pin_D, cube & 0b00000001 << j);
    }
    digitalWrite(pin_CLK, 1);
    digitalWrite(pin_CLK, 0);

}
digitalWrite(pin_B, 1);
if (layer < 7)
{
    temp = layer;
    layer++;
}
else
{
    temp = 7;
    layer = 0;
}
}
/////////////////////////////////////////////////////////////////////
///////////////////////主函数初始化/////////////////////////////////
///////////////////////////////////////////////////////////////////
void setup() {
ini();
}
///////////////////////////////////////////////////////////////////
/////////////////////主函数执行部分///////////////////////////////
/////////////////////////////////////////////////////////////////
void loop()
{
fill(0x00);//清除状态
Flash();//显示动画
delay(5000);
}


///////////////////////////////////////////////////////////////
//////////////////////中层构建函数////////////////////////////
/////////////////////////////////////////////////////////////

/*******************************************************
space子函数
函数功能:用作星空背景
入口参数:   iterations背景长度
*********************************************************/
void space(int iterations) {
int i, ii;
int rnd_y;
int rnd_z;
int rnd_num;
int time;
time = 700;

for (ii = 0; ii < iterations; ii++)
{
    time = time - (iterations / 15);
    rnd_num = rand() % 4;

    for (i = 0; i < rnd_num; i++)
    {
      rnd_y = rand() % 8;
      rnd_z = rand() % 8;
      setvoxel(7, rnd_y, rnd_z);
    }

    delay_ms(time);
    shift(AXIS_X, -1);
}

for (ii = 0; ii < iterations; ii++)
{
    time = time + (iterations / 15);
    rnd_num = rand() % 4;

    for (i = 0; i < rnd_num; i++)
    {
      rnd_y = rand() % 8;
      rnd_z = rand() % 8;
      setvoxel(7, rnd_y, rnd_z);
    }

    delay_ms(time);
    shift(AXIS_X, -1);
}

}
/*******************************************************
firework子函数
函数功能:用作烟花显示
入口参数:   位置和时长
*********************************************************/
void firework(int i, int j, int time) {

fill(0x00);

setvoxel(3 - i, 4 - j, 0);
delay_ms(900 - time);

clrvoxel(3 - i, 4 - j, 0);
setvoxel(4 - i, 4 - j, 1);
delay_ms(1200 - time);

clrvoxel(4 - i, 4 - j, 1);
setvoxel(4 - i, 5 - j, 2);
delay_ms(1400 - time);

clrvoxel(4 - i, 5 - j, 2);
setvoxel(3 - i, 5 - j, 3);
delay_ms(1700 - time);

clrvoxel(3 - i, 5 - j, 3);
setvoxel(3 - i, 4 - j, 4);
delay_ms(2000 - time);

clrvoxel(3 - i, 4 - j, 4);
setvoxel(4 - i, 4 - j, 5);
delay_ms(2000 - time);

clrvoxel(4 - i, 4 - j, 5);
setvoxel(4 - i, 3 - j, 6);
delay_ms(2000 - time);

//Explode
clrvoxel(4 - i, 3 - j, 6);
setvoxel(4 - i, 3 - j, 7);
setvoxel(4 - i, 4 - j, 6);
setvoxel(4 - i, 2 - j, 6);
setvoxel(3 - i, 3 - j, 6);
setvoxel(5 - i, 3 - j, 6);
delay_ms(2000 - time);

shift(AXIS_Z, -1);
setvoxel(4 - i, 5 - j, 5);
setvoxel(4 - i, 1 - j, 5);
setvoxel(2 - i, 3 - j, 5);
setvoxel(6 - i, 3 - j, 5);
delay_ms(900 - time);

shift(AXIS_Z, -1);
setvoxel(4 - i, 6 - j, 3);
setvoxel(4 - i, 0 - j, 3);
setvoxel(1 - i, 3 - j, 3);
setvoxel(7 - i, 3 - j, 3);
delay_ms(900 - time);

shift(AXIS_Z, -1);
setvoxel(4 - i, 7 - j, 1);
setvoxel(3 - i, 0 - j, 1);
setvoxel(0 - i, 3 - j, 1);
setvoxel(7 - i, 2 - j, 1);
delay_ms(1400 - time);

shift(AXIS_Z, -1);
delay_ms(1400 - time);

shift(AXIS_Z, -1);
delay_ms(1400 - time);

shift(AXIS_Z, -1);
delay_ms(1400 - time);

shift(AXIS_Z, -1);
delay_ms(700 - time);

fill(0x00);
}
/*******************************************************
draw_positions_axis子函数
函数功能:暂未用,可拓展
入口参数:
*********************************************************/
voiddraw_positions_axis (char axis, unsigned char positions, int invert)
{
int x, y, p;

fill(0x00);

for (x = 0; x < 8; x++)
{
    for (y = 0; y < 8; y++)
    {
      if (invert)
      {
      p = (7 - positions[(x * 8) + y]);
      } else
      {
      p = positions[(x * 8) + y];
      }

      if (axis == AXIS_Z)
      setvoxel(x, y, p);

      if (axis == AXIS_Y)
      setvoxel(x, p, y);

      if (axis == AXIS_X)
      setvoxel(p, y, x);
    }
}

}
/*******************************************************
effect_boxside_randsend_parallel子函数
函数功能:暂未用,可拓展
入口参数:
*********************************************************/
voideffect_boxside_randsend_parallel (char axis, int origin, int delay, int mode)
{
int i;
int done;
unsigned char cubepos;
unsigned char pos;
int notdone = 1;
int notdone2 = 1;
int sent = 0;

for (i = 0; i < 64; i++)
{
    pos = 0;
}

while (notdone)
{
    if (mode == 1)
    {
      notdone2 = 1;
      while (notdone2 && sent < 64)
      {
      i = rand() % 64;
      if (pos == 0)
      {
          sent++;
          pos += 1;
          notdone2 = 0;
      }
      }
    } else if (mode == 2)
    {
      if (sent < 64)
      {
      pos += 1;
      sent++;
      }
    }

    done = 0;
    for (i = 0; i < 64; i++)
    {
      if (pos > 0 && pos < 7)
      {
      pos += 1;
      }

      if (pos == 7)
      done++;
    }

    if (done == 64)
      notdone = 0;

    for (i = 0; i < 64; i++)
    {
      if (origin == 0)
      {
      cubepos = pos;
      } else
      {
      cubepos = (7 - pos);
      }
    }


    delay_ms(delay);
    draw_positions_axis(axis, cubepos, 0);

}

}

/*******************************************************
effect_rain子函数
函数功能:用作下雨动画
入口参数:      雨长和雨速
*********************************************************/
voideffect_rain (int iterations, int time)
{
int i, ii;
int rnd_x;
int rnd_y;
int rnd_num;

for (ii = 0; ii < iterations; ii++)
{
    rnd_num = rand() % 4;

    for (i = 0; i < rnd_num; i++)
    {
      rnd_x = rand() % 8;
      rnd_y = rand() % 8;
      setvoxel(rnd_x, rnd_y, 7);
    }

    delay_ms(time);
    shift(AXIS_Z, -1);
}
}
/*******************************************************
effect_random_filler子函数
函数功能:随机填充和清除,用作场景切换
入口参数:      速度和填充(清除)
*********************************************************/
voideffect_random_filler (int delay, int state)
{
int x, y, z;
int loop = 0;


if (state == 1)
{
    fill(0x00);
} else
{
    fill(0xff);
}

while (loop < 511)
{
    x = rand() % 8;
    y = rand() % 8;
    z = rand() % 8;

    if ((state == 0 && getvoxel(x, y, z) == 0x01) || (state == 1 && getvoxel(x, y, z) == 0x00))
    {
      altervoxel(x, y, z, state);
      delay_ms(delay);
      loop++;
    }
}
}

/*******************************************************
effect_blinky2子函数
函数功能:屏闪,用于落幕
入口参数:      无
*********************************************************/
voideffect_blinky2()
{
int i, r;
fill(0x00);

for (r = 0; r < 2; r++)
{
    i = 750;
    while (i > 0)
    {
      fill(0x00);
      delay_ms(i);

      fill(0xff);
      delay_ms(100);

      i = i - (15 + (1000 / (i / 10)));
    }

    delay_ms(1000);

    i = 750;
    while (i > 0)
    {
      fill(0x00);
      delay_ms(751 - i);

      fill(0xff);
      delay_ms(100);

      i = i - (15 + (1000 / (i / 10)));
    }
}

}
/*******************************************************
effect_planboing子函数
函数功能:用作场景切换
入口参数:      轴面和速度
*********************************************************/
voideffect_planboing (int plane, int speed)
{
int i;
for (i = 0; i < 8; i++)
{
    fill(0x00);
    setplane(plane, i);
    delay_ms(speed);
}

for (i = 7; i >= 0; i--)
{
    fill(0x00);
    setplane(plane, i);
    delay_ms(speed);
}
}
//////////////////////////////////////////////////////////////////////
/////////////////////底层基础函数////////////////////////////////////
////////////////////////////////////////////////////////////////////
/*******************************************************
setvoxel子函数
函数功能:点亮一个点
入口参数:      点位置
*********************************************************/
voidsetvoxel(int x, int y, int z)
{
if (inrange(x, y, z))
    cube |= (1 << x);
}

/*******************************************************
clrvoxel子函数
函数功能:清除一个点
入口参数:      点位置
*********************************************************/
// Set a single voxel to ON
voidclrvoxel(int x, int y, int z)
{
if (inrange(x, y, z))
    cube &= ~(1 << x);
}


/*******************************************************
inrange子函数
函数功能:判断是否在显示范围在光立方内
入口参数:      位置
*********************************************************/
unsigned charinrange(int x, int y, int z)
{
if (x >= 0 && x < 8 && y >= 0 && y < 8 && z >= 0 && z < 8)
{
    return 0x01;
} else
{
    // One of the coordinates was outside the cube.
    return 0x00;
}
}
/*******************************************************
getvoxel子函数
函数功能:获取点的状态
入口参数:      位置
*********************************************************/
unsigned chargetvoxel(int x, int y, int z)
{
if (inrange(x, y, z))
{
    if (cube & (1 << x))
    {
      return 0x01;
    } else
    {
      return 0x00;
    }
} else
{
    return 0x00;
}
}
/*******************************************************
altervoxel子函数
函数功能:设置点的状态
入口参数:      位置和状态
*********************************************************/
voidaltervoxel(int x, int y, int z, int state)
{
if (state == 1)
{
    setvoxel(x, y, z);
} else
{
    clrvoxel(x, y, z);
}
}
/*******************************************************
flpvoxel子函数
函数功能:切换状态
入口参数:      位置
*********************************************************/
voidflpvoxel(int x, int y, int z)
{
if (inrange(x, y, z))
    cube ^= (1 << x);
}
/*******************************************************
argorder子函数
函数功能:排序数值
入口参数:      变量和指针
*********************************************************/
voidargorder(int ix1, int ix2, int *ox1, int *ox2)
{
if (ix1 > ix2)
{
    int tmp;
    tmp = ix1;
    ix1 = ix2;
    ix2 = tmp;
}
*ox1 = ix1;
*ox2 = ix2;
}
/*******************************************************
setplane_z子函数
函数功能:点亮Z平面
入口参数:      位置
*********************************************************/
voidsetplane_z (int z)
{
int i;
if (z >= 0 && z < 8)
{
    for (i = 0; i < 8; i++)
      cube = 0xff;
}
}
/*******************************************************
clrplane_z子函数
函数功能:清除z层平面
入口参数:      位置
*********************************************************/
voidclrplane_z (int z)
{
int i;
if (z >= 0 && z < 8)
{
    for (i = 0; i < 8; i++)
      cube = 0x00;
}
}
/*******************************************************
setplane_x子函数
函数功能:点亮x平面
入口参数:      位置
*********************************************************/
voidsetplane_x (int x)
{
int z;
int y;
if (x >= 0 && x < 8)
{
    for (z = 0; z < 8; z++)
    {
      for (y = 0; y < 8; y++)
      {
      cube |= (1 << x);
      }
    }
}
}
/*******************************************************
clrplane_x子函数
函数功能:清除x平面
入口参数:      位置
*********************************************************/
voidclrplane_x (int x)
{
int z;
int y;
if (x >= 0 && x < 8)
{
    for (z = 0; z < 8; z++)
    {
      for (y = 0; y < 8; y++)
      {
      cube &= ~(1 << x);
      }
    }
}
}
/*******************************************************
setplane_y子函数
函数功能:点亮y层
入口参数:      位置
*********************************************************/
voidsetplane_y (int y)
{
int z;
if (y >= 0 && y < 8)
{
    for (z = 0; z < 8; z++)
      cube = 0xff;
}
}
/*******************************************************
clrplane_y子函数
函数功能:清除y平面
入口参数:      位置
*********************************************************/
voidclrplane_y (int y)
{
int z;
if (y >= 0 && y < 8)
{
    for (z = 0; z < 8; z++)
      cube = 0x00;
}
}
/*******************************************************
setplane子函数
函数功能:点亮层
入口参数:      轴和位置
*********************************************************/
voidsetplane (char axis, unsigned char i)
{
switch (axis)
{
    case AXIS_X:
      setplane_x(i);
      break;

    case AXIS_Y:
      setplane_y(i);
      break;

    case AXIS_Z:
      setplane_z(i);
      break;
}
}
/*******************************************************
clrplane子函数
函数功能:清除平面
入口参数:      轴和位置
*********************************************************/
voidclrplane (char axis, unsigned char i)
{
switch (axis)
{
    case AXIS_X:
      clrplane_x(i);
      break;

    case AXIS_Y:
      clrplane_y(i);
      break;

    case AXIS_Z:
      clrplane_z(i);
      break;
}
}
/*******************************************************
fill子函数
函数功能:设置层相同状态
入口参数:      状态
*********************************************************/
voidfill (unsigned char pattern)
{
int z;
int y;
for (z = 0; z < 8; z++)
{
    for (y = 0; y < 8; y++)
    {
      cube = pattern;
    }
}
}


/*******************************************************
box_filled子函数
函数功能:盒子实体
入口参数:      起始点
*********************************************************/
voidbox_filled(int x1, int y1, int z1, int x2, int y2, int z2)
{
int iy;
int iz;

argorder(x1, x2, &x1, &x2);
argorder(y1, y2, &y1, &y2);
argorder(z1, z2, &z1, &z2);

for (iz = z1; iz <= z2; iz++)
{
    for (iy = y1; iy <= y2; iy++)
    {
      cube |= byteline(x1, x2);
    }
}

}
/*******************************************************
box_walls子函数
函数功能:空盒
入口参数:      起始点
*********************************************************/
voidbox_walls(int x1, int y1, int z1, int x2, int y2, int z2)
{
int iy;
int iz;

argorder(x1, x2, &x1, &x2);
argorder(y1, y2, &y1, &y2);
argorder(z1, z2, &z1, &z2);

for (iz = z1; iz <= z2; iz++)
{
    for (iy = y1; iy <= y2; iy++)
    {
      if (iy == y1 || iy == y2 || iz == z1 || iz == z2)
      {
      cube = byteline(x1, x2);
      } else
      {
      cube |= ((0x01 << x1) | (0x01 << x2));
      }
    }
}

}
/*******************************************************
box_wireframe子函数
函数功能:盒子框架
入口参数:      起始点
*********************************************************/
voidbox_wireframe(int x1, int y1, int z1, int x2, int y2, int z2)
{
int iy;
int iz;

argorder(x1, x2, &x1, &x2);
argorder(y1, y2, &y1, &y2);
argorder(z1, z2, &z1, &z2);

// Lines along X axis
cube = byteline(x1, x2);
cube = byteline(x1, x2);
cube = byteline(x1, x2);
cube = byteline(x1, x2);

// Lines along Y axis
for (iy = y1; iy <= y2; iy++)
{
    setvoxel(x1, iy, z1);
    setvoxel(x1, iy, z2);
    setvoxel(x2, iy, z1);
    setvoxel(x2, iy, z2);
}

// Lines along Z axis
for (iz = z1; iz <= z2; iz++)
{
    setvoxel(x1, y1, iz);
    setvoxel(x1, y2, iz);
    setvoxel(x2, y1, iz);
    setvoxel(x2, y2, iz);
}

}
/*******************************************************
byteline子函数
函数功能:返回二进制值
入口参数:      1的起始值
*********************************************************/
charbyteline (int start, int end)
{
return ((0xff << start) & ~(0xff << (end + 1)));
}
/*******************************************************
flipbyte子函数
函数功能:旋转180
入口参数:      字节
*********************************************************/
charflipbyte (char byte)
{
char flop = 0x00;

flop = (flop & 0b11111110) | (0b00000001 & (byte >> 7));
flop = (flop & 0b11111101) | (0b00000010 & (byte >> 5));
flop = (flop & 0b11111011) | (0b00000100 & (byte >> 3));
flop = (flop & 0b11110111) | (0b00001000 & (byte >> 1));
flop = (flop & 0b11101111) | (0b00010000 & (byte << 1));
flop = (flop & 0b11011111) | (0b00100000 & (byte << 3));
flop = (flop & 0b10111111) | (0b01000000 & (byte << 5));
flop = (flop & 0b01111111) | (0b10000000 & (byte << 7));
return flop;
}
/*******************************************************
line子函数
函数功能:画线
入口参数:      起始点
*********************************************************/
voidline(int x1, int y1, int z1, int x2, int y2, int z2)
{
float xy;
float xz;
unsigned char x, y, z;
unsigned char lasty, lastz;


if (x1 > x2)
{
    int tmp;
    tmp = x2; x2 = x1; x1 = tmp;
    tmp = y2; y2 = y1; y1 = tmp;
    tmp = z2; z2 = z1; z1 = tmp;
}


if (y1 > y2)
{
    xy = (float)(y1 - y2) / (float)(x2 - x1);
    lasty = y2;
} else
{
    xy = (float)(y2 - y1) / (float)(x2 - x1);
    lasty = y1;
}

if (z1 > z2)
{
    xz = (float)(z1 - z2) / (float)(x2 - x1);
    lastz = z2;
} else
{
    xz = (float)(z2 - z1) / (float)(x2 - x1);
    lastz = z1;
}



for (x = x1; x <= x2; x++)
{
    y = (xy * (x - x1)) + y1;
    z = (xz * (x - x1)) + z1;
    setvoxel(x, y, z);
}

}
/*******************************************************
delay_ms子函数
函数功能:延时
入口参数:      时长
*********************************************************/
voiddelay_ms(uint16_t x)
{
uint8_t y, z;
for ( ; x > 0 ; x--) {
    for ( y = 0 ; y < 90 ; y++) {
      for ( z = 0 ; z < 6 ; z++) {
      asm volatile ("nop");
      }
    }
}
}


/*******************************************************
shift子函数
函数功能:平移
入口参数:      轴和方向
*********************************************************/
voidshift (char axis, int direction)
{
int i, x , y;
int ii, iii;
int state;

for (i = 0; i < 8; i++)
{
    if (direction == -1)
    {
      ii = i;
    } else
    {
      ii = (7 - i);
    }


    for (x = 0; x < 8; x++)
    {
      for (y = 0; y < 8; y++)
      {
      if (direction == -1)
      {
          iii = ii + 1;
      } else
      {
          iii = ii - 1;
      }

      if (axis == AXIS_Z)
      {
          state = getvoxel(x, y, iii);
          altervoxel(x, y, ii, state);
      }

      if (axis == AXIS_Y)
      {
          state = getvoxel(x, iii, y);
          altervoxel(x, ii, y, state);
      }

      if (axis == AXIS_X)
      {
          state = getvoxel(iii, y, x);
          altervoxel(ii, y, x, state);
      }
      }
    }
}

if (direction == -1)
{
    i = 7;
} else
{
    i = 0;
}

for (x = 0; x < 8; x++)
{
    for (y = 0; y < 8; y++)
    {
      if (axis == AXIS_Z)
      clrvoxel(x, y, i);

      if (axis == AXIS_Y)
      clrvoxel(x, i, y);

      if (axis == AXIS_X)
      clrvoxel(i, y, x);
    }
}
}
/*******************************************************
to_cube子函数
函数功能:输出数组数据
入口参数:      数组
*********************************************************/
void to_cube(unsigned char a)
{
for (int k = 7; k >= 0; k--)
{
    for (int i = 0; i < 8; i++)
    {
      cube = a;
    }
}
}
/*******************************************************
to_cube_for_numflash子函数
函数功能:用于数字动画
入口参数:      数组
*********************************************************/
void to_cube_for_numflash(unsigned char a)
{
for (int k = 7; k >= 0; k--)
{
    for (int i = 0; i < 8; i++)
    {
      cube = a;
    }
    shift(AXIS_Y, 1);
    delay(100);
}
}
/////////////////////////////////////////////////////////////////
/////////////////动画演示部分///////////////////////////////////
///////////////////////////////////////////////////////////////
//时钟
void flash_num()
{
to_cube_for_numflash(Num_0);
to_cube_for_numflash(Num_1);
to_cube_for_numflash(Num_2);
to_cube_for_numflash(Num_3);
to_cube_for_numflash(Num_4);
to_cube_for_numflash(Num_5);
for (int i = 0; i < 7; i++)
{
    to_cube(Num_6);
    shift(AXIS_Y, 1);
    delay(90);
}
for (int j = 0; j < 8; j++)
{
    shift(AXIS_Y, 1);
    delay(80);
}
}

//////////////////////////////////////////////////////////////
//起床
void flash_wakeup()
{
to_cube(Tang);
delay(300);
to_cube(Zuo);
delay(400);
to_cube(Zhan);
delay(500);
}

////////////////////////////////////////////////////////////
//窗帘
void flash_curtain()
{
int i = 0;
fill(0xff);
delay(80);
for (i = 0; i < 8; i++)
{
    for (int y = 0; y <= i; y++)
    {
      for (int z = 0; z < 8; z++)
      {
      cube = C_yundong;
      }
    }
    delay(120);
}
delay(500);
}

///////////////////////////////////////////////////////////
//下雨
void flash_rian()
{
int time = 400;
effect_rain(30, time);
for (int i = 10; time > 30 && i > 0; time -= time / i, i--)
{
    effect_rain(4 * i, time);
}
effect_boxside_randsend_parallel (AXIS_Z, 1, 100, 1);
effect_rain(20, time);
for (int i = 1; i < 7; time = 50 * i, i++)
{
    effect_rain(6 * i, time);
}
effect_rain(60, 800);
fill(0x00);
delay(200);
effect_random_filler(20, 1);
effect_random_filler(20, 0);
}

/////////////////////////////////////////////////////////
//太阳
void flash_sun()
{
to_cube(T_ys);
delay(100);
to_cube(T_ym);
delay(200);
//    to_cube(T_yl);
//    delay(500);
for (int i = 0; i < 60; i += 2)
{
    to_cube(T_yl);
    delay(i);
    fill(0x00);
    delay(60 - i);
}
to_cube(T_yl);
delay(1000);
}

/////////////////////////////////////////////////////////
//白云
void flash_cloud(int iterations)
{
int i, ii;
int rnd_y;
int rnd_z;
int rnd_num;
int time;
time = 700;
for (ii = 0; ii < iterations; ii++)
{
    time = time - (iterations / 15);
    rnd_num = rand() % 4;
    for (i = 0; i < rnd_num; i++)
    {
      rnd_y = rand() % 8;
      rnd_z = rand() % 2 + 6;
      setvoxel(7, rnd_y, rnd_z);
    }
    delay_ms(time);
    shift(AXIS_X, -1);
}

for (ii = 0; ii < iterations; ii++)
{
    time = time + (iterations / 15);
    rnd_num = rand() % 4;
    for (i = 0; i < rnd_num; i++)
    {
      rnd_y = rand() % 8;
      rnd_z = rand() % 2 + 6;
      setvoxel(7, rnd_y, rnd_z);
    }
    delay_ms(time);
    shift(AXIS_X, -1);
}
}

/////////////////////////////////////////////////////////
//彩虹
void flash_rainbow()
{
for (int k = 7; k >= 0; k--)
{
    for (int i = 0; i < 8; i++)
    {
      cube = C_hong;
    }
    shift(AXIS_Y, 1);
    delay(120);
}
delay(2000);
}

/////////////////////////////////////////////////////////
//山峰
void flash_hill()
{
to_cube(S_feng);
delay(500);
for (int i = 0; i < 7; i++)
{
    shift(AXIS_Y, 1);
    delay(60);
}
for (int i = 0; i < 6; i++)
{
    shift(AXIS_Z, -1);
    delay(120);
}
}

/////////////////////////////////////////////////////////
//月亮
void flash_moon()
{
cube = Y_liang;
cube = Y_liang;
cube = Y_liang;
cube = Y_liang;
for (int i = 0; i < 6; i++)
{
    shift(AXIS_Z, 1);
    delay(80);
}
for (int i = 0; i < 6; i++)
{
    shift(AXIS_X, -1);
    delay(60);
}
for (int i = 0; i < 8; i++)
{
    shift(AXIS_Y, -1);
    delay(60);
}
}

/////////////////////////////////////////////////////////
//小船
void flash_ship()
{
to_cube(X_chuan);
for (int i = 0; i < 7; i++)
{
    shift(AXIS_Y, 1);
    delay(100);
}
for (int i = 0; i < 8; i++)
{
    shift(AXIS_Y, -1);
    delay(100);
}
////////////////////////////////
for (int k = 7; k >= 0; k--)
{
    for (int i = 0; i < 8; i++)
    {
      cube = X_chuan;
    }
    shift(AXIS_Y, 1);
    delay(100);
}
for (int i = 0; i < 8; i++)
{
    shift(AXIS_Y, 1);
    delay(100);
}

for (int k = 7; k >= 0; k--)
{
    for (int i = 0; i < 8; i++)
    {
      cube = X_chuan;
    }
    shift(AXIS_Y, -1);
    delay(100);
}
for (int i = 0; i < 8; i++)
{
    shift(AXIS_Y, -1);
    delay(100);
}
}

/////////////////////////////////////////////////////////
//喷泉
void spring(int iterations) {

int i, ii;
int rnd_y;
int rnd_z;
int rnd_num;

for (ii = 0; ii < iterations + 6; ii++)
{
    rnd_num = rand() % 6;

    for (i = 0; i < rnd_num; i++)
    {
      rnd_y = rand() % 8;
      rnd_z = rand() % 8;
      setvoxel(rnd_y, rnd_z, 0);
    }

    delay_ms(rand() % 200 + 460);
    shift(AXIS_Z, 1);
}

for (ii = 0; ii < iterations + 8; ii++)
{
    rnd_num = rand() % 10;

    for (i = 0; i < rnd_num; i++)
    {
      rnd_y = rand() % 8;
      rnd_z = rand() % 8;
      setvoxel(rnd_y, rnd_z, 7);
    }

    delay_ms(rand() % 100 + 200);
    shift(AXIS_Z, -1);
}
fill(0x00);
delay(rand() % 30);
}
/////////////////////////////////
void flash_spring()
{
for (int i = 0; i < 10; i++)
{
    spring(rand() % 20);
}
}

/////////////////////////////////////////////////////////
//阶梯
void flash_ladder()
{
for (int y = 1; y < 8; y++)
{
    box_filled(0, 0, 0, 1, y, 1);
    delay(30);
}
for (int x = 1; x < 8; x++)
{
    box_filled(0, 7, 1, x, 6, 2);
    delay(30);
}
for (int y = 7; y >= 0; y--)
{
    box_filled(7, 7, 2, 6, y, 3);
    delay(30);
}
for (int x = 7; x >= 0; x--)
{
    box_filled(7, 0, 3, x, 1, 4);
    delay(30);
}
/////////////////////////////////
for (int y = 1; y < 8; y++)
{
    box_filled(0, 0, 4, 1, y, 5);
    delay(30);
}
for (int x = 1; x < 8; x++)
{
    box_filled(0, 7, 5, x, 6, 6);
    delay(30);
}
for (int y = 7; y >= 0; y--)
{
    box_filled(7, 7, 6, 6, y, 7);
    delay(30);
}
for (int x = 7; x >= 0; x--)
{
    box_filled(7, 0, 7, x, 1, 7);
    delay(30);
}
   /////////////////////////////////
for (int y = 1; y <= 7; y++)
{
    box_filled(0, 0, 7, 1, y, 7);
    delay(30);
}
for (int x = 1; x <= 5; x++)
{
    box_filled(0, 7, 7, x, 6, 7);
    delay(30);
}
for (int y = 5; y >= 2; y--)
{
    box_filled(5, 7, 7, 4, y, 7);
    delay(30);
}
for (int x = 4; x >= 2; x--)
{
    box_filled(4, 2, 7, x, 1, 7);
    delay(30);
}
/////////////////////////////////
for (int z = 7; z >= 0; z--)
{
    box_filled(2, 3, 7, 3, 5, z);
    delay(30);
}
delay(500);
effect_planboing(AXIS_Z, 1000);
}

/////////////////////////////////////////////////////////
//烟花
void flash_firework()
{
firework(0, 0, 0);
firework(-2, -2, 50);
firework(1, 1, -250);
firework(0, 1, 200);
firework(1, -3, 400);
firework(2, -3, 600);
firework(2, 1, 500);
firework(2, -2, 200);
firework(2, 1, 0);
firework(0, 0, 0);
firework(2, -2, 500);
space(6);
}

/////////////////////////////////////////////////////////
//星空
void flash_stars()
{
//////////////摩羯
space(8);
to_cube(X_mj);
delay(500);
//////////////水瓶
space(8);
to_cube(X_sp);
delay(500);
//////////////双鱼
space(8);
to_cube(X_sy);
delay(500);
//////////////白羊
space(8);
to_cube(X_by);
delay(500);
//////////////金牛
space(8);
to_cube(X_jn);
delay(500);
//////////////双子
space(8);
to_cube(X_dz);
delay(500);
//////////////巨蟹
space(8);
to_cube(X_jx);
delay(500);
//////////////狮子
space(8);
to_cube(X_sz);
delay(500);
//////////////处女
space(8);
to_cube(X_cn);
delay(500);
//////////////天秤
space(8);
to_cube(X_tc);
delay(500);
//////////////天蝎
space(8);
to_cube(X_tx);
delay(500);
//////////////射手
space(8);
to_cube(X_ss);
delay(500);
//////////////
space(20);
delay(500);
//////////////
effect_random_filler(20, 1);
effect_blinky2();
}
///////////////////////////////////////////////////////////////////
//////////////////动画规划////////////////////////////////////////
/////////////////////////////////////////////////////////////////
void Flash()
{
flash_num();//时钟显示部分
flash_wakeup();//起床部分
flash_curtain();//窗帘部分
flash_rian();//下雨部分
flash_sun();//太阳部分
flash_cloud(100);//云层部分
flash_rainbow();//彩虹部分
flash_hill();//山峰部分
flash_moon();//月亮部分
flash_ship();//小船部分
flash_spring();//喷泉部分
flash_ladder();//望台部分
flash_firework();//烟花部分
flash_stars();//星空部分
}


////////////////////////诗//////////////////////////////
/*******************************************************
        我的世界

我的呼吸伴着“嘀嗒”
从零点数到了清晨
揉去满眼的倦意
拉开一帘细雨
我吻着你春泥的香甜
呼出的风
挽手不离
我知道,那是
你最后一滴泪
我以为你已变成了那
一抹光
那么耀眼
我也知道
那曾是我们的希望

别了,云儿
我恐怕连你的衣角
都来不及触及
散落幻化的七彩
我看见有你的颜色
连接到山的那边
我要走多久
才会到达夕阳的终点
我以为
这是最靠近你的地方

一丝澄澈的冰凉
透过我的心房
我们共乘一帆小船
只是
我在湖面
而你,是在
湖底还是天上
我去了你最喜欢的小岛
我喜欢的是
这里还有你的味道
我又想起了
你看过的音乐喷泉
还有
爬上瞭望台的你
堵着耳朵看烟花时的笑
你还说你喜欢烟花鸣爆

我伸出双手
仰望星空
我知道摩羯的温度
我也听过双鱼天真烂漫的笑语
不过
我都快忘记 我想要的
只是还你一个拥抱

                                                                                                By--于江湖
                                                                                                2016.06.02
*********************************************************/
////////////////////////诗///////////////////////////////

340 发表于 2017-3-12 17:07:11

看见一点都不像,沙发帮顶。

baidiguan 发表于 2017-5-10 20:21:44

来段视频效果吧:lol
页: [1]
查看完整版本: 8*8*8 LED光立方_我的世界