A few classes to interface one or more ShiftBrite module to the FRDM KL25Z.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
JoKer
Date:
Wed Aug 20 05:24:38 2014 +0000
Parent:
1:4a62ae180af0
Child:
3:9376bf1f1bbd
Commit message:
Add movie class

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
movie.cpp Show annotated file Show diff for this revision Revisions of this file
movie.h Show annotated file Show diff for this revision Revisions of this file
sbDriver.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Aug 19 07:16:17 2014 +0000
+++ b/main.cpp	Wed Aug 20 05:24:38 2014 +0000
@@ -1,11 +1,28 @@
 #include "mbed.h"
 #include "sbDriver.h"
+#include "movie.h"
 /*MUST TRY THIS!!!!
 Inheritance is one of the great features of object oriented languages like C++. The stream methods are not included in serial to allow you to use them in other classes as well. They are for example also included in the TextLCD class and in my enhanced TextLCD class. All you need to do to make use of the powerful printf features is implement a putc() method in your own new class.
 from https://mbed.org/questions/1029/pcprintf-a-method/
 
 */
 
+//6 leds example. Format it suitably for easy reading
+unsigned short int aMovie[] = {
+    /* LED1      LED2      LED3      LED4      LED5      LED6 */
+    1023,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0, /*Frame 0*/
+       0,0,0, 1023,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0, /*Frame 1*/
+       0,0,0,    0,0,0,    1023,0,0, 0,0,0,    0,0,0,    0,0,0, /*Frame 2*/
+       0,0,0,    0,0,0,    0,0,0,    1023,0,0, 0,0,0,    0,0,0, /*Frame 3*/
+       0,0,0,    0,0,0,    0,0,0,    0,0,0,    1023,0,0, 0,0,0, /*Frame 4*/
+       0,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0,    1023,0,0, /*Frame 5*/
+       0,0,0,    0,0,0,    0,0,0,    0,0,0,    1023,0,0, 0,0,0, /*Frame 6*/
+       0,0,0,    0,0,0,    0,0,0,    1023,0,0, 0,0,0,    0,0,0, /*Frame 7*/
+       0,0,0,    0,0,0,    1023,0,0, 0,0,0,    0,0,0,    0,0,0, /*Frame 8*/
+       0,0,0, 1023,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0  /*Frame 9*/
+   /*A simple 'cylon' scanner 'movie'*/    
+};
+    
 Serial PC(PTA2, PTA1);//So I can use Bluetooth/Serial as output to terminal and input
 
 
@@ -31,20 +48,17 @@
 //sbDisplay.setLed(0,0X550000);
 //sbDisplay.setLed(4,0XFF,0X00,0X55);
 //in this case, 6 of these statements wold be required to build one frame
-//BUILD A FRAME
-/*sbDisplay.setLed(0,0XFF0000);//Red
+
+
+//HOW TO BUILD A FRAME IF YOU ARE NOT USING THE MOVIE CLASS
+sbDisplay.setLed(0,0XFF0000);//Red
 sbDisplay.setLed(1,0X00FF00);//Green
 sbDisplay.setLed(2,0X0000FF);//Blue
 sbDisplay.setLed(3,0XFFFF00);//Yellow
 sbDisplay.setLed(4,0X00FFFF); //Cyan?
-sbDisplay.setLed(5,0XFF00FF); // Purple*/
+sbDisplay.setLed(5,0XFF00FF); // Purple
 
-sbDisplay.setLed(0,0XFFFFFF);//White
-sbDisplay.setLed(1,0XFFFFFF);//
-sbDisplay.setLed(2,0XFFFFFF);//
-sbDisplay.setLed(3,0XFFFFFF);//
-sbDisplay.setLed(4,0XFFFFFF); //
-sbDisplay.setLed(5,0XFFFFFF); //
+//HOW TO ADJUST THE CURRENT CONTROLLERS USED FOR DOT CORRECTION
 sbDisplay.setCurrentCorr(0,0,0);//suggested default value cor current control regs. Values g&b=100, r=120
 sbDisplay.displayFrame();//force an update
 wait(2);
@@ -56,38 +70,30 @@
 PC.printf("Hallo (size=%d)\r\n", sizeof(unsigned long int));
 
 
-//Setup the framerate and update the display
-//I chose standard 24fps
-t.attach_us(&sbDisplay,&shiftBriteDisplay::displayFrame,41666);//call updateFrame 24 times per second (every 41666uS)
-//t.attach(&sbDisplay,&shiftBriteDisplay::displayFrame,0.5);//Only every 0.5s for testing
+         
+ 
+    sbDisplay.setLed(0,0X0F0000);//Red
+    sbDisplay.setLed(1,0X000F00);//Green
+    sbDisplay.setLed(2,0X00000F);//Blue
+    sbDisplay.setLed(3,0X0F0F00);//Yellow
+    sbDisplay.setLed(4,0X000F0F); //Cyan?
+    sbDisplay.setLed(5,0X0F000F); // Purple
+    sbDisplay.displayFrame(); //get it on the LEDS
+    wait(0.5);
+    sbDisplay.setCurrentCorr(0,0,0);//Dim
+ 
+    sbDisplay.displayFrame(); //get it on the LEDS
+//Alternative to calling displayFrame() yourself, setup the framerate and update the display
+//t.attach_us(&sbDisplay,&shiftBriteDisplay::displayFrame,41666);//call updateFrame 24 times per second (every 41666uS)
+//t.attach(&sbDisplay,&shiftBriteDisplay::displayFrame,0.5);// or only every 0.5s for testing
+   
+    
+//USING THE MOVIE CLASS
+//Note, it uses the previously declared sbDisplay object.
+    movie myMovie(aMovie,sbDisplay,sizeof(aMovie));
+    myMovie.setRepeat(1);
+    t.attach(&myMovie,&movie::play,0.05);//Beware, if you go too fast here the MBED will crash
 
-         
-    while(1){
-/*TEST THE CURRENT ADJUSMENT FUNCTIONALITY
-        sbDisplay.setCurrentCorr(0,0,0);
-        wait(0.5);
-        sbDisplay.setCurrentCorr(30,30,30);
-        wait(0.5);
-         sbDisplay.setCurrentCorr(60,60,60);
-        wait(0.5);
-        sbDisplay.setCurrentCorr(90,90,30);
-        wait(0.5);
-        sbDisplay.setCurrentCorr(120,120,30);
-        wait(0.5);
-*/
-        sbDisplay.setLed(0,0XFF0000);//Red
-        sbDisplay.setLed(1,0X00FF00);//Green
-        sbDisplay.setLed(2,0X0000FF);//Blue
-        sbDisplay.setLed(3,0XFFFF00);//Yellow
-        sbDisplay.setLed(4,0X00FFFF); //Cyan?
-        sbDisplay.setLed(5,0XFF00FF); // Purple
-        wait(0.5);
-        sbDisplay.setLed(5,0XFF0000);//Red
-        sbDisplay.setLed(4,0X00FF00);//Green
-        sbDisplay.setLed(3,0X0000FF);//Blue
-        sbDisplay.setLed(2,0XFFFF00);//Yellow
-        sbDisplay.setLed(1,0X00FFFF); //Cyan?
-        sbDisplay.setLed(0,0XFF00FF); // Purple
-         wait(0.5);
+   while(1){
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/movie.cpp	Wed Aug 20 05:24:38 2014 +0000
@@ -0,0 +1,57 @@
+#include "mbed.h"
+#include "movie.h"
+#include "sbDriver.h"
+
+movie::movie(unsigned short int * movie_p, shiftBriteDisplay &display, unsigned int movieSize):r_display(display){
+    //Constructor - sets up initial condition
+    //r_display = display;  //setup a reference to the actual display
+    this->movie_p = movie_p; // pointer to the data. This data is simply and array of short integers but sequenced as frames
+    //i.e. if the display has 2 leds and the data is {12,13,14,15,16,17,18,19,20,21,22} then the first frame is 12,13,14+15,16,17
+    //corresponding to rgb_led1 and rgb_led2
+    currentFrame = 0;
+    frameSize = r_display.getModuleCount();
+    this->movieSize = movieSize; // in BYTES. Need to (((movieSize/sizeof(unsigned short int))/frameSize)/3) to get actual frames
+}
+
+void movie::play(){
+    /**Advance therough the movie one frame whenever this member method is called
+    Display is referenced with r_display
+    */
+
+    unsigned int dot = 0;
+//    unsigned int dotCol = 0;
+    unsigned short int rTemp, gTemp, bTemp;
+    //moduleCount is how many LEDS, assume 4, called frameSize
+    //currentFrame is how many frames we've loaded
+    //currentDot is which module in the frame, i.e. 0-3 in this example
+    //starts at 0
+    //So, first value is at *(movie_p + (currentFrame*moduleCount*currentDot))
+    // i.e. *(movie_p + 0*4*0) which is *(movie_p + 0), i.e. the very first byte
+    // so, colours are are *(movie_p + 0 + 0,1,2)
+    // Increment current Dot
+    // *(movie_p + (0*4*1) + 0,1,2) = 4+0, 4+1, 4+2)
+    // Iterate through all Dots
+    //then, increment the currentFrame and store for next time
+    //remember to check that we do not exceed the bounds of the data
+    //serial_p->printf("movie pointer l=%p r=%u v=%u",&movie_p,movie_p,*movie_p);
+    for(dot = 0; dot != frameSize; dot++){
+ //       movie_p+=(unsigned short int*)((currentFrame*frameSize*dot)+0);
+       /* rTemp = *(movie_p + ((currentFrame*frameSize*dot)+0));
+        gTemp = *(movie_p + ((currentFrame*frameSize*dot)+1));
+        bTemp = *(movie_p + ((currentFrame*frameSize*dot)+2));  */
+        rTemp = *(movie_p + ((currentFrame*frameSize*3)+(dot*3)+0));
+        gTemp = *(movie_p + ((currentFrame*frameSize*3)+(dot*3)+1));
+        bTemp = *(movie_p + ((currentFrame*frameSize*3)+(dot*3)+2));
+       //serial_p->printf("Frame: %d  Module: %d r:%d g:%d b:%d\r\n",currentFrame,dot,rTemp,gTemp,bTemp);
+        r_display.setLed(dot,rTemp,gTemp,bTemp);
+    }
+    
+    r_display.displayFrame();//update the display - MAKE IT HAPPEN
+    
+    currentFrame++;
+    //Check bounds
+    if((currentFrame)>=((movieSize/sizeof(unsigned short int))/frameSize)/3){
+        if(f_repeat == 1) currentFrame = 0;//start from first frame
+        else currentFrame--; //force to repeat last frame
+    }
+ }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/movie.h	Wed Aug 20 05:24:38 2014 +0000
@@ -0,0 +1,74 @@
+#ifndef MOVIE_H
+#define MOVIE_H
+#include "mbed.h"
+#include "sbDriver.h"
+
+    /** movie class
+    * Used to send multiple frames of data to the Shiftbrite display.
+    *
+    * It does not inherit anything but does use a reference to a shiftBriteDisplay object.
+    * EXAMPLE:
+    * @code
+        #include "mbed.h"
+        #include "sbDriver.h"
+        #include "movie.h"
+        //6 leds example. Format it suitably for easy reading
+        unsigned short int aMovie[] = {
+        // LED1      LED2      LED3      LED4      LED5      LED6 
+        1023,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0,     //Frame 0
+            0,0,0, 1023,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0,     //Frame 1
+            0,0,0,    0,0,0,    1023,0,0, 0,0,0,    0,0,0,    0,0,0,     //Frame 2
+            0,0,0,    0,0,0,    0,0,0,    1023,0,0, 0,0,0,    0,0,0,     //Frame 3
+            0,0,0,    0,0,0,    0,0,0,    0,0,0,    1023,0,0, 0,0,0,     //Frame 4
+            0,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0,    1023,0,0,  //Frame 5
+            0,0,0,    0,0,0,    0,0,0,    0,0,0,    1023,0,0, 0,0,0,     //Frame 6
+            0,0,0,    0,0,0,    0,0,0,    1023,0,0, 0,0,0,    0,0,0,     //Frame 7
+            0,0,0,    0,0,0,    1023,0,0, 0,0,0,    0,0,0,    0,0,0,     //Frame 8
+            0,0,0, 1023,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0      //Frame 9
+        /A simple 'cylon' scanner 'movie'    
+        };
+    
+        Serial PC(PTA2, PTA1);//
+
+        //Instanced of DigitalOut for control SB signals
+        DigitalOut latch(PTC16);//010=latch
+        DigitalOut enable(PTA13);//0= enabled
+        DigitalOut reset(PTC12);
+        //Instance of the SPI contoller for SB data
+        SPI spi(PTD2,NC,PTD1);//PDT2 = MOSI=DATA. PDT1=CLK
+
+
+        int main() {
+    
+            //Instanciate a ticker object to handle framerate updates for the SB display    
+            Ticker t;
+
+            //Instanciate a string of 6 sb modules and tell the driver object where the control/data pins are
+            shiftBriteDisplay sbDisplay(&PC,latch, enable, reset, spi,6);
+
+            movie myMovie(aMovie,sbDisplay,sizeof(aMovie));
+            myMovie.setRepeat(1);
+            t.attach(&myMovie,&movie::play,0.05);//Beware, if you go too fast here the MBED will crash
+
+            while(1){
+            }
+        }
+        @codeend
+    */
+
+class movie{
+    unsigned long int movieSize; //how many BYTES are in the movie array
+    //NB, this is NOT the number of frames. frame Tot = ((movieSize/sizeof(unsigned short int))/frameSize)/3
+    unsigned short int frameSize; //how many leds are in a display
+    unsigned long int currentFrame; // which frame are we displaying now
+    unsigned char f_repeat; // repeat when end is reached
+    unsigned short int * movie_p;//This is where the movie data is stored
+    shiftBriteDisplay r_display;//reference to the display
+public:
+    //movie(unsigned short int * movie_p, shiftBriteDisplay &display); // constructor - frameCount=how many frames in the movie
+    movie(unsigned short int * movie_p, shiftBriteDisplay &display, unsigned int movieSize);
+    //This must dynamically allocte 
+    void play();//cycles through all known frames, one frame each time it is called
+    void setRepeat(unsigned char r){f_repeat = r;};//repeat from the start or keep on showing the final frame of the movie
+};
+#endif
\ No newline at end of file
--- a/sbDriver.h	Tue Aug 19 07:16:17 2014 +0000
+++ b/sbDriver.h	Wed Aug 20 05:24:38 2014 +0000
@@ -181,7 +181,10 @@
 //    void setCurrentCorr( unsigned long int rgbValue=0x786464);//ALL modules
     void setCurrentCorr( unsigned short int red/* = 0x78*/, unsigned short int green/* = 0x64*/, unsigned short int blue/* = 0x64*/);//ALL modules
     void setCurrentCorr();//overload - meaning, read the vals from the class member and set accordingly
+
 //    Display update    
     void displayFrame();// write a whole display's worth of data.
+    unsigned int getModuleCount(){return moduleCount;}
+    
 };
 #endif
\ No newline at end of file