Wim Huiskamp / TM1640

Dependents:   mbed_TM1640

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TM1640.h Source File

TM1640.h

00001 /* mbed TM1640 Library, for TM1640 LED controller
00002  * Copyright (c) 2016, v01: WH, Initial version
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, inclumosig without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #ifndef TM1640_H
00024 #define TM1640_H
00025 
00026 // Select one of the testboards for TM1640 LED controller
00027 #include "TM1640_Config.h"
00028 
00029 /** An interface for driving TM1640 LED controller
00030  *
00031  * @code
00032  * #include "mbed.h"
00033  * #include "TM1640.h" 
00034  * 
00035  * DisplayData_t size is 16 bytes (16 grids @ 8 segments)
00036  * TM1640::DisplayData_t all_str  = {0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF, 0xFF,0xFF};  
00037  *
00038   * // TM1640 declaration
00039  * TM1640 TM1640(p5,p7);
00040  *
00041  * int main() {
00042  *   TM1640.cls(); 
00043  *   TM1640.writeData(all_str);
00044  *   wait(1);
00045  *   TM1640.setBrightness(TM1640_BRT0);
00046  *   wait(1);
00047  *   TM1640.setBrightness(TM1640_BRT3);
00048  *
00049  *   while (1) {
00050  *     TM1640.cls(); 
00051  *     wait(0.5); 
00052  *     TM1640.writeData(all_str);
00053  *     wait(0.5); 
00054  *   }  
00055  * }
00056  * @endcode
00057  */
00058 
00059 
00060 //TM1640 Display data
00061 #define TM1640_MAX_NR_GRIDS   16
00062 #define TM1640_BYTES_PER_GRID  1
00063 
00064 //Memory size in bytes for Display
00065 #define TM1640_DISPLAY_MEM  (TM1640_MAX_NR_GRIDS * TM1640_BYTES_PER_GRID)
00066 
00067 //Reserved bits for commands
00068 #define TM1640_CMD_MSK      0xC0
00069 
00070 //Data setting commands
00071 #define TM1640_DATA_SET_CMD 0x40
00072 #define TM1640_DATA_WR      0x00
00073 #define TM1640_ADDR_INC     0x00
00074 #define TM1640_ADDR_FIXED   0x04
00075 #define TM1640_MODE_NORM    0x00
00076 #define TM1640_MODE_TEST    0x08
00077 
00078 //Address setting commands
00079 #define TM1640_ADDR_SET_CMD 0xC0
00080 #define TM1640_ADDR_MSK     0x0F
00081 
00082 //Display control commands
00083 #define TM1640_DSP_CTRL_CMD 0x80
00084 #define TM1640_BRT_MSK      0x07
00085 #define TM1640_BRT0         0x00 //Pulsewidth 1/16
00086 #define TM1640_BRT1         0x01
00087 #define TM1640_BRT2         0x02
00088 #define TM1640_BRT3         0x03
00089 #define TM1640_BRT4         0x04
00090 #define TM1640_BRT5         0x05
00091 #define TM1640_BRT6         0x06
00092 #define TM1640_BRT7         0x07 //Pulsewidth 14/16
00093 
00094 #define TM1640_BRT_DEF      TM1640_BRT3
00095 
00096 #define TM1640_DSP_OFF      0x00
00097 #define TM1640_DSP_ON       0x08
00098 
00099 
00100 /** A class for driving TM1640 LED controller
00101  *
00102  * @brief Supports 16 Grids @ 8 Segments. 
00103  *        Serial bus interface device. 
00104  */
00105 class TM1640 {
00106  public:
00107 
00108   /** Datatype for displaydata */
00109   typedef char DisplayData_t[TM1640_DISPLAY_MEM];
00110     
00111  /** Constructor for class for driving TM1640 LED controller
00112   *
00113   * @brief Supports 16 Grids @ 8 segments. 
00114   *        Serial bus interface device. 
00115   *
00116   *  @param  PinName mosi Serial bus MOSI pin
00117   *  @param  PinName sclk Serial bus SCLK pin  
00118   */
00119   TM1640(PinName mosi, PinName sclk);
00120       
00121   /** Clear the screen and locate to 0
00122    */ 
00123   void cls();  
00124 
00125   /** Write databyte to TM1640
00126    *  @param  char data byte written at given address
00127    *  @param  int address display memory location to write byte
00128    *  @return none
00129    */ 
00130    void writeData(char data, int address); 
00131 
00132    /** Write Display datablock to TM1640
00133     *  @param  DisplayData_t data Array of TM1640_DISPLAY_MEM (=16) bytes for displaydata
00134     *  @param  length number bytes to write (valid range 0..(TM1640_MAX_NR_GRIDS * TM1640_BYTES_PER_GRID) (=16), when starting at address 0)  
00135     *  @param  int address display memory location to write bytes (default = 0) 
00136     *  @return none
00137     */ 
00138     void writeData(DisplayData_t data, int length = (TM1640_MAX_NR_GRIDS * TM1640_BYTES_PER_GRID), int address = 0);
00139  
00140   /** Set Brightness
00141     *
00142     * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)  
00143     * @return none
00144     */
00145   void setBrightness(char brightness = TM1640_BRT_DEF);
00146   
00147   /** Set the Display mode On/off
00148     *
00149     * @param bool display mode
00150     */
00151   void setDisplay(bool on);
00152   
00153   private:  
00154     DigitalOut _mosi;
00155     DigitalOut _sclk;  
00156     char _display;
00157     char _bright; 
00158   
00159   /** Init the SPI interface and the controller
00160     * @param  none
00161     * @return none
00162     */ 
00163     void _init();
00164 
00165 
00166   /** Generate Start condition for TM1640
00167     *  @param  none
00168     *  @return none
00169     */ 
00170     void _start();
00171   
00172   /** Generate Stop condition for TM1640
00173     *  @param  none
00174     *  @return none
00175     */ 
00176     void _stop();
00177 
00178   /** Send byte to TM1640
00179     *  @param  int data
00180     *  @return none
00181     */ 
00182     void _write(int data);
00183 
00184   /** Write command and parameter to TM1640
00185     *  @param  int cmd Command byte
00186     *  &Param  int data Parameters for command
00187     *  @return none
00188     */ 
00189     void _writeCmd(int cmd, int data);  
00190 };
00191 
00192 
00193 #if (LM1640_TEST == 1) 
00194 // Derived class for TM1640 used in LM1640 display unit
00195 //
00196 
00197 #include "Font_7Seg.h"
00198 
00199 #define LM1640_NR_GRIDS  16
00200 #define LM1640_NR_DIGITS 16
00201 #define LM1640_NR_UDC    8
00202 
00203 /** Constructor for class for driving TM1640 controller as used in LM1640
00204   *
00205   *  @brief Supports 16 Digits of 7 Segments + DP.
00206   *  
00207   *  @param  PinName mosi Serial bus MOSI pin
00208   *  @param  PinName sclk Serial bus SCLK pin 
00209   */
00210 class TM1640_LM1640 : public TM1640, public Stream {
00211  public:
00212 
00213   /** Enums for Icons */
00214   //  Grid encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00215   enum Icon {
00216     DP1   = ( 1<<24) | S7_DP1,  /**<  Decimal Point 1 */
00217     DP2   = ( 2<<24) | S7_DP2,  /**<  Decimal Point 2 */
00218     DP3   = ( 3<<24) | S7_DP3,  /**<  Decimal Point 3 */
00219     DP4   = ( 4<<24) | S7_DP4,  /**<  Decimal Point 4 */
00220     DP5   = ( 5<<24) | S7_DP5,  /**<  Decimal Point 5 */
00221     DP6   = ( 6<<24) | S7_DP6,  /**<  Decimal Point 6 */
00222     DP7   = ( 7<<24) | S7_DP7,  /**<  Decimal Point 7 */
00223     DP8   = ( 8<<24) | S7_DP8,  /**<  Decimal Point 8 */   
00224     DP9   = ( 9<<24) | S7_DP9,  /**<  Decimal Point 9 */
00225     DP10  = (10<<24) | S7_DP10, /**<  Decimal Point 10 */
00226     DP11  = (11<<24) | S7_DP11, /**<  Decimal Point 11 */
00227     DP12  = (12<<24) | S7_DP12, /**<  Decimal Point 12 */
00228     DP13  = (13<<24) | S7_DP13, /**<  Decimal Point 13 */
00229     DP14  = (14<<24) | S7_DP14, /**<  Decimal Point 14 */
00230     DP15  = (15<<24) | S7_DP15, /**<  Decimal Point 15 */
00231     DP16  = (16<<24) | S7_DP16  /**<  Decimal Point 16 */  
00232 
00233   };
00234   
00235   typedef char UDCData_t[LM1640_NR_UDC];
00236   
00237  /** Constructor for class for driving TM1640 LED controller as used in LM1640
00238    *
00239    * @brief Supports 16 Digits of 7 Segments + DP.
00240    *  
00241    *  @param  PinName mosi Serial bus MOSI pin
00242    *  @param  PinName sclk Serial bus SCLK pin   
00243    */
00244   TM1640_LM1640(PinName mosi, PinName sclk);
00245 
00246 #if DOXYGEN_ONLY
00247     /** Write a character to the Display
00248      *
00249      * @param c The character to write to the display
00250      */
00251     int putc(int c);
00252 
00253     /** Write a formatted string to the Display
00254      *
00255      * @param format A printf-style format string, followed by the
00256      *               variables to use in formatting the string.
00257      */
00258     int printf(const char* format, ...);   
00259 #endif
00260 
00261      /** Locate cursor to a screen column
00262      *
00263      * @param column  The horizontal position from the left, indexed from 0
00264      */
00265     void locate(int column);
00266     
00267     /** Clear the screen and locate to 0
00268      * @param bool clrAll Clear Icons also (default = false)
00269      */
00270     void cls(bool clrAll = false);
00271 
00272     /** Set Icon
00273      *
00274      * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00275      * @return none
00276      */
00277     void setIcon(Icon icon);
00278 
00279     /** Clr Icon
00280      *
00281      * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00282      * @return none
00283      */
00284     void clrIcon(Icon icon);
00285 
00286    /** Set User Defined Characters (UDC)
00287      *
00288      * @param unsigned char udc_idx   The Index of the UDC (0..7)
00289      * @param int udc_data            The bitpattern for the UDC (16 bits)       
00290      */
00291     void setUDC(unsigned char udc_idx, int udc_data);
00292 
00293 
00294    /** Number of screen columns
00295     *
00296     * @param none
00297     * @return columns
00298     */
00299     int columns();   
00300 
00301    /** Write databyte to TM1640
00302      *  @param  char data byte written at given address
00303      *  @param  int address display memory location to write byte
00304      *  @return none
00305      */ 
00306     void writeData(char data, int address){
00307       TM1640::writeData(data, address);
00308     }        
00309 
00310    /** Write Display datablock to TM1640
00311     *  @param  DisplayData_t data Array of TM1640_DISPLAY_MEM (=16) bytes for displaydata
00312     *  @param  length number bytes to write (valid range 0..(LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID) (=16), when starting at address 0)  
00313     *  @param  int address display memory location to write bytes (default = 0) 
00314     *  @return none
00315     */   
00316     void writeData(DisplayData_t data, int length = (LM1640_NR_GRIDS * TM1640_BYTES_PER_GRID), int address = 0) {
00317       TM1640::writeData(data, length, address);
00318     }  
00319 
00320 protected:  
00321     // Stream implementation functions
00322     virtual int _putc(int value);
00323     virtual int _getc();
00324 
00325 private:
00326     int _column;
00327     int _columns;   
00328     
00329     DisplayData_t _displaybuffer;
00330     UDCData_t _UDC_7S; 
00331 };
00332 #endif
00333 
00334 #endif