Some time ago, I wrote about device independent control of monochrome LED cubes (see this previous blog post which contains information referenced in this article).
Recently, someone contacted me about extending this framework to color cubes for a project they were considering. As it turn out, this was relatively straightforward and has added additional capability to the existing MD_Cubo library.
My correspondent had obtained a zirrfa 4x4x4 RGB LED cube. This is a relatively low cost kit that comes with its own processor that runs a looping ‘demo’. However, it also has a serial port that allows an external processor to stop the demo and manage the displays through this on-board processor. In terms of the last article, this is a ‘set and forget‘ architecture, as the refresh of the cube is handled by secondary hardware components rather than the Arduino processor.
So what’s different with a color cube?
The only difference between an on/off LED cube and a color cube is … the colored LED!
So turning the LED on/off now becomes a matter of setting a LED to a specific color. Some thought also reveals that an on/off cube is a special case of a colored cube – it just has 2 colors (black for off and non-black for on). A monochrome cube can therefore be mapped to a color cube using just 2 colors.
So essentially the task turned into evolving the core class methods from a boolean parameter (on/off) for LED state to a 32-bit integer that provides the desired RGB color. The old boolean setVoxel() is now written in terms of an overloaded RGB setVoxel(), using the default RGB colors for on and off.
It is also important to realise that animations for monochrome cubes exploit on/off patterns while animations for color cubes will leverage color changes. Trying to map these animations to each other would result in every colored LED being ‘on’ in monochrome space. Essentially, the whole monochrome cube could be lit most of the time when showing the color animation. So a new method (isColorCube()) is needed that allows the programmer to determine whether the cube being programmed supports color.
The addition of some new macros to allow packing and unpacking RGB values in a standard manner and the definition of a whole lot of standard colors rounded out the required changes.
Once again, the extensibility of C++ class inheritance makes it easy to extend the class around the common core with minimal changes needed to the code for the existing cubes.