A class library for "mbeduino" + "4x4x4 LED Cube shield" demo. \\ There are two demo programs for this library available. Please find those at...\\ (1) Very simplified demo sample code : \\ http://mbed.org/users/okano/programs/LED_Cube444_mbeduino-very_simple_demo/\\ (2) More complex (fancy) demo : \\ http://mbed.org/users/okano/programs/LED_Cube444_mbeduino_demo/\\ Another application code is available for hardware test...\\ http://mbed.org/users/okano/programs/mbeduino_LED_Cube444_test/\\ About the hardware :\\ http://mbed.org/users/okini3939/notebook/mbeduino/ \\ http://www.galileo-7.com/?pid=20015630 \\ - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\\ "mbeduino" + "4x4x4 LED Cube shield"のデモ用に作成したクラスライブラリです.\\ このクラスライブラリには2つのデモ・プログラム(サンプル・コード)が用意されています.\\ (1) 単純化したデモ・サンプル\\ http://mbed.org/users/okano/programs/LED_Cube444_mbeduino-very_simple_demo/\\ (2) 点灯バリエーションを盛り込んだデモ\\ http://mbed.org/users/okano/programs/LED_Cube444_mbeduino_demo/\\ またこれとは別に,ハードウェアのテスト用のコードを公開してあります\\ http://mbed.org/users/okano/programs/mbeduino_LED_Cube444_test/\\ ハードウェアについては以下のリンクを参照下さい\\ http://mbed.org/users/okini3939/notebook/mbeduino/ \\ http://www.galileo-7.com/?pid=20015630 \\

Committer:
okano
Date:
Wed Dec 08 02:03:02 2010 +0000
Revision:
1:86d2cad72832
Parent:
0:6c57a47e2079
rev0.51: Document modification done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 1:86d2cad72832 1 /** LED Cube 4x4x4 control library
okano 0:6c57a47e2079 2 *
okano 1:86d2cad72832 3 * @class LED_Cube444
okano 1:86d2cad72832 4 * @author Tedd OKANO
okano 1:86d2cad72832 5 * @version 0.51(08-Dec-2010)
okano 1:86d2cad72832 6 *
okano 1:86d2cad72832 7 * This is a library for a demo code of mbeduino + 4x4x4 LED Cube shield
okano 0:6c57a47e2079 8 *
okano 0:6c57a47e2079 9 * mbeduino = http://mbed.org/users/okini3939/notebook/mbeduino/ (Japanese)
okano 0:6c57a47e2079 10 * 4x4x4 LED Cube shield = http://www.galileo-7.com/?pid=20015630 (Japanese)
okano 0:6c57a47e2079 11 *
okano 0:6c57a47e2079 12 * Released under the MIT License: http://mbed.org/license/mit
okano 0:6c57a47e2079 13 *
okano 1:86d2cad72832 14 * revision 0.5 14-Oct-2010 1st release
okano 1:86d2cad72832 15 * revision 0.51 08-Dec-2010 Document reformatted
okano 0:6c57a47e2079 16 */
okano 0:6c57a47e2079 17
okano 1:86d2cad72832 18
okano 0:6c57a47e2079 19 #include "LED_Cube444.h"
okano 0:6c57a47e2079 20
okano 0:6c57a47e2079 21 /** Class constructor for LED_Cube444
okano 0:6c57a47e2079 22 *
okano 0:6c57a47e2079 23 * This will create the instance and start periodical routine to display class internal buffer content to LED cube
okano 0:6c57a47e2079 24 */
okano 0:6c57a47e2079 25
okano 0:6c57a47e2079 26 LED_Cube444::LED_Cube444() : sr_data(SR_DATA_OUT), sr_clk(SR_CLCK_OUT), cathode( CATHODE0, CATHODE1, CATHODE2, CATHODE3 ), syncronous( 1 ) {
okano 0:6c57a47e2079 27 cathode = 0x0;
okano 0:6c57a47e2079 28 t.attach( this, &LED_Cube444::display, DISPLAY_REFRESH_RETE );
okano 0:6c57a47e2079 29 }
okano 0:6c57a47e2079 30
okano 0:6c57a47e2079 31 /** Set bits into cube
okano 0:6c57a47e2079 32 *
okano 0:6c57a47e2079 33 * The 16 bit word array will be copied into class internal buffer.
okano 0:6c57a47e2079 34 * It will be displayed at next update timing by periodical routine
okano 0:6c57a47e2079 35 *
okano 0:6c57a47e2079 36 * @param v[4] whole cube bits can be represented in this array. each U16 data represents the bits on each planes.
okano 0:6c57a47e2079 37 */
okano 0:6c57a47e2079 38
okano 0:6c57a47e2079 39 void LED_Cube444::set_bits( U16 v[4] ) {
okano 0:6c57a47e2079 40 buffer_copy( v, temp_buffer );
okano 0:6c57a47e2079 41 }
okano 0:6c57a47e2079 42
okano 0:6c57a47e2079 43 /** Set bits into cube
okano 0:6c57a47e2079 44 *
okano 0:6c57a47e2079 45 * The 16 bit word will be copied into specified layer of class internal buffer.
okano 0:6c57a47e2079 46 * It will be displayed at next update timing by periodical routine
okano 0:6c57a47e2079 47 *
okano 0:6c57a47e2079 48 * @param v Bit pattern for a layer. Overwrites the buffer contents
okano 0:6c57a47e2079 49 */
okano 0:6c57a47e2079 50
okano 0:6c57a47e2079 51 void LED_Cube444::set_bits( int layer, U16 v ) {
okano 0:6c57a47e2079 52 temp_buffer[ layer ] = v;
okano 0:6c57a47e2079 53 }
okano 0:6c57a47e2079 54
okano 0:6c57a47e2079 55 /** Set bit into cube
okano 0:6c57a47e2079 56 *
okano 0:6c57a47e2079 57 * Set a specified bit in the buffer.
okano 0:6c57a47e2079 58 * It will be displayed at next update timing by periodical routine
okano 0:6c57a47e2079 59 *
okano 0:6c57a47e2079 60 * @param x Coodinate: x
okano 0:6c57a47e2079 61 * @param y Coodinate: y
okano 0:6c57a47e2079 62 * @param z Coodinate: z
okano 0:6c57a47e2079 63 * @param v Value for the bit (0 or 1)
okano 0:6c57a47e2079 64 */
okano 0:6c57a47e2079 65
okano 0:6c57a47e2079 66 void LED_Cube444::set_bit( int x, int y, int z, U16 v ) {
okano 0:6c57a47e2079 67 if ( v )
okano 0:6c57a47e2079 68 temp_buffer[ z ] |= 0x1 << ((y << 2) + x);
okano 0:6c57a47e2079 69 else
okano 0:6c57a47e2079 70 temp_buffer[ z ] &= ~(0x1 << ((y << 2) + x));
okano 0:6c57a47e2079 71 }
okano 0:6c57a47e2079 72
okano 0:6c57a47e2079 73 /** Clear
okano 0:6c57a47e2079 74 *
okano 0:6c57a47e2079 75 * Tuen-off all LEDs
okano 0:6c57a47e2079 76 */
okano 0:6c57a47e2079 77
okano 0:6c57a47e2079 78 void LED_Cube444::clear( void ) {
okano 0:6c57a47e2079 79 set_all_words( 0x0000 );
okano 0:6c57a47e2079 80 }
okano 0:6c57a47e2079 81
okano 0:6c57a47e2079 82 /** All on
okano 0:6c57a47e2079 83 *
okano 0:6c57a47e2079 84 * Tuen-on all LEDs
okano 0:6c57a47e2079 85 */
okano 0:6c57a47e2079 86
okano 0:6c57a47e2079 87 void LED_Cube444::all_on( void ) {
okano 0:6c57a47e2079 88 set_all_words( 0xFFFF );
okano 0:6c57a47e2079 89 }
okano 0:6c57a47e2079 90
okano 0:6c57a47e2079 91 /** Set bits into cube
okano 0:6c57a47e2079 92 *
okano 0:6c57a47e2079 93 * The 16 bit word will be copied into all layers of class internal buffer.
okano 0:6c57a47e2079 94 * It will be displayed at next update timing by periodical routine
okano 0:6c57a47e2079 95 *
okano 0:6c57a47e2079 96 * @param v Bit pattern for layers. Overwrites the buffer contents
okano 0:6c57a47e2079 97 */
okano 0:6c57a47e2079 98
okano 0:6c57a47e2079 99 void LED_Cube444::set_all_words( int v ) {
okano 0:6c57a47e2079 100 temp_buffer[ 0 ] = v;
okano 0:6c57a47e2079 101 temp_buffer[ 1 ] = v;
okano 0:6c57a47e2079 102 temp_buffer[ 2 ] = v;
okano 0:6c57a47e2079 103 temp_buffer[ 3 ] = v;
okano 0:6c57a47e2079 104 }
okano 0:6c57a47e2079 105
okano 0:6c57a47e2079 106 /** Setting for synchronous display update
okano 0:6c57a47e2079 107 *
okano 0:6c57a47e2079 108 * If the "synchronous display" option is set (default) the display
okano 0:6c57a47e2079 109 * (main) buffer will be updated each timing if just before layer 0 update.
okano 0:6c57a47e2079 110 * This machanism has been made to avoid flicker when main routine update
okano 0:6c57a47e2079 111 * buffer so frequently.
okano 0:6c57a47e2079 112 * To implement this mechanism, this class has 2 buffers.
okano 0:6c57a47e2079 113 * One is called main buffer and another is temporary buffer.
okano 0:6c57a47e2079 114 * All API calls that makes modifications of the buffer content will affect to the temp buffer.
okano 0:6c57a47e2079 115 * The display will be done with main buffer contents. So the timing of update can be controled by buffer copying.
okano 0:6c57a47e2079 116 * If the "synchronous display" option is cleard, the temporary buffer will be used for display (not tested).
okano 0:6c57a47e2079 117 *
okano 0:6c57a47e2079 118 * @param v TRUE for set, FALSE for clear
okano 0:6c57a47e2079 119 */
okano 0:6c57a47e2079 120
okano 0:6c57a47e2079 121 void LED_Cube444::set_synchronous_draw( int v ) {
okano 0:6c57a47e2079 122 syncronous = v;
okano 0:6c57a47e2079 123 }
okano 0:6c57a47e2079 124
okano 0:6c57a47e2079 125 /** Displaying
okano 0:6c57a47e2079 126 *
okano 0:6c57a47e2079 127 * This function will be called by Ticker which set by this class' constructor.
okano 0:6c57a47e2079 128 * It displays just one layer by single call.
okano 0:6c57a47e2079 129 */
okano 0:6c57a47e2079 130
okano 0:6c57a47e2079 131 void LED_Cube444::display( void ) {
okano 0:6c57a47e2079 132 static int layer = 0;
okano 0:6c57a47e2079 133 U16 *the_buffer;
okano 0:6c57a47e2079 134
okano 0:6c57a47e2079 135 if ( !layer & syncronous )
okano 0:6c57a47e2079 136 buffer_copy(temp_buffer, main_buffer);
okano 0:6c57a47e2079 137
okano 0:6c57a47e2079 138 if ( syncronous )
okano 0:6c57a47e2079 139 the_buffer = main_buffer;
okano 0:6c57a47e2079 140 else
okano 0:6c57a47e2079 141 the_buffer = temp_buffer;
okano 0:6c57a47e2079 142
okano 0:6c57a47e2079 143 cathode = 0x0;
okano 0:6c57a47e2079 144 set_serialregister( the_buffer[ layer ] );
okano 0:6c57a47e2079 145 cathode = 0x1 << layer;
okano 0:6c57a47e2079 146
okano 0:6c57a47e2079 147 layer++;
okano 0:6c57a47e2079 148 layer &= 0x3;
okano 0:6c57a47e2079 149 }
okano 0:6c57a47e2079 150
okano 0:6c57a47e2079 151 /** Data for serial register
okano 0:6c57a47e2079 152 *
okano 0:6c57a47e2079 153 * Drives the pins to send serial data to serial register on the shield
okano 0:6c57a47e2079 154 *
okano 0:6c57a47e2079 155 * @param v Bit pattern for a layer.
okano 0:6c57a47e2079 156 */
okano 0:6c57a47e2079 157
okano 0:6c57a47e2079 158 void LED_Cube444::set_serialregister( U16 v ) {
okano 0:6c57a47e2079 159 for ( int i = 0; i < SR_BIT_LENGTH; i++ ) {
okano 0:6c57a47e2079 160 sr_data = ((v >> i) & 0x1);
okano 0:6c57a47e2079 161 sr_clk = 0;
okano 0:6c57a47e2079 162 sr_clk = 1;
okano 0:6c57a47e2079 163 }
okano 0:6c57a47e2079 164 }
okano 0:6c57a47e2079 165
okano 0:6c57a47e2079 166 /** Array copy function
okano 0:6c57a47e2079 167 *
okano 0:6c57a47e2079 168 * Just copies the array. Loop is unrolled because it;s not big array.
okano 0:6c57a47e2079 169 *
okano 0:6c57a47e2079 170 * @param src Pointer to source array.
okano 0:6c57a47e2079 171 * @param trg Pointer to target array.
okano 0:6c57a47e2079 172 */
okano 0:6c57a47e2079 173
okano 0:6c57a47e2079 174 void LED_Cube444::buffer_copy( U16 *src, U16 *trg ) {
okano 0:6c57a47e2079 175 trg[ 0 ] = src[ 0 ];
okano 0:6c57a47e2079 176 trg[ 1 ] = src[ 1 ];
okano 0:6c57a47e2079 177 trg[ 2 ] = src[ 2 ];
okano 0:6c57a47e2079 178 trg[ 3 ] = src[ 3 ];
okano 0:6c57a47e2079 179 }