8x8x8 Led Cube driven by 4x PCF8575 for the columns and 1x PCF8574 for the layers

Committer:
Bas
Date:
Sat Sep 08 22:14:10 2012 +0000
Revision:
0:920c5ed65a45
Child:
1:e08a4d27f534
updated the addressing of the columns. they are connected for best wiring, not in sequence of the PCF8575's

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bas 0:920c5ed65a45 1 #include "led_cube.h"
Bas 0:920c5ed65a45 2
Bas 0:920c5ed65a45 3 LED_CUBE::LED_CUBE(I2C* _interface)
Bas 0:920c5ed65a45 4 : row(_interface, 0x07,PCF8574_TYPE),
Bas 0:920c5ed65a45 5 colums_0_1(_interface, 0x00),
Bas 0:920c5ed65a45 6 colums_2_3(_interface, 0x01),
Bas 0:920c5ed65a45 7 colums_4_5(_interface, 0x02),
Bas 0:920c5ed65a45 8 colums_6_7(_interface, 0x03)
Bas 0:920c5ed65a45 9 {
Bas 0:920c5ed65a45 10 _interface->frequency(400000); //400KHz
Bas 0:920c5ed65a45 11 duty.attach_us(this, &LED_CUBE::UpdateRow,1250);// each row = 1/8 duty
Bas 0:920c5ed65a45 12 }
Bas 0:920c5ed65a45 13
Bas 0:920c5ed65a45 14 LED_CUBE::~LED_CUBE() {}
Bas 0:920c5ed65a45 15
Bas 0:920c5ed65a45 16 void LED_CUBE::UpdateRow()
Bas 0:920c5ed65a45 17 {
Bas 0:920c5ed65a45 18 static int rownr = 0;
Bas 0:920c5ed65a45 19
Bas 0:920c5ed65a45 20 if (rownr < 0 || rownr > 7) {
Bas 0:920c5ed65a45 21 rownr = 0;
Bas 0:920c5ed65a45 22 }
Bas 0:920c5ed65a45 23 row.WriteByte(0xFF); // switch off all rows before updating next colums to avoid ghost data from previous colums
Bas 0:920c5ed65a45 24 colums_0_1.WriteBytes(~cubeData[8 * rownr + 0],~cubeData[8 * rownr + 2]); //update the columns
Bas 0:920c5ed65a45 25 colums_2_3.WriteBytes(~cubeData[8 * rownr + 1],~cubeData[8 * rownr + 3]);
Bas 0:920c5ed65a45 26 colums_4_5.WriteBytes(~cubeData[8 * rownr + 6],~cubeData[8 * rownr + 4]);
Bas 0:920c5ed65a45 27 colums_6_7.WriteBytes(~cubeData[8 * rownr + 7],~cubeData[8 * rownr + 5]);
Bas 0:920c5ed65a45 28 row.WriteByte(~(1<<rownr)); // now display the row
Bas 0:920c5ed65a45 29 rownr++;
Bas 0:920c5ed65a45 30 }
Bas 0:920c5ed65a45 31
Bas 0:920c5ed65a45 32 //clear everything
Bas 0:920c5ed65a45 33 void LED_CUBE::clear_cube(void){
Bas 0:920c5ed65a45 34 row.WriteByte(0xFF);
Bas 0:920c5ed65a45 35 colums_0_1.WriteBytes(0xFF,0xFF);
Bas 0:920c5ed65a45 36 colums_2_3.WriteBytes(0xFF,0xFF);
Bas 0:920c5ed65a45 37 colums_4_5.WriteBytes(0xFF,0xFF);
Bas 0:920c5ed65a45 38 colums_6_7.WriteBytes(0xFF,0xFF);
Bas 0:920c5ed65a45 39 clearVoxels();
Bas 0:920c5ed65a45 40 }
Bas 0:920c5ed65a45 41
Bas 0:920c5ed65a45 42 //set a voxel in my space saver cube array
Bas 0:920c5ed65a45 43 void LED_CUBE::setVoxel(unsigned char x, unsigned char y, unsigned char z){
Bas 0:920c5ed65a45 44 cubeData[z*8 + y] |= 0x01<<x;
Bas 0:920c5ed65a45 45 }
Bas 0:920c5ed65a45 46
Bas 0:920c5ed65a45 47 //clear a single voxel in my space saver cube array
Bas 0:920c5ed65a45 48 void LED_CUBE::clearVoxel(unsigned char x, unsigned char y, unsigned char z){
Bas 0:920c5ed65a45 49 cubeData[z*8 + y] &= ~(0x01<<x);
Bas 0:920c5ed65a45 50 }
Bas 0:920c5ed65a45 51
Bas 0:920c5ed65a45 52 //return the value of the voxel in my cube array
Bas 0:920c5ed65a45 53 unsigned char LED_CUBE::getVoxel(unsigned char x, unsigned char y, unsigned char z){
Bas 0:920c5ed65a45 54 if(cubeData[z*8 + y] & 0x01<<x){
Bas 0:920c5ed65a45 55 return 1;
Bas 0:920c5ed65a45 56 }else{
Bas 0:920c5ed65a45 57 return 0;
Bas 0:920c5ed65a45 58 }
Bas 0:920c5ed65a45 59 }
Bas 0:920c5ed65a45 60
Bas 0:920c5ed65a45 61 void LED_CUBE::copyColumn(unsigned char x, unsigned char y, unsigned char col){
Bas 0:920c5ed65a45 62 for(unsigned char z = 0; z < Zd; z++){
Bas 0:920c5ed65a45 63 if(col&(0x01<<z)){
Bas 0:920c5ed65a45 64 setVoxel(x, y, z);
Bas 0:920c5ed65a45 65 }else{
Bas 0:920c5ed65a45 66 clearVoxel(x, y, z);
Bas 0:920c5ed65a45 67 }
Bas 0:920c5ed65a45 68 }
Bas 0:920c5ed65a45 69 }
Bas 0:920c5ed65a45 70 //clear all the voxels in the array
Bas 0:920c5ed65a45 71 void LED_CUBE::clearVoxels(void){
Bas 0:920c5ed65a45 72 memset(cubeData, 0, 64*sizeof(unsigned char));
Bas 0:920c5ed65a45 73 }