diff options
author | vg <vgm+dev@devys.org> | 2020-07-07 16:24:01 +0200 |
---|---|---|
committer | vg <vgm+dev@devys.org> | 2020-07-07 16:24:01 +0200 |
commit | 66dcf910bd4744d8ced56cb9586aa937a1a2d4c5 (patch) | |
tree | df4dca1ae4af1e5df0be0d1f4f2cd0d54751f8e8 /avr-test/src | |
download | hic-66dcf910bd4744d8ced56cb9586aa937a1a2d4c5.tar.gz hic-66dcf910bd4744d8ced56cb9586aa937a1a2d4c5.tar.bz2 hic-66dcf910bd4744d8ced56cb9586aa937a1a2d4c5.zip |
Diffstat (limited to 'avr-test/src')
-rw-r--r-- | avr-test/src/cube.h | 32 | ||||
-rw-r--r-- | avr-test/src/draw.cpp | 559 | ||||
-rw-r--r-- | avr-test/src/draw.h | 71 | ||||
-rw-r--r-- | avr-test/src/effect.cpp | 1021 | ||||
-rw-r--r-- | avr-test/src/effect.h | 54 | ||||
-rw-r--r-- | avr-test/src/fuses.txt | 6 | ||||
-rw-r--r-- | avr-test/src/lisence.txt | 5 | ||||
-rw-r--r-- | avr-test/src/main.c.old | 285 | ||||
-rw-r--r-- | avr-test/src/main.cpp | 508 | ||||
-rw-r--r-- | avr-test/src/main.h | 45 |
10 files changed, 2586 insertions, 0 deletions
diff --git a/avr-test/src/cube.h b/avr-test/src/cube.h new file mode 100644 index 0000000..01f19da --- /dev/null +++ b/avr-test/src/cube.h @@ -0,0 +1,32 @@ +#ifndef CUBE_H +#define CUBE_H + +// Some of the functions are created to be portable +// These functions will work on cubes of different sizes by +// changing this constant +#define CUBE_SIZE 8 +#define CUBE_BYTES 64 + +// If you change this to anything greather than 8, you also have +// change how the cube buffer works and probably all the functions +// in draw.c + +// Cube buffer +// Data from this array is loaded onto the cube for each duty cycle +extern volatile unsigned char cube[CUBE_SIZE][CUBE_SIZE]; + +// Framebuffer +// Animations that take a lot of time to compute are temporarily +// stored to this array, then loaded into cube[8][8] when the image +// is ready to be displayed +extern volatile unsigned char fb[CUBE_SIZE][CUBE_SIZE]; + +// Some effects can render on different axis +// for example send pixels along an axis +// for better readability, we use the following predefined constants +#define AXIS_X 0x78 +#define AXIS_Y 0x79 +#define AXIS_Z 0x7a + +#endif + diff --git a/avr-test/src/draw.cpp b/avr-test/src/draw.cpp new file mode 100644 index 0000000..faaa346 --- /dev/null +++ b/avr-test/src/draw.cpp @@ -0,0 +1,559 @@ +#include "draw.h" +#include "string.h" + +// Set a single voxel to ON +void setvoxel(int x, int y, int z) +{ + if (inrange(x,y,z)) + cube[z][y] |= (1 << x); +} + +// Set a single voxel in the temporary cube buffer to ON +void tmpsetvoxel(int x, int y, int z) +{ + if (inrange(x,y,z)) + fb[z][y] |= (1 << x); +} + +// Set a single voxel to OFF +void clrvoxel(int x, int y, int z) +{ + if (inrange(x,y,z)) + cube[z][y] &= ~(1 << x); +} + +// Set a single voxel to OFF +void tmpclrvoxel(int x, int y, int z) +{ + if (inrange(x,y,z)) + fb[z][y] &= ~(1 << x); +} + +// This function validates that we are drawing inside the cube. +unsigned char inrange(int x, int y, int z) +{ + if (x >= 0 && x < CUBE_SIZE && y >= 0 && y < CUBE_SIZE && z >= 0 && z < CUBE_SIZE) + { + return 1; + } else + { + // One of the coordinates was outside the cube. + return 0; + } +} + +// Get the current status of a voxel +unsigned char getvoxel(int x, int y, int z) +{ + if (inrange(x,y,z)) + { + if (cube[z][y] & (1 << x)) + { + return 1; + } else + { + return 0; + } + } else + { + return 0; + } +} + +// In some effect we want to just take bool and write it to a voxel +// this function calls the apropriate voxel manipulation function. +void altervoxel(int x, int y, int z, int state) +{ + if (state == 1) + { + setvoxel(x,y,z); + } else + { + clrvoxel(x,y,z); + } +} + +// Flip the state of a voxel. +// If the voxel is 1, its turned into a 0, and vice versa. +void flpvoxel(int x, int y, int z) +{ + if (inrange(x, y, z)) + cube[z][y] ^= (1 << x); +} + +// Makes sure x1 is alwas smaller than x2 +// This is usefull for functions that uses for loops, +// to avoid infinite loops +void argorder(int ix1, int ix2, int *ox1, int *ox2) +{ + if (ix1>ix2) + { + int tmp; + tmp = ix1; + ix1= ix2; + ix2 = tmp; + } + *ox1 = ix1; + *ox2 = ix2; +} + +// Sets all voxels along a X/Y plane at a given point +// on axis Z +void setplane_z (int z) +{ + int i; + if (z>=0 && z<CUBE_SIZE) + { + for (i=0;i<CUBE_SIZE;i++) + cube[z][i] = 0xff; + } +} + +// Clears voxels in the same manner as above +void clrplane_z (int z) +{ + int i; + if (z>=0 && z<CUBE_SIZE) + { + for (i=0;i<CUBE_SIZE;i++) + cube[z][i] = 0x00; + } +} + +void setplane_x (int x) +{ + int z; + int y; + if (x>=0 && x<CUBE_SIZE) + { + for (z=0;z<CUBE_SIZE;z++) + { + for (y=0;y<CUBE_SIZE;y++) + { + cube[z][y] |= (1 << x); + } + } + } +} + +void clrplane_x (int x) +{ + int z; + int y; + if (x>=0 && x<CUBE_SIZE) + { + for (z=0;z<CUBE_SIZE;z++) + { + for (y=0;y<CUBE_SIZE;y++) + { + cube[z][y] &= ~(1 << x); + } + } + } +} + +void setplane_y (int y) +{ + int z; + if (y>=0 && y<CUBE_SIZE) + { + for (z=0;z<CUBE_SIZE;z++) + cube[z][y] = 0xff; + } +} + +void clrplane_y (int y) +{ + int z; + if (y>=0 && y<CUBE_SIZE) + { + for (z=0;z<CUBE_SIZE;z++) + cube[z][y] = 0x00; + } +} + +void setplane (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; + } +} + +void clrplane (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 a value into all 64 byts of the cube buffer +// Mostly used for clearing. fill(0x00) +// or setting all on. fill(0xff) +void fill (unsigned char pattern) +{ + int z; + int y; + for (z=0;z<CUBE_SIZE;z++) + { + for (y=0;y<CUBE_SIZE;y++) + { + cube[z][y] = pattern; + } + } +} + +void tmpfill (unsigned char pattern) +{ + int z; + int y; + for (z=0;z<CUBE_SIZE;z++) + { + for (y=0;y<CUBE_SIZE;y++) + { + fb[z][y] = pattern; + } + } +} + +// Draw a box with all walls drawn and all voxels inside set +void box_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[iz][iy] |= byteline(x1,x2); + } + } + +} + +// Darw a hollow box with side walls. +void box_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[iz][iy] = byteline(x1,x2); + } else + { + cube[iz][iy] |= ((0x01 << x1) | (0x01 << x2)); + } + } + } + +} + +// Draw a wireframe box. This only draws the corners and edges, +// no walls. +void box_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[z1][y1] = byteline(x1,x2); + cube[z1][y2] = byteline(x1,x2); + cube[z2][y1] = byteline(x1,x2); + cube[z2][y2] = 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); + } + +} + +// Returns a byte with a row of 1's drawn in it. +// byteline(2,5) gives 0b00111100 +char byteline (int start, int end) +{ + return ((0xff<<start) & ~(0xff<<(end+1))); +} + +// Flips a byte 180 degrees. +// MSB becomes LSB, LSB becomes MSB. +char flipbyte (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; +} + +// Draw a line between any coordinates in 3d space. +// Uses integer values for input, so dont expect smooth animations. +void line(int x1, int y1, int z1, int x2, int y2, int z2) +{ + float xy; // how many voxels do we move on the y axis for each step on the x axis + float xz; // how many voxels do we move on the y axis for each step on the x axis + unsigned char x,y,z; + unsigned char lasty,lastz; + + // We always want to draw the line from x=0 to x=7. + // If x1 is bigget than x2, we need to flip all the values. + 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 each step of x, y increments by: + for (x = x1; x<=x2;x++) + { + y = (xy*(x-x1))+y1; + z = (xz*(x-x1))+z1; + setvoxel(x,y,z); + } + +} + +// Delay loop. +// This is not calibrated to milliseconds, +// but we had allready made to many effects using this +// calibration when we figured it might be a good idea +// to calibrate it. +void delay_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"); + } + } + } +} + +// Copies the contents of fb (temp cube buffer) into the rendering buffer +void tmp2cube (void) +{ + memcpy((void*)cube, (const void*)fb, 64); // copy the current cube into a buffer. +} + +// Shift the entire contents of the cube along an axis +// This is great for effects where you want to draw something +// on one side of the cube and have it flow towards the other +// side. Like rain flowing down the Z axiz. +void shift (char axis, int direction) +{ + int i, x ,y; + int ii, iii; + int state; + + for (i = 0; i < CUBE_SIZE; i++) + { + if (direction == -1) + { + ii = i; + } else + { + ii = (7-i); + } + + + for (x = 0; x < CUBE_SIZE; x++) + { + for (y = 0; y < CUBE_SIZE; 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 < CUBE_SIZE; x++) + { + for (y = 0; y < CUBE_SIZE; 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); + } + } +} + +// Flip the cube 180 degrees along the y axis. +void mirror_y (void) +{ + unsigned char buffer[CUBE_SIZE][CUBE_SIZE]; + unsigned char x,y,z; + + memcpy(buffer, (const void*)cube, CUBE_BYTES); // copy the current cube into a buffer. + + fill(0x00); + for (z=0; z<CUBE_SIZE; z++) + { + for (y=0; y<CUBE_SIZE; y++) + { + for (x=0; x<CUBE_SIZE; x++) + { + if (buffer[z][y] & (0x01 << x)) + setvoxel(x,CUBE_SIZE-1-y,z); + } + } + } + +} + +// Flip the cube 180 degrees along the x axis +void mirror_x (void) +{ + unsigned char buffer[CUBE_SIZE][CUBE_SIZE]; + unsigned char y,z; + + memcpy(buffer, (const void*)cube, CUBE_BYTES); // copy the current cube into a buffer. + + fill(0x00); + + for (z=0; z<CUBE_SIZE; z++) + { + for (y=0; y<CUBE_SIZE; y++) + { + // This will break with different buffer sizes.. + cube[z][y] = flipbyte(buffer[z][y]); + } + } +} + +// flip the cube 180 degrees along the z axis +void mirror_z (void) +{ + unsigned char buffer[CUBE_SIZE][CUBE_SIZE]; + unsigned char z, y; + + memcpy(buffer, (const void*)cube, CUBE_BYTES); // copy the current cube into a buffer. + + for (y=0; y<CUBE_SIZE; y++) + { + for (z=0; z<CUBE_SIZE; z++) + { + cube[CUBE_SIZE-1-z][y] = buffer[z][y]; + } + } +} + diff --git a/avr-test/src/draw.h b/avr-test/src/draw.h new file mode 100644 index 0000000..abe93d2 --- /dev/null +++ b/avr-test/src/draw.h @@ -0,0 +1,71 @@ +#ifndef DRAW_H +#define DRAW_H + +#include <avr/io.h> +#include <avr/pgmspace.h> + +#include "cube.h" + +extern const unsigned char font[480]; + +// Red led on D2 +#define LED_RED 0x04 +// Green led D3 +#define LED_GREEN 0x08 +// Program led on D4 +#define LED_PGM 0x10; +// Leds connected to port D +#define LED_PORT PORTD +// Programming button on D5 +#define PGM_BTN 0x20 + +void delay_ms (uint16_t x); + + +void setvoxel(int x, int y, int z); +void clrvoxel(int x, int y, int z); +void tmpsetvoxel(int x, int y, int z); +void tmpclrvoxel(int x, int y, int z); + +unsigned char inrange(int x, int y, int z); +unsigned char getvoxel(int x, int y, int z); +void flpvoxel(int x, int y, int z); + +void altervoxel(int x, int y, int z, int state); +void setplane_z(int z); +void clrplane_z(int z); +void setplane_x(int x); +void clrplane_x(int x); +void setplane_y(int y); +void clrplane_y(int y); + +void setplane (char axis, unsigned char i); +void clrplane (char axis, unsigned char i); + +void setline_z(int x, int y, int z1, int z2); +void setline_x(int z, int y, int x1, int x2); +void setline_y(int z, int x, int y1, int y2); +void clrline_z(int x, int y, int z1, int z2); +void clrline_x(int z, int y, int x1, int x2); +void clrline_y(int z, int x, int y1, int y2); +void fill(unsigned char pattern); +void tmpfill(unsigned char pattern); +void line(int x1, int y1, int z1, int x2, int y2, int z2); +void drawchar(char chr, int offset, int layer); +char flipbyte(char byte); +void charfly (char chr, int direction, char axis, int mode, uint16_t delay); +void strfly (char * str, int direction, char axis, int mode, uint16_t delay, uint16_t pause); +void box_filled(int x1, int y1, int z1, int x2, int y2, int z2); +void box_walls(int x1, int y1, int z1, int x2, int y2, int z2); +void box_wireframe(int x1, int y1, int z1, int x2, int y2, int z2); +char byteline (int start, int end); + +void tmp2cube (void); +void shift (char axis, int direction); + +void mirror_x(void); +void mirror_y(void); +void mirror_z(void); + +#endif + diff --git a/avr-test/src/effect.cpp b/avr-test/src/effect.cpp new file mode 100644 index 0000000..1f47155 --- /dev/null +++ b/avr-test/src/effect.cpp @@ -0,0 +1,1021 @@ +#include "effect.h" +#include "draw.h" +#include <math.h> +#include <avr/interrupt.h> + +//char myrand(); +int myrand(); + +void effect_test (void) +{ + + int x,y,i; + + for (i=0;i<1000;i++) + { + x = sin(i/8)*2+3.5; + y = cos(i/8)*2+3.5; + + setvoxel(x,y,1); + setvoxel(x,y,1); + delay_ms(1000); + fill(0x00); + } + +} + + +// Draw a plane on one axis and send it back and forth once. +void effect_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); + } +} + +void effect_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))); + } + } + +} + +void effect_box_shrink_grow (int iterations, int rot, int flip, uint16_t delay) +{ + int x, i, xyz; + for (x=0;x<iterations;x++) + { + for (i=0;i<16;i++) + { + xyz = 7-i; // This reverses counter i between 0 and 7. + if (i > 7) + xyz = i-8; // at i > 7, i 8-15 becomes xyz 0-7. + + fill(0x00); delay_ms(1); + cli(); // disable interrupts while the cube is being rotated + box_wireframe(0,0,0,xyz,xyz,xyz); + + if (flip > 0) // upside-down + mirror_z(); + + if (rot == 1 || rot == 3) + mirror_y(); + + if (rot == 2 || rot == 3) + mirror_x(); + + sei(); // enable interrupts + delay_ms(delay); + fill(0x00); + } + } +} + +// Creates a wireframe box that shrinks or grows out from the center of the cube. +void effect_box_woopwoop (int delay, int grow) +{ + int i,ii; + + fill(0x00); + for (i=0;i<4;i++) + { + ii = i; + if (grow > 0) + ii = 3-i; + + box_wireframe(4+ii,4+ii,4+ii,3-ii,3-ii,3-ii); + delay_ms(delay); + fill(0x00); + } +} + + +// Send a voxel flying from one side of the cube to the other +// If its at the bottom, send it to the top.. +void sendvoxel_z (unsigned char x, unsigned char y, unsigned char z, int delay) +{ + int i, ii; + for (i=0; i<8; i++) + { + if (z == 7) + { + ii = 7-i; + clrvoxel(x,y,ii+1); + } else + { + ii = i; + clrvoxel(x,y,ii-1); + } + setvoxel(x,y,ii); + delay_ms(delay); + } +} + +// Send all the voxels from one side of the cube to the other +// Start at z and send to the opposite side. +// Sends in random order. +void sendplane_rand_z (unsigned char z, int delay, int wait) +{ + unsigned char loop = 16; + unsigned char x, y; + + fill(0x00); + + setplane_z(z); + + // Send voxels at random untill all 16 have crossed the cube. + while(loop) + { + x = myrand()%4; + y = myrand()%4; + if (getvoxel(x,y,z)) + { + // Send the voxel flying + sendvoxel_z(x,y,z,delay); + delay_ms(wait); + loop--; // one down, loop-- to go. when this hits 0, the loop exits. + } + } +} + +// For each coordinate along X and Y, a voxel is set either at level 0 or at level 7 +// for n iterations, a random voxel is sent to the opposite side of where it was. +void sendvoxels_rand_z (int iterations, int delay, int wait) +{ + unsigned char x, y, last_x = 0, last_y = 0, i; + + fill(0x00); + + // Loop through all the X and Y coordinates + for (x=0;x<8;x++) + { + for (y=0;y<8;y++) + { + // Then set a voxel either at the top or at the bottom + // myrand()%2 returns either 0 or 1. multiplying by 7 gives either 0 or 7. + setvoxel(x,y,((myrand()%2)*7)); + } + } + + for (i=0;i<iterations;i++) + { + // Pick a random x,y position + x = myrand()%8; + y = myrand()%8; + // but not the sameone twice in a row + if (y != last_y && x != last_x) + { + // If the voxel at this x,y is at the bottom + if (getvoxel(x,y,0)) + { + // send it to the top + sendvoxel_z(x,y,0,delay); + } else + { + // if its at the top, send it to the bottom + sendvoxel_z(x,y,7,delay); + } + delay_ms(wait); + + // Remember the last move + last_y = y; + last_x = x; + } + } + +} + + +// Big ugly function :p but it looks pretty +void boingboing(uint16_t iterations, int delay, unsigned char mode, unsigned char drawmode) +{ + fill(0x00); // Blank the cube + + int x, y, z; // Current coordinates for the point + int dx, dy, dz; // Direction of movement + int lol, i; // lol? + unsigned char crash_x, crash_y, crash_z; + + y = myrand()%8; + x = myrand()%8; + z = myrand()%8; + + // Coordinate array for the snake. + int snake[8][3]; + for (i=0;i<8;i++) + { + snake[i][0] = x; + snake[i][1] = y; + snake[i][2] = z; + } + + + dx = 1; + dy = 1; + dz = 1; + + while(iterations) + { + crash_x = 0; + crash_y = 0; + crash_z = 0; + + + // Let's mix things up a little: + if (myrand()%3 == 0) + { + // Pick a random axis, and set the speed to a random number. + lol = myrand()%3; + if (lol == 0) + dx = myrand()%3 - 1; + + if (lol == 1) + dy = myrand()%3 - 1; + + if (lol == 2) + dz = myrand()%3 - 1; + } + + // The point has reached 0 on the x-axis and is trying to go to -1 + // aka a crash + if (dx == -1 && x == 0) + { + crash_x = 0x01; + if (myrand()%3 == 1) + { + dx = 1; + } else + { + dx = 0; + } + } + + // y axis 0 crash + if (dy == -1 && y == 0) + { + crash_y = 0x01; + if (myrand()%3 == 1) + { + dy = 1; + } else + { + dy = 0; + } + } + + // z axis 0 crash + if (dz == -1 && z == 0) + { + crash_z = 0x01; + if (myrand()%3 == 1) + { + dz = 1; + } else + { + dz = 0; + } + } + + // x axis 7 crash + if (dx == 1 && x == 7) + { + crash_x = 0x01; + if (myrand()%3 == 1) + { + dx = -1; + } else + { + dx = 0; + } + } + + // y axis 7 crash + if (dy == 1 && y == 7) + { + crash_y = 0x01; + if (myrand()%3 == 1) + { + dy = -1; + } else + { + dy = 0; + } + } + + // z azis 7 crash + if (dz == 1 && z == 7) + { + crash_z = 0x01; + if (myrand()%3 == 1) + { + dz = -1; + } else + { + dz = 0; + } + } + + // mode bit 0 sets crash action enable + if (mode | 0x01) + { + if (crash_x) + { + if (dy == 0) + { + if (y == 7) + { + dy = -1; + } else if (y == 0) + { + dy = +1; + } else + { + if (myrand()%2 == 0) + { + dy = -1; + } else + { + dy = 1; + } + } + } + if (dz == 0) + { + if (z == 7) + { + dz = -1; + } else if (z == 0) + { + dz = 1; + } else + { + if (myrand()%2 == 0) + { + dz = -1; + } else + { + dz = 1; + } + } + } + } + + if (crash_y) + { + if (dx == 0) + { + if (x == 7) + { + dx = -1; + } else if (x == 0) + { + dx = 1; + } else + { + if (myrand()%2 == 0) + { + dx = -1; + } else + { + dx = 1; + } + } + } + if (dz == 0) + { + if (z == 3) + { + dz = -1; + } else if (z == 0) + { + dz = 1; + } else + { + if (myrand()%2 == 0) + { + dz = -1; + } else + { + dz = 1; + } + } + } + } + + if (crash_z) + { + if (dy == 0) + { + if (y == 7) + { + dy = -1; + } else if (y == 0) + { + dy = 1; + } else + { + if (myrand()%2 == 0) + { + dy = -1; + } else + { + dy = 1; + } + } + } + if (dx == 0) + { + if (x == 7) + { + dx = -1; + } else if (x == 0) + { + dx = 1; + } else + { + if (myrand()%2 == 0) + { + dx = -1; + } else + { + dx = 1; + } + } + } + } + } + + // mode bit 1 sets corner avoid enable + if (mode | 0x02) + { + if ( // We are in one of 8 corner positions + (x == 0 && y == 0 && z == 0) || + (x == 0 && y == 0 && z == 7) || + (x == 0 && y == 7 && z == 0) || + (x == 0 && y == 7 && z == 7) || + (x == 7 && y == 0 && z == 0) || + (x == 7 && y == 0 && z == 7) || + (x == 7 && y == 7 && z == 0) || + (x == 7 && y == 7 && z == 7) + ) + { + // At this point, the voxel would bounce + // back and forth between this corner, + // and the exact opposite corner + // We don't want that! + + // So we alter the trajectory a bit, + // to avoid corner stickyness + lol = myrand()%3; + if (lol == 0) + dx = 0; + + if (lol == 1) + dy = 0; + + if (lol == 2) + dz = 0; + } + } + + // one last sanity check + if (x == 0 && dx == -1) + dx = 1; + + if (y == 0 && dy == -1) + dy = 1; + + if (z == 0 && dz == -1) + dz = 1; + + if (x == 7 && dx == 1) + dx = -1; + + if (y == 7 && dy == 1) + dy = -1; + + if (z == 7 && dz == 1) + dz = -1; + + + // Finally, move the voxel. + x = x + dx; + y = y + dy; + z = z + dz; + + if (drawmode == 0x01) // show one voxel at time + { + setvoxel(x,y,z); + delay_ms(delay); + clrvoxel(x,y,z); + } else if (drawmode == 0x02) // flip the voxel in question + { + flpvoxel(x,y,z); + delay_ms(delay); + } if (drawmode == 0x03) // draw a snake + { + for (i=7;i>=0;i--) + { + snake[i][0] = snake[i-1][0]; + snake[i][1] = snake[i-1][1]; + snake[i][2] = snake[i-1][2]; + } + snake[0][0] = x; + snake[0][1] = y; + snake[0][2] = z; + + for (i=0;i<8;i++) + { + setvoxel(snake[i][0],snake[i][1],snake[i][2]); + } + delay_ms(delay); + for (i=0;i<8;i++) + { + clrvoxel(snake[i][0],snake[i][1],snake[i][2]); + } + } + + + iterations--; + } +} + +// Set or clear exactly 512 voxels in a random order. +void effect_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 = myrand()%8; + y = myrand()%8; + z = myrand()%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++; + } + } +} + + +void effect_rain (int iterations) +{ + int i, ii; + int rnd_x; + int rnd_y; + int rnd_num; + + for (ii=0;ii<iterations;ii++) + { + rnd_num = myrand()%4; + + for (i=0; i < rnd_num;i++) + { + rnd_x = myrand()%8; + rnd_y = myrand()%8; + setvoxel(rnd_x,rnd_y,7); + } + + delay_ms(1000); + shift(AXIS_Z,-1); + } +} + +void effect_z_updown (int iterations, int delay) +{ + unsigned char positions[64]; + unsigned char destinations[64]; + + int i,y,move; + + for (i=0; i<64; i++) + { + positions[i] = 4; + destinations[i] = myrand()%8; + } + + for (i=0; i<8; i++) + { + effect_z_updown_move(positions, destinations, AXIS_Z); + delay_ms(delay); + } + + for (i=0;i<iterations;i++) + { + for (move=0;move<8;move++) + { + effect_z_updown_move(positions, destinations, AXIS_Z); + delay_ms(delay); + } + + delay_ms(delay*4); + + + for (y=0;y<32;y++) + { + destinations[myrand()%64] = myrand()%8; + } + + } + +} + +void effect_z_updown_move (unsigned char positions[64], unsigned char destinations[64], char axis) +{ + int px; + for (px=0; px<64; px++) + { + if (positions[px]<destinations[px]) + { + positions[px]++; + } + if (positions[px]>destinations[px]) + { + positions[px]--; + } + } + + draw_positions_axis (AXIS_Z, positions,0); +} + +void effect_axis_updown_randsuspend (char axis, int delay, int sleep, int invert) +{ + unsigned char positions[64]; + unsigned char destinations[64]; + + int i,px; + + // Set 64 random positions + for (i=0; i<64; i++) + { + positions[i] = 0; // Set all starting positions to 0 + destinations[i] = myrand()%8; + } + + // Loop 8 times to allow destination 7 to reach all the way + for (i=0; i<8; i++) + { + // For every iteration, move all position one step closer to their destination + for (px=0; px<64; px++) + { + if (positions[px]<destinations[px]) + { + positions[px]++; + } + } + // Draw the positions and take a nap + draw_positions_axis (axis, positions,invert); + delay_ms(delay); + } + + // Set all destinations to 7 (opposite from the side they started out) + for (i=0; i<64; i++) + { + destinations[i] = 7; + } + + // Suspend the positions in mid-air for a while + delay_ms(sleep); + + // Then do the same thing one more time + for (i=0; i<8; i++) + { + for (px=0; px<64; px++) + { + if (positions[px]<destinations[px]) + { + positions[px]++; + } + if (positions[px]>destinations[px]) + { + positions[px]--; + } + } + draw_positions_axis (axis, positions,invert); + delay_ms(delay); + } +} + +void draw_positions_axis (char axis, unsigned char positions[64], 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); + } + } + +} + + +void effect_boxside_randsend_parallel (char axis, int origin, int delay, int mode) +{ + int i; + int done; + unsigned char cubepos[64]; + unsigned char pos[64]; + int notdone = 1; + int notdone2 = 1; + int sent = 0; + + for (i=0;i<64;i++) + { + pos[i] = 0; + } + + while (notdone) + { + if (mode == 1) + { + notdone2 = 1; + while (notdone2 && sent<64) + { + i = myrand()%64; + if (pos[i] == 0) + { + sent++; + pos[i] += 1; + notdone2 = 0; + } + } + } else if (mode == 2) + { + if (sent<64) + { + pos[sent] += 1; + sent++; + } + } + + done = 0; + for (i=0;i<64;i++) + { + if (pos[i] > 0 && pos[i] <7) + { + pos[i] += 1; + } + + if (pos[i] == 7) + done++; + } + + if (done == 64) + notdone = 0; + + for (i=0;i<64;i++) + { + if (origin == 0) + { + cubepos[i] = pos[i]; + } else + { + cubepos[i] = (7-pos[i]); + } + } + + + delay_ms(delay); + draw_positions_axis(axis,cubepos,0); + LED_PORT ^= LED_RED; + } + +} + + + + +// Light all leds layer by layer, +// then unset layer by layer +void effect_loadbar(int delay) +{ + fill(0x00); + + int z,y; + + for (z=0;z<8;z++) + { + for (y=0;y<8;y++) + cube[z][y] = 0xff; + + delay_ms(delay); + } + + delay_ms(delay*3); + + for (z=0;z<8;z++) + { + for (y=0;y<8;y++) + cube[z][y] = 0x00; + + delay_ms(delay); + } +} + + +// Set n number of voxels at random positions +void effect_random_sparkle_flash (int iterations, int voxels, int delay) +{ + int i; + int v; + for (i = 0; i < iterations; i++) + { + for (v=0;v<=voxels;v++) + setvoxel(myrand()%8,myrand()%8,myrand()%8); + + delay_ms(delay); + fill(0x00); + } +} + +// blink 1 random voxel, blink 2 random voxels..... blink 20 random voxels +// and back to 1 again. +void effect_random_sparkle (void) +{ + int i; + + for (i=1;i<20;i++) + { + effect_random_sparkle_flash(5,i,200); + } + + for (i=20;i>=1;i--) + { + effect_random_sparkle_flash(5,i,200); + } + +} + +int effect_telcstairs_do(int x, int val, int delay) +{ + int y,z; + + for(y = 0, z = x; y <= z; y++, x--) + { + if(x < CUBE_SIZE && y < CUBE_SIZE) + { + cube[x][y] = val; + } + } + delay_ms(delay); + return z; +} + +void effect_telcstairs (int invert, int delay, int val) +{ + int x; + + if(invert) + { + for(x = CUBE_SIZE*2; x >= 0; x--) + { + x = effect_telcstairs_do(x,val,delay); + } + } + else + { + for(x = 0; x < CUBE_SIZE*2; x++) + { + x = effect_telcstairs_do(x,val,delay); + } + } +} + +void effect_wormsqueeze (int size, int axis, int direction, int iterations, int delay) +{ + int x, y, i,j,k, dx, dy; + int cube_size; + int origin = 0; + + if (direction == -1) + origin = 7; + + cube_size = 8-(size-1); + + x = myrand()%cube_size; + y = myrand()%cube_size; + + for (i=0; i<iterations; i++) + { + dx = ((myrand()%3)-1); + dy = ((myrand()%3)-1); + + if ((x+dx) > 0 && (x+dx) < cube_size) + x += dx; + + if ((y+dy) > 0 && (y+dy) < cube_size) + y += dy; + + shift(axis, direction); + + + for (j=0; j<size;j++) + { + for (k=0; k<size;k++) + { + if (axis == AXIS_Z) + setvoxel(x+j,y+k,origin); + + if (axis == AXIS_Y) + setvoxel(x+j,origin,y+k); + + if (axis == AXIS_X) + setvoxel(origin,y+j,x+k); + } + } + + delay_ms(delay); + } +} + +void effect_pathmove (unsigned char *path, int length) +{ + int i,z; + unsigned char state; + + for (i=(length-1);i>=1;i--) + { + for (z=0;z<8;z++) + { + + state = getvoxel(((path[(i-1)]>>4) & 0x0f), (path[(i-1)] & 0x0f), z); + altervoxel(((path[i]>>4) & 0x0f), (path[i] & 0x0f), z, state); + } + } + for (i=0;i<8;i++) + clrvoxel(((path[0]>>4) & 0x0f), (path[0] & 0x0f),i); +} + + diff --git a/avr-test/src/effect.h b/avr-test/src/effect.h new file mode 100644 index 0000000..3398082 --- /dev/null +++ b/avr-test/src/effect.h @@ -0,0 +1,54 @@ +#ifndef EFFECT_H +#define EFFECT_H + +#include <avr/io.h> +#include <avr/pgmspace.h> +#include <stdlib.h> + +#include "cube.h" + +void effect_box_shrink_grow (int iterations, int rot, int flip, uint16_t delay); + +void effect_hollow_1 (int iterations, uint16_t delay); +void effect_hollow_2 (int iterations, int corner, uint16_t delay); + +void sendvoxel_z (unsigned char x, unsigned char y, unsigned char z, int delay); +void sendplane_rand_z (unsigned char z, int delay, int wait); +void sendvoxels_rand_z (int iterations, int delay, int wait); +void boingboing(uint16_t iterations, int delay, unsigned char mode, unsigned char drawmode); + +void effect_planboing (int plane, int speed); + +void effect_random_filler (int delay, int state); + +void effect_z_updown (int iterations, int delay); +void effect_rain(int iterations); +void effect_stringfly2(char * str); +void effect_blinky2(void); +void draw_positions_axis (char axis, unsigned char positions[64], int invert); +void effect_axis_updown_randsuspend (char axis, int delay, int sleep, int invert); + +void effect_random_sparkle_flash (int iterations, int voxels, int delay); +void effect_random_sparkle (void); + +void effect_box_woopwoop (int delay, int grow); +void effect_telcstairs (int invert, int delay, int val); +void effect_loadbar(int delay); + +void effect_boxside_randsend_parallel (char axis, int origin, int delay, int mode); +void effect_smileyspin (int count, int delay, char bitmap); +void effect_pathmove (unsigned char *path, int length); +void effect_rand_patharound (int iterations, int delay); +void effect_pathspiral (int iterations, int delay); +void effect_path_text (int delay, char *str); +void effect_path_bitmap (int delay, char bitmap, int iterations); +void effect_wormsqueeze (int size, int axis, int direction, int iterations, int delay); + +void effect_z_updown (int iterations, int delay); +void effect_z_updown_move (unsigned char positions[64], unsigned char destinations[64], char axis); + + + + +#endif + diff --git a/avr-test/src/fuses.txt b/avr-test/src/fuses.txt new file mode 100644 index 0000000..f24e5f5 --- /dev/null +++ b/avr-test/src/fuses.txt @@ -0,0 +1,6 @@ + +lfuse: 0b11101111 +hfuse: 0b11001001 + + + diff --git a/avr-test/src/lisence.txt b/avr-test/src/lisence.txt new file mode 100644 index 0000000..812dab5 --- /dev/null +++ b/avr-test/src/lisence.txt @@ -0,0 +1,5 @@ +Created by Christian Moen (christian@lynet.no) and Ståle Kristoffersen (staalekb@ifi.uio.no) 2011. + +Lisence: http://creativecommons.org/licenses/by-nc-sa/3.0/ + +Happy hacking!! :D diff --git a/avr-test/src/main.c.old b/avr-test/src/main.c.old new file mode 100644 index 0000000..be31861 --- /dev/null +++ b/avr-test/src/main.c.old @@ -0,0 +1,285 @@ +/* + * Code to controll an 8x8x8 ledcube using avr + * http://www.instructables.com/id/Led-Cube-8x8x8/ + * See lisence.txt for lisence. + */ +#include "main.h" +#include "effect.h" +#include "launch_effect.h" +#include "draw.h" + +// Main loop +// the AVR enters this function at boot time +int main (void) +{ + + // This function initiates IO ports, timers, interrupts and + // serial communications + ioinit(); + + // This variable specifies which layer is currently being drawn by the + // cube interrupt routine. We assign a value to it to make sure it's not >7. + current_layer = 1; + + int i; + + // Boot wait + // This function serves 3 purposes + // 1) We delay starting up any interrupts, as drawing the cube causes a lot + // noise that can confuse the ISP programmer. + // 2) Listen for button press. One button means go into rs232 mode, + // The other means go into autonomous mode and start doing stuff. + // 3) Random seed. The bootwait function counts forever from 0 to 255. + // Whenever you press the button, this counter stops, and the number it + // stopped at is used as a random seed. This ensures true randomness at + // every boot. Without this (or some similar process) the cube would + // produce the same "random" sequence every time + i = bootwait(); + + // Enable interrupts + // This starts the routine that draws the cube content + sei(); + + // Result for bootwait() is 2: + // Go to rs232 mode. this function loops forever. + if (i == 2) + { + rs232(); + } + + // Result of bootwait() is something other than 2: + // Do awesome effects. Loop forever. + while (1) + { + // Show the effects in a predefined order + for (i=0; i<EFFECTS_TOTAL; i++) + launch_effect(i); + + // Show the effects in a random order. + // Comment the two lines above and uncomment this + // if you want the effects in a random order. + //launch_effect(rand()%EFFECTS_TOTAL); + } + +} + +/* + * Multiplexer/framebuffer routine + * This function is called by an interrupt generated by timer 2. + * Every time it runs, it does the following: + * 1) Disable the output for the multiplexer array. + * 2) Turn of all layers. + * 3) Load the current layer from the cube buffer onto the + * multiplexer array. + * 4) Enable output from the multiplexer array. + * 5) Turn on the current layer. + * 6) Increment the current_layer variable, so the next layer is + * drawn the next time this function runs. +*/ + +ISR(TIMER2_COMP_vect) +{ + int i; + + LAYER_SELECT = 0x00; // Turn all cathode layers off. (all transistors off) + OE_PORT |= OE_MASK; // Set OE high, disabling all outputs on latch array + + // Loop through all 8 bytes of data in the current layer + // and latch it onto the cube. + for (i = 0; i < 8; i++) + { + // Set the data on the data-bus of the latch array. + PORTA = cube[current_layer][i]; + // Increment the latch address chip, 74HC138, to create + // a rising edge (LOW to HIGH) on the current latch. + LATCH_ADDR = (LATCH_ADDR & LATCH_MASK_INV) | (LATCH_MASK & (i+1)); + } + + OE_PORT &= ~OE_MASK; // Set OE low, enabling outputs on the latch array + LAYER_SELECT = (0x01 << current_layer); // Transistor ON for current layer + + // Increment the curren_layer counter so that the next layer is + // drawn the next time this function runs. + current_layer++; + // We want to count from 0-7, so set it to 0 when it reaches 8. + if (current_layer == 8) + current_layer = 0; +} + + +void ioinit (void) +{ + DDRA = 0xff; // DATA bus output + DDRB = 0xef; // Button on B4 + DDRC = 0xff; // Layer select output + DDRD = 0xdf; // Button on D5 + + + PORTA = 0x00; // Set data bus off + PORTC = 0x00; // Set layer select off + PORTB = 0x10; // Enable pull up on button. + PORTD = 0x20; // Enable pull up on button. + + + // Timer 2 + // Frame buffer interrupt + // 14745600/128/11 = 10472.72 interrupts per second + // 10472.72/8 = 1309 frames per second + OCR2 = 10; // interrupt at counter = 10 + TCCR2 |= (1 << CS20) | (1 << CS22); // Prescaler = 128. + TCCR2 |= (1 << WGM21); // CTC mode. Reset counter when OCR2 is reached. + TCNT2 = 0x00; // initial counter value = 0; + TIMSK |= (1 << OCIE2); // Enable CTC interrupt + + + + // Initiate RS232 + // USART Baud rate is defined in MYUBRR + UBRRH = MYUBRR >> 8; + UBRRL = MYUBRR; + // UCSRC - USART control register + // bit 7-6 sync/ascyn 00 = async, 01 = sync + // bit 5-4 parity 00 = disabled + // bit 3 stop bits 0 = 1 bit 1 = 2 bits + // bit 2-1 frame length 11 = 8 + // bit 0 clock polarity = 0 + UCSRC = 0b10000110; + // Enable RS232, tx and rx + UCSRB = (1<<RXEN)|(1<<TXEN); + UDR = 0x00; // send an empty byte to indicate powerup. + + +} + +// Boot wait function +// This function does 3 things: +// 1) Delay startup of interrupt. I've had some problems with in circuit +// serial programming when the cube was running. I guess switching all +// those LEDs on and off generates some noise. +// 2) Set a random random seed based on the delay between boot time and +// the time you press a button. +// 3) Select mode of operation, autonomous or rs232 controlled. +unsigned int bootwait (void) +{ + // All the LED_PORT... code blinks the red and green status LEDs. + + unsigned int x = 0; + LED_PORT |= LED_GREEN; + while (1) + { + x++; // increment x by one. + srand(x); // use counter x as random seed + + delay_ms(1000); + LED_PORT &= ~LED_GREEN; // green off, red on + LED_PORT |= LED_RED; + + // Listen for button presses and return with the + // apropriate number. + if (!(PIND & RS232_BTN)) + return 2; + + if (!(PINB & MAIN_BTN)) + return 1; + + delay_ms(1000); + LED_PORT &= ~LED_RED; // red off, green on + LED_PORT |= LED_GREEN; + + // Same as above. I do it twise because there are two delays + // in this loop, used for the red and green led blinking.. + if (!(PIND & RS232_BTN)) + return 2; + + if (!(PINB & MAIN_BTN)) + return 1; + } +} + +// Take input from a computer and load it onto the cube buffer +void rs232(void) +{ + int tempval; + int x = 0; + int y = 0; + int escape = 0; + + while (1) + { + // Switch state on red LED for debugging + // Should switch state every time the code + // is waiting for a byte to be received. + LED_PORT ^= LED_RED; + + // Wait until a byte has been received + while ( !(UCSRA & (1<<RXC)) ); + + // Load the received byte from rs232 into a buffer. + tempval = UDR; + + // Uncommet this to echo data back to the computer + // for debugging purposes. + //UDR = tempval; + + // Every time the cube receives a 0xff byte, + // it goes into sync escape mode. + // if a 0x00 byte is then received, the x and y counters + // are reset to 0. This way the x and y counters are + // always the same on the computer and in the cube. + // To send an 0xff byte, you have to send it twice! + + // Go into sync escape mode + if (tempval == 0xff) + { + // Wait for the next byte + while ( !(UCSRA & (1<<RXC)) ); + // Get the next byte + tempval = UDR; + + // Sync signal is received. + // Reset x and y counters to 0. + if (tempval == 0x00) + { + x = 0; + y = 0; + escape = 1; + } + // if no 0x00 byte is received, proceed with + // the byte we just received. + } + + if (escape == 0) + { + // Load data into the current position in the buffer + fb[x][y] = tempval; + + // Check if we have reached the limits of the buffer array. + if (y == 7) + { + if (x == 7) + { + // All data is loaded. Reset both counters + y = 0; + x = 0; + // Copy the data onto the cube. + tmp2cube(); + } else + { + // A layer is loaded, reset y and increment x. + x++; + y = 0; + } + } else + { + // We are in the middle of loading a layer. increment y. + y++; + } + + } else + { + escape = 0; + } + } +} + + diff --git a/avr-test/src/main.cpp b/avr-test/src/main.cpp new file mode 100644 index 0000000..6f3d6ba --- /dev/null +++ b/avr-test/src/main.cpp @@ -0,0 +1,508 @@ +/* (c) copyright N.C. 2011 */ + +// ATMEL ATMEGA8 +// +// +-\/-+ +// (RESET) PC6 1| |28 PC5 (ADC5/SCL) +// (RXD) PD0 2| |27 PC4 (ADC4/SDA) +// (TXD) PD1 3| |26 PC3 (ADC3) +// (INT0) PD2 4| |25 PC2 (ADC2) +// (INT1) PD3 5| |24 PC1 (ADC1) +// (XCK/T0) PD4 6| |23 PC0 (ADC0) +// VCC 7| |22 GND +// GND 8| |21 AREF +// (XTAL1/TOSC1) PB6 9| |20 AVCC +// (XTAL2/TOSC2) PB7 10| |19 PB5 (SCK) +// (T1) PD5 11| |18 PB4 (MISO) +// (AIN0) PD6 12| |17 PB3 (MOSI/OC2) +// (AIN1) PD7 13| |16 PB2 (SS/OC1B) +// (ICP1) PB0 14| |15 PB1 (OC1A) +// +----+ + +extern "C" { +#include <avr/io.h> +#include <avr/interrupt.h> + +#include <stdlib.h> // rand +} + +//char myrand() +//{ +// static short rand = 0; +// rand=(rand*109+89)%251; +// return rand; +//} + +int myrand() { return rand(); } + +#define CUBE_SIZE 8 + +//#define AXIS_X 1 +//#define AXIS_Y 2 +//#define AXIS_Z 3 + +volatile unsigned char cube[8][8]; +//volatile unsigned char current_layer = 0; +extern volatile unsigned char current_layer; +volatile bool in_wait = false; + +volatile unsigned char fb[CUBE_SIZE][CUBE_SIZE]; +/***************************************************************************** + * TIME MANAGEMENT + *****************************************************************************/ + +#define F_CPU 8000000UL + +#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L ) +#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() ) +#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() ) + +// the prescaler is set so that timer0 ticks every 64 clock cycles, and the +// the overflow handler is called every 256 ticks. +#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256)) + +// the whole number of milliseconds per timer0 overflow +#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000) + +// the fractional number of milliseconds per timer0 overflow. we shift right +// by three to fit these numbers into a byte. (for the clock speeds we care +// about - 8 and 16 MHz - this doesn't lose precision.) +#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3) +#define FRACT_MAX (1000 >> 3) + +//volatile uint32_t timer0_overflow_count = 0; +volatile uint32_t timer0_millis = 0; +//static uint8_t timer0_fract = 0; + + +ISR(TIMER0_OVF_vect) +{ + // copy these to local variables so they can be stored in registers + // (volatile variables must be read from memory on every access) + uint32_t m = timer0_millis; + //uint8_t f = timer0_fract; + static uint8_t timer0_fract = 0; + + m += MILLIS_INC; + //f += FRACT_INC; + timer0_fract += FRACT_INC; + //if (f >= FRACT_MAX) { + if (timer0_fract >= FRACT_MAX) { + //f -= FRACT_MAX; + timer0_fract -= FRACT_MAX; + ++m; + } + + //timer0_fract = f; + timer0_millis = m; + //timer0_overflow_count++; + +//static uint32_t last_time = 0; + //if (timer0_overflow_count & 0x1) + //if (m - last_time >= 5) { + //debounce_keys(); // called nearly each 2ms (0,002048s) + //last_time = m; + //} +} + +/* +inline uint32_t millis() +{ + uint32_t m; + uint8_t oldSREG = SREG; + + // disable interrupts while we read timer0_millis or we might get an + // inconsistent value (e.g. in the middle of a write to timer0_millis) + cli(); + m = timer0_millis; + SREG = oldSREG; + + return m; +} +*/ + +inline uint32_t millis() +{ + return timer0_millis; +} + +void delay(uint32_t ms) +{ + in_wait = true; + uint32_t time1 = millis(); + while ((millis()) - time1 < ms); + in_wait = false; +} +//void delay_ms(uint16_t x) +//{ +// in_wait = true; +// uint8_t y, z; +// for ( ; x > 0 ; x--){ +// for ( y = 0 ; y < 90 ; y++){ +// for ( z = 0 ; z < 6 ; z++){ +// asm volatile ("nop"); +// } +// } +// } +// in_wait = false; +//} + + +/***************************************************************************** + * ACCESSORS + *****************************************************************************/ + +//unsigned char inrange(int x, int y, int z) +//{ +// if (x >= 0 && x < CUBE_SIZE && y >= 0 && y < CUBE_SIZE && z >= 0 && z < CUBE_SIZE) +// { +// return 1; +// } else +// { +// // One of the coordinates was outside the cube. +// return 0; +// } +//} + +//bool get_led(unsigned char x, unsigned char y, unsigned char z) +//{ +// /* +// assert(x >= 0 && x <= 7); +// assert(y >= 0 && y <= 7); +// assert(z >= 0 && z <= 7); +// */ +// +// if (inrange(x, y, z)) { +// return cube[y][z] & (1 << x); +// } +// +// return false; +//} + +//void set_led(unsigned char x, unsigned char y, unsigned char z, bool on) +//{ +// +// if (!inrange(x, y, z)) { +// return; +// } +// +// /* +// assert(x >= 0 && x <= 7); +// assert(y >= 0 && y <= 7); +// assert(z >= 0 && z <= 7); +// */ +// +// if (on) { +// cube[y][z] |= ((unsigned char)1) << x; +// } +// else { +// cube[y][z] &= ~(((unsigned char)1) << x); +// } +//} + +void clear_led() +{ + for (unsigned char z = 0; z < 8; ++z) { + for (unsigned char y = 0; y < 8; ++y) { + cube[y][z] = 0; + } + } +} + +/***************************************************************************** + * RENDER + *****************************************************************************/ + +//ISR(TIMER2_COMP_vect) +//{ +// //if (!in_wait) return; +// PORTC &= ~0x28; // layer and latch low +// unsigned char current_layer_ = current_layer; +// +// for (char j = 0; j < 8; ++j) { +// //for (char j = 0; j < 4; ++j) { +// unsigned char val = cube[7-j][current_layer_]; +// //unsigned char val2 = cube[3-j][current_layer_]; +// for (char i = 0; i < 8; ++i/*, val >>= 1*/) { +// PORTC &= ~0x10; +// //PORTD = (PORTD & ~0x80) | ((val2 << (7-i)) & 0x80); +// PORTB = (PORTB & ~0x01) | ((val >> i) & 0x01); +// //PORTB |= 0x01; +// +// //PORTD |= 0x80; +// //PORTD = (PORTD & ~0x40) | (((val << (7-i)) & 0x80) >> 1); +// PORTC |= 0x10; +// } +// } +// +// PORTC = (PORTC & ~0x07) | current_layer_; +// ++current_layer_; +// current_layer = current_layer_ & 0x07; +// +// PORTC |= 0x28; // layer and latch high +//} + +ISR(TIMER2_COMP_vect) +{ + //if (!in_wait) return; + PORTC &= ~0x28; // layer and latch low + unsigned char current_layer_ = current_layer; + + for (unsigned char j = 7; j < 255; --j) { + //for (char j = 0; j < 4; ++j) { + unsigned char val = cube[j][current_layer_]; + PORTC &= ~0x10; + PORTB = (PORTB & ~0x01) | ((val ) & 0x01); + PORTC |= 0x10; + PORTC &= ~0x10; + PORTB = (PORTB & ~0x01) | ((val >> 1) & 0x01); + PORTC |= 0x10; + PORTC &= ~0x10; + PORTB = (PORTB & ~0x01) | ((val >> 2) & 0x01); + PORTC |= 0x10; + PORTC &= ~0x10; + PORTB = (PORTB & ~0x01) | ((val >> 3) & 0x01); + PORTC |= 0x10; + PORTC &= ~0x10; + PORTB = (PORTB & ~0x01) | ((val >> 4) & 0x01); + PORTC |= 0x10; + PORTC &= ~0x10; + PORTB = (PORTB & ~0x01) | ((val >> 5) & 0x01); + PORTC |= 0x10; + PORTC &= ~0x10; + PORTB = (PORTB & ~0x01) | ((val >> 6) & 0x01); + PORTC |= 0x10; + PORTC &= ~0x10; + PORTB = (PORTB & ~0x01) | ((val >> 7) & 0x01); + //PORTD = val; + PORTC |= 0x10; + } + + PORTC = (PORTC & ~0x07) | current_layer_ | 0x28; + ++current_layer_; + if (current_layer_ > 7) current_layer_ = 0; + //current_layer = current_layer_ & 0x07; + current_layer = current_layer_; + + //PORTC |= 0x28; // layer and latch high +} + + + +//void draw_positions_axis (char axis, unsigned char positions[64], int invert) +//{ +// int x, y, p; +// +// //fill(0x00); +// clear_led(); +// +// 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); +// set_led(x, y, p, true); +// +// if (axis == AXIS_Y) +// //setvoxel(x,p,y); +// set_led(x,p,y, true); +// +// if (axis == AXIS_X) +// set_led(p,y,x, true); +// } +// } +// +//} + + + + +//void effect_boxside_randsend_parallel (char axis, int origin, int delay, int mode) +//{ +// int i; +// int done; +// unsigned char cubepos[64]; +// unsigned char pos[64]; +// int notdone = 1; +// int notdone2 = 1; +// int sent = 0; +// +// for (i=0;i<64;i++) +// { +// pos[i] = 0; +// } +// +// while (notdone) +// { +// if (mode == 1) +// { +// notdone2 = 1; +// while (notdone2 && sent<64) +// { +// i = myrand()%64; +// if (pos[i] == 0) +// { +// sent++; +// pos[i] += 1; +// notdone2 = 0; +// } +// } +// } else if (mode == 2) +// { +// if (sent<64) +// { +// pos[sent] += 1; +// sent++; +// } +// } +// +// done = 0; +// for (i=0;i<64;i++) +// { +// if (pos[i] > 0 && pos[i] <7) +// { +// pos[i] += 1; +// } +// +// if (pos[i] == 7) +// done++; +// } +// +// if (done == 64) +// notdone = 0; +// +// for (i=0;i<64;i++) +// { +// if (origin == 0) +// { +// cubepos[i] = pos[i]; +// } else +// { +// cubepos[i] = (7-pos[i]); +// } +// } +// +// +// delay_ms(delay); +// draw_positions_axis(axis,cubepos,0); +// +// } +// +//} + + + +/***************************************************************************** + * MAIN + *****************************************************************************/ +#include "main.h" +#include "effect.h" +#include "draw.h" + +int main() +{ + /* + * ======================================================================= + * Initialisation + * ======================================================================= + */ + + //*** init time management + TCNT0 = 0; // init timer count to 0 + TCCR0 |= 0x03; // prescaler: 64 + TIMSK |= 0x01; // enable timer 0 overflow interrupt + + // Timer 2 + // Frame buffer interrupt + // 14745600/128/11 = 10472.72 interrupts per second + // 10472.72/8 = 1309 frames per second + OCR2 = 11; // interrupt at counter = 10 + TCCR2 |= (1 << CS20) | (0 << CS21) | (1 << CS22); // Prescaler = 128. + TCCR2 |= (1 << WGM21); // CTC mode. Reset counter when OCR2 is reached. + TCNT2 = 0x00; // initial counter value = 0; + TIMSK |= (1 << OCIE2); // Enable CTC interrupt + + PORTD = 0; + PORTB = 0; + PORTC = 0; + DDRD = 0xff; + DDRB = 0xff; + DDRC = 0xff; + + //*** set interupts + sei(); + + /* + * ======================================================================= + * MAIN LOOP + * ======================================================================= + */ + + for (;;) { + + //clear_led(); + //delay_ms(1000); + for (unsigned char z = 0; z < 8; ++z) { + for (unsigned char y = 0; y < 8; ++y) { + cube[y][z] = 0xFF; + } + } + //continue; + delay(1000); + + // Show the effects in a predefined order + //for (char i=0; i<EFFECTS_TOTAL; i++) + //launch_effect(i); + sendvoxels_rand_z(20,220,2000); + effect_rain(100); + effect_random_filler(5,1); + effect_z_updown(20,1000); + effect_wormsqueeze (2, AXIS_Z, -1, 100, 1000); + effect_blinky2(); + + + // Show the effects in a random order. + // Comment the two lines above and uncomment this + // if you want the effects in a random order. + //launch_effect(rand()%EFFECTS_TOTAL); + + for (char i = 0; i < 10; ++i) { + effect_boxside_randsend_parallel (AXIS_X, 0, 150, 1); + effect_boxside_randsend_parallel (AXIS_X, 1, 150, 1); + effect_boxside_randsend_parallel (AXIS_Y, 0, 150, 1); + effect_boxside_randsend_parallel (AXIS_Y, 1, 150, 1); + effect_boxside_randsend_parallel (AXIS_Z, 0, 150, 1); + effect_boxside_randsend_parallel (AXIS_Z, 1, 150, 1); + } + + continue; + + //return; + for (char z = 0; z < 8; ++z) { + for (char y = 0; y < 8; ++y) { + for (char x = 0; x < 8; ++x) { + //set_led(x, y, z, true); + delay(5); + delay(100); + //delay(500); + //delay(1000); + //delay_ms(1000); + } + } + } + + //delay(1000); + //PORTB ^= 0x01; + } + + return 0; // normally never return, just to be complient with c99 standard +} diff --git a/avr-test/src/main.h b/avr-test/src/main.h new file mode 100644 index 0000000..0a755f7 --- /dev/null +++ b/avr-test/src/main.h @@ -0,0 +1,45 @@ +#ifndef MAIN_H +#define MAIN_H + +#include <avr/io.h> +#include <avr/pgmspace.h> +#include <avr/interrupt.h> +#include <stdlib.h> + +#include "cube.h" + +// Define USART stuff +#define FOSC 14745600 +#define BAUD 38400 +#define MYUBRR (((((FOSC * 10) / (16L * BAUD)) + 5) / 10) - 1) + +#define DATA_BUS PORTA +#define LAYER_SELECT PORTC +#define LATCH_ADDR PORTB +#define LATCH_MASK 0x07 +#define LATCH_MASK_INV 0xf8 +#define OE_PORT PORTB +#define OE_MASK 0x08 + +// Red led on D2 +#define LED_RED 0x04 +// Green led D3 +#define LED_GREEN 0x08 +// Program led on D4 +#define LED_PGM 0x10; +// Leds connected to port D +#define LED_PORT PORTD +// Rs232 button on D5 +#define RS232_BTN 0x20 +// Main button on B4 +#define MAIN_BTN 0x10 + +void ioinit (void); +void bootmsg (void); + +volatile unsigned char current_layer; +volatile unsigned char pgm_mode; +void rs232(void); +unsigned int bootwait (void); +#endif + |