Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
myLED.cpp
00001 /** LED Cube 4x4x4 control library 00002 * 00003 * @class LED_Cube444 00004 * @author Yuji Notsu 00005 * @version 0.1(30-Dec-2010) : 1st revision 00006 * 00007 * This is a library for a demo code of 4x4x4 LED Cube 00008 * How to make of 4x4x4 LED cube is reffered to Make: vol.8 00009 * Material : 00010 * 64 LED, 4 NPN Transistor(2SC2120 etc.), 00011 * 4 of 1kOhm Resistance(for use of base current limitation), 00012 * 16 of 150[Ohm] Resistance(for use of LED current limitation) 00013 * and mbed 00014 * 00015 */ 00016 00017 00018 #include "myLED.h" 00019 00020 //** Class constructor for LED_Cube444 00021 // * 00022 // * This will create the instance and start periodical routine to display class internal buffer content to LED cube 00023 // */ 00024 00025 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 ) { 00026 cathode = 0x0; 00027 t.attach( this, &LED_Cube444::display, DISPLAY_REFRESH_RATE ); 00028 } 00029 00030 //** Set bits into cube 00031 // * 00032 // * The 16 bit word array will be copied into class internal buffer. 00033 // * It will be displayed at next update timing by periodical routine 00034 // * 00035 // * @param v[4] whole cube bits can be represented in this array. each U16 data represents the bits on each planes. 00036 // */ 00037 00038 void LED_Cube444::set_bits( U16 v[4] ) { 00039 buffer_copy( v, temp_buffer ); 00040 } 00041 00042 //** Set bits into cube 00043 // * 00044 // * The 16 bit word will be copied into specified layer of class internal buffer. 00045 // * It will be displayed at next update timing by periodical routine 00046 // * 00047 // * @param v Bit pattern for a layer. Overwrites the buffer contents 00048 // */ 00049 00050 void LED_Cube444::set_bits( int layer, U16 v ) { 00051 temp_buffer[ layer ] = v; 00052 } 00053 00054 //** Set bit into cube 00055 // * 00056 // * Set a specified bit in the buffer. 00057 // * It will be displayed at next update timing by periodical routine 00058 // * 00059 // * @param x Coodinate: x 00060 // * @param y Coodinate: y 00061 // * @param z Coodinate: z 00062 // * @param v Value for the bit (0 or 1) 00063 // */ 00064 00065 void LED_Cube444::set_bit( int x, int y, int z, U16 v ) { 00066 if ( v ) 00067 temp_buffer[ z ] |= 0x1 << ((y << 2) + x); 00068 else 00069 temp_buffer[ z ] &= ~(0x1 << ((y << 2) + x)); 00070 } 00071 00072 //** Clear 00073 // * 00074 // * Tuen-off all LEDs 00075 // */ 00076 00077 void LED_Cube444::clear( void ) { 00078 set_all_words( 0x0000 ); 00079 } 00080 00081 //** All on 00082 // * 00083 // * Tuen-on all LEDs 00084 // */ 00085 00086 void LED_Cube444::all_on( void ) { 00087 set_all_words( 0xFFFF ); 00088 } 00089 00090 /** Set bits into cube 00091 * 00092 * The 16 bit word will be copied into all layers of class internal buffer. 00093 * It will be displayed at next update timing by periodical routine 00094 * 00095 * @param v Bit pattern for layers. Overwrites the buffer contents 00096 */ 00097 00098 void LED_Cube444::set_all_words( int v ) { 00099 temp_buffer[ 0 ] = v; 00100 temp_buffer[ 1 ] = v; 00101 temp_buffer[ 2 ] = v; 00102 temp_buffer[ 3 ] = v; 00103 } 00104 00105 //** Setting for synchronous display update 00106 // * 00107 // * If the "synchronous display" option is set (default) the display 00108 // * (main) buffer will be updated each timing if just before layer 0 update. 00109 // * This machanism has been made to avoid flicker when main routine update 00110 // * buffer so frequently. 00111 // * To implement this mechanism, this class has 2 buffers. 00112 // * One is called main buffer and another is temporary buffer. 00113 // * All API calls that makes modifications of the buffer content will affect to the temp buffer. 00114 // * The display will be done with main buffer contents. So the timing of update can be controled by buffer copying. 00115 // * If the "synchronous display" option is cleard, the temporary buffer will be used for display (not tested). 00116 // * 00117 // * @param v TRUE for set, FALSE for clear 00118 // */ 00119 00120 void LED_Cube444::set_synchronous_draw( int v ) { 00121 syncronous = v; 00122 } 00123 00124 /** Displaying 00125 * 00126 * This function will be called by Ticker which set by this class' constructor. 00127 * It displays just one layer by single call. 00128 */ 00129 00130 void LED_Cube444::display( void ) { 00131 static int layer = 0; 00132 U16 *the_buffer; 00133 00134 if ( !layer & syncronous ) 00135 buffer_copy(temp_buffer, main_buffer); 00136 00137 if ( syncronous ) 00138 the_buffer = main_buffer; 00139 else 00140 the_buffer = temp_buffer; 00141 00142 cathode = 0x0; 00143 set_serialregister( the_buffer[ layer ] ); 00144 cathode = 0x1 << layer; 00145 00146 layer++; 00147 layer &= 0x3; 00148 } 00149 00150 //** Data for serial register 00151 // * 00152 // * Drives the pins to send serial data to serial register on the shield 00153 // * 00154 // * @param v Bit pattern for a layer. 00155 // */ 00156 00157 void LED_Cube444::set_serialregister( U16 v ) { 00158 // for ( int i = 0; i < SR_BIT_LENGTH; i++ ) { 00159 // sr_data = ((v >> i) & 0x1); 00160 // sr_clk = 0; 00161 // sr_clk = 1; 00162 // } 00163 for(int i=0;i< SR_BIT_LENGTH;i++) 00164 { 00165 anode = v & 0xffff; 00166 } 00167 00168 } 00169 00170 //** Array copy function 00171 // * 00172 // * Just copies the array. Loop is unrolled because it;s not big array. 00173 // * 00174 // * @param src Pointer to source array. 00175 // * @param trg Pointer to target array. 00176 // */ 00177 00178 void LED_Cube444::buffer_copy( U16 *src, U16 *trg ) { 00179 trg[ 0 ] = src[ 0 ]; 00180 trg[ 1 ] = src[ 1 ]; 00181 trg[ 2 ] = src[ 2 ]; 00182 trg[ 3 ] = src[ 3 ]; 00183 }
Generated on Sat Jul 16 2022 01:57:56 by
1.7.2