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

Dependencies:   mbed

Committer:
JoKer
Date:
Fri Aug 22 01:18:49 2014 +0000
Revision:
7:a0f62fc80de0
Parent:
5:aa0424f31fa1
Cleaned up main. Divided into 3 sections to demonstrate various aspects of the libraries and their usage.

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 //REFER TO FRDM_LIGHTFX modules for hardware code
JoKer 4:d2f8ddb423e2 7
JoKer 0:f76850de7b57 8 //=============================================================================================
JoKer 0:f76850de7b57 9
JoKer 0:f76850de7b57 10 /** Colour class
JoKer 0:f76850de7b57 11 * Used as a base class for storing ShiftBrite colour information
JoKer 0:f76850de7b57 12 *
JoKer 0:f76850de7b57 13 * It is inherited by rgbLed class and is of little use if instanciated directly.
JoKer 0:f76850de7b57 14 * Please see class shiftBriteDisplay for intended use.
JoKer 0:f76850de7b57 15 */
JoKer 0:f76850de7b57 16
JoKer 0:f76850de7b57 17 class colour{
JoKer 0:f76850de7b57 18 private:
JoKer 0:f76850de7b57 19 unsigned short int value; //0-1023 range
JoKer 0:f76850de7b57 20
JoKer 0:f76850de7b57 21 public:
JoKer 0:f76850de7b57 22 void setColour(unsigned short int v){ v <= 1023 ? value = v : value = 1023;} // include range limiting
JoKer 0:f76850de7b57 23 unsigned short int getColour(void){return value;}
JoKer 0:f76850de7b57 24 colour operator =( unsigned short int v); //overload = relative to unsigned short int
JoKer 0:f76850de7b57 25 colour operator =( colour c); // overload = relative to colour
JoKer 0:f76850de7b57 26 };
JoKer 0:f76850de7b57 27
JoKer 0:f76850de7b57 28 //=============================================================================================
JoKer 0:f76850de7b57 29 /** rgbLed class
JoKer 0:f76850de7b57 30 * Used to instanciate a single rgb ShiftBrite object.
JoKer 0:f76850de7b57 31 *
JoKer 0:f76850de7b57 32 * ShiftBrite module consists of a single RGB led but
JoKer 0:f76850de7b57 33 * modules can be hooked in serial to form a chain of 2 or more modules
JoKer 0:f76850de7b57 34 *
JoKer 0:f76850de7b57 35 * This class is used as a dynamically allocated member in class shiftBriteDisplay.
JoKer 0:f76850de7b57 36 * It contains NO member functions for updating the physical shiftbright module so
JoKer 0:f76850de7b57 37 * is of limited use if instanciated directly. Please see class shiftBriteDisplay for intended use.
JoKer 0:f76850de7b57 38 */
JoKer 0:f76850de7b57 39 class rgbLed : public colour{//Inherit colour as a led consists of 3 colours
JoKer 0:f76850de7b57 40 private:
JoKer 0:f76850de7b57 41 colour red, green, blue;
JoKer 0:f76850de7b57 42 //Could add in 3 members here to track each module's individual current control (Dot correction)
JoKer 0:f76850de7b57 43 //but, this is not a critical app and I'd rather hang on to the extra memory so I elected
JoKer 0:f76850de7b57 44 //to have one set of Dot Corr values for ALL modules. This is handled by shiftBriteDisplay class. see below
JoKer 0:f76850de7b57 45 public:
JoKer 0:f76850de7b57 46 // the constructors
JoKer 0:f76850de7b57 47 rgbLed(unsigned long int rgbValue); // constructor for RGB value e.g. 0xFFFFFF. Will expand 0XFF to 0x3FF as per colour range
JoKer 0:f76850de7b57 48 rgbLed(unsigned short int red, unsigned short int green, unsigned short int blue); //overload for seperate r,g,b arguments
JoKer 0:f76850de7b57 49 rgbLed();//overload for no arguments
JoKer 0:f76850de7b57 50
JoKer 0:f76850de7b57 51 // the setters
JoKer 0:f76850de7b57 52 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 53 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 54 unsigned long int getPacket();
JoKer 0:f76850de7b57 55
JoKer 0:f76850de7b57 56 // the getters
JoKer 0:f76850de7b57 57 unsigned short int getRed(){return red.getColour();}
JoKer 0:f76850de7b57 58 unsigned short int getGreen(){return green.getColour();}
JoKer 0:f76850de7b57 59 unsigned short int getBlue(){return blue.getColour();}
JoKer 0:f76850de7b57 60
JoKer 0:f76850de7b57 61 };
JoKer 0:f76850de7b57 62
JoKer 0:f76850de7b57 63 //=============================================================================================
JoKer 0:f76850de7b57 64 /** shiftBriteDisplay class.
JoKer 0:f76850de7b57 65 * Used to write data to one (or more) shiftBrite module(s) via the SPI port on FRDM KL25Z module.
JoKer 0:f76850de7b57 66 *
JoKer 0:f76850de7b57 67 * Dynamically allocates storage for each module, based on <moduleCount> provided to the constructor.
JoKer 0:f76850de7b57 68 * Takes in references for the required hardware: SPI, Latch, Enable, and, if implemented, Reset.
JoKer 0:f76850de7b57 69 * Shiftbrite modules does NOT have a reset line but can be reset by removing the power. The reset line
JoKer 0:f76850de7b57 70 * referenced will toggle to allow you to implement the required hardware.
JoKer 1:4a62ae180af0 71 * @param Serial Pointer to instance of Serial object.
JoKer 1:4a62ae180af0 72 @param Latch Pin used to control data latching on shiftbrite module.
JoKer 1:4a62ae180af0 73 @param Enable Pin used to control LED output buffers of shiftbrite module.
JoKer 1:4a62ae180af0 74 @param Reset Pin used to signal a circuit that control the +ve power to the shiftbrite modules. This can be ignored if no reset required.
JoKer 1:4a62ae180af0 75 @param SPI Port to send serial data to shoftbrite module. (Data and Clock).
JoKer 1:4a62ae180af0 76 @param moduleCount int, the number of connected modules.
JoKer 0:f76850de7b57 77 *
JoKer 0:f76850de7b57 78 * Example:
JoKer 0:f76850de7b57 79 * @code
JoKer 0:f76850de7b57 80 #include "mbed.h"
JoKer 0:f76850de7b57 81 #include "sbDriver.h"
JoKer 0:f76850de7b57 82 DigitalOut latch(PTC16);//to LI pin of shiftBrite module
JoKer 0:f76850de7b57 83 DigitalOut enable(PTA13);//to EI pin of shiftBrite module
JoKer 0:f76850de7b57 84 DigitalOut reset(PTC12);//to power control circuit of your doing - NOT MANDATORY
JoKer 0:f76850de7b57 85 SPI spi(PTD2,NC,PTD1);//PDT2 = MOSI to DI and PDT1 to CI of shiftBrite module
JoKer 0:f76850de7b57 86
JoKer 4:d2f8ddb423e2 87 shiftBriteDisplay sbDisplay(latch, enable, reset, spi,6);//for, say, 6 modules wired in SERIES.
JoKer 0:f76850de7b57 88
JoKer 0:f76850de7b57 89 //If you wish to change the DOT CORR registers
JoKer 0:f76850de7b57 90 sbDisplay.setCurrentCorr(0x78,0x64,0x64);//use values you want to set as default. These are the suggested values
JoKer 0:f76850de7b57 91
JoKer 0:f76850de7b57 92 //Now, you can either call a member function to update the actual display OR
JoKer 0:f76850de7b57 93 //set it up using a Ticker. This is how you setup a ticker
JoKer 0:f76850de7b57 94 Ticker t;
JoKer 0:f76850de7b57 95 t.attach_us(&sbDisplay,&shiftBriteDisplay::displayFrame,41666);//call updateFrame 24 times per second (every 41666uS)
JoKer 0:f76850de7b57 96 //Ticker will automatically call sbDisplay.displayFrame()
JoKer 0:f76850de7b57 97 while(1){
JoKer 0:f76850de7b57 98 //Now just fill in the colours you want
JoKer 0:f76850de7b57 99 sbDisplay.setLed(0,1023,0,0); //FULL on red on the LAST display in the chain
JoKer 0:f76850de7b57 100 sbDisplay.setLed(5,0,1023,0); //FULL on green on first (remember the display has 6 leds in this example) LED
JoKer 0:f76850de7b57 101 //etc......
JoKer 0:f76850de7b57 102 }
JoKer 0:f76850de7b57 103
JoKer 0:f76850de7b57 104 * @endcode
JoKer 0:f76850de7b57 105 *
JoKer 0:f76850de7b57 106 * @note - Usage of shifBriteDisplay::setLed() member is as follows:
JoKer 0:f76850de7b57 107 * @code object.setLed(LedModuleNum,red, green, blue); //where colours are in the range of 0-1023@endcode
JoKer 0:f76850de7b57 108 * or
JoKer 0:f76850de7b57 109 * @code object.setLed(LedModuleNnum,RGB);// where RGB is in a range from 0x000000 to 0XFFFFFF (0xRRGGBB)@endcode
JoKer 0:f76850de7b57 110 * @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 111 * I suggest the first method is the best.
JoKer 0:f76850de7b57 112 *
JoKer 0:f76850de7b57 113 *
JoKer 0:f76850de7b57 114 * @endnote
JoKer 0:f76850de7b57 115 */
JoKer 0:f76850de7b57 116 class shiftBriteDisplay{
JoKer 0:f76850de7b57 117 private:
JoKer 4:d2f8ddb423e2 118 //Serial *serial_p; // for debug printing
JoKer 0:f76850de7b57 119
JoKer 0:f76850de7b57 120 //Harware control lines - common to all leds in display
JoKer 0:f76850de7b57 121 DigitalOut sb_latch;
JoKer 0:f76850de7b57 122 DigitalOut sb_enable;
JoKer 0:f76850de7b57 123 DigitalOut sb_reset;//Note, this effected by toggling power to the modules, it's not inherent shiftbrite functionality
JoKer 0:f76850de7b57 124 SPI spi;
JoKer 0:f76850de7b57 125
JoKer 0:f76850de7b57 126 //Led module(s)
JoKer 0:f76850de7b57 127 rgbLed *module_p; // points to the array of modules with same number of elements as LED modules
JoKer 0:f76850de7b57 128 unsigned int moduleCount;
JoKer 0:f76850de7b57 129 unsigned char rCorr, gCorr, bCorr; //These are the 'global' values for the current correction reg. DOT CORRECTION.
JoKer 0:f76850de7b57 130
JoKer 0:f76850de7b57 131 unsigned short int f_update;// flags that object is in an update cycle so that new req are ignored
JoKer 0:f76850de7b57 132
JoKer 0:f76850de7b57 133 // hardware control member methods
JoKer 0:f76850de7b57 134 void priv_SBEnable(void){sb_enable = 0;}
JoKer 0:f76850de7b57 135 void priv_SBDisable(void){sb_enable = 1;}
JoKer 0:f76850de7b57 136 void priv_SBLatch(void){wait(0.0001);sb_latch = 1;wait(0.0001);sb_latch = 0;}
JoKer 0:f76850de7b57 137 void priv_reset(void);//{sb_reset=0;wait(0.1);sb_reset=1;
JoKer 0:f76850de7b57 138
JoKer 0:f76850de7b57 139 // writes a single rgbLed RGB values as stored in the module_p array
JoKer 0:f76850de7b57 140 void send(rgbLed M, unsigned short int commandStatus=0); // accesses rgbLed.getPacket()
JoKer 0:f76850de7b57 141
JoKer 0:f76850de7b57 142 public:
JoKer 0:f76850de7b57 143 //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 144 // Also, initialize the SB current control registers(this WILL need to be done each time the SB modules are reset)
JoKer 0:f76850de7b57 145
JoKer 3:9376bf1f1bbd 146 /**Constructor
JoKer 3:9376bf1f1bbd 147 See the example code for usage instructions.
JoKer 3:9376bf1f1bbd 148 */
JoKer 4:d2f8ddb423e2 149 // shiftBriteDisplay (Serial *port,DigitalOut &latch, DigitalOut &enable, DigitalOut &reset, SPI &spiPort, unsigned int moduleCount); //constructor
JoKer 0:f76850de7b57 150
JoKer 4:d2f8ddb423e2 151 shiftBriteDisplay (DigitalOut &latch, DigitalOut &enable, DigitalOut &reset, SPI &spiPort, unsigned int moduleCount); //constructor
JoKer 0:f76850de7b57 152 // Destructor
JoKer 0:f76850de7b57 153 ~shiftBriteDisplay();//destructor - needs to release module_p
JoKer 0:f76850de7b57 154
JoKer 3:9376bf1f1bbd 155 // Setters
JoKer 3:9376bf1f1bbd 156 /**used to set the colour for a specific dot (LED) using the RGB system. e.g. 0XFF0000 is full blue. I would suggest not using this
JoKer 3:9376bf1f1bbd 157 * unless you have no option because it only allows specifying 256 levels for each colour whereas the ShiftBright modules are
JoKer 4:d2f8ddb423e2 158 * capable of 1024. Use the overloaded member function. Example:
JoKer 4:d2f8ddb423e2 159 * @code
JoKer 4:d2f8ddb423e2 160 myDisplay.setLed(ledNum,0X007F00);//Set led module 'ledNum' to half brightness green
JoKer 4:d2f8ddb423e2 161 myDisplay.setLed(ledNum+1,0X00FF00); // Set module 'ledNum+1' to full brightness green
JoKer 4:d2f8ddb423e2 162 @endcode
JoKer 3:9376bf1f1bbd 163 */
JoKer 3:9376bf1f1bbd 164 void setLed(unsigned int moduleNum, unsigned long int rgbValue);
JoKer 3:9376bf1f1bbd 165
JoKer 3:9376bf1f1bbd 166
JoKer 3:9376bf1f1bbd 167 /**used to set the colour for a specific dot (LED) using the full ability of the ShiftBrite modules. Each colour is supplied
JoKer 4:d2f8ddb423e2 168 * individualy in the range of 0-1023 (0-0x3FF). Example:
JoKer 4:d2f8ddb423e2 169 * @code
JoKer 4:d2f8ddb423e2 170 myDisplay.setLed(ledNum,0,512,0);//Set led module 'ledNum' to half brightness green using a decimal no
JoKer 4:d2f8ddb423e2 171 myDisplay.setLed(ledNum+1,0,0x3FF,0); // Set module 'ledNum+1' to full brightness green using a hex no
JoKer 4:d2f8ddb423e2 172 @endcode
JoKer 4:d2f8ddb423e2 173
JoKer 3:9376bf1f1bbd 174 */
JoKer 0:f76850de7b57 175 void setLed(unsigned int moduleNum, unsigned short int red, unsigned short int green, unsigned short int blue);//Overloaded
JoKer 0:f76850de7b57 176 // void setCurrentCorr( unsigned long int rgbValue=0x786464);//ALL modules
JoKer 3:9376bf1f1bbd 177
JoKer 3:9376bf1f1bbd 178 /** used to adjust the maximum current to each colour. Consists of 3 values, one each for red, green and blue.
JoKer 3:9376bf1f1bbd 179 * This allow you to adjust the brightness level of the reds, green and blues. Is usefull if one colour is brighter than the others
JoKer 4:d2f8ddb423e2 180 * that causes colours to mix incorrectly. When used it will record the arguments as the default values. Range is from 0 to 127 for each colour.
JoKer 4:d2f8ddb423e2 181 * This equated to rought 30% to 100% full brightness. NOTE, This is NOT for setting the brightness of the display (even though
JoKer 4:d2f8ddb423e2 182 * it can be abused that way), it is intended for adjusting the brightness balance between colours so that, for example, 0X3FF red and 0X3FF
JoKer 4:d2f8ddb423e2 183 * green makes yellow and not reddy-yellow or greeny-yellow etc.
JoKer 4:d2f8ddb423e2 184 * @code
JoKer 4:d2f8ddb423e2 185 myDisplay.setCurrentCorr(0x78,0x64,0x64); // set and record values as default. Retained only until master reset or powerdown.
JoKer 4:d2f8ddb423e2 186 @endcode
JoKer 4:d2f8ddb423e2 187 *
JoKer 3:9376bf1f1bbd 188 */
JoKer 3:9376bf1f1bbd 189 void setCurrentCorr( unsigned short int red, unsigned short int green, unsigned short int blue);//ALL modules
JoKer 3:9376bf1f1bbd 190
JoKer 4:d2f8ddb423e2 191 /**Overloaded version of above but calls up the stored values.
JoKer 4:d2f8ddb423e2 192 * These can either be the default (as Suggested by the ShiftBrite manufacturer) if they were not overwritten
JoKer 4:d2f8ddb423e2 193 * with the alternate member function (above).
JoKer 3:9376bf1f1bbd 194 */
JoKer 0:f76850de7b57 195 void setCurrentCorr();//overload - meaning, read the vals from the class member and set accordingly
JoKer 2:3935d2ed40cd 196
JoKer 3:9376bf1f1bbd 197 // Getters
JoKer 3:9376bf1f1bbd 198
JoKer 3:9376bf1f1bbd 199 // Display update
JoKer 3:9376bf1f1bbd 200 /** used to refresh the display. All colour values are written out.
JoKer 3:9376bf1f1bbd 201 */
JoKer 0:f76850de7b57 202 void displayFrame();// write a whole display's worth of data.
JoKer 2:3935d2ed40cd 203 unsigned int getModuleCount(){return moduleCount;}
JoKer 2:3935d2ed40cd 204
JoKer 3:9376bf1f1bbd 205 // Basic effects
JoKer 3:9376bf1f1bbd 206 /**rotateLeft()
JoKer 3:9376bf1f1bbd 207 * is used to shift the whole display colours one step left.
JoKer 3:9376bf1f1bbd 208 *The first colour is rotated around and shifted in the last dot.
JoKer 4:d2f8ddb423e2 209 * @code
JoKer 4:d2f8ddb423e2 210 for(loop=0; loop !=NumOfLeds; loop++){
JoKer 4:d2f8ddb423e2 211 sbDisplay.rotateLeft();
JoKer 4:d2f8ddb423e2 212 sbDisplay.displayFrame();
JoKer 4:d2f8ddb423e2 213 wait(0.2);
JoKer 4:d2f8ddb423e2 214 }
JoKer 4:d2f8ddb423e2 215 * @endcode
JoKer 3:9376bf1f1bbd 216 */
JoKer 3:9376bf1f1bbd 217 void rotateLeft();
JoKer 3:9376bf1f1bbd 218
JoKer 3:9376bf1f1bbd 219 /**shiftLeft()
JoKer 3:9376bf1f1bbd 220 * is used to shift the whole display colours one step left.
JoKer 5:aa0424f31fa1 221 * A blank dot is shifted in unless a colour is given in the argument.
JoKer 4:d2f8ddb423e2 222 * @code
JoKer 4:d2f8ddb423e2 223 for(loop=0; loop !=NumOfLeds; loop++){
JoKer 5:aa0424f31fa1 224 sbDisplay.shiftLeft();//or, to shift in a colour .shiftLeft(1000,200,0);
JoKer 4:d2f8ddb423e2 225 sbDisplay.displayFrame();
JoKer 4:d2f8ddb423e2 226 wait(0.2);
JoKer 4:d2f8ddb423e2 227 }
JoKer 4:d2f8ddb423e2 228 * @endcode
JoKer 3:9376bf1f1bbd 229 */
JoKer 5:aa0424f31fa1 230 void shiftLeft(unsigned short int inR=0,unsigned short int inG=0,unsigned short int inB=0);//info shifted out is lost
JoKer 3:9376bf1f1bbd 231
JoKer 3:9376bf1f1bbd 232 /**rotateRight()
JoKer 3:9376bf1f1bbd 233 * is used to shift the whole display colours one step right.
JoKer 3:9376bf1f1bbd 234 *The first colour is rotated around and shifted in the last dot.
JoKer 4:d2f8ddb423e2 235 * @code
JoKer 4:d2f8ddb423e2 236 for(loop=0; loop !=NumOfLeds; loop++){
JoKer 4:d2f8ddb423e2 237 sbDisplay.rotateRight();
JoKer 4:d2f8ddb423e2 238 sbDisplay.displayFrame();
JoKer 4:d2f8ddb423e2 239 wait(0.2);
JoKer 4:d2f8ddb423e2 240 }
JoKer 4:d2f8ddb423e2 241 * @endcode
JoKer 3:9376bf1f1bbd 242 */
JoKer 3:9376bf1f1bbd 243 void rotateRight();
JoKer 3:9376bf1f1bbd 244
JoKer 3:9376bf1f1bbd 245
JoKer 3:9376bf1f1bbd 246 /**shiftRight()
JoKer 3:9376bf1f1bbd 247 * is used to shift the whole display colours one step right.
JoKer 5:aa0424f31fa1 248 * A blank dot is shifted in unless a colour is given as an argument.
JoKer 4:d2f8ddb423e2 249 * @code
JoKer 4:d2f8ddb423e2 250 for(loop=0; loop !=NumOfLeds; loop++){
JoKer 5:aa0424f31fa1 251 sbDisplay.shiftRight(); // or, to feed in a colour sbDisplay.shiftRight(0,0XF0,0x30);
JoKer 4:d2f8ddb423e2 252 sbDisplay.displayFrame();
JoKer 4:d2f8ddb423e2 253 wait(0.2);
JoKer 4:d2f8ddb423e2 254 }
JoKer 4:d2f8ddb423e2 255 * @endcode
JoKer 3:9376bf1f1bbd 256 */
JoKer 5:aa0424f31fa1 257 void shiftRight(unsigned short int inR=0,unsigned short int inG=0,unsigned short int inB=0);//info shifted out is lost
JoKer 3:9376bf1f1bbd 258
JoKer 3:9376bf1f1bbd 259 /**Display output is turned enabled.
JoKer 3:9376bf1f1bbd 260 */
JoKer 3:9376bf1f1bbd 261 void turnOn();
JoKer 3:9376bf1f1bbd 262
JoKer 3:9376bf1f1bbd 263 /**Display output is turned disabled.
JoKer 3:9376bf1f1bbd 264 */
JoKer 3:9376bf1f1bbd 265 void turnOff();
JoKer 3:9376bf1f1bbd 266
JoKer 3:9376bf1f1bbd 267 /**Used to exchange display values left to right and right to left.
JoKer 3:9376bf1f1bbd 268 */
JoKer 3:9376bf1f1bbd 269 void flip(); //swop positions
JoKer 3:9376bf1f1bbd 270
JoKer 3:9376bf1f1bbd 271 /**Changes all display colours with it's mathematical opposite.
JoKer 3:9376bf1f1bbd 272 */
JoKer 3:9376bf1f1bbd 273 void invert(); //invert colours
JoKer 3:9376bf1f1bbd 274
JoKer 3:9376bf1f1bbd 275
JoKer 0:f76850de7b57 276 };
JoKer 0:f76850de7b57 277 #endif