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.h@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) |
y_notsu | 0:808f7981f647 | 6 | * |
y_notsu | 0:808f7981f647 | 7 | * This is a library for a demo code of simple 4x4x4 LED Cube shield |
y_notsu | 0:808f7981f647 | 8 | * |
y_notsu | 0:808f7981f647 | 9 | * Released under the MIT License: http://mbed.org/license/mit |
y_notsu | 0:808f7981f647 | 10 | * |
y_notsu | 0:808f7981f647 | 11 | * revision 0.1 30-Dec-2010 1st release |
y_notsu | 0:808f7981f647 | 12 | */ |
y_notsu | 0:808f7981f647 | 13 | |
y_notsu | 0:808f7981f647 | 14 | |
y_notsu | 0:808f7981f647 | 15 | #ifndef _LED_Cube |
y_notsu | 0:808f7981f647 | 16 | #define _LED_Cube |
y_notsu | 0:808f7981f647 | 17 | |
y_notsu | 0:808f7981f647 | 18 | #include "mbed.h" |
y_notsu | 0:808f7981f647 | 19 | |
y_notsu | 0:808f7981f647 | 20 | typedef unsigned short U16; // bit length should be same or more than SR_BIT_LENGTH |
y_notsu | 0:808f7981f647 | 21 | |
y_notsu | 0:808f7981f647 | 22 | #define CUBE_SIZE 4 // n of nodes on one edge |
y_notsu | 0:808f7981f647 | 23 | #define SR_BIT_LENGTH (CUBE_SIZE * CUBE_SIZE) // n of bits on one layer |
y_notsu | 0:808f7981f647 | 24 | |
y_notsu | 0:808f7981f647 | 25 | //#define SR_DATA_OUT ARD_D2 // data line to the shiftregister |
y_notsu | 0:808f7981f647 | 26 | //#define SR_CLCK_OUT ARD_D3 // clock line to the shiftregister |
y_notsu | 0:808f7981f647 | 27 | #define A0 p26 |
y_notsu | 0:808f7981f647 | 28 | #define A1 p25 |
y_notsu | 0:808f7981f647 | 29 | #define A2 p24 |
y_notsu | 0:808f7981f647 | 30 | #define A3 p23 |
y_notsu | 0:808f7981f647 | 31 | #define A4 p22 |
y_notsu | 0:808f7981f647 | 32 | #define A5 p21 |
y_notsu | 0:808f7981f647 | 33 | #define A6 p20 |
y_notsu | 0:808f7981f647 | 34 | #define A7 p19 |
y_notsu | 0:808f7981f647 | 35 | #define A8 p18 |
y_notsu | 0:808f7981f647 | 36 | #define A9 p17 |
y_notsu | 0:808f7981f647 | 37 | #define A10 p16 |
y_notsu | 0:808f7981f647 | 38 | #define A11 p15 |
y_notsu | 0:808f7981f647 | 39 | #define A12 p14 |
y_notsu | 0:808f7981f647 | 40 | #define A13 p13 |
y_notsu | 0:808f7981f647 | 41 | #define A14 p12 |
y_notsu | 0:808f7981f647 | 42 | #define A15 p11 |
y_notsu | 0:808f7981f647 | 43 | #define CATHODE0 p30 // cathode control line for bottom layer |
y_notsu | 0:808f7981f647 | 44 | #define CATHODE1 p29 // cathode control line for 2nd layer from bottom |
y_notsu | 0:808f7981f647 | 45 | #define CATHODE2 p28 // cathode control line for 3nd layer from bottom |
y_notsu | 0:808f7981f647 | 46 | #define CATHODE3 p27 // cathode control line for top layer |
y_notsu | 0:808f7981f647 | 47 | |
y_notsu | 0:808f7981f647 | 48 | #define DISPLAY_REFRESH_RATE 0.002 |
y_notsu | 0:808f7981f647 | 49 | |
y_notsu | 0:808f7981f647 | 50 | |
y_notsu | 0:808f7981f647 | 51 | |
y_notsu | 0:808f7981f647 | 52 | |
y_notsu | 0:808f7981f647 | 53 | class LED_Cube444 { |
y_notsu | 0:808f7981f647 | 54 | public: |
y_notsu | 0:808f7981f647 | 55 | |
y_notsu | 0:808f7981f647 | 56 | /** Class constructor for LED_Cube444 |
y_notsu | 0:808f7981f647 | 57 | * |
y_notsu | 0:808f7981f647 | 58 | * This will create the instance and start periodical routine to display class internal buffer content to LED cube |
y_notsu | 0:808f7981f647 | 59 | */ |
y_notsu | 0:808f7981f647 | 60 | |
y_notsu | 0:808f7981f647 | 61 | LED_Cube444(); |
y_notsu | 0:808f7981f647 | 62 | |
y_notsu | 0:808f7981f647 | 63 | /** Set bits into cube |
y_notsu | 0:808f7981f647 | 64 | * |
y_notsu | 0:808f7981f647 | 65 | * The 16 bit word array will be copied into class internal buffer. |
y_notsu | 0:808f7981f647 | 66 | * It will be displayed at next update timing by periodical routine |
y_notsu | 0:808f7981f647 | 67 | * |
y_notsu | 0:808f7981f647 | 68 | * @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 | 69 | */ |
y_notsu | 0:808f7981f647 | 70 | |
y_notsu | 0:808f7981f647 | 71 | void set_bits( U16 v[4] ); |
y_notsu | 0:808f7981f647 | 72 | |
y_notsu | 0:808f7981f647 | 73 | /** Set bits into cube |
y_notsu | 0:808f7981f647 | 74 | * |
y_notsu | 0:808f7981f647 | 75 | * The 16 bit word will be copied into specified layer of class internal buffer. |
y_notsu | 0:808f7981f647 | 76 | * It will be displayed at next update timing by periodical routine |
y_notsu | 0:808f7981f647 | 77 | * |
y_notsu | 0:808f7981f647 | 78 | * @param v Bit pattern for a layer. Overwrites the buffer contents |
y_notsu | 0:808f7981f647 | 79 | */ |
y_notsu | 0:808f7981f647 | 80 | |
y_notsu | 0:808f7981f647 | 81 | void set_bits( int layer, U16 v ); |
y_notsu | 0:808f7981f647 | 82 | |
y_notsu | 0:808f7981f647 | 83 | /** Set bit into cube |
y_notsu | 0:808f7981f647 | 84 | * |
y_notsu | 0:808f7981f647 | 85 | * Set a specified bit in the buffer. |
y_notsu | 0:808f7981f647 | 86 | * It will be displayed at next update timing by periodical routine |
y_notsu | 0:808f7981f647 | 87 | * |
y_notsu | 0:808f7981f647 | 88 | * @param x Coodinate: x |
y_notsu | 0:808f7981f647 | 89 | * @param y Coodinate: y |
y_notsu | 0:808f7981f647 | 90 | * @param z Coodinate: z |
y_notsu | 0:808f7981f647 | 91 | * @param v Value for the bit (0 or 1) |
y_notsu | 0:808f7981f647 | 92 | */ |
y_notsu | 0:808f7981f647 | 93 | |
y_notsu | 0:808f7981f647 | 94 | void set_bit( int x, int y, int z, U16 v ); |
y_notsu | 0:808f7981f647 | 95 | |
y_notsu | 0:808f7981f647 | 96 | /** Clear |
y_notsu | 0:808f7981f647 | 97 | * |
y_notsu | 0:808f7981f647 | 98 | * Turn-OFF all LEDs |
y_notsu | 0:808f7981f647 | 99 | */ |
y_notsu | 0:808f7981f647 | 100 | |
y_notsu | 0:808f7981f647 | 101 | void clear( void ); |
y_notsu | 0:808f7981f647 | 102 | |
y_notsu | 0:808f7981f647 | 103 | /** All on |
y_notsu | 0:808f7981f647 | 104 | * |
y_notsu | 0:808f7981f647 | 105 | * Turn-ON all LEDs |
y_notsu | 0:808f7981f647 | 106 | */ |
y_notsu | 0:808f7981f647 | 107 | |
y_notsu | 0:808f7981f647 | 108 | void all_on( void ); |
y_notsu | 0:808f7981f647 | 109 | |
y_notsu | 0:808f7981f647 | 110 | /** Set bits into cube |
y_notsu | 0:808f7981f647 | 111 | * |
y_notsu | 0:808f7981f647 | 112 | * The 16 bit word will be copied into all layers of class internal buffer. |
y_notsu | 0:808f7981f647 | 113 | * It will be displayed at next update timing by periodical routine |
y_notsu | 0:808f7981f647 | 114 | * |
y_notsu | 0:808f7981f647 | 115 | * @param v Bit pattern for layers. Overwrites the buffer contents |
y_notsu | 0:808f7981f647 | 116 | */ |
y_notsu | 0:808f7981f647 | 117 | |
y_notsu | 0:808f7981f647 | 118 | void set_all_words( int v ); |
y_notsu | 0:808f7981f647 | 119 | |
y_notsu | 0:808f7981f647 | 120 | //* |
y_notsu | 0:808f7981f647 | 121 | // * Setting for synchronous display update |
y_notsu | 0:808f7981f647 | 122 | // * |
y_notsu | 0:808f7981f647 | 123 | // * If the "synchronous display" option is set (default) the display |
y_notsu | 0:808f7981f647 | 124 | // * (main) buffer will be updated each timing if just before layer 0 update. |
y_notsu | 0:808f7981f647 | 125 | // * This machanism has been made to avoid flicker when main routine update |
y_notsu | 0:808f7981f647 | 126 | // * buffer so frequently. |
y_notsu | 0:808f7981f647 | 127 | // * To implement this mechanism, this class has 2 buffers. |
y_notsu | 0:808f7981f647 | 128 | // * One is called main buffer and another is temporary buffer. |
y_notsu | 0:808f7981f647 | 129 | // * All API calls that makes modifications of the buffer content will affect to the temp buffer. |
y_notsu | 0:808f7981f647 | 130 | // * The display will be done with main buffer contents. So the timing of update can be controled by buffer copying. |
y_notsu | 0:808f7981f647 | 131 | // * If the "synchronous display" option is cleard, the temporary buffer will be used for display (not tested). |
y_notsu | 0:808f7981f647 | 132 | // * |
y_notsu | 0:808f7981f647 | 133 | // * @param v TRUE for set, FALSE for clear |
y_notsu | 0:808f7981f647 | 134 | // */ |
y_notsu | 0:808f7981f647 | 135 | |
y_notsu | 0:808f7981f647 | 136 | void set_synchronous_draw( int v ); |
y_notsu | 0:808f7981f647 | 137 | |
y_notsu | 0:808f7981f647 | 138 | private: |
y_notsu | 0:808f7981f647 | 139 | //DigitalOut sr_data; |
y_notsu | 0:808f7981f647 | 140 | //DigitalOut sr_clk; |
y_notsu | 0:808f7981f647 | 141 | BusOut anode; |
y_notsu | 0:808f7981f647 | 142 | BusOut cathode; |
y_notsu | 0:808f7981f647 | 143 | Ticker t; |
y_notsu | 0:808f7981f647 | 144 | U16 main_buffer[ CUBE_SIZE ]; |
y_notsu | 0:808f7981f647 | 145 | U16 temp_buffer[ CUBE_SIZE ]; |
y_notsu | 0:808f7981f647 | 146 | int syncronous; |
y_notsu | 0:808f7981f647 | 147 | |
y_notsu | 0:808f7981f647 | 148 | //** Displaying |
y_notsu | 0:808f7981f647 | 149 | // * |
y_notsu | 0:808f7981f647 | 150 | // * This function will be called by Ticker which set by this class' constructor. |
y_notsu | 0:808f7981f647 | 151 | // * It displays just one layer by single call. |
y_notsu | 0:808f7981f647 | 152 | // */ |
y_notsu | 0:808f7981f647 | 153 | |
y_notsu | 0:808f7981f647 | 154 | void display( void ); |
y_notsu | 0:808f7981f647 | 155 | |
y_notsu | 0:808f7981f647 | 156 | //** Data for serial register |
y_notsu | 0:808f7981f647 | 157 | //* |
y_notsu | 0:808f7981f647 | 158 | //* Drives the pins to send serial data to serial register on the shield |
y_notsu | 0:808f7981f647 | 159 | //* |
y_notsu | 0:808f7981f647 | 160 | //* @param v Bit pattern for a layer. |
y_notsu | 0:808f7981f647 | 161 | //*/ |
y_notsu | 0:808f7981f647 | 162 | |
y_notsu | 0:808f7981f647 | 163 | void set_serialregister( U16 v ); |
y_notsu | 0:808f7981f647 | 164 | |
y_notsu | 0:808f7981f647 | 165 | //** Array copy function |
y_notsu | 0:808f7981f647 | 166 | //* |
y_notsu | 0:808f7981f647 | 167 | //* Just copies the array. Loop is unrolled because it;s not big array. |
y_notsu | 0:808f7981f647 | 168 | //* |
y_notsu | 0:808f7981f647 | 169 | //* @param src Pointer to source array. |
y_notsu | 0:808f7981f647 | 170 | //* @param trg Pointer to target array. |
y_notsu | 0:808f7981f647 | 171 | //*/ |
y_notsu | 0:808f7981f647 | 172 | |
y_notsu | 0:808f7981f647 | 173 | void buffer_copy( U16 *src, U16 *trg ); |
y_notsu | 0:808f7981f647 | 174 | }; |
y_notsu | 0:808f7981f647 | 175 | |
y_notsu | 0:808f7981f647 | 176 | #endif // _LED_Cube |
y_notsu | 0:808f7981f647 | 177 | |
y_notsu | 0:808f7981f647 | 178 | /* test code */ |
y_notsu | 0:808f7981f647 | 179 | /* |
y_notsu | 0:808f7981f647 | 180 | #include "mbed.h" |
y_notsu | 0:808f7981f647 | 181 | #include "myLED.h" |
y_notsu | 0:808f7981f647 | 182 | |
y_notsu | 0:808f7981f647 | 183 | LED_Cube444 cube; |
y_notsu | 0:808f7981f647 | 184 | |
y_notsu | 0:808f7981f647 | 185 | int main() { |
y_notsu | 0:808f7981f647 | 186 | cube.all_on(); |
y_notsu | 0:808f7981f647 | 187 | wait(0.5); |
y_notsu | 0:808f7981f647 | 188 | cube.clear(); |
y_notsu | 0:808f7981f647 | 189 | wait(0.5); |
y_notsu | 0:808f7981f647 | 190 | cube.set_bits( 0, 0xA5A5 ); |
y_notsu | 0:808f7981f647 | 191 | cube.set_bits( 1, 0x5A5A ); |
y_notsu | 0:808f7981f647 | 192 | cube.set_bits( 2, 0xA5A5 ); |
y_notsu | 0:808f7981f647 | 193 | cube.set_bits( 3, 0x5A5A ); |
y_notsu | 0:808f7981f647 | 194 | wait( 0.5 ); |
y_notsu | 0:808f7981f647 | 195 | |
y_notsu | 0:808f7981f647 | 196 | cube.set_bits( 0, 0x5A5A ); |
y_notsu | 0:808f7981f647 | 197 | cube.set_bits( 1, 0xA5A5 ); |
y_notsu | 0:808f7981f647 | 198 | cube.set_bits( 2, 0x5A5A ); |
y_notsu | 0:808f7981f647 | 199 | cube.set_bits( 3, 0xA5A5 ); |
y_notsu | 0:808f7981f647 | 200 | wait( 0.5 ); |
y_notsu | 0:808f7981f647 | 201 | |
y_notsu | 0:808f7981f647 | 202 | cube.set_bits( 0, 0xA5A5 ); |
y_notsu | 0:808f7981f647 | 203 | cube.set_bits( 1, 0x5A5A ); |
y_notsu | 0:808f7981f647 | 204 | cube.set_bits( 2, 0xA5A5 ); |
y_notsu | 0:808f7981f647 | 205 | cube.set_bits( 3, 0x5A5A ); |
y_notsu | 0:808f7981f647 | 206 | wait( 0.5 ); |
y_notsu | 0:808f7981f647 | 207 | |
y_notsu | 0:808f7981f647 | 208 | cube.set_bits( 0, 0x5A5A ); |
y_notsu | 0:808f7981f647 | 209 | cube.set_bits( 1, 0xA5A5 ); |
y_notsu | 0:808f7981f647 | 210 | cube.set_bits( 2, 0x5A5A ); |
y_notsu | 0:808f7981f647 | 211 | cube.set_bits( 3, 0xA5A5 ); |
y_notsu | 0:808f7981f647 | 212 | wait( 0.5 ); |
y_notsu | 0:808f7981f647 | 213 | |
y_notsu | 0:808f7981f647 | 214 | cube.clear(); |
y_notsu | 0:808f7981f647 | 215 | |
y_notsu | 0:808f7981f647 | 216 | int v = 1; |
y_notsu | 0:808f7981f647 | 217 | |
y_notsu | 0:808f7981f647 | 218 | while ( 1 ) { |
y_notsu | 0:808f7981f647 | 219 | |
y_notsu | 0:808f7981f647 | 220 | for ( int x = 0; x < 4; x++ ) { |
y_notsu | 0:808f7981f647 | 221 | for ( int y = 0; y < 4; y++ ) { |
y_notsu | 0:808f7981f647 | 222 | for ( int z = 0; z < 4; z++ ) { |
y_notsu | 0:808f7981f647 | 223 | cube.set_bit( x, y, z, v ); |
y_notsu | 0:808f7981f647 | 224 | wait( 0.02); |
y_notsu | 0:808f7981f647 | 225 | } |
y_notsu | 0:808f7981f647 | 226 | } |
y_notsu | 0:808f7981f647 | 227 | } |
y_notsu | 0:808f7981f647 | 228 | |
y_notsu | 0:808f7981f647 | 229 | v = !v; |
y_notsu | 0:808f7981f647 | 230 | } |
y_notsu | 0:808f7981f647 | 231 | } |
y_notsu | 0:808f7981f647 | 232 | */ |