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

Dependencies:   mbed

Committer:
JoKer
Date:
Tue Aug 19 07:09:20 2014 +0000
Revision:
0:f76850de7b57
Child:
1:4a62ae180af0
Working version of shiftBrite classes. V1.0 - 19/8/2014

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JoKer 0:f76850de7b57 1 //Low level driver for shiftbrite modules
JoKer 0:f76850de7b57 2
JoKer 0:f76850de7b57 3 #ifndef SHIFTBTIRE
JoKer 0:f76850de7b57 4 #define SHIFTBTIRE
JoKer 0:f76850de7b57 5 #include "mbed.h"
JoKer 0:f76850de7b57 6 //#include <string> // NOT string.h... hope this works with mbed
JoKer 0:f76850de7b57 7 //REFER TO FRDM_LIGHTFX modules for hardware code
JoKer 0:f76850de7b57 8 //=============================================================================================
JoKer 0:f76850de7b57 9 //class frame{//Would this help?
JoKer 0:f76850de7b57 10 /*house a complete frame of data
JoKer 0:f76850de7b57 11 This would include a shiftBriteDisplay as a member
JoKer 0:f76850de7b57 12 The shiftBriteDisplay would then be fed a frame of data at a time
JoKer 0:f76850de7b57 13 OR
JoKer 0:f76850de7b57 14 would it be better to have a movie class, holding all frames, as an member in shiftBriteDisplay
JoKer 0:f76850de7b57 15 shiftBriteDisplay would then call loadFrame from the movie class
JoKer 0:f76850de7b57 16 Movie class is essentially an array of frames with a circular buffer type of implementation
JoKer 0:f76850de7b57 17
JoKer 0:f76850de7b57 18 I think first option better as this would mean I can easily change movie content and have the movie
JoKer 0:f76850de7b57 19 in control of updating the frame etc.
JoKer 0:f76850de7b57 20
JoKer 0:f76850de7b57 21
JoKer 0:f76850de7b57 22 */
JoKer 0:f76850de7b57 23
JoKer 0:f76850de7b57 24 //};
JoKer 0:f76850de7b57 25 //class movie{
JoKer 0:f76850de7b57 26 /*house several frames that can be played in sequence*/
JoKer 0:f76850de7b57 27 //};
JoKer 0:f76850de7b57 28 //=============================================================================================
JoKer 0:f76850de7b57 29
JoKer 0:f76850de7b57 30 /** Colour class
JoKer 0:f76850de7b57 31 * Used as a base class for storing ShiftBrite colour information
JoKer 0:f76850de7b57 32 *
JoKer 0:f76850de7b57 33 * It is inherited by rgbLed class and is of little use if instanciated directly.
JoKer 0:f76850de7b57 34 * Please see class shiftBriteDisplay for intended use.
JoKer 0:f76850de7b57 35 */
JoKer 0:f76850de7b57 36
JoKer 0:f76850de7b57 37 class colour{
JoKer 0:f76850de7b57 38 private:
JoKer 0:f76850de7b57 39 unsigned short int value; //0-1023 range
JoKer 0:f76850de7b57 40
JoKer 0:f76850de7b57 41 public:
JoKer 0:f76850de7b57 42 void setColour(unsigned short int v){ v <= 1023 ? value = v : value = 1023;} // include range limiting
JoKer 0:f76850de7b57 43 unsigned short int getColour(void){return value;}
JoKer 0:f76850de7b57 44 colour operator =( unsigned short int v); //overload = relative to unsigned short int
JoKer 0:f76850de7b57 45 colour operator =( colour c); // overload = relative to colour
JoKer 0:f76850de7b57 46 };
JoKer 0:f76850de7b57 47
JoKer 0:f76850de7b57 48 //=============================================================================================
JoKer 0:f76850de7b57 49 /** rgbLed class
JoKer 0:f76850de7b57 50 * Used to instanciate a single rgb ShiftBrite object.
JoKer 0:f76850de7b57 51 *
JoKer 0:f76850de7b57 52 * ShiftBrite module consists of a single RGB led but
JoKer 0:f76850de7b57 53 * modules can be hooked in serial to form a chain of 2 or more modules
JoKer 0:f76850de7b57 54 *
JoKer 0:f76850de7b57 55 * This class is used as a dynamically allocated member in class shiftBriteDisplay.
JoKer 0:f76850de7b57 56 * It contains NO member functions for updating the physical shiftbright module so
JoKer 0:f76850de7b57 57 * is of limited use if instanciated directly. Please see class shiftBriteDisplay for intended use.
JoKer 0:f76850de7b57 58 */
JoKer 0:f76850de7b57 59 class rgbLed : public colour{//Inherit colour as a led consists of 3 colours
JoKer 0:f76850de7b57 60 private:
JoKer 0:f76850de7b57 61 colour red, green, blue;
JoKer 0:f76850de7b57 62 //Could add in 3 members here to track each module's individual current control (Dot correction)
JoKer 0:f76850de7b57 63 //but, this is not a critical app and I'd rather hang on to the extra memory so I elected
JoKer 0:f76850de7b57 64 //to have one set of Dot Corr values for ALL modules. This is handled by shiftBriteDisplay class. see below
JoKer 0:f76850de7b57 65 public:
JoKer 0:f76850de7b57 66 // the constructors
JoKer 0:f76850de7b57 67 rgbLed(unsigned long int rgbValue); // constructor for RGB value e.g. 0xFFFFFF. Will expand 0XFF to 0x3FF as per colour range
JoKer 0:f76850de7b57 68 rgbLed(unsigned short int red, unsigned short int green, unsigned short int blue); //overload for seperate r,g,b arguments
JoKer 0:f76850de7b57 69 rgbLed();//overload for no arguments
JoKer 0:f76850de7b57 70
JoKer 0:f76850de7b57 71 // the setters
JoKer 0:f76850de7b57 72 void setRgbLed(unsigned long int rgbValue); // RGB value e.g. 0xFFFFFF. Will expand 0XFF to 0x3FF as per colour range, also, update packet
JoKer 0:f76850de7b57 73 void setRgbLed(unsigned short int red, unsigned short int green, unsigned short int blue); //overload for seperate r,g,b arguments, also update packet
JoKer 0:f76850de7b57 74 unsigned long int getPacket();
JoKer 0:f76850de7b57 75
JoKer 0:f76850de7b57 76 // the getters
JoKer 0:f76850de7b57 77 unsigned short int getRed(){return red.getColour();}
JoKer 0:f76850de7b57 78 unsigned short int getGreen(){return green.getColour();}
JoKer 0:f76850de7b57 79 unsigned short int getBlue(){return blue.getColour();}
JoKer 0:f76850de7b57 80
JoKer 0:f76850de7b57 81 };
JoKer 0:f76850de7b57 82
JoKer 0:f76850de7b57 83 //=============================================================================================
JoKer 0:f76850de7b57 84 /** shiftBriteDisplay class.
JoKer 0:f76850de7b57 85 * Used to write data to one (or more) shiftBrite module(s) via the SPI port on FRDM KL25Z module.
JoKer 0:f76850de7b57 86 *
JoKer 0:f76850de7b57 87 * Dynamically allocates storage for each module, based on <moduleCount> provided to the constructor.
JoKer 0:f76850de7b57 88 * Takes in references for the required hardware: SPI, Latch, Enable, and, if implemented, Reset.
JoKer 0:f76850de7b57 89 * Shiftbrite modules does NOT have a reset line but can be reset by removing the power. The reset line
JoKer 0:f76850de7b57 90 * referenced will toggle to allow you to implement the required hardware.
JoKer 0:f76850de7b57 91 * @param Serial Pointer
JoKer 0:f76850de7b57 92 @param Latch Pin
JoKer 0:f76850de7b57 93 @param Enable Pin
JoKer 0:f76850de7b57 94 @param Reset Pin
JoKer 0:f76850de7b57 95 @param SPI Port (Data and Clock)
JoKer 0:f76850de7b57 96 @param moduleCount int
JoKer 0:f76850de7b57 97 *
JoKer 0:f76850de7b57 98 * Example:
JoKer 0:f76850de7b57 99 * @code
JoKer 0:f76850de7b57 100 #include "mbed.h"
JoKer 0:f76850de7b57 101 #include "sbDriver.h"
JoKer 0:f76850de7b57 102 //This code is licenced as "BEERWARE" and used at own your own risk and discretion. Coded by Johan Kritzinger 8/2014.
JoKer 0:f76850de7b57 103 Serial PC(PTA2, PTA1);//Access to serial port
JoKer 0:f76850de7b57 104 DigitalOut latch(PTC16);//to LI pin of shiftBrite module
JoKer 0:f76850de7b57 105 DigitalOut enable(PTA13);//to EI pin of shiftBrite module
JoKer 0:f76850de7b57 106 DigitalOut reset(PTC12);//to power control circuit of your doing - NOT MANDATORY
JoKer 0:f76850de7b57 107 SPI spi(PTD2,NC,PTD1);//PDT2 = MOSI to DI and PDT1 to CI of shiftBrite module
JoKer 0:f76850de7b57 108
JoKer 0:f76850de7b57 109 shiftBriteDisplay sbDisplay(&PC,latch, enable, reset, spi,6);//for, say, 6 modules wired in SERIES.
JoKer 0:f76850de7b57 110
JoKer 0:f76850de7b57 111 //If you wish to change the DOT CORR registers
JoKer 0:f76850de7b57 112 sbDisplay.setCurrentCorr(0x78,0x64,0x64);//use values you want to set as default. These are the suggested values
JoKer 0:f76850de7b57 113
JoKer 0:f76850de7b57 114 //Now, you can either call a member function to update the actual display OR
JoKer 0:f76850de7b57 115 //set it up using a Ticker. This is how you setup a ticker
JoKer 0:f76850de7b57 116 Ticker t;
JoKer 0:f76850de7b57 117 t.attach_us(&sbDisplay,&shiftBriteDisplay::displayFrame,41666);//call updateFrame 24 times per second (every 41666uS)
JoKer 0:f76850de7b57 118 //Ticker will automatically call sbDisplay.displayFrame()
JoKer 0:f76850de7b57 119 while(1){
JoKer 0:f76850de7b57 120 //Now just fill in the colours you want
JoKer 0:f76850de7b57 121 sbDisplay.setLed(0,1023,0,0); //FULL on red on the LAST display in the chain
JoKer 0:f76850de7b57 122 sbDisplay.setLed(5,0,1023,0); //FULL on green on first (remember the display has 6 leds in this example) LED
JoKer 0:f76850de7b57 123 //etc......
JoKer 0:f76850de7b57 124 }
JoKer 0:f76850de7b57 125
JoKer 0:f76850de7b57 126 * @endcode
JoKer 0:f76850de7b57 127 *
JoKer 0:f76850de7b57 128 * @note - Usage of shifBriteDisplay::setLed() member is as follows:
JoKer 0:f76850de7b57 129 * @code object.setLed(LedModuleNum,red, green, blue); //where colours are in the range of 0-1023@endcode
JoKer 0:f76850de7b57 130 * or
JoKer 0:f76850de7b57 131 * @code object.setLed(LedModuleNnum,RGB);// where RGB is in a range from 0x000000 to 0XFFFFFF (0xRRGGBB)@endcode
JoKer 0:f76850de7b57 132 * @note NB, In the second method each colour has a range of 0-255 (0-0xFF) but that is expanded to the full range. This can be convenient but
JoKer 0:f76850de7b57 133 * I suggest the first method is the best.
JoKer 0:f76850de7b57 134 *
JoKer 0:f76850de7b57 135 *
JoKer 0:f76850de7b57 136 *
JoKer 0:f76850de7b57 137 * TO DO:
JoKer 0:f76850de7b57 138 * See if thee is a better way to deal with the serial port.
JoKer 0:f76850de7b57 139 * Write a frame and movie class to abstract dealing with individual LED's
JoKer 0:f76850de7b57 140 * @endnote
JoKer 0:f76850de7b57 141 */
JoKer 0:f76850de7b57 142 class shiftBriteDisplay{
JoKer 0:f76850de7b57 143 private:
JoKer 0:f76850de7b57 144 Serial *serial_p; // for debug printing
JoKer 0:f76850de7b57 145
JoKer 0:f76850de7b57 146 //Harware control lines - common to all leds in display
JoKer 0:f76850de7b57 147 DigitalOut sb_latch;
JoKer 0:f76850de7b57 148 DigitalOut sb_enable;
JoKer 0:f76850de7b57 149 DigitalOut sb_reset;//Note, this effected by toggling power to the modules, it's not inherent shiftbrite functionality
JoKer 0:f76850de7b57 150 SPI spi;
JoKer 0:f76850de7b57 151
JoKer 0:f76850de7b57 152 //Led module(s)
JoKer 0:f76850de7b57 153 rgbLed *module_p; // points to the array of modules with same number of elements as LED modules
JoKer 0:f76850de7b57 154 unsigned int moduleCount;
JoKer 0:f76850de7b57 155 unsigned char rCorr, gCorr, bCorr; //These are the 'global' values for the current correction reg. DOT CORRECTION.
JoKer 0:f76850de7b57 156
JoKer 0:f76850de7b57 157 unsigned short int f_update;// flags that object is in an update cycle so that new req are ignored
JoKer 0:f76850de7b57 158
JoKer 0:f76850de7b57 159 // hardware control member methods
JoKer 0:f76850de7b57 160 void priv_SBEnable(void){sb_enable = 0;}
JoKer 0:f76850de7b57 161 void priv_SBDisable(void){sb_enable = 1;}
JoKer 0:f76850de7b57 162 void priv_SBLatch(void){wait(0.0001);sb_latch = 1;wait(0.0001);sb_latch = 0;}
JoKer 0:f76850de7b57 163 void priv_reset(void);//{sb_reset=0;wait(0.1);sb_reset=1;
JoKer 0:f76850de7b57 164
JoKer 0:f76850de7b57 165 // writes a single rgbLed RGB values as stored in the module_p array
JoKer 0:f76850de7b57 166 void send(rgbLed M, unsigned short int commandStatus=0); // accesses rgbLed.getPacket()
JoKer 0:f76850de7b57 167
JoKer 0:f76850de7b57 168 public:
JoKer 0:f76850de7b57 169 //TO DO - Modify the constructor to initialise the spi PWM, and setup defaults for the SB control sinals (i.e. enable, latch and reset)
JoKer 0:f76850de7b57 170 // Also, initialize the SB current control registers(this WILL need to be done each time the SB modules are reset)
JoKer 0:f76850de7b57 171
JoKer 0:f76850de7b57 172 // Constructor
JoKer 0:f76850de7b57 173 shiftBriteDisplay (Serial *port,DigitalOut &latch, DigitalOut &enable, DigitalOut &reset, SPI &spiPort, unsigned int moduleCount); //constructor
JoKer 0:f76850de7b57 174
JoKer 0:f76850de7b57 175 // Destructor
JoKer 0:f76850de7b57 176 ~shiftBriteDisplay();//destructor - needs to release module_p
JoKer 0:f76850de7b57 177
JoKer 0:f76850de7b57 178 // Setters
JoKer 0:f76850de7b57 179 void setLed(unsigned int moduleNum, unsigned long int rgbValue);
JoKer 0:f76850de7b57 180 void setLed(unsigned int moduleNum, unsigned short int red, unsigned short int green, unsigned short int blue);//Overloaded
JoKer 0:f76850de7b57 181 // void setCurrentCorr( unsigned long int rgbValue=0x786464);//ALL modules
JoKer 0:f76850de7b57 182 void setCurrentCorr( unsigned short int red/* = 0x78*/, unsigned short int green/* = 0x64*/, unsigned short int blue/* = 0x64*/);//ALL modules
JoKer 0:f76850de7b57 183 void setCurrentCorr();//overload - meaning, read the vals from the class member and set accordingly
JoKer 0:f76850de7b57 184 // Display update
JoKer 0:f76850de7b57 185 void displayFrame();// write a whole display's worth of data.
JoKer 0:f76850de7b57 186 };
JoKer 0:f76850de7b57 187 #endif