This is a driver for a project I made in school. It involves a Raspberry Pi connected to the MBED, which in turn is connected to a Ledcube. The MBED delivers data for the leds to the Ledcube continuously at a 100 Hz rate, using SPI. This data is read from an internal buffer. For animations, the Raspberry Pi just sends data when the current "image" has to change, using I2C.
LedCube.cpp
00001 #include "LedCube.h" 00002 00003 LedCube::LedCube(){ 00004 //construct needed arrays to store the output values 00005 status = address; 00006 x_current=0; 00007 y_current=0; 00008 z_current=0; 00009 00010 layer_current=0; 00011 layer_oneHot= 1; 00012 rank_current = 0; 00013 } 00014 //return the next byte of data to send 00015 unsigned char LedCube::getNextValue(){ 00016 unsigned char output; 00017 switch(status){ 00018 case address : 00019 status=red; 00020 output = layer_oneHot; 00021 break; 00022 case red : 00023 output = rValues[layer_current][rank_current];//return red value 00024 if(rank_current!=7)rank_current++; 00025 else { 00026 status = green; // next time give red color 00027 rank_current =0; //put the rank back on zero 00028 } 00029 break; 00030 case green : 00031 output = gValues[layer_current][rank_current];//return green value 00032 if(rank_current!=7)rank_current++; 00033 else { 00034 status = blue; // next time give green color 00035 rank_current =0; //put the rank back on zero 00036 } 00037 break; 00038 case blue : 00039 output = bValues[layer_current][rank_current];//return blue value 00040 if (rank_current != 7){ 00041 rank_current++; //increase the rank 00042 } 00043 else { 00044 layer_oneHot = layer_oneHot << 1; 00045 if(layer_oneHot == 0) layer_oneHot = 1; //get the new address 00046 status = address; // next time give blue color 00047 rank_current =0; //put the rank back on zero for the next layer 00048 if(layer_current!=7) layer_current++; else layer_current = 0; //if its not the last layer increase it 00049 } 00050 break; 00051 } 00052 return output; 00053 } 00054 //setData set the arrays to the new frame 00055 void LedCube::setData(unsigned char* data){ 00056 //convert raw data too rgb values 00057 for(int i = 0; i < 8; i++) { 00058 int z = i * 64; // a layer is 64 byte 00059 for(int j = 0; j < 8; j++) { 00060 int y = j * 8; //a collum is 8 rows 00061 unsigned char weight = 128; 00062 for(int k = 0; k < 8; k++) { //iterate for each row 00063 unsigned char digitbuffer = data[z+y+k]; //read byte consisting of color 1 led 00064 //now select the right color 00065 if(k == 0) { 00066 rValues[i][j] = 0; 00067 gValues[i][j] = 0; 00068 bValues[i][j] = 0; 00069 } 00070 if(digitbuffer >= 4) { 00071 rValues[i][j] += weight; 00072 digitbuffer = digitbuffer - 4; 00073 } 00074 if(digitbuffer >= 2) { 00075 gValues[i][j] += weight; 00076 digitbuffer = digitbuffer - 2; 00077 } 00078 if(digitbuffer >= 1) { 00079 bValues[i][j] += weight; 00080 } 00081 weight = weight >> 1; 00082 } 00083 } 00084 } 00085 } 00086 void LedCube::printValues(Color_led color){ 00087 unsigned char(*toprint)[8][8]; 00088 switch (color) 00089 { 00090 case l_red: 00091 toprint = &rValues; 00092 break; 00093 case l_green: 00094 toprint = &gValues; 00095 break; 00096 case l_blue: 00097 toprint = &bValues; 00098 break; 00099 default: 00100 toprint = &rValues; //set the default to red should not happen anyway 00101 break; 00102 } 00103 for (int i = 0; i < 8; i++){ 00104 for (int j = 0; j < 8; j++){ 00105 printf("%03d ", (*toprint)[i][j]); 00106 } 00107 printf("\n\r"); 00108 } 00109 } 00110 void LedCube::printAll(){ 00111 printf("Red Matrix: \n\r"); 00112 printValues(l_red); 00113 printf("\n\rGreen Matrix: \n\r"); 00114 printValues(l_green); 00115 printf("\n\rBlue Matrix: \n\r"); 00116 printValues(l_blue); 00117 }
Generated on Thu Aug 25 2022 12:14:09 by
1.7.2