8x8x8 cube programming

Forum for posting topics and questions about anything.
andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: 8x8x8 cube programming

Post by andrew » Wed Sep 23, 2020 8:31 am

This should hopefully fix it....

HC8x8x8Cube.cpp


It's just the Line() and _Diff() functions that have been modified slightly if you just want to cut and paste them into your current file.

I'll hold off uploading this fix to the library thread for a while just in case..
You do not have the required permissions to view the files attached to this post.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

BobBurton
Posts: 34
Joined: Sat Jun 06, 2020 3:11 pm

Re: 8x8x8 cube programming

Post by BobBurton » Wed Sep 23, 2020 12:05 pm

Thanks Andrew
I have applied the changes to the library and tested it. All OK

Whilst you are tinkering with the Line() function can I suggest another change to allow it to be used to turn off a line of voxels ? I had originally copied the code for the Line function and created a new function named LineOff() but it occurred to me that it would be better implemented as an option of the Line() function so I changed it to this
  1. void Line(byte x1, byte y1, byte z1, byte x2, byte y2, byte z2, boolean show = true)
  2. {
  3.     double stepX, stepY, stepZ;
  4.     byte index;
  5.  
  6.    
  7.     if(_Diff(x1, x2) >= _Diff(y1,y2) && _Diff(x1, x2) >= _Diff(z1, z2))
  8.     {
  9.         stepX = 1;
  10.         if(x1 > x2)
  11.             stepX = -1;
  12.  
  13.        
  14.         _Diff(x2,x1)? stepY = (double)(y2 - y1)/_Diff(x2,x1) : 0;
  15.         _Diff(x2,x1)? stepZ = (double)(z2 - z1)/_Diff(x2,x1) : 0;
  16.        
  17.         for(index =0; index <= _Diff(x2,x1); index++)
  18.         {
  19.             if (show)
  20.             {
  21.                 SetVoxel((stepX*index) +x1, (stepY*index)+y1, (stepZ*index)+z1);   
  22.             }
  23.             else
  24.             {
  25.                 ClearVoxel((stepX*index) +x1, (stepY*index)+y1, (stepZ*index)+z1);
  26.             }
  27.         }
  28.  
  29.     }else if(_Diff(y1, y2) > _Diff(x1,x2) && _Diff(y1,y2) > _Diff(z1, z2))
  30.     {
  31.         stepY = 1;
  32.         if(y1 > y2)
  33.             stepY = -1;
  34.        
  35.         _Diff(y2,y1)? stepX = (double)(x2 - x1)/_Diff(y2,y1) : 0;
  36.         _Diff(y2,y1)? stepZ = (double)(z2 - z1)/_Diff(y2,y1) : 0;
  37.        
  38.         for(index =0; index <= _Diff(y2,y1); index++)
  39.         {
  40.             if (show)
  41.             {
  42.                 SetVoxel((stepX*index)+x1, (stepY*index)+y1, (stepZ*index)+z1);
  43.             }      
  44.             else
  45.             {
  46.                 ClearVoxel((stepX*index)+x1, (stepY*index)+y1, (stepZ*index)+z1);  
  47.             }
  48.         }
  49.     }else
  50.     {
  51.         stepZ = 1;
  52.         if(z1 > z2)
  53.             stepZ = -1;
  54.        
  55.         _Diff(z2,z1)? stepX = (double)(x2 - x1)/_Diff(z2,z1) : 0;
  56.         _Diff(z2,z1)? stepY = (double)(y2 - y1)/_Diff(z2,z1) : 0;
  57.        
  58.         for(index =0; index <= _Diff(z2,z1); index++)
  59.         {
  60.             if (show)
  61.             {
  62.                 SetVoxel((stepX*index)+x1, (stepY*index)+y1, (stepZ*index)+z1);
  63.             }
  64.             else
  65.             {
  66.                 ClearVoxel((stepX*index)+x1, (stepY*index)+y1, (stepZ*index)+z1);      
  67.             }
  68.         }      
  69.     }  
  70. }
with a corresponding function prototype in the .h file
  1. void Line(byte x1, byte y1, byte z1, byte x2, byte y2, byte z2, boolean show = true);
These changes do not affect the use of the Line() function but allow a line of voxels to be turned off by calling the Line() function with a seventh parameter of false

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: 8x8x8 cube programming

Post by andrew » Thu Sep 24, 2020 9:48 am

Thanks for confirming. I've added a new library function called DrawMode(). This will allow you to do what you need but its far more flexible and is more inline with how our graphics libraries work. It has one parameter which sets one of 3 drawing modes:

DRAWMODE_NORMAL This is the default mode and any of the drawing functions will work as normal in this mode.

DRAWMODE_ERASE When set to this mode any of the drawing functions will turn off LEDs rather than turning them on.

DRAWMODE_INVERT When set to this mode any of the drawing functions will toggle the current state of the LEDs.


With a bit of imagination it should allow for some more types of interesting effects but here's a couple of very basic examples:

  1. void setup()
  2. {
  3.   CubeInit();
  4. }
  5.  
  6. void loop()
  7. {
  8.   DrawMode(DRAWMODE_NORMAL);
  9.   Line (0, 0, 0, 7, 0, 0);
  10.   delay(1000);
  11.   DrawMode(DRAWMODE_ERASE);
  12.   Line (0, 0, 0, 7, 0, 0);
  13.   delay(1000);
  14. }

This of course can be simplified with the DRAWMODE_INVERT option but it demonstrates the two other modes.


  1. #include "HC8x8x8Cube.h"
  2. #include "HC8x8x8_Cube_Patterns.h"
  3.  
  4. void setup()
  5. {
  6.   CubeInit();
  7.  
  8.   Cube(1, 1, 1, 6, 6, 6, 1);
  9.  
  10.   DrawMode(DRAWMODE_INVERT);
  11. }
  12.  
  13.  
  14. void loop()
  15. {
  16.     Cube(2, 2, 2, 5, 5, 5, 1);
  17.     delay(500);
  18. }

The updated library containing this new function is here:

HC8x8x8Cube_v0_3.zip
You do not have the required permissions to view the files attached to this post.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

BobBurton
Posts: 34
Joined: Sat Jun 06, 2020 3:11 pm

Re: 8x8x8 cube programming

Post by BobBurton » Thu Sep 24, 2020 5:40 pm

Downloaded and installed

A quick example from me
  1. #include <HC8x8x8Cube.h>
  2.  
  3. void setup()
  4. {
  5.   CubeInit();
  6.   DrawMode(DRAWMODE_INVERT);
  7.   Cube(0, 0, 0, 7, 7, 7, false);
  8.   Cube(2, 2, 2, 5, 5, 5, false);
  9. }
  10.  
  11. void loop()
  12. {
  13.   Cube(0, 0, 0, 7, 7, 7, false);
  14.   Cube(2, 2, 2, 5, 5, 5, false);
  15.   Cube(1, 1, 1, 6, 6, 6, false);
  16.   Cube(3, 3, 3, 4, 4, 4, false);
  17.   delay(500);
  18. }
All seems fine thank you

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: 8x8x8 cube programming

Post by andrew » Fri Sep 25, 2020 8:02 am

Great. I'll still hold off uploading V0.3 of the library for the moment though.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply

Return to “General Discussion”