Johan Kritzinger
/
FRDMKL25Z-ShiftBrite
A few classes to interface one or more ShiftBrite module to the FRDM KL25Z.
sbDriver.h@3:9376bf1f1bbd, 2014-08-20 (annotated)
- Committer:
- JoKer
- Date:
- Wed Aug 20 08:11:28 2014 +0000
- Revision:
- 3:9376bf1f1bbd
- Parent:
- 2:3935d2ed40cd
- Child:
- 4:d2f8ddb423e2
Updated documentation and some code cleanup.
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:4a62ae180af0 | 91 | * @param Serial Pointer to instance of Serial object. |
JoKer | 1:4a62ae180af0 | 92 | @param Latch Pin used to control data latching on shiftbrite module. |
JoKer | 1:4a62ae180af0 | 93 | @param Enable Pin used to control LED output buffers of shiftbrite module. |
JoKer | 1:4a62ae180af0 | 94 | @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 | 95 | @param SPI Port to send serial data to shoftbrite module. (Data and Clock). |
JoKer | 1:4a62ae180af0 | 96 | @param moduleCount int, the number of connected modules. |
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 | 3:9376bf1f1bbd | 103 | Serial PC(PTA2, PTA1);//Access to serial port for sending error messages |
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 | 3:9376bf1f1bbd | 172 | /**Constructor |
JoKer | 3:9376bf1f1bbd | 173 | See the example code for usage instructions. |
JoKer | 3:9376bf1f1bbd | 174 | */ |
JoKer | 0:f76850de7b57 | 175 | shiftBriteDisplay (Serial *port,DigitalOut &latch, DigitalOut &enable, DigitalOut &reset, SPI &spiPort, unsigned int moduleCount); //constructor |
JoKer | 0:f76850de7b57 | 176 | |
JoKer | 0:f76850de7b57 | 177 | // Destructor |
JoKer | 0:f76850de7b57 | 178 | ~shiftBriteDisplay();//destructor - needs to release module_p |
JoKer | 0:f76850de7b57 | 179 | |
JoKer | 3:9376bf1f1bbd | 180 | // Setters |
JoKer | 3:9376bf1f1bbd | 181 | /**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 | 182 | * unless you have no option because it only allows specifying 256 levels for each colour whereas the ShiftBright modules are |
JoKer | 3:9376bf1f1bbd | 183 | * capable of 1024. Use the overloaded member function. |
JoKer | 3:9376bf1f1bbd | 184 | */ |
JoKer | 3:9376bf1f1bbd | 185 | void setLed(unsigned int moduleNum, unsigned long int rgbValue); |
JoKer | 3:9376bf1f1bbd | 186 | |
JoKer | 3:9376bf1f1bbd | 187 | |
JoKer | 3:9376bf1f1bbd | 188 | /**used to set the colour for a specific dot (LED) using the full ability of the ShiftBrite modules. Each colour is supplied |
JoKer | 3:9376bf1f1bbd | 189 | * individualy in the range of 0-1023 (0-0x3FF). |
JoKer | 3:9376bf1f1bbd | 190 | */ |
JoKer | 0:f76850de7b57 | 191 | void setLed(unsigned int moduleNum, unsigned short int red, unsigned short int green, unsigned short int blue);//Overloaded |
JoKer | 0:f76850de7b57 | 192 | // void setCurrentCorr( unsigned long int rgbValue=0x786464);//ALL modules |
JoKer | 3:9376bf1f1bbd | 193 | |
JoKer | 3:9376bf1f1bbd | 194 | /** used to adjust the maximum current to each colour. Consists of 3 values, one each for red, green and blue. |
JoKer | 3:9376bf1f1bbd | 195 | * 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 | 3:9376bf1f1bbd | 196 | * that causes colours to mix incorrectly. When used it will record the arguments as the default values. |
JoKer | 3:9376bf1f1bbd | 197 | */ |
JoKer | 3:9376bf1f1bbd | 198 | |
JoKer | 3:9376bf1f1bbd | 199 | void setCurrentCorr( unsigned short int red, unsigned short int green, unsigned short int blue);//ALL modules |
JoKer | 3:9376bf1f1bbd | 200 | |
JoKer | 3:9376bf1f1bbd | 201 | /** same as above but call up the stored default values. |
JoKer | 3:9376bf1f1bbd | 202 | */ |
JoKer | 0:f76850de7b57 | 203 | void setCurrentCorr();//overload - meaning, read the vals from the class member and set accordingly |
JoKer | 2:3935d2ed40cd | 204 | |
JoKer | 3:9376bf1f1bbd | 205 | // Getters |
JoKer | 3:9376bf1f1bbd | 206 | |
JoKer | 3:9376bf1f1bbd | 207 | // Display update |
JoKer | 3:9376bf1f1bbd | 208 | /** used to refresh the display. All colour values are written out. |
JoKer | 3:9376bf1f1bbd | 209 | */ |
JoKer | 0:f76850de7b57 | 210 | void displayFrame();// write a whole display's worth of data. |
JoKer | 2:3935d2ed40cd | 211 | unsigned int getModuleCount(){return moduleCount;} |
JoKer | 2:3935d2ed40cd | 212 | |
JoKer | 3:9376bf1f1bbd | 213 | // Basic effects |
JoKer | 3:9376bf1f1bbd | 214 | /**rotateLeft() |
JoKer | 3:9376bf1f1bbd | 215 | * is used to shift the whole display colours one step left. |
JoKer | 3:9376bf1f1bbd | 216 | *The first colour is rotated around and shifted in the last dot. |
JoKer | 3:9376bf1f1bbd | 217 | */ |
JoKer | 3:9376bf1f1bbd | 218 | void rotateLeft(); |
JoKer | 3:9376bf1f1bbd | 219 | |
JoKer | 3:9376bf1f1bbd | 220 | /**shiftLeft() |
JoKer | 3:9376bf1f1bbd | 221 | * is used to shift the whole display colours one step left. |
JoKer | 3:9376bf1f1bbd | 222 | * A blank dot is shifted in. |
JoKer | 3:9376bf1f1bbd | 223 | */ |
JoKer | 3:9376bf1f1bbd | 224 | void shiftLeft();//info shifted out is lost |
JoKer | 3:9376bf1f1bbd | 225 | |
JoKer | 3:9376bf1f1bbd | 226 | /**rotateRight() |
JoKer | 3:9376bf1f1bbd | 227 | * is used to shift the whole display colours one step right. |
JoKer | 3:9376bf1f1bbd | 228 | *The first colour is rotated around and shifted in the last dot. |
JoKer | 3:9376bf1f1bbd | 229 | */ |
JoKer | 3:9376bf1f1bbd | 230 | void rotateRight(); |
JoKer | 3:9376bf1f1bbd | 231 | |
JoKer | 3:9376bf1f1bbd | 232 | |
JoKer | 3:9376bf1f1bbd | 233 | /**shiftRight() |
JoKer | 3:9376bf1f1bbd | 234 | * is used to shift the whole display colours one step right. |
JoKer | 3:9376bf1f1bbd | 235 | * A blank dot is shifted in. |
JoKer | 3:9376bf1f1bbd | 236 | */ |
JoKer | 3:9376bf1f1bbd | 237 | void shiftRight();//info shifted out is lost |
JoKer | 3:9376bf1f1bbd | 238 | |
JoKer | 3:9376bf1f1bbd | 239 | /**Display output is turned enabled. |
JoKer | 3:9376bf1f1bbd | 240 | */ |
JoKer | 3:9376bf1f1bbd | 241 | void turnOn(); |
JoKer | 3:9376bf1f1bbd | 242 | |
JoKer | 3:9376bf1f1bbd | 243 | /**Display output is turned disabled. |
JoKer | 3:9376bf1f1bbd | 244 | */ |
JoKer | 3:9376bf1f1bbd | 245 | void turnOff(); |
JoKer | 3:9376bf1f1bbd | 246 | |
JoKer | 3:9376bf1f1bbd | 247 | /**Used to exchange display values left to right and right to left. |
JoKer | 3:9376bf1f1bbd | 248 | */ |
JoKer | 3:9376bf1f1bbd | 249 | void flip(); //swop positions |
JoKer | 3:9376bf1f1bbd | 250 | |
JoKer | 3:9376bf1f1bbd | 251 | /**Changes all display colours with it's mathematical opposite. |
JoKer | 3:9376bf1f1bbd | 252 | */ |
JoKer | 3:9376bf1f1bbd | 253 | void invert(); //invert colours |
JoKer | 3:9376bf1f1bbd | 254 | |
JoKer | 3:9376bf1f1bbd | 255 | |
JoKer | 0:f76850de7b57 | 256 | }; |
JoKer | 0:f76850de7b57 | 257 | #endif |