Library for TM1650 LED controller (32 LEDs max, 28 keys max)

Dependents:   mbed_TM1650

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TM1650.h Source File

TM1650.h

00001 /* mbed TM1650 Library, for TM1650 LED controller
00002  * Copyright (c) 2017, 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 TM1650_H
00024 #define TM1650_H
00025 
00026 // Select one of the testboards for TM1650 LED controller
00027 #include "TM1650_Config.h"
00028 
00029 /** An interface for driving TM1650 LED controller
00030  *
00031  * @code
00032  * #include "mbed.h"
00033  * #include "TM1650.h" 
00034  * 
00035  * Serial pc(USBTX, USBRX);
00036  *
00037  * //DisplayData_t size is 4 bytes (4 grids @ 8 segments)
00038  * TM1650::DisplayData_t all_str  = {0xFF, 0xFF, 0xFF, 0xFF};  
00039  *
00040  * // KeyData_t size is 1 bytes  
00041  * TM1650::KeyData_t keydata; 
00042  *
00043  * // TM1650 declaration, Select the desired type in TM1650_Config.h
00044  * TM1650_MEIBAI MEIBAI(p6, p7);       //LPC1768
00045  * //TM1650_MEIBAI MEIBAI(D9, D10);      //F401
00046  *
00047  * int main() {
00048  *   MEIBAI.cls(); 
00049  *   MEIBAI.writeData(all_str);
00050  *   wait(1);
00051  *   MEIBAI.setBrightness(TM1650_BRT0);
00052  *   wait(1);
00053  *   MEIBAI.setBrightness(TM1650_BRT3);
00054  *
00055  *   while (1) {
00056  *     MEIBAI.cls(); 
00057  *     wait(0.5); 
00058  *     MEIBAI.writeData(all_str);
00059  *     wait(1.0);
00060  *     MEIBAI.cls(); 
00061  *     MEIBAI.printf(" HI ");  
00062  *     wait(1.0); 
00063  *
00064  *     // Check and read keydata
00065  *     if (MEIBAI.getKeys(&keydata)) {
00066  *       pc.printf("Keydata = 0x%02x\r\n", keydata);
00067  *
00068  *       if (keydata == TM1650_SW1_BIT) { //sw1  
00069  *         MEIBAI.cls(); 
00070  *         MEIBAI.printf("--01"); 
00071  *       }  
00072  *     } // Check keydata
00073  *   } // while 
00074  * }
00075  * @endcode
00076  */
00077 
00078 
00079 //TM1650 Display data
00080 #define TM1650_MAX_NR_GRIDS    4
00081 #define TM1650_BYTES_PER_GRID  1
00082 
00083 //Significant bits Keymatrix data
00084 //#define TM1638_KEY_MSK      0xFF 
00085 
00086 //Memory size in bytes for Display and Keymatrix
00087 #define TM1650_DISPLAY_MEM  (TM1650_MAX_NR_GRIDS * TM1650_BYTES_PER_GRID)
00088 #define TM1650_KEY_MEM         1
00089 
00090 //Data write command
00091 //Valid data addresses are 0x68, 0x6A, 0x6C, 0x6E
00092 #define TM1650_DATA_WR_CMD  0x68
00093 #define TM1650_ADDR_MSK     0x03 //0..3
00094 
00095 //Display control command
00096 #define TM1650_DSP_CTRL_CMD 0x48
00097 
00098 #define TM1650_BRT_MSK      0x70
00099 //#define TM1650_BRT_SHFT        4
00100 #define TM1650_BRT0         0x10 //Pulsewidth 1/16
00101 #define TM1650_BRT1         0x20
00102 #define TM1650_BRT2         0x30
00103 #define TM1650_BRT3         0x40
00104 #define TM1650_BRT4         0x50
00105 #define TM1650_BRT5         0x60
00106 #define TM1650_BRT6         0x70
00107 #define TM1650_BRT7         0x00 //Pulsewidth 15/16
00108 
00109 #define TM1650_BRT_DEF      TM1650_BRT3
00110 
00111 #define TM1650_DSP_8S       0x00
00112 #define TM1650_DSP_7S       0x08
00113 
00114 #define TM1650_DSP_OFF      0x00
00115 #define TM1650_DSP_ON       0x01
00116 
00117 
00118 //Keydata read command
00119 #define TM1650_KEY_RD_CMD   0x49
00120 
00121 //Access to 28 Switches
00122 //d7 d6 d5 d4 d3 d2 d1 d0
00123 // .  1  .  .  .  1  .  .   = Valid Key
00124 #define TM1650_SW_MSK       0x44
00125 
00126 //A -> Dig1 .. Dig4
00127 #define TM1650_SW1_BIT      0x44
00128 #define TM1650_SW2_BIT      0x45
00129 #define TM1650_SW3_BIT      0x46
00130 #define TM1650_SW4_BIT      0x47
00131 //B -> Dig1 .. Dig4
00132 #define TM1650_SW5_BIT      0x4C
00133 #define TM1650_SW6_BIT      0x4D
00134 #define TM1650_SW7_BIT      0x4E
00135 #define TM1650_SW8_BIT      0x4F
00136 //C -> Dig1 .. Dig4
00137 #define TM1650_SW9_BIT      0x54
00138 #define TM1650_SW10_BIT     0x55
00139 #define TM1650_SW11_BIT     0x56
00140 #define TM1650_SW12_BIT     0x57
00141 //D -> Dig1 .. Dig4
00142 #define TM1650_SW13_BIT     0x5C
00143 #define TM1650_SW14_BIT     0x5D
00144 #define TM1650_SW15_BIT     0x5E
00145 #define TM1650_SW16_BIT     0x5F
00146 //E -> Dig1 .. Dig4
00147 #define TM1650_SW17_BIT     0x64
00148 #define TM1650_SW18_BIT     0x65
00149 #define TM1650_SW19_BIT     0x66
00150 #define TM1650_SW20_BIT     0x67
00151 //F -> Dig1 .. Dig4
00152 #define TM1650_SW21_BIT     0x6C
00153 #define TM1650_SW22_BIT     0x6D
00154 #define TM1650_SW23_BIT     0x6E
00155 #define TM1650_SW24_BIT     0x6F
00156 //G -> Dig1 .. Dig4
00157 #define TM1650_SW25_BIT     0x74
00158 #define TM1650_SW26_BIT     0x75
00159 #define TM1650_SW27_BIT     0x76
00160 #define TM1650_SW28_BIT     0x77
00161 
00162 
00163 
00164 /** A class for driving TM1650 LED controller
00165  *
00166  * @brief Supports 4 Grids @ 8 Segments and upto 28 Keys. 
00167  *        Serial bus interface device. 
00168  */
00169 class TM1650 {
00170  public:
00171 
00172   /** Datatype for displaydata */
00173   typedef char DisplayData_t[TM1650_DISPLAY_MEM];
00174 
00175   /** Datatypes for keymatrix data */
00176   typedef char KeyData_t;
00177 
00178  /** Constructor for class for driving TM1650 LED controller
00179   *
00180   * @brief Supports 4 Grids @ 8 segments and 28 Keys. 
00181   *        Serial bus interface device. 
00182   *
00183   *  @param  PinName dio Serial bus DIO pin
00184   *  @param  PinName sck Serial bus CLK pin 
00185   */
00186   TM1650(PinName dio, PinName clk);
00187 
00188 
00189   /** Clear the screen and locate to 0
00190    */ 
00191   void cls();  
00192 
00193   /** Write databyte to TM1650
00194    *  @param  char data byte written at given address
00195    *  @param  int address display memory location to write byte
00196    *  @return none
00197    */ 
00198    void writeData(char data, int address); 
00199 
00200    /** Write Display datablock to TM1650
00201     *  @param  DisplayData_t data Array of TM1650_DISPLAY_MEM (=4) bytes for displaydata
00202     *  @param  length number bytes to write (valid range 0..(TM1650_MAX_NR_GRIDS * TM1650_BYTES_PER_GRID) (=4), when starting at address 0)  
00203     *  @param  int address display memory location to write bytes (default = 0) 
00204     *  @return none
00205     */ 
00206     void writeData(DisplayData_t data, int length = (TM1650_MAX_NR_GRIDS * TM1650_BYTES_PER_GRID), int address = 0);
00207 
00208   /** Read keydata block from TM1650
00209    *
00210    *  @param  *keydata Ptr to bytes for keydata
00211    *  @return bool keypress True when at least one key was pressed
00212    */   
00213   bool getKeys(KeyData_t *keydata);
00214 
00215   /** Set Brightness
00216     *
00217     * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/16 dutycycle)  
00218     * @return none
00219     */
00220   void setBrightness(char brightness = TM1650_BRT_DEF);
00221   
00222   /** Set the Display mode On/off
00223     *
00224     * @param bool display mode
00225     * @return none
00226     */
00227   void setDisplay(bool on);
00228   
00229  private:  
00230     DigitalInOut _dio;
00231     DigitalOut   _clk;  
00232 
00233     char _bright;
00234     char _segment;
00235     char _display;    
00236   
00237   /** Init the Serial interface and the controller
00238     * @param  none
00239     * @return none
00240     */ 
00241     void _init();
00242 
00243   /** Generate Start condition for TM1650
00244     *  @param  none
00245     *  @return none
00246     */ 
00247     void _start();
00248   
00249   /** Generate Stop condition for TM1650
00250     *  @param  none
00251     *  @return none
00252     */ 
00253     void _stop();
00254 
00255   /** Send byte to TM1650
00256     *  @param  int data
00257     *  @return none
00258     */ 
00259     void _write(int data);
00260 
00261   /** Read byte from TM1650
00262     *  @param  none  
00263     *  @return read byte 
00264     */ 
00265     char _read();
00266 
00267   /** Write command and parameter to TM1650
00268     *  @param  int cmd Command byte
00269     *  @Param  int data Parameters for command
00270     *  @return none
00271     */ 
00272     void _writeCmd(int cmd, int data);  
00273 };
00274 
00275 #if (MEIBAI_TEST == 1) 
00276 // Derived class for TM1650 used in MEIBAI display unit with 4 Digits and 3 keys
00277 //
00278 
00279 #include "Font_7Seg.h"
00280 
00281 #define MEIBAI_NR_GRIDS  4
00282 #define MEIBAI_NR_DIGITS 4
00283 #define MEIBAI_NR_UDC    8
00284 
00285 
00286 /** Constructor for class for driving TM1650 controller as used in MEIBAI
00287   *
00288   *  @brief Supports 4 Digits of 7 Segments + DP. 
00289   *         Also Supports 3 Keys. Serial bus interface device. 
00290   *  
00291   *  @param  PinName dio Serial bus DIO pin
00292   *  @param  PinName sck Serial bus CLK pin  
00293   */
00294 class TM1650_MEIBAI : public TM1650, public Stream {
00295  public:
00296 
00297   /** Enums for Icons */
00298   //  Grid encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00299   enum Icon {
00300     DP1   = ( 1<<24) | S7_DP1,  /**<  Digit 1 */
00301     DP2   = ( 2<<24) | S7_DP2,  /**<  Digit 2 */
00302     DP3   = ( 3<<24) | S7_DP3,  /**<  Digit 3 */
00303     DP4   = ( 4<<24) | S7_DP4,  /**<  Digit 4 */
00304   };
00305   
00306   typedef char UDCData_t[MEIBAI_NR_UDC];
00307 
00308 
00309  /** Constructor for class for driving TM1650 LED controller
00310   *
00311   * @brief Supports 4 Digits of 7 Segments + DP.
00312   *        Also Supports 3 Keys. Serial bus interface device. 
00313   *
00314   *  @param  PinName dio Serial bus DIO pin
00315   *  @param  PinName sck Serial bus CLK pin
00316   */
00317   TM1650_MEIBAI(PinName dio, PinName clk);
00318 
00319   
00320 #if DOXYGEN_ONLY
00321     /** Write a character to the Display
00322      *
00323      * @param c The character to write to the display
00324      * @return c
00325      */
00326     int putc(int c);
00327 
00328     /** Write a formatted string to the Display
00329      *
00330      * @param format A printf-style format string, followed by the
00331      *               variables to use in formatting the string.
00332      * @return chars written
00333      */
00334     int printf(const char* format, ...);   
00335 #endif
00336 
00337     /** Locate cursor to a screen column
00338      *
00339      * @param column  The horizontal position from the left, indexed from 0
00340      * @return none     
00341      */
00342     void locate(int column);
00343     
00344     /** Clear the screen and locate to 0
00345      * @param bool clrAll Clear Icons also (default = false)
00346      * @return none     
00347      */
00348     void cls(bool clrAll = false);
00349 
00350     /** Set Icon
00351      *
00352      * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00353      * @return none
00354      */
00355     void setIcon(Icon icon);
00356 
00357     /** Clr Icon
00358      *
00359      * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00360      * @return none
00361      */
00362     void clrIcon(Icon icon);
00363 
00364    /** Set User Defined Characters (UDC)
00365      *
00366      * @param unsigned char udc_idx   The Index of the UDC (0..7)
00367      * @param int udc_data            The bitpattern for the UDC (16 bits)
00368      * @return none            
00369      */
00370     void setUDC(unsigned char udc_idx, int udc_data);
00371 
00372 
00373    /** Number of screen columns
00374     *
00375     * @param none
00376     * @return columns
00377     */
00378     int columns();   
00379 
00380    /** Write databyte to TM1650
00381      *  @param  char data byte written at given address
00382      *  @param  int address display memory location to write byte
00383      *  @return none
00384      */ 
00385     void writeData(char data, int address){
00386       TM1650::writeData(data, address);
00387     }        
00388 
00389    /** Write Display datablock to TM1650
00390     *  @param  DisplayData_t data Array of TM1650_DISPLAY_MEM (=4) bytes for displaydata
00391     *  @param  length number bytes to write (valid range 0..(MEIBAI_NR_GRIDS * TM1650_BYTES_PER_GRID) (=4), when starting at address 0)  
00392     *  @param  int address display memory location to write bytes (default = 0)
00393     *  @return none
00394     */   
00395     void writeData(DisplayData_t data, int length = (MEIBAI_NR_GRIDS * TM1650_BYTES_PER_GRID), int address = 0) {
00396       TM1650::writeData(data, length, address);
00397     }  
00398 
00399 protected:  
00400     // Stream implementation functions
00401     virtual int _putc(int value);
00402     virtual int _getc();
00403 
00404 private:
00405     int _column;
00406     int _columns;   
00407     
00408     DisplayData_t _displaybuffer;
00409     UDCData_t _UDC_7S; 
00410 };
00411 #endif
00412 
00413 #endif