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