Nick Lowyck / Mbed 2 deprecated Ledcube

Dependencies:   mbed mbed-rtos

Files at this revision

API Documentation at this revision

Comitter:
sNICKer1103
Date:
Thu May 08 18:59:52 2014 +0000
Parent:
4:a091b8f8216d
Commit message:
ter

Changed in this revision

Cube/LedCube.cpp Show annotated file Show diff for this revision Revisions of this file
Cube/LedCube.h Show annotated file Show diff for this revision Revisions of this file
I2CDriver/I2CDriver.cpp Show annotated file Show diff for this revision Revisions of this file
SPIDriver/SPIDriver.cpp Show annotated file Show diff for this revision Revisions of this file
SPIDriver/SPIDriver.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Cube/LedCube.cpp	Sun May 04 20:41:10 2014 +0000
+++ b/Cube/LedCube.cpp	Thu May 08 18:59:52 2014 +0000
@@ -3,81 +3,64 @@
 LedCube::LedCube(){
     //construct needed arrays to store the output values
     status = address;
+    x_current=0;
+    y_current=0;
+    z_current=0;
     
     layer_current=0; 
-    rank_current=0;
-    layer_oneHot=1;
-    
-    clearValues();
-}
-
-LedCube::~LedCube(){
+    layer_oneHot= 1;
+    rank_current = 0;
 }
-
-void LedCube::clearValues(){
-    for(int i = 0; i < 8; i++){
-        for(int j = 0; j < 8; j++){
-            rValues[i][j] = 0;
-            gValues[i][j] = 0;
-            bValues[i][j] = 0;    
-        }
-    }
-}
-
 //return the next byte of data to send
-char LedCube::getNextValue(){
-
-    char bytevalue;
+unsigned char LedCube::getNextValue(){
+    unsigned char output;
     switch(status){
         case address  :
-           bytevalue = layer_oneHot;
-           status=red;  // next time give red color 
+           status=red;
+           output = layer_oneHot;
            break;
         case red  :
-           bytevalue = rValues[layer_current][rank_current];//return red value
+           output = rValues[layer_current][rank_current];//return red value
            if(rank_current!=7)rank_current++;
            else {
-                status = green; // next time give green color 
+                status = green; // next time give red color 
                 rank_current =0; //put the rank back on zero
            }
            break;      
         case green  :
-           bytevalue = gValues[layer_current][rank_current];//return green value
+           output = gValues[layer_current][rank_current];//return green value
            if(rank_current!=7)rank_current++;
            else {
-                status = blue; // next time give blue color 
+                status = blue; // next time give green 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++;
+           output = bValues[layer_current][rank_current];//return blue value
+           if (rank_current != 7){ 
+               rank_current++;  //increase the rank            
+           }
            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;
+                if(layer_oneHot == 0) layer_oneHot = 1; //get the new address
+                status = address; // next time give blue color 
+                rank_current =0; //put the rank back on zero for the next layer
+                if(layer_current!=7) layer_current++; else layer_current = 0; //if its not the last layer increase it
            }
            break;      
     }
-    return bytevalue;
+    return output;
 }
 //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)
+void LedCube::setData(unsigned char* data){
+    //convert raw data too rgb values 
+    for(int i = 0; i < 8; i++) { 
             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 
+                        int y = j * 8; //a collum is 8 rows
+                        unsigned char weight = 128;
+                        for(int k = 0; k < 8; k++) {  //iterate for each row
+                            unsigned 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;
@@ -99,58 +82,36 @@
                         }
              }
      }
-     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;
+void LedCube::printValues(Color_led color){
+    unsigned char(*toprint)[8][8];
+    switch (color)
+    {
+    case l_red:
+        toprint = &rValues;
         break;
-        case LedCube::secondhalfwhite : 
-            for(int i = 0; i < 256; i++) leddata[i] = 0;
-            for(int i = 256; i < 512; i++) leddata[i] = 7;
+    case l_green:
+        toprint = &gValues;
+        break;
+    case l_blue:
+        toprint = &bValues;
+        break;
+    default:
+        toprint = &rValues; //set the default to red should not happen anyway
         break;
     }
-    setData(leddata);
-}
-
-void LedCube::printrValues(){
-    for(int i = 0; i < 8; i ++){
-        for(int j = 0; j < 8; j++){
-            printf("%03d ", rValues[i][j]);
-        }   
-        printf("\n\r"); 
+    for (int i = 0; i < 8; i++){
+        for (int j = 0; j < 8; j++){
+            printf("%03d ", (*toprint)[i][j]);
+        }
+        printf("\n\r");
     }
 }
-
-void LedCube::printgValues(){
-    for(int i = 0; i < 8; i ++){
-        for(int j = 0; j < 8; j++){
-            printf("%03d ", gValues[i][j]);
-        }   
-        printf("\n\r"); 
-    }
-}
-
-void LedCube::printbValues(){
-    for(int i = 0; i < 8; i ++){
-        for(int j = 0; j < 8; j++){
-            printf("%03d ", bValues[i][j]);
-        }   
-        printf("\n\r"); 
-    }
-}
-
 void LedCube::printAll(){
     printf("Red Matrix: \n\r");
-    printrValues();
+    printValues(l_red);
     printf("\n\rGreen Matrix: \n\r");
-    printgValues();
+    printValues(l_green);
     printf("\n\rBlue Matrix: \n\r");
-    printbValues();
-}
+    printValues(l_blue);
+}
\ No newline at end of file
--- a/Cube/LedCube.h	Sun May 04 20:41:10 2014 +0000
+++ b/Cube/LedCube.h	Thu May 08 18:59:52 2014 +0000
@@ -1,35 +1,30 @@
-#ifndef LedCube_h
-#define LedCube_h
-
+#include "iostream"
 #include "mbed.h"
-#include "iostream"
-
-class LedCube
-{
-private:
-
-    char rValues[8][8] ; //matrix (8*8*8): top to bottom, eerst r dan g dan b dan laag (per laag, dus 33 chars)(één char:rij voor één kleur)
-    char gValues[8][8] ;
-    char bValues[8][8] ;
+#ifndef LedCube_h
+#define LedCube_h 
+enum Color_led { l_red, l_green, l_blue };
+class LedCube{
+    private:
+        unsigned char rValues[8][8] ;
+        unsigned char gValues[8][8];
+        unsigned char bValues[8][8];
 
-    enum {address,red,green,blue} status;
-    int layer_current;
-    int rank_current;
-
-    char layer_oneHot;
+        //indexes
+        int x_current;
+        int y_current;
+        int z_current;
+        
+        enum{address,red,green,blue} status;
+        int layer_current;
+        int rank_current;
 
-public:
-    enum TestConfig {firsthalfwhite, secondhalfwhite};
-    LedCube();
-    ~LedCube();
-    void clearValues();
-    char getNextValue();
-    void setData(char* data);
-    void testMode(TestConfig testconfig);
-    void printrValues();
-    void printgValues();
-    void printbValues();
-    void printAll();
+        unsigned char layer_oneHot;
+
+    public:
+        LedCube();
+        unsigned char getNextValue();
+        void setData(unsigned char* data);
+        void printValues(Color_led color);
+        void printAll();
 };
 #endif
-
--- a/I2CDriver/I2CDriver.cpp	Sun May 04 20:41:10 2014 +0000
+++ b/I2CDriver/I2CDriver.cpp	Thu May 08 18:59:52 2014 +0000
@@ -29,7 +29,9 @@
 {
     if(receive() == I2CDriver::WriteAddressed) {
         slave.read(databuffer, SIZE);
-        ledc->setData(databuffer);
+        unsigned char databfr[512];
+        for(int i =0; i < 512; i++) databfr[i] = databuffer[i];
+        ledc->setData(databfr);
     }
     //printf("Gepollt!!\n\r");
 }
--- a/SPIDriver/SPIDriver.cpp	Sun May 04 20:41:10 2014 +0000
+++ b/SPIDriver/SPIDriver.cpp	Thu May 08 18:59:52 2014 +0000
@@ -30,28 +30,42 @@
 
 void SPIDriver::pulseLatch(){
     *latch = 1;
+    wait(0.000001);
     *latch = 0;    
 }
 
 void SPIDriver::sendLayer(LedCube* ledc){
-        write(ledc->getNextValue());      //eerst laag selecteren, SPI zendt normaalgezien MSB first 
+        unsigned char valueled = ledc->getNextValue();
+    //    printf("%d ", valueled);
+        write(valueled);      //eerst laag selecteren, SPI zendt normaalgezien MSB first 
         for(int j = 0; j < 24; j++) {      
-            write(ledc->getNextValue());  //ledwaarden zelf nog altijd apart, omdat ze mogelijk gespiegeld moeten doorgestuurd worden
+            unsigned char valueled = ledc->getNextValue();
+     //       printf("%d ", valueled);
+            write(valueled);  //ledwaarden zelf nog altijd apart, omdat ze mogelijk gespiegeld moeten doorgestuurd worden
         }                                  //(LSB first)                              
 }
 
 void SPIDriver::sendFrame(LedCube* ledc){
-    for(int i = 0; i < 8; i++) {                                            
-        wait(0.00125);    //voor 100 Hz
+    for(int i = 0; i < 8; i++) {  
+        sendLayer(ledc);                                          
+        wait(0.00025);     //voor 100 Hz
         pulseLatch();
     }//end outer for loop
    // printf("Frame gezonden!!\n\r");
 }
 
+void SPIDriver::sendTestFrame(LedCube* ledc){
+    for(int i = 0; i < 8; i++) {  
+        sendLayer(ledc);                                          
+        wait(0.00125);    
+        pulseLatch();
+    }
+}
+
 void SPIDriver::sendTest(LedCube* ledc, int frames){
     printf("Testing...\n\r");
     for(int i = 0; i < frames; i++) {
-        sendFrame(ledc);
+        sendTestFrame(ledc);
     }
 }
 
@@ -59,11 +73,11 @@
     printf("Initiating Test phase\n\r");
     
     LedCube* testledcube1 = new LedCube();
-    testledcube1->testMode(LedCube::firsthalfwhite);
+  //  testledcube1->testMode(LedCube::firsthalfwhite);
     sendTest(testledcube1, xframes);
     
     LedCube* testledcube2 = new LedCube();
-    testledcube2->testMode(LedCube::secondhalfwhite);
+  //  testledcube2->testMode(LedCube::secondhalfwhite);
     sendTest(testledcube2, xframes);
     
     printf("Test phase terminated!\n\r");    
--- a/SPIDriver/SPIDriver.h	Sun May 04 20:41:10 2014 +0000
+++ b/SPIDriver/SPIDriver.h	Thu May 08 18:59:52 2014 +0000
@@ -13,6 +13,7 @@
         void pulseLatch();
         void sendLayer(LedCube* ledc);
         void sendFrame(LedCube* ledc);
+        void sendTestFrame(LedCube* ledc);
         void sendTest(LedCube* ledc, int frames);
         void testPhase(int xframes);
         uint8_t reverse_byte(uint8_t byte);
--- a/main.cpp	Sun May 04 20:41:10 2014 +0000
+++ b/main.cpp	Thu May 08 18:59:52 2014 +0000
@@ -5,41 +5,75 @@
 #include "rtos.h"
 
 const int FRAMES = 700;
-const int SPIFREQ = 10000000; //frequency of SPI interface
-const char ADDRESS = 0xA0; //address van I2C interface
+const int SPIFREQ = 1000000; //frequency of SPI interface
+const char ADDRESS = 0x60; //address van I2C interface
+unsigned char buffer[512];
+Mutex valuesmutex;
 
 LedCube* ledcube = new LedCube();
-SPIDriver* spidriver = new SPIDriver(p5, NC, p7, p8, p10, SPIFREQ);
+SPIDriver* spidriver = new SPIDriver(p5, NC, p7, p8, p11, SPIFREQ);
 I2CDriver* i2cdriver = new I2CDriver(p28, p27, ADDRESS);
-//DigitalOut blank(p9);
+DigitalOut blank(p9);
 
 void streamThread(void const* args);
 void inputThread(void const* args);
 
 int main()
 {
-    spidriver->testPhase(FRAMES);
+    /* while(true){
+         spidriver->sendLayer(ledcube);
+         wait(0.5);
+     }  */
+    /*  blank = 1;
+      wait_ms(1);*/
+    blank = 0;
+
+    for(int i = 0; i < 512; i++) buffer[i] = 1;
+
+    ledcube->setData(buffer);
+
+    /* while(1){}*/
 
-    printf("Initiating stream from following matrices\n\r");
-    ledcube->printAll();
-    printf("Streaming...\n\r");
+    /* while(true) {
+          char ledvalue = ledcube->getNextValue();
+          printf("%d ", ledvalue);
+          spidriver->write(ledvalue);
+          wait(0.2);
+      }*/
+
+    //  spidriver->testPhase(FRAMES);
 
+    /*  printf("Initiating stream from following matrices\n\r");
+      ledcube->printAll();
+      printf("Streaming...\n\r");*/
+    /*   spidriver->sendLayer(ledcube);
+       wait(0.00125);    //voor 100 Hz
+       spidriver->pulseLatch();*/
+     
+   /* while(true) {
+        spidriver->sendFrame(ledcube);
+    }*/
+    
     Thread thread1(streamThread);
     Thread thread2(inputThread);
+    while(true){}
     
-    while(true){}
 }//end main
 
 void streamThread(void const* args)
 {
     while(true) {
+        valuesmutex.lock();
         spidriver->sendFrame(ledcube);
+        valuesmutex.unlock();
     }
 }
 
 void inputThread(void const* args)
 {
     while(true) {
+        valuesmutex.lock();
         i2cdriver->pollForInput(ledcube);
+        valuesmutex.unlock();
     }
 }
\ No newline at end of file