Library for TM1640 LED controller Initial version

Dependents:   mbed_TM1640

See here for more information.

Committer:
wim
Date:
Thu Jan 28 19:25:49 2016 +0000
Revision:
0:7e120ffa56d3
Library for TM1640 LED controller; Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:7e120ffa56d3 1 /* mbed TM1640 Library, for TM1640 LED controller
wim 0:7e120ffa56d3 2 * Copyright (c) 2016, v01: WH, Initial version
wim 0:7e120ffa56d3 3 *
wim 0:7e120ffa56d3 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 0:7e120ffa56d3 5 * of this software and associated documentation files (the "Software"), to deal
wim 0:7e120ffa56d3 6 * in the Software without restriction, inclumosig without limitation the rights
wim 0:7e120ffa56d3 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 0:7e120ffa56d3 8 * copies of the Software, and to permit persons to whom the Software is
wim 0:7e120ffa56d3 9 * furnished to do so, subject to the following conditions:
wim 0:7e120ffa56d3 10 *
wim 0:7e120ffa56d3 11 * The above copyright notice and this permission notice shall be included in
wim 0:7e120ffa56d3 12 * all copies or substantial portions of the Software.
wim 0:7e120ffa56d3 13 *
wim 0:7e120ffa56d3 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 0:7e120ffa56d3 15 * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 0:7e120ffa56d3 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 0:7e120ffa56d3 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 0:7e120ffa56d3 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:7e120ffa56d3 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 0:7e120ffa56d3 20 * THE SOFTWARE.
wim 0:7e120ffa56d3 21 */
wim 0:7e120ffa56d3 22
wim 0:7e120ffa56d3 23 #ifndef TM1640_H
wim 0:7e120ffa56d3 24 #define TM1640_H
wim 0:7e120ffa56d3 25
wim 0:7e120ffa56d3 26 // Select one of the testboards for TM1640 LED controller
wim 0:7e120ffa56d3 27 #include "TM1640_Config.h"
wim 0:7e120ffa56d3 28
wim 0:7e120ffa56d3 29 /** An interface for driving TM1640 LED controller
wim 0:7e120ffa56d3 30 *
wim 0:7e120ffa56d3 31 * @code
wim 0:7e120ffa56d3 32 * #include "mbed.h"
wim 0:7e120ffa56d3 33 * #include "TM1640.h"
wim 0:7e120ffa56d3 34 *
wim 0:7e120ffa56d3 35 * DisplayData_t size is 16 bytes (16 grids @ 8 segments)
wim 0:7e120ffa56d3 36 * TM1640::DisplayData_t all_str = {0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF};
wim 0:7e120ffa56d3 37 *
wim 0:7e120ffa56d3 38 * // TM1640 declaration
wim 0:7e120ffa56d3 39 * TM1640 TM1640(p5,p7);
wim 0:7e120ffa56d3 40 *
wim 0:7e120ffa56d3 41 * int main() {
wim 0:7e120ffa56d3 42 * TM1640.cls();
wim 0:7e120ffa56d3 43 * TM1640.writeData(all_str);
wim 0:7e120ffa56d3 44 * wait(1);
wim 0:7e120ffa56d3 45 * TM1640.setBrightness(TM1640_BRT0);
wim 0:7e120ffa56d3 46 * wait(1);
wim 0:7e120ffa56d3 47 * TM1640.setBrightness(TM1640_BRT3);
wim 0:7e120ffa56d3 48 *
wim 0:7e120ffa56d3 49 * while (1) {
wim 0:7e120ffa56d3 50 * TM1640.cls();
wim 0:7e120ffa56d3 51 * wait(0.5);
wim 0:7e120ffa56d3 52 * TM1640.writeData(all_str);
wim 0:7e120ffa56d3 53 * wait(0.5);
wim 0:7e120ffa56d3 54 * }
wim 0:7e120ffa56d3 55 * }
wim 0:7e120ffa56d3 56 * @endcode
wim 0:7e120ffa56d3 57 */
wim 0:7e120ffa56d3 58
wim 0:7e120ffa56d3 59
wim 0:7e120ffa56d3 60 //TM1640 Display data
wim 0:7e120ffa56d3 61 #define TM1640_MAX_NR_GRIDS 16
wim 0:7e120ffa56d3 62 #define TM1640_BYTES_PER_GRID 1
wim 0:7e120ffa56d3 63
wim 0:7e120ffa56d3 64 //Memory size in bytes for Display
wim 0:7e120ffa56d3 65 #define TM1640_DISPLAY_MEM (TM1640_MAX_NR_GRIDS * TM1640_BYTES_PER_GRID)
wim 0:7e120ffa56d3 66
wim 0:7e120ffa56d3 67 //Reserved bits for commands
wim 0:7e120ffa56d3 68 #define TM1640_CMD_MSK 0xC0
wim 0:7e120ffa56d3 69
wim 0:7e120ffa56d3 70 //Data setting commands
wim 0:7e120ffa56d3 71 #define TM1640_DATA_SET_CMD 0x40
wim 0:7e120ffa56d3 72 #define TM1640_DATA_WR 0x00
wim 0:7e120ffa56d3 73 #define TM1640_ADDR_INC 0x00
wim 0:7e120ffa56d3 74 #define TM1640_ADDR_FIXED 0x04
wim 0:7e120ffa56d3 75 #define TM1640_MODE_NORM 0x00
wim 0:7e120ffa56d3 76 #define TM1640_MODE_TEST 0x08
wim 0:7e120ffa56d3 77
wim 0:7e120ffa56d3 78 //Address setting commands
wim 0:7e120ffa56d3 79 #define TM1640_ADDR_SET_CMD 0xC0
wim 0:7e120ffa56d3 80 #define TM1640_ADDR_MSK 0x0F
wim 0:7e120ffa56d3 81
wim 0:7e120ffa56d3 82 //Display control commands
wim 0:7e120ffa56d3 83 #define TM1640_DSP_CTRL_CMD 0x80
wim 0:7e120ffa56d3 84 #define TM1640_BRT_MSK 0x07
wim 0:7e120ffa56d3 85 #define TM1640_BRT0 0x00 //Pulsewidth 1/16
wim 0:7e120ffa56d3 86 #define TM1640_BRT1 0x01
wim 0:7e120ffa56d3 87 #define TM1640_BRT2 0x02
wim 0:7e120ffa56d3 88 #define TM1640_BRT3 0x03
wim 0:7e120ffa56d3 89 #define TM1640_BRT4 0x04
wim 0:7e120ffa56d3 90 #define TM1640_BRT5 0x05
wim 0:7e120ffa56d3 91 #define TM1640_BRT6 0x06
wim 0:7e120ffa56d3 92 #define TM1640_BRT7 0x07 //Pulsewidth 14/16
wim 0:7e120ffa56d3 93
wim 0:7e120ffa56d3 94 #define TM1640_BRT_DEF TM1640_BRT3
wim 0:7e120ffa56d3 95
wim 0:7e120ffa56d3 96 #define TM1640_DSP_OFF 0x00
wim 0:7e120ffa56d3 97 #define TM1640_DSP_ON 0x08
wim 0:7e120ffa56d3 98
wim 0:7e120ffa56d3 99
wim 0:7e120ffa56d3 100 /** A class for driving TM1640 LED controller
wim 0:7e120ffa56d3 101 *
wim 0:7e120ffa56d3 102 * @brief Supports 16 Grids @ 8 Segments.
wim 0:7e120ffa56d3 103 * Serial bus interface device.
wim 0:7e120ffa56d3 104 */
wim 0:7e120ffa56d3 105 class TM1640 {
wim 0:7e120ffa56d3 106 public:
wim 0:7e120ffa56d3 107
wim 0:7e120ffa56d3 108 /** Datatype for displaydata */
wim 0:7e120ffa56d3 109 typedef char DisplayData_t[TM1640_DISPLAY_MEM];
wim 0:7e120ffa56d3 110
wim 0:7e120ffa56d3 111 /** Constructor for class for driving TM1640 LED controller
wim 0:7e120ffa56d3 112 *
wim 0:7e120ffa56d3 113 * @brief Supports 16 Grids @ 8 segments.
wim 0:7e120ffa56d3 114 * Serial bus interface device.
wim 0:7e120ffa56d3 115 *
wim 0:7e120ffa56d3 116 * @param PinName mosi Serial bus MOSI pin
wim 0:7e120ffa56d3 117 * @param PinName sclk Serial bus SCLK pin
wim 0:7e120ffa56d3 118 */
wim 0:7e120ffa56d3 119 TM1640(PinName mosi, PinName sclk);
wim 0:7e120ffa56d3 120
wim 0:7e120ffa56d3 121 /** Clear the screen and locate to 0
wim 0:7e120ffa56d3 122 */
wim 0:7e120ffa56d3 123 void cls();
wim 0:7e120ffa56d3 124
wim 0:7e120ffa56d3 125 /** Write databyte to TM1640
wim 0:7e120ffa56d3 126 * @param char data byte written at given address
wim 0:7e120ffa56d3 127 * @param int address display memory location to write byte
wim 0:7e120ffa56d3 128 * @return none
wim 0:7e120ffa56d3 129 */
wim 0:7e120ffa56d3 130 void writeData(char data, int address);
wim 0:7e120ffa56d3 131
wim 0:7e120ffa56d3 132 /** Write Display datablock to TM1640
wim 0:7e120ffa56d3 133 * @param DisplayData_t data Array of TM1640_DISPLAY_MEM (=16) bytes for displaydata
wim 0:7e120ffa56d3 134 * @param length number bytes to write (valid range 0..(TM1640_MAX_NR_GRIDS * TM1640_BYTES_PER_GRID) (=16), when starting at address 0)
wim 0:7e120ffa56d3 135 * @param int address display memory location to write bytes (default = 0)
wim 0:7e120ffa56d3 136 * @return none
wim 0:7e120ffa56d3 137 */
wim 0:7e120ffa56d3 138 void writeData(DisplayData_t data, int length = (TM1640_MAX_NR_GRIDS * TM1640_BYTES_PER_GRID), int address = 0);
wim 0:7e120ffa56d3 139
wim 0:7e120ffa56d3 140 /** Set Brightness
wim 0:7e120ffa56d3 141 *
wim 0:7e120ffa56d3 142 * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)
wim 0:7e120ffa56d3 143 * @return none
wim 0:7e120ffa56d3 144 */
wim 0:7e120ffa56d3 145 void setBrightness(char brightness = TM1640_BRT_DEF);
wim 0:7e120ffa56d3 146
wim 0:7e120ffa56d3 147 /** Set the Display mode On/off
wim 0:7e120ffa56d3 148 *
wim 0:7e120ffa56d3 149 * @param bool display mode
wim 0:7e120ffa56d3 150 */
wim 0:7e120ffa56d3 151 void setDisplay(bool on);
wim 0:7e120ffa56d3 152
wim 0:7e120ffa56d3 153 private:
wim 0:7e120ffa56d3 154 DigitalOut _mosi;
wim 0:7e120ffa56d3 155 DigitalOut _sclk;
wim 0:7e120ffa56d3 156 char _display;
wim 0:7e120ffa56d3 157 char _bright;
wim 0:7e120ffa56d3 158
wim 0:7e120ffa56d3 159 /** Init the SPI interface and the controller
wim 0:7e120ffa56d3 160 * @param none
wim 0:7e120ffa56d3 161 * @return none
wim 0:7e120ffa56d3 162 */
wim 0:7e120ffa56d3 163 void _init();
wim 0:7e120ffa56d3 164
wim 0:7e120ffa56d3 165
wim 0:7e120ffa56d3 166 /** Generate Start condition for TM1640
wim 0:7e120ffa56d3 167 * @param none
wim 0:7e120ffa56d3 168 * @return none
wim 0:7e120ffa56d3 169 */
wim 0:7e120ffa56d3 170 void _start();
wim 0:7e120ffa56d3 171
wim 0:7e120ffa56d3 172 /** Generate Stop condition for TM1640
wim 0:7e120ffa56d3 173 * @param none
wim 0:7e120ffa56d3 174 * @return none
wim 0:7e120ffa56d3 175 */
wim 0:7e120ffa56d3 176 void _stop();
wim 0:7e120ffa56d3 177
wim 0:7e120ffa56d3 178 /** Send byte to TM1640
wim 0:7e120ffa56d3 179 * @param int data
wim 0:7e120ffa56d3 180 * @return none
wim 0:7e120ffa56d3 181 */
wim 0:7e120ffa56d3 182 void _write(int data);
wim 0:7e120ffa56d3 183
wim 0:7e120ffa56d3 184 /** Write command and parameter to TM1640
wim 0:7e120ffa56d3 185 * @param int cmd Command byte
wim 0:7e120ffa56d3 186 * &Param int data Parameters for command
wim 0:7e120ffa56d3 187 * @return none
wim 0:7e120ffa56d3 188 */
wim 0:7e120ffa56d3 189 void _writeCmd(int cmd, int data);
wim 0:7e120ffa56d3 190 };
wim 0:7e120ffa56d3 191
wim 0:7e120ffa56d3 192
wim 0:7e120ffa56d3 193 #if (LM1640_TEST == 1)
wim 0:7e120ffa56d3 194 // Derived class for TM1640 used in LM1640 display unit
wim 0:7e120ffa56d3 195 //
wim 0:7e120ffa56d3 196
wim 0:7e120ffa56d3 197 #include "Font_7Seg.h"
wim 0:7e120ffa56d3 198
wim 0:7e120ffa56d3 199 #define LM1640_NR_GRIDS 16
wim 0:7e120ffa56d3 200 #define LM1640_NR_DIGITS 16
wim 0:7e120ffa56d3 201 #define LM1640_NR_UDC 8
wim 0:7e120ffa56d3 202
wim 0:7e120ffa56d3 203 /** Constructor for class for driving TM1640 controller as used in LM1640
wim 0:7e120ffa56d3 204 *
wim 0:7e120ffa56d3 205 * @brief Supports 16 Digits of 7 Segments + DP.
wim 0:7e120ffa56d3 206 *
wim 0:7e120ffa56d3 207 * @param PinName mosi Serial bus MOSI pin
wim 0:7e120ffa56d3 208 * @param PinName sclk Serial bus SCLK pin
wim 0:7e120ffa56d3 209 */
wim 0:7e120ffa56d3 210 class TM1640_LM1640 : public TM1640, public Stream {
wim 0:7e120ffa56d3 211 public:
wim 0:7e120ffa56d3 212
wim 0:7e120ffa56d3 213 /** Enums for Icons */
wim 0:7e120ffa56d3 214 // Grid encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 0:7e120ffa56d3 215 enum Icon {
wim 0:7e120ffa56d3 216 DP1 = ( 1<<24) | S7_DP1, /**< Decimal Point 1 */
wim 0:7e120ffa56d3 217 DP2 = ( 2<<24) | S7_DP2, /**< Decimal Point 2 */
wim 0:7e120ffa56d3 218 DP3 = ( 3<<24) | S7_DP3, /**< Decimal Point 3 */
wim 0:7e120ffa56d3 219 DP4 = ( 4<<24) | S7_DP4, /**< Decimal Point 4 */
wim 0:7e120ffa56d3 220 DP5 = ( 5<<24) | S7_DP5, /**< Decimal Point 5 */
wim 0:7e120ffa56d3 221 DP6 = ( 6<<24) | S7_DP6, /**< Decimal Point 6 */
wim 0:7e120ffa56d3 222 DP7 = ( 7<<24) | S7_DP7, /**< Decimal Point 7 */
wim 0:7e120ffa56d3 223 DP8 = ( 8<<24) | S7_DP8, /**< Decimal Point 8 */
wim 0:7e120ffa56d3 224 DP9 = ( 9<<24) | S7_DP9, /**< Decimal Point 9 */
wim 0:7e120ffa56d3 225 DP10 = (10<<24) | S7_DP10, /**< Decimal Point 10 */
wim 0:7e120ffa56d3 226 DP11 = (11<<24) | S7_DP11, /**< Decimal Point 11 */
wim 0:7e120ffa56d3 227 DP12 = (12<<24) | S7_DP12, /**< Decimal Point 12 */
wim 0:7e120ffa56d3 228 DP13 = (13<<24) | S7_DP13, /**< Decimal Point 13 */
wim 0:7e120ffa56d3 229 DP14 = (14<<24) | S7_DP14, /**< Decimal Point 14 */
wim 0:7e120ffa56d3 230 DP15 = (15<<24) | S7_DP15, /**< Decimal Point 15 */
wim 0:7e120ffa56d3 231 DP16 = (16<<24) | S7_DP16 /**< Decimal Point 16 */
wim 0:7e120ffa56d3 232
wim 0:7e120ffa56d3 233 };
wim 0:7e120ffa56d3 234
wim 0:7e120ffa56d3 235 typedef char UDCData_t[LM1640_NR_UDC];
wim 0:7e120ffa56d3 236
wim 0:7e120ffa56d3 237 /** Constructor for class for driving TM1640 LED controller as used in LM1640
wim 0:7e120ffa56d3 238 *
wim 0:7e120ffa56d3 239 * @brief Supports 16 Digits of 7 Segments + DP.
wim 0:7e120ffa56d3 240 *
wim 0:7e120ffa56d3 241 * @param PinName mosi Serial bus MOSI pin
wim 0:7e120ffa56d3 242 * @param PinName sclk Serial bus SCLK pin
wim 0:7e120ffa56d3 243 */
wim 0:7e120ffa56d3 244 TM1640_LM1640(PinName mosi, PinName sclk);
wim 0:7e120ffa56d3 245
wim 0:7e120ffa56d3 246 #if DOXYGEN_ONLY
wim 0:7e120ffa56d3 247 /** Write a character to the Display
wim 0:7e120ffa56d3 248 *
wim 0:7e120ffa56d3 249 * @param c The character to write to the display
wim 0:7e120ffa56d3 250 */
wim 0:7e120ffa56d3 251 int putc(int c);
wim 0:7e120ffa56d3 252
wim 0:7e120ffa56d3 253 /** Write a formatted string to the Display
wim 0:7e120ffa56d3 254 *
wim 0:7e120ffa56d3 255 * @param format A printf-style format string, followed by the
wim 0:7e120ffa56d3 256 * variables to use in formatting the string.
wim 0:7e120ffa56d3 257 */
wim 0:7e120ffa56d3 258 int printf(const char* format, ...);
wim 0:7e120ffa56d3 259 #endif
wim 0:7e120ffa56d3 260
wim 0:7e120ffa56d3 261 /** Locate cursor to a screen column
wim 0:7e120ffa56d3 262 *
wim 0:7e120ffa56d3 263 * @param column The horizontal position from the left, indexed from 0
wim 0:7e120ffa56d3 264 */
wim 0:7e120ffa56d3 265 void locate(int column);
wim 0:7e120ffa56d3 266
wim 0:7e120ffa56d3 267 /** Clear the screen and locate to 0
wim 0:7e120ffa56d3 268 * @param bool clrAll Clear Icons also (default = false)
wim 0:7e120ffa56d3 269 */
wim 0:7e120ffa56d3 270 void cls(bool clrAll = false);
wim 0:7e120ffa56d3 271
wim 0:7e120ffa56d3 272 /** Set Icon
wim 0:7e120ffa56d3 273 *
wim 0:7e120ffa56d3 274 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 0:7e120ffa56d3 275 * @return none
wim 0:7e120ffa56d3 276 */
wim 0:7e120ffa56d3 277 void setIcon(Icon icon);
wim 0:7e120ffa56d3 278
wim 0:7e120ffa56d3 279 /** Clr Icon
wim 0:7e120ffa56d3 280 *
wim 0:7e120ffa56d3 281 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 0:7e120ffa56d3 282 * @return none
wim 0:7e120ffa56d3 283 */
wim 0:7e120ffa56d3 284 void clrIcon(Icon icon);
wim 0:7e120ffa56d3 285
wim 0:7e120ffa56d3 286 /** Set User Defined Characters (UDC)
wim 0:7e120ffa56d3 287 *
wim 0:7e120ffa56d3 288 * @param unsigned char udc_idx The Index of the UDC (0..7)
wim 0:7e120ffa56d3 289 * @param int udc_data The bitpattern for the UDC (16 bits)
wim 0:7e120ffa56d3 290 */
wim 0:7e120ffa56d3 291 void setUDC(unsigned char udc_idx, int udc_data);
wim 0:7e120ffa56d3 292
wim 0:7e120ffa56d3 293
wim 0:7e120ffa56d3 294 /** Number of screen columns
wim 0:7e120ffa56d3 295 *
wim 0:7e120ffa56d3 296 * @param none
wim 0:7e120ffa56d3 297 * @return columns
wim 0:7e120ffa56d3 298 */
wim 0:7e120ffa56d3 299 int columns();
wim 0:7e120ffa56d3 300
wim 0:7e120ffa56d3 301 /** Write databyte to TM1640
wim 0:7e120ffa56d3 302 * @param char data byte written at given address
wim 0:7e120ffa56d3 303 * @param int address display memory location to write byte
wim 0:7e120ffa56d3 304 * @return none
wim 0:7e120ffa56d3 305 */
wim 0:7e120ffa56d3 306 void writeData(char data, int address){
wim 0:7e120ffa56d3 307 TM1640::writeData(data, address);
wim 0:7e120ffa56d3 308 }
wim 0:7e120ffa56d3 309
wim 0:7e120ffa56d3 310 /** Write Display datablock to TM1640
wim 0:7e120ffa56d3 311 * @param DisplayData_t data Array of TM1640_DISPLAY_MEM (=16) bytes for displaydata
wim 0:7e120ffa56d3 312 * @param length number bytes to write (valid range 0..(LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID) (=16), when starting at address 0)
wim 0:7e120ffa56d3 313 * @param int address display memory location to write bytes (default = 0)
wim 0:7e120ffa56d3 314 * @return none
wim 0:7e120ffa56d3 315 */
wim 0:7e120ffa56d3 316 void writeData(DisplayData_t data, int length = (LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID), int address = 0) {
wim 0:7e120ffa56d3 317 TM1640::writeData(data, length, address);
wim 0:7e120ffa56d3 318 }
wim 0:7e120ffa56d3 319
wim 0:7e120ffa56d3 320 protected:
wim 0:7e120ffa56d3 321 // Stream implementation functions
wim 0:7e120ffa56d3 322 virtual int _putc(int value);
wim 0:7e120ffa56d3 323 virtual int _getc();
wim 0:7e120ffa56d3 324
wim 0:7e120ffa56d3 325 private:
wim 0:7e120ffa56d3 326 int _column;
wim 0:7e120ffa56d3 327 int _columns;
wim 0:7e120ffa56d3 328
wim 0:7e120ffa56d3 329 DisplayData_t _displaybuffer;
wim 0:7e120ffa56d3 330 UDCData_t _UDC_7S;
wim 0:7e120ffa56d3 331 };
wim 0:7e120ffa56d3 332 #endif
wim 0:7e120ffa56d3 333
wim 0:7e120ffa56d3 334 #endif