This program is simple 4x4x4 LED cube demo program. 4 cathode and 16 anode is defined by 2 BusOut. Almost LED cube444 class function is made by Tedd Okano (Thank you).
Revision 0:808f7981f647, committed 2010-12-30
- Comitter:
- y_notsu
- Date:
- Thu Dec 30 15:17:29 2010 +0000
- Commit message:
- Dec 30th, 2010 : 1st release
Changed in this revision
myLED.cpp | Show annotated file Show diff for this revision Revisions of this file |
myLED.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 808f7981f647 myLED.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/myLED.cpp Thu Dec 30 15:17:29 2010 +0000 @@ -0,0 +1,183 @@ +/** LED Cube 4x4x4 control library + * + * @class LED_Cube444 + * @author Yuji Notsu + * @version 0.1(30-Dec-2010) : 1st revision + * + * This is a library for a demo code of 4x4x4 LED Cube + * How to make of 4x4x4 LED cube is reffered to Make: vol.8 + * Material : + * 64 LED, 4 NPN Transistor(2SC2120 etc.), + * 4 of 1kOhm Resistance(for use of base current limitation), + * 16 of 150[Ohm] Resistance(for use of LED current limitation) + * and mbed + * + */ + + +#include "myLED.h" + +//** Class constructor for LED_Cube444 +// * +// * This will create the instance and start periodical routine to display class internal buffer content to LED cube +// */ + +LED_Cube444::LED_Cube444() : anode(A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15), cathode( CATHODE0, CATHODE1, CATHODE2, CATHODE3 ), syncronous( 1 ) { + cathode = 0x0; + t.attach( this, &LED_Cube444::display, DISPLAY_REFRESH_RATE ); +} + +//** Set bits into cube +// * +// * The 16 bit word array will be copied into class internal buffer. +// * It will be displayed at next update timing by periodical routine +// * +// * @param v[4] whole cube bits can be represented in this array. each U16 data represents the bits on each planes. +// */ + +void LED_Cube444::set_bits( U16 v[4] ) { + buffer_copy( v, temp_buffer ); +} + +//** Set bits into cube +// * +// * The 16 bit word will be copied into specified layer of class internal buffer. +// * It will be displayed at next update timing by periodical routine +// * +// * @param v Bit pattern for a layer. Overwrites the buffer contents +// */ + +void LED_Cube444::set_bits( int layer, U16 v ) { + temp_buffer[ layer ] = v; +} + +//** Set bit into cube +// * +// * Set a specified bit in the buffer. +// * It will be displayed at next update timing by periodical routine +// * +// * @param x Coodinate: x +// * @param y Coodinate: y +// * @param z Coodinate: z +// * @param v Value for the bit (0 or 1) +// */ + +void LED_Cube444::set_bit( int x, int y, int z, U16 v ) { + if ( v ) + temp_buffer[ z ] |= 0x1 << ((y << 2) + x); + else + temp_buffer[ z ] &= ~(0x1 << ((y << 2) + x)); +} + +//** Clear +// * +// * Tuen-off all LEDs +// */ + +void LED_Cube444::clear( void ) { + set_all_words( 0x0000 ); +} + +//** All on +// * +// * Tuen-on all LEDs +// */ + +void LED_Cube444::all_on( void ) { + set_all_words( 0xFFFF ); +} + +/** Set bits into cube + * + * The 16 bit word will be copied into all layers of class internal buffer. + * It will be displayed at next update timing by periodical routine + * + * @param v Bit pattern for layers. Overwrites the buffer contents + */ + +void LED_Cube444::set_all_words( int v ) { + temp_buffer[ 0 ] = v; + temp_buffer[ 1 ] = v; + temp_buffer[ 2 ] = v; + temp_buffer[ 3 ] = v; +} + +//** Setting for synchronous display update +// * +// * If the "synchronous display" option is set (default) the display +// * (main) buffer will be updated each timing if just before layer 0 update. +// * This machanism has been made to avoid flicker when main routine update +// * buffer so frequently. +// * To implement this mechanism, this class has 2 buffers. +// * One is called main buffer and another is temporary buffer. +// * All API calls that makes modifications of the buffer content will affect to the temp buffer. +// * The display will be done with main buffer contents. So the timing of update can be controled by buffer copying. +// * If the "synchronous display" option is cleard, the temporary buffer will be used for display (not tested). +// * +// * @param v TRUE for set, FALSE for clear +// */ + +void LED_Cube444::set_synchronous_draw( int v ) { + syncronous = v; +} + +/** Displaying + * + * This function will be called by Ticker which set by this class' constructor. + * It displays just one layer by single call. + */ + +void LED_Cube444::display( void ) { + static int layer = 0; + U16 *the_buffer; + + if ( !layer & syncronous ) + buffer_copy(temp_buffer, main_buffer); + + if ( syncronous ) + the_buffer = main_buffer; + else + the_buffer = temp_buffer; + + cathode = 0x0; + set_serialregister( the_buffer[ layer ] ); + cathode = 0x1 << layer; + + layer++; + layer &= 0x3; +} + +//** Data for serial register +// * +// * Drives the pins to send serial data to serial register on the shield +// * +// * @param v Bit pattern for a layer. +// */ + +void LED_Cube444::set_serialregister( U16 v ) { + // for ( int i = 0; i < SR_BIT_LENGTH; i++ ) { + // sr_data = ((v >> i) & 0x1); + // sr_clk = 0; + // sr_clk = 1; + // } + for(int i=0;i< SR_BIT_LENGTH;i++) + { + anode = v & 0xffff; + } + +} + +//** Array copy function +// * +// * Just copies the array. Loop is unrolled because it;s not big array. +// * +// * @param src Pointer to source array. +// * @param trg Pointer to target array. +// */ + +void LED_Cube444::buffer_copy( U16 *src, U16 *trg ) { + trg[ 0 ] = src[ 0 ]; + trg[ 1 ] = src[ 1 ]; + trg[ 2 ] = src[ 2 ]; + trg[ 3 ] = src[ 3 ]; +}
diff -r 000000000000 -r 808f7981f647 myLED.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/myLED.h Thu Dec 30 15:17:29 2010 +0000 @@ -0,0 +1,232 @@ +/** LED Cube 4x4x4 control library + * + * @class LED_Cube444 + * @author Yuji Notsu + * @version 0.1(30-Dec-2010) + * + * This is a library for a demo code of simple 4x4x4 LED Cube shield + * + * Released under the MIT License: http://mbed.org/license/mit + * + * revision 0.1 30-Dec-2010 1st release + */ + + +#ifndef _LED_Cube +#define _LED_Cube + +#include "mbed.h" + +typedef unsigned short U16; // bit length should be same or more than SR_BIT_LENGTH + +#define CUBE_SIZE 4 // n of nodes on one edge +#define SR_BIT_LENGTH (CUBE_SIZE * CUBE_SIZE) // n of bits on one layer + +//#define SR_DATA_OUT ARD_D2 // data line to the shiftregister +//#define SR_CLCK_OUT ARD_D3 // clock line to the shiftregister +#define A0 p26 +#define A1 p25 +#define A2 p24 +#define A3 p23 +#define A4 p22 +#define A5 p21 +#define A6 p20 +#define A7 p19 +#define A8 p18 +#define A9 p17 +#define A10 p16 +#define A11 p15 +#define A12 p14 +#define A13 p13 +#define A14 p12 +#define A15 p11 +#define CATHODE0 p30 // cathode control line for bottom layer +#define CATHODE1 p29 // cathode control line for 2nd layer from bottom +#define CATHODE2 p28 // cathode control line for 3nd layer from bottom +#define CATHODE3 p27 // cathode control line for top layer + +#define DISPLAY_REFRESH_RATE 0.002 + + + + +class LED_Cube444 { +public: + + /** Class constructor for LED_Cube444 + * + * This will create the instance and start periodical routine to display class internal buffer content to LED cube + */ + + LED_Cube444(); + + /** Set bits into cube + * + * The 16 bit word array will be copied into class internal buffer. + * It will be displayed at next update timing by periodical routine + * + * @param v[4] whole cube bits can be represented in this array. each U16 data represents the bits on each planes. + */ + + void set_bits( U16 v[4] ); + + /** Set bits into cube + * + * The 16 bit word will be copied into specified layer of class internal buffer. + * It will be displayed at next update timing by periodical routine + * + * @param v Bit pattern for a layer. Overwrites the buffer contents + */ + + void set_bits( int layer, U16 v ); + + /** Set bit into cube + * + * Set a specified bit in the buffer. + * It will be displayed at next update timing by periodical routine + * + * @param x Coodinate: x + * @param y Coodinate: y + * @param z Coodinate: z + * @param v Value for the bit (0 or 1) + */ + + void set_bit( int x, int y, int z, U16 v ); + + /** Clear + * + * Turn-OFF all LEDs + */ + + void clear( void ); + + /** All on + * + * Turn-ON all LEDs + */ + + void all_on( void ); + + /** Set bits into cube + * + * The 16 bit word will be copied into all layers of class internal buffer. + * It will be displayed at next update timing by periodical routine + * + * @param v Bit pattern for layers. Overwrites the buffer contents + */ + + void set_all_words( int v ); + + //* + // * Setting for synchronous display update + // * + // * If the "synchronous display" option is set (default) the display + // * (main) buffer will be updated each timing if just before layer 0 update. + // * This machanism has been made to avoid flicker when main routine update + // * buffer so frequently. + // * To implement this mechanism, this class has 2 buffers. + // * One is called main buffer and another is temporary buffer. + // * All API calls that makes modifications of the buffer content will affect to the temp buffer. + // * The display will be done with main buffer contents. So the timing of update can be controled by buffer copying. + // * If the "synchronous display" option is cleard, the temporary buffer will be used for display (not tested). + // * + // * @param v TRUE for set, FALSE for clear + // */ + + void set_synchronous_draw( int v ); + +private: + //DigitalOut sr_data; + //DigitalOut sr_clk; + BusOut anode; + BusOut cathode; + Ticker t; + U16 main_buffer[ CUBE_SIZE ]; + U16 temp_buffer[ CUBE_SIZE ]; + int syncronous; + + //** Displaying + // * + // * This function will be called by Ticker which set by this class' constructor. + // * It displays just one layer by single call. + // */ + + void display( void ); + + //** Data for serial register + //* + //* Drives the pins to send serial data to serial register on the shield + //* + //* @param v Bit pattern for a layer. + //*/ + + void set_serialregister( U16 v ); + + //** Array copy function + //* + //* Just copies the array. Loop is unrolled because it;s not big array. + //* + //* @param src Pointer to source array. + //* @param trg Pointer to target array. + //*/ + + void buffer_copy( U16 *src, U16 *trg ); +}; + +#endif // _LED_Cube + +/* test code */ +/* + #include "mbed.h" + #include "myLED.h" + + LED_Cube444 cube; + + int main() { + cube.all_on(); + wait(0.5); + cube.clear(); + wait(0.5); + cube.set_bits( 0, 0xA5A5 ); + cube.set_bits( 1, 0x5A5A ); + cube.set_bits( 2, 0xA5A5 ); + cube.set_bits( 3, 0x5A5A ); + wait( 0.5 ); + + cube.set_bits( 0, 0x5A5A ); + cube.set_bits( 1, 0xA5A5 ); + cube.set_bits( 2, 0x5A5A ); + cube.set_bits( 3, 0xA5A5 ); + wait( 0.5 ); + + cube.set_bits( 0, 0xA5A5 ); + cube.set_bits( 1, 0x5A5A ); + cube.set_bits( 2, 0xA5A5 ); + cube.set_bits( 3, 0x5A5A ); + wait( 0.5 ); + + cube.set_bits( 0, 0x5A5A ); + cube.set_bits( 1, 0xA5A5 ); + cube.set_bits( 2, 0x5A5A ); + cube.set_bits( 3, 0xA5A5 ); + wait( 0.5 ); + + cube.clear(); + + int v = 1; + + while ( 1 ) { + + for ( int x = 0; x < 4; x++ ) { + for ( int y = 0; y < 4; y++ ) { + for ( int z = 0; z < 4; z++ ) { + cube.set_bit( x, y, z, v ); + wait( 0.02); + } + } + } + + v = !v; + } + } + */