Published
Fork of TLC5940 by
TLC5955.h@4:ab6b451bbf40, 2018-06-09 (annotated)
- Committer:
- roysandberg
- Date:
- Sat Jun 09 23:22:13 2018 +0000
- Revision:
- 4:ab6b451bbf40
Published
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roysandberg | 4:ab6b451bbf40 | 1 | #ifndef TLC5955_H |
roysandberg | 4:ab6b451bbf40 | 2 | #define TLC5955_H |
roysandberg | 4:ab6b451bbf40 | 3 | |
roysandberg | 4:ab6b451bbf40 | 4 | //#include "FastPWM.h" |
roysandberg | 4:ab6b451bbf40 | 5 | #include "mbed.h" |
roysandberg | 4:ab6b451bbf40 | 6 | |
roysandberg | 4:ab6b451bbf40 | 7 | #define SHORTS_PER_CHANNEL 3 |
roysandberg | 4:ab6b451bbf40 | 8 | #define CHANNELS_PER_IC 16 |
roysandberg | 4:ab6b451bbf40 | 9 | #define NUMBER_OF_ICS 3 |
roysandberg | 4:ab6b451bbf40 | 10 | |
roysandberg | 4:ab6b451bbf40 | 11 | #define TRUE 1 |
roysandberg | 4:ab6b451bbf40 | 12 | #define FALSE 0 |
roysandberg | 4:ab6b451bbf40 | 13 | |
roysandberg | 4:ab6b451bbf40 | 14 | // slow this down 10x for testing |
roysandberg | 4:ab6b451bbf40 | 15 | //#define SEQUENCER_RATE 0.5f |
roysandberg | 4:ab6b451bbf40 | 16 | //#define SEQUENCER_RATE 0.025f |
roysandberg | 4:ab6b451bbf40 | 17 | #define SEQUENCER_RATE 0.030f |
roysandberg | 4:ab6b451bbf40 | 18 | |
roysandberg | 4:ab6b451bbf40 | 19 | /** |
roysandberg | 4:ab6b451bbf40 | 20 | * SPI speed used by the mbed to communicate with the TLC5955 |
roysandberg | 4:ab6b451bbf40 | 21 | * The TLC5955 supports up to 30Mhz. This should be kept as high |
roysandberg | 4:ab6b451bbf40 | 22 | * as possible to ensure that data has time to be sent each reset cycle. |
roysandberg | 4:ab6b451bbf40 | 23 | */ |
roysandberg | 4:ab6b451bbf40 | 24 | // 4Mhz is fastest supported rate on nRF51 |
roysandberg | 4:ab6b451bbf40 | 25 | #define SPI_SPEED 4000000 |
roysandberg | 4:ab6b451bbf40 | 26 | |
roysandberg | 4:ab6b451bbf40 | 27 | void rebuildGammaTables(uint8_t amplitude); |
roysandberg | 4:ab6b451bbf40 | 28 | |
roysandberg | 4:ab6b451bbf40 | 29 | /** |
roysandberg | 4:ab6b451bbf40 | 30 | * This class controls a TLC5955 PWM driver IC. |
roysandberg | 4:ab6b451bbf40 | 31 | * It supports sending dot correction and grayscale data. However, it does not support error checking or writing the EEPROM. |
roysandberg | 4:ab6b451bbf40 | 32 | * After |
roysandberg | 4:ab6b451bbf40 | 33 | * 4096 pulses, the private member funciton reset is called by the ticker. It resets the display by pulsing the BLANK pin. If new |
roysandberg | 4:ab6b451bbf40 | 34 | * data has been set to be sent by the functions setNewGSData or setNewDCData, it is sent here. The definition GSCLK_SPEED in TLC5955.h |
roysandberg | 4:ab6b451bbf40 | 35 | * controls how often this function is called. A higher GSCLK_SPEED will increase the rate at which the screen is updated but also increase |
roysandberg | 4:ab6b451bbf40 | 36 | * CPU time spent in that function. The default value is 1Mhz. The rate at which the reset function is called can be calculated by: |
roysandberg | 4:ab6b451bbf40 | 37 | * (1/GSCLK_SPEED) * 4096. |
roysandberg | 4:ab6b451bbf40 | 38 | */ |
roysandberg | 4:ab6b451bbf40 | 39 | class TLC5955 |
roysandberg | 4:ab6b451bbf40 | 40 | { |
roysandberg | 4:ab6b451bbf40 | 41 | public: |
roysandberg | 4:ab6b451bbf40 | 42 | |
roysandberg | 4:ab6b451bbf40 | 43 | typedef enum |
roysandberg | 4:ab6b451bbf40 | 44 | { |
roysandberg | 4:ab6b451bbf40 | 45 | I_3_2_MA = 0, |
roysandberg | 4:ab6b451bbf40 | 46 | I_8_0_MA = 1, |
roysandberg | 4:ab6b451bbf40 | 47 | I_11_2_MA = 2, |
roysandberg | 4:ab6b451bbf40 | 48 | I_15_9_MA = 3, |
roysandberg | 4:ab6b451bbf40 | 49 | I_19_1_MA = 4, |
roysandberg | 4:ab6b451bbf40 | 50 | I_23_9_MA = 5, |
roysandberg | 4:ab6b451bbf40 | 51 | I_27_1_MA = 6, |
roysandberg | 4:ab6b451bbf40 | 52 | I_31_9_MA = 7 |
roysandberg | 4:ab6b451bbf40 | 53 | } led_power_t; |
roysandberg | 4:ab6b451bbf40 | 54 | |
roysandberg | 4:ab6b451bbf40 | 55 | /** |
roysandberg | 4:ab6b451bbf40 | 56 | * Set up the TLC5955 |
roysandberg | 4:ab6b451bbf40 | 57 | * @param SCLK - The SCK pin of the SPI bus |
roysandberg | 4:ab6b451bbf40 | 58 | * @param MOSI - The MOSI pin of the SPI bus |
roysandberg | 4:ab6b451bbf40 | 59 | * @param GSCLK - The GSCLK pin of the TLC5955(s) |
roysandberg | 4:ab6b451bbf40 | 60 | * @param BLANK - The BLANK pin of the TLC5955(s) <- REMOVE |
roysandberg | 4:ab6b451bbf40 | 61 | * @param XLAT - The XLAT pin of the TLC5955(s) |
roysandberg | 4:ab6b451bbf40 | 62 | * @param DCPRG - The DCPRG pin of the TLC5955(s) <- REMOVE |
roysandberg | 4:ab6b451bbf40 | 63 | * @param VPRG - The VPRG pin of the TLC5955(s) <- REMOVE |
roysandberg | 4:ab6b451bbf40 | 64 | * @param number - The number of TLC5955s (if you are daisy chaining) |
roysandberg | 4:ab6b451bbf40 | 65 | */ |
roysandberg | 4:ab6b451bbf40 | 66 | TLC5955(PinName SCLK, PinName MOSI, PinName GSCLK, |
roysandberg | 4:ab6b451bbf40 | 67 | PinName XLAT, const int number = 1); |
roysandberg | 4:ab6b451bbf40 | 68 | |
roysandberg | 4:ab6b451bbf40 | 69 | void setChannel(int channelNum, unsigned short red, unsigned short green, unsigned short blue); |
roysandberg | 4:ab6b451bbf40 | 70 | void latchData(); |
roysandberg | 4:ab6b451bbf40 | 71 | |
roysandberg | 4:ab6b451bbf40 | 72 | |
roysandberg | 4:ab6b451bbf40 | 73 | /** |
roysandberg | 4:ab6b451bbf40 | 74 | * Set the next chunk of grayscale data to be sent |
roysandberg | 4:ab6b451bbf40 | 75 | * @param data - Array of 16 bit shorts containing 16 12 bit grayscale data chunks per TLC5955 |
roysandberg | 4:ab6b451bbf40 | 76 | * @note These must be in intervals of at least (1/GSCLK_SPEED) * 4096 to be sent |
roysandberg | 4:ab6b451bbf40 | 77 | */ |
roysandberg | 4:ab6b451bbf40 | 78 | void setNewGSData(unsigned short* data); |
roysandberg | 4:ab6b451bbf40 | 79 | |
roysandberg | 4:ab6b451bbf40 | 80 | /** |
roysandberg | 4:ab6b451bbf40 | 81 | * Set the next chunk of dot correction data to be sent |
roysandberg | 4:ab6b451bbf40 | 82 | * @param data - Array of 8 bit chars containing 16 6 bit dot correction data chunks per TLC5955 |
roysandberg | 4:ab6b451bbf40 | 83 | * @note These must be in intervals of at least (1/GSCLK_SPEED) * 4096 to be sent. Also, this function is optional. If you do not |
roysandberg | 4:ab6b451bbf40 | 84 | * use it, then the TLC5955 will use the EEPROM, which (by default) conatins the data 0x3F. |
roysandberg | 4:ab6b451bbf40 | 85 | */ |
roysandberg | 4:ab6b451bbf40 | 86 | void setNewControlData(unsigned short _globalBrightnessRed, unsigned short _globalBrightnessGreen, unsigned short _globalBrightnessBlue, |
roysandberg | 4:ab6b451bbf40 | 87 | led_power_t _maximumCurrentRed, led_power_t _maximumCurrentGreen, led_power_t _maximumCurrentBlue, |
roysandberg | 4:ab6b451bbf40 | 88 | unsigned short* _dotCorrect); |
roysandberg | 4:ab6b451bbf40 | 89 | void clockOutData(); |
roysandberg | 4:ab6b451bbf40 | 90 | void clearBit (unsigned short* value, int bitOffset); |
roysandberg | 4:ab6b451bbf40 | 91 | void setBit (unsigned short* value, int bitOffset); |
roysandberg | 4:ab6b451bbf40 | 92 | void packBit(unsigned int aBit); |
roysandberg | 4:ab6b451bbf40 | 93 | void packByte (unsigned int aByte); |
roysandberg | 4:ab6b451bbf40 | 94 | void packShort (unsigned int aShort); |
roysandberg | 4:ab6b451bbf40 | 95 | protected: |
roysandberg | 4:ab6b451bbf40 | 96 | /** |
roysandberg | 4:ab6b451bbf40 | 97 | * Set the next chunk of grayscale data to be sent while in the current reset cycle |
roysandberg | 4:ab6b451bbf40 | 98 | * @note This is useful to send the next set of data right after the first is finished being displayed. |
roysandberg | 4:ab6b451bbf40 | 99 | * The primary purpose for this is multiplexing, although it could be used for anything else. |
roysandberg | 4:ab6b451bbf40 | 100 | */ |
roysandberg | 4:ab6b451bbf40 | 101 | virtual void setNextData() {} |
roysandberg | 4:ab6b451bbf40 | 102 | |
roysandberg | 4:ab6b451bbf40 | 103 | |
roysandberg | 4:ab6b451bbf40 | 104 | // Number of TLC5955s in series |
roysandberg | 4:ab6b451bbf40 | 105 | const int number; |
roysandberg | 4:ab6b451bbf40 | 106 | |
roysandberg | 4:ab6b451bbf40 | 107 | private: |
roysandberg | 4:ab6b451bbf40 | 108 | |
roysandberg | 4:ab6b451bbf40 | 109 | // SPI port - only MOSI and SCK are used |
roysandberg | 4:ab6b451bbf40 | 110 | SPI spi; |
roysandberg | 4:ab6b451bbf40 | 111 | |
roysandberg | 4:ab6b451bbf40 | 112 | // PWM output |
roysandberg | 4:ab6b451bbf40 | 113 | DigitalOut gsclk; |
roysandberg | 4:ab6b451bbf40 | 114 | |
roysandberg | 4:ab6b451bbf40 | 115 | // Digital out pins used for the TLC5955 |
roysandberg | 4:ab6b451bbf40 | 116 | DigitalOut xlat; |
roysandberg | 4:ab6b451bbf40 | 117 | |
roysandberg | 4:ab6b451bbf40 | 118 | // Call a reset function to manage sending data and GSCLK updating |
roysandberg | 4:ab6b451bbf40 | 119 | Ticker reset_ticker; |
roysandberg | 4:ab6b451bbf40 | 120 | |
roysandberg | 4:ab6b451bbf40 | 121 | int currentBitLocation; |
roysandberg | 4:ab6b451bbf40 | 122 | |
roysandberg | 4:ab6b451bbf40 | 123 | // Has new GS/Control data been loaded? |
roysandberg | 4:ab6b451bbf40 | 124 | volatile bool newGSData; |
roysandberg | 4:ab6b451bbf40 | 125 | volatile bool newControlData; |
roysandberg | 4:ab6b451bbf40 | 126 | |
roysandberg | 4:ab6b451bbf40 | 127 | // Do we need to send an XLAT pulse? (Was GS data clocked in last reset?) |
roysandberg | 4:ab6b451bbf40 | 128 | volatile bool need_xlat; |
roysandberg | 4:ab6b451bbf40 | 129 | |
roysandberg | 4:ab6b451bbf40 | 130 | // Buffer to store data until it is sent |
roysandberg | 4:ab6b451bbf40 | 131 | unsigned short gsBuffer[(SHORTS_PER_CHANNEL * CHANNELS_PER_IC * NUMBER_OF_ICS) + 1]; // one extra word to fit the MSB control bit. Extra bits will be clocked out. |
roysandberg | 4:ab6b451bbf40 | 132 | |
roysandberg | 4:ab6b451bbf40 | 133 | unsigned short internalData[SHORTS_PER_CHANNEL * CHANNELS_PER_IC * NUMBER_OF_ICS]; // internal data storage for use by per-channel setting function |
roysandberg | 4:ab6b451bbf40 | 134 | unsigned short globalBrightnessRed; |
roysandberg | 4:ab6b451bbf40 | 135 | unsigned short globalBrightnessGreen; |
roysandberg | 4:ab6b451bbf40 | 136 | unsigned short globalBrightnessBlue; |
roysandberg | 4:ab6b451bbf40 | 137 | unsigned short maximumCurrentRed; |
roysandberg | 4:ab6b451bbf40 | 138 | unsigned short maximumCurrentGreen; |
roysandberg | 4:ab6b451bbf40 | 139 | unsigned short maximumCurrentBlue; |
roysandberg | 4:ab6b451bbf40 | 140 | unsigned short* dotCorrect; |
roysandberg | 4:ab6b451bbf40 | 141 | |
roysandberg | 4:ab6b451bbf40 | 142 | // Function to reset the display and send the next chunks of data |
roysandberg | 4:ab6b451bbf40 | 143 | void reset(); |
roysandberg | 4:ab6b451bbf40 | 144 | }; |
roysandberg | 4:ab6b451bbf40 | 145 | |
roysandberg | 4:ab6b451bbf40 | 146 | |
roysandberg | 4:ab6b451bbf40 | 147 | /** |
roysandberg | 4:ab6b451bbf40 | 148 | * This class allows a TLC5955 to be multiplexed. |
roysandberg | 4:ab6b451bbf40 | 149 | * It inherits the TLC5955 class and uses it to control the TLC5955 driver(s). It does not support sending dot corection data. |
roysandberg | 4:ab6b451bbf40 | 150 | * This class sets the new grayscale data every iteration of the GSCLK reset loop. It then updates the current row using the |
roysandberg | 4:ab6b451bbf40 | 151 | * user defined function SetRows. The framerate you will recieve using this function can be calculate by: 1 / (((1/GSCLK_SPEED) * 4096) * rows). |
roysandberg | 4:ab6b451bbf40 | 152 | * I reccomend maintaining a framerate above 30fps. However, keep in mind that as your framerate increases, so does your CPU usage. |
roysandberg | 4:ab6b451bbf40 | 153 | * |
roysandberg | 4:ab6b451bbf40 | 154 | * Using the TLC5955Mux class to control an 8x8 LED matrix: |
roysandberg | 4:ab6b451bbf40 | 155 | * @code |
roysandberg | 4:ab6b451bbf40 | 156 | * #include "mbed.h" |
roysandberg | 4:ab6b451bbf40 | 157 | * #include "TLC5955.h" |
roysandberg | 4:ab6b451bbf40 | 158 | * |
roysandberg | 4:ab6b451bbf40 | 159 | * // Bus connecting to the rows of the LED matrix through PNP transistors |
roysandberg | 4:ab6b451bbf40 | 160 | * BusOut rows(p22, p23, p24, p25, p26, p27, p28, p29); |
roysandberg | 4:ab6b451bbf40 | 161 | * |
roysandberg | 4:ab6b451bbf40 | 162 | * // Function to update the rows using the BusOut class |
roysandberg | 4:ab6b451bbf40 | 163 | * void SetRows(int nextRow) |
roysandberg | 4:ab6b451bbf40 | 164 | * { |
roysandberg | 4:ab6b451bbf40 | 165 | * // I am using PNP transistors, so inversion is necessary |
roysandberg | 4:ab6b451bbf40 | 166 | * rows = ~(1 << nextRow); |
roysandberg | 4:ab6b451bbf40 | 167 | * } |
roysandberg | 4:ab6b451bbf40 | 168 | * |
roysandberg | 4:ab6b451bbf40 | 169 | * // Create the TLC5955Mux instance |
roysandberg | 4:ab6b451bbf40 | 170 | * TLC5955Mux tlc(p7, p5, p21, p9, p10, p11, p12, 1, 8, &SetRows); |
roysandberg | 4:ab6b451bbf40 | 171 | * |
roysandberg | 4:ab6b451bbf40 | 172 | * int main() |
roysandberg | 4:ab6b451bbf40 | 173 | * { |
roysandberg | 4:ab6b451bbf40 | 174 | * tlc[0][0] = 0xFFF; // Turn on the top left LED |
roysandberg | 4:ab6b451bbf40 | 175 | * while(1) |
roysandberg | 4:ab6b451bbf40 | 176 | * { |
roysandberg | 4:ab6b451bbf40 | 177 | * |
roysandberg | 4:ab6b451bbf40 | 178 | * } |
roysandberg | 4:ab6b451bbf40 | 179 | * } |
roysandberg | 4:ab6b451bbf40 | 180 | * @endcode |
roysandberg | 4:ab6b451bbf40 | 181 | */ |
roysandberg | 4:ab6b451bbf40 | 182 | class TLC5955Mux : private TLC5955 |
roysandberg | 4:ab6b451bbf40 | 183 | { |
roysandberg | 4:ab6b451bbf40 | 184 | public: |
roysandberg | 4:ab6b451bbf40 | 185 | /** |
roysandberg | 4:ab6b451bbf40 | 186 | * Set up the TLC5955 |
roysandberg | 4:ab6b451bbf40 | 187 | * @param SCLK - The SCK pin of the SPI bus |
roysandberg | 4:ab6b451bbf40 | 188 | * @param MOSI - The MOSI pin of the SPI bus |
roysandberg | 4:ab6b451bbf40 | 189 | * @param GSCLK - The GSCLK pin of the TLC5955(s) |
roysandberg | 4:ab6b451bbf40 | 190 | * @param BLANK - The BLANK pin of the TLC5955(s) |
roysandberg | 4:ab6b451bbf40 | 191 | * @param XLAT - The XLAT pin of the TLC5955(s) |
roysandberg | 4:ab6b451bbf40 | 192 | * @param DCPRG - The DCPRG pin of the TLC5955(s) |
roysandberg | 4:ab6b451bbf40 | 193 | * @param VPRG - The VPRG pin of the TLC5955(s) |
roysandberg | 4:ab6b451bbf40 | 194 | * @param number - The number of TLC5955s (if you are daisy chaining) |
roysandberg | 4:ab6b451bbf40 | 195 | * @param rows - The number of rows you are multiplexing |
roysandberg | 4:ab6b451bbf40 | 196 | * @param SetRows - The function pointer to your function that sets the current row. |
roysandberg | 4:ab6b451bbf40 | 197 | * @note The SetRows function allows you to set exactly how you want your rows |
roysandberg | 4:ab6b451bbf40 | 198 | * to be updated. The TLC5955Mux class calls this function with an argument of int that contains the number of the row to |
roysandberg | 4:ab6b451bbf40 | 199 | * be turned on. If the TLC5955Mux class needs the first row to be turned on, the int will be 0. |
roysandberg | 4:ab6b451bbf40 | 200 | */ |
roysandberg | 4:ab6b451bbf40 | 201 | TLC5955Mux(PinName SCLK, PinName MOSI, PinName GSCLK, |
roysandberg | 4:ab6b451bbf40 | 202 | PinName XLAT, const int number, |
roysandberg | 4:ab6b451bbf40 | 203 | const int rows, void (*SetRows)(int)); |
roysandberg | 4:ab6b451bbf40 | 204 | |
roysandberg | 4:ab6b451bbf40 | 205 | // Destructor used to delete memory |
roysandberg | 4:ab6b451bbf40 | 206 | ~TLC5955Mux(); |
roysandberg | 4:ab6b451bbf40 | 207 | |
roysandberg | 4:ab6b451bbf40 | 208 | /** |
roysandberg | 4:ab6b451bbf40 | 209 | * Set the contents of the buffer that contains the multiplexed data |
roysandberg | 4:ab6b451bbf40 | 210 | * @param data - The data to set to the buffer containing 16 12 bit grayscale data chunks per TLC5955 |
roysandberg | 4:ab6b451bbf40 | 211 | * @returns The data provided |
roysandberg | 4:ab6b451bbf40 | 212 | */ |
roysandberg | 4:ab6b451bbf40 | 213 | unsigned short* operator=(unsigned short* data); |
roysandberg | 4:ab6b451bbf40 | 214 | |
roysandberg | 4:ab6b451bbf40 | 215 | /** |
roysandberg | 4:ab6b451bbf40 | 216 | * Get a pointer to one of the rows of the multiplexed data |
roysandberg | 4:ab6b451bbf40 | 217 | * @param index - The row that you would like the contents of |
roysandberg | 4:ab6b451bbf40 | 218 | * @returns A pointer to the data containing the requested row containing 16 12 bit grayscale data chunks per TLC5955 |
roysandberg | 4:ab6b451bbf40 | 219 | * @note This operator can also be used to change or get the value of an individual LED. |
roysandberg | 4:ab6b451bbf40 | 220 | * For example: |
roysandberg | 4:ab6b451bbf40 | 221 | * @code |
roysandberg | 4:ab6b451bbf40 | 222 | * TLC5955Mux[0][0] = 0xFFF; |
roysandberg | 4:ab6b451bbf40 | 223 | * @endcode |
roysandberg | 4:ab6b451bbf40 | 224 | */ |
roysandberg | 4:ab6b451bbf40 | 225 | unsigned short* operator[](int index); |
roysandberg | 4:ab6b451bbf40 | 226 | |
roysandberg | 4:ab6b451bbf40 | 227 | private: |
roysandberg | 4:ab6b451bbf40 | 228 | // Virtual function overriden from TLC5955 class |
roysandberg | 4:ab6b451bbf40 | 229 | virtual void setNextData(); |
roysandberg | 4:ab6b451bbf40 | 230 | |
roysandberg | 4:ab6b451bbf40 | 231 | // Number of rows |
roysandberg | 4:ab6b451bbf40 | 232 | const int rows; |
roysandberg | 4:ab6b451bbf40 | 233 | |
roysandberg | 4:ab6b451bbf40 | 234 | // Function to set the current row |
roysandberg | 4:ab6b451bbf40 | 235 | void (*SetRows)(int); |
roysandberg | 4:ab6b451bbf40 | 236 | |
roysandberg | 4:ab6b451bbf40 | 237 | // The current row |
roysandberg | 4:ab6b451bbf40 | 238 | volatile int currentIndex; |
roysandberg | 4:ab6b451bbf40 | 239 | }; |
roysandberg | 4:ab6b451bbf40 | 240 | |
roysandberg | 4:ab6b451bbf40 | 241 | #endif |