Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Cube/LedCube.cpp
- Committer:
- sNICKer1103
- Date:
- 2014-05-02
- Revision:
- 3:da30c350c339
- Parent:
- 2:f2700008c9d9
- Child:
- 4:a091b8f8216d
File content as of revision 3:da30c350c339:
#include "LedCube.h"
LedCube::LedCube(){
//construct needed arrays to store the output values
status = address;
layer_current=0;
rank_current=0;
layer_oneHot=1;
}
LedCube::~LedCube(){
}
//return the next byte of data to send
char LedCube::getNextValue(){
char bytevalue;
switch(status){
case address :
bytevalue = layer_oneHot;
status=red; // next time give red color
break;
case red :
bytevalue = rValues[layer_current][rank_current];//return red value
if(rank_current!=7)rank_current++;
else {
status = green; // next time give green color
rank_current =0; //put the rank back on zero
}
break;
case green :
bytevalue = gValues[layer_current][rank_current];//return green value
if(rank_current!=7)rank_current++;
else {
status = blue; // next time give blue color
rank_current =0; //put the rank back on zero
}
break;
case blue :
bytevalue = bValues[layer_current][rank_current];//return blue value
if(rank_current!=7)rank_current++;
else {
status = address; // next time give address color
rank_current =0; //put the rank back on zero
layer_oneHot = layer_oneHot << 1;
if(layer_current!=7)layer_current++;
else layer_current=0;
if(layer_oneHot == 0) layer_oneHot = 1;
}
break;
}
return bytevalue;
}
//setData set the arrays to the new frame
void LedCube::setData(char* data){
printf("Converting following data: \n\r");
for(int i = 0; i < 512; i++) printf("%d ", data[i]);
printf("\n\r\n\r");
//convert raw data to rgb values
for(int i = 0; i < 8; i++) { //char array (afkomstig van raspberry pi) omzetten naar rgb matrix (voor ledcube)
int z = i * 64; // a layer is 64 byte
for(int j = 0; j < 8; j++) {
int y = j * 8; //a row is 8 elements
char weight = 128;
for(int k = 0; k < 8; k++) { //iterate for each element
char digitbuffer = data[z+y+k]; //read byte consisting of color 1 led
//now select the right color
if(k == 0) {
rValues[i][j] = 0;
gValues[i][j] = 0;
bValues[i][j] = 0;
}
if(digitbuffer >= 4) {
rValues[i][j] += weight;
digitbuffer = digitbuffer - 4;
}
if(digitbuffer >= 2) {
gValues[i][j] += weight;
digitbuffer = digitbuffer - 2;
}
if(digitbuffer >= 1) {
bValues[i][j] += weight;
}
weight = weight >> 1;
}
}
}
printf("Result: \n\r");
printAll();
printf("Conversion terminated\n\r");
}
void LedCube::testMode(TestConfig testconfig){
char leddata[512];
switch(testconfig){
case LedCube::firsthalfwhite :
for(int i = 0; i < 256; i++) leddata[i] = 7;
for(int i = 256; i < 512; i++) leddata[i] = 0;
break;
case LedCube::secondhalfwhite :
for(int i = 0; i < 256; i++) leddata[i] = 0;
for(int i = 256; i < 512; i++) leddata[i] = 7;
break;
}
setData(leddata);
}
void LedCube::printrValues(){
for(int i = 0; i < 8; i ++){
for(int j = 0; j < 8; j++){
printf("%d ", rValues[i][j]);
}
printf("\n\r");
}
}
void LedCube::printgValues(){
for(int i = 0; i < 8; i ++){
for(int j = 0; j < 8; j++){
printf("%d ", gValues[i][j]);
}
printf("\n\r");
}
}
void LedCube::printbValues(){
for(int i = 0; i < 8; i ++){
for(int j = 0; j < 8; j++){
printf("%d ", bValues[i][j]);
}
printf("\n\r");
}
}
void LedCube::printAll(){
printf("Red Matrix: \n\r");
printrValues();
printf("\n\rGreen Matrix: \n\r");
printgValues();
printf("\n\rBlue Matrix: \n\r");
printbValues();
}