TM1638 LED controller. Max 80 LEDs, Max 24 Key scan. Supports LED&KEY, QYF-TM1638 and JY-LKM1638 module.

Dependents:   mbed_TM1638 Otjimaniya RL0201-V1

See here for more information.

Revision:
4:b2bbdc58967e
Parent:
2:532ce15ea9ec
--- a/TM1638.h	Tue Jan 19 18:58:22 2016 +0000
+++ b/TM1638.h	Sun Jan 31 12:11:06 2016 +0000
@@ -1,6 +1,7 @@
 /* mbed TM1638 Library, for TM1638 LED controller
  * Copyright (c) 2015, v01: WH, Initial version
  *               2016, v02: WH, refactored display and keyboard defines 
+ *               2016, v03: WH, Added QYF-TM1638 and LKM1638, refactoring of writeData() 
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -119,9 +120,10 @@
  */
 class TM1638 {
  public:
-
-  /** Datatypes for display and keymatrix data */
+  /** Datatype for display data */
   typedef char DisplayData_t[TM1638_DISPLAY_MEM];
+  
+  /** Datatypes for keymatrix data */
   typedef char KeyData_t[TM1638_KEY_MEM];
     
  /** Constructor for class for driving TM1638 LED controller
@@ -139,19 +141,20 @@
   void cls();  
 
   /** Write databyte to TM1638
+   *  @param  char data byte written at given address
    *  @param  int address display memory location to write byte
-   *  @param  char data byte written at given address
    *  @return none
    */ 
-  void writeData(int address, char data); 
+   void writeData(char data, int address); 
+
+   /** Write Display datablock to TM1638
+    *  @param  DisplayData_t data Array of TM1638_DISPLAY_MEM (=16) bytes for displaydata
+    *  @param  length number bytes to write (valid range 0..(TM1638_MAX_NR_GRIDS * TM1638_BYTES_PER_GRID) (=16), when starting at address 0)  
+    *  @param  int address display memory location to write bytes (default = 0) 
+    *  @return none
+    */ 
+    void writeData(DisplayData_t data, int length = (TM1638_MAX_NR_GRIDS * TM1638_BYTES_PER_GRID), int address = 0);
  
- /** Write Display datablock to TM1638
-   *  @param  DisplayData_t data Array of TM1638_DISPLAY_MEM (=16) bytes for displaydata (starting at address 0)
-   *  @param  length number bytes to write (valid range 0..TM1638_DISPLAY_MEM (=16), starting at address 0)   
-   *  @return none
-   */   
-  void writeData(DisplayData_t data, int length = TM1638_DISPLAY_MEM);
-
   /** Read keydata block from TM1638
    *  @param  *keydata Ptr to Array of TM1638_KEY_MEM (=4) bytes for keydata
    *  @return bool keypress True when at least one key was pressed
@@ -164,7 +167,7 @@
 
   /** Set Brightness
     *
-    * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)  
+    * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/16 dutycycle)  
     * @return none
     */
   void setBrightness(char brightness = TM1638_BRT_DEF);
@@ -243,23 +246,23 @@
   /** Enums for Icons */
   //  Grid encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
   enum Icon {
-    LD1  = (1<<24) | S7_LD1,
-    LD2  = (2<<24) | S7_LD2,
-    LD3  = (3<<24) | S7_LD3,
-    LD4  = (4<<24) | S7_LD4,
-    LD5  = (5<<24) | S7_LD5,
-    LD6  = (6<<24) | S7_LD6,
-    LD7  = (7<<24) | S7_LD7,
-    LD8  = (8<<24) | S7_LD8,
+    LD1  = (1<<24) | S7_LD1, /**<  LED1 */
+    LD2  = (2<<24) | S7_LD2, /**<  LED2 */
+    LD3  = (3<<24) | S7_LD3, /**<  LED3 */
+    LD4  = (4<<24) | S7_LD4, /**<  LED4 */
+    LD5  = (5<<24) | S7_LD5, /**<  LED5 */
+    LD6  = (6<<24) | S7_LD6, /**<  LED6 */
+    LD7  = (7<<24) | S7_LD7, /**<  LED7 */
+    LD8  = (8<<24) | S7_LD8, /**<  LED8 */
                                 
-    DP1  = (1<<24) | S7_DP1,
-    DP2  = (2<<24) | S7_DP2,
-    DP3  = (3<<24) | S7_DP3,
-    DP4  = (4<<24) | S7_DP4,
-    DP5  = (5<<24) | S7_DP5,
-    DP6  = (6<<24) | S7_DP6,
-    DP7  = (7<<24) | S7_DP7,
-    DP8  = (8<<24) | S7_DP8                            
+    DP1  = (1<<24) | S7_DP1, /**<  Decimal Point 1 */
+    DP2  = (2<<24) | S7_DP2, /**<  Decimal Point 2 */
+    DP3  = (3<<24) | S7_DP3, /**<  Decimal Point 3 */
+    DP4  = (4<<24) | S7_DP4, /**<  Decimal Point 4 */
+    DP5  = (5<<24) | S7_DP5, /**<  Decimal Point 5 */
+    DP6  = (6<<24) | S7_DP6, /**<  Decimal Point 6 */
+    DP7  = (7<<24) | S7_DP7, /**<  Decimal Point 7 */
+    DP8  = (8<<24) | S7_DP8  /**<  Decimal Point 8 */  
   };
   
   typedef char UDCData_t[LEDKEY8_NR_UDC];
@@ -328,21 +331,366 @@
     int columns();   
 
    /** Write databyte to TM1638
+     *  @param  char data byte written at given address
      *  @param  int address display memory location to write byte
-     *  @param  char data byte written at given address
      *  @return none
      */ 
-    void writeData(int address, char data){
-      TM1638::writeData(address, data);
+    void writeData(char data, int address){
+      TM1638::writeData(data, address);
+    }        
+
+   /** Write Display datablock to TM1638
+    *  @param  DisplayData_t data Array of TM1638_DISPLAY_MEM (=16) bytes for displaydata
+    *  @param  length number bytes to write (valid range 0..(LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID) (=16), when starting at address 0)  
+    *  @param  int address display memory location to write bytes (default = 0) 
+    *  @return none
+    */   
+    void writeData(DisplayData_t data, int length = (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID), int address = 0) {
+      TM1638::writeData(data, length, address);
+    }  
+
+protected:  
+    // Stream implementation functions
+    virtual int _putc(int value);
+    virtual int _getc();
+
+private:
+    int _column;
+    int _columns;   
+    
+    DisplayData_t _displaybuffer;
+    UDCData_t _UDC_7S; 
+};
+#endif
+
+
+#if (QYF_TEST == 1) 
+// Derived class for TM1638 used in QYF-TM1638 display unit
+//
+
+#include "Font_7Seg.h"
+
+#define QYF_NR_GRIDS  8
+#define QYF_NR_DIGITS 8
+#define QYF_NR_UDC    8
+
+//Access to 16 Switches
+#define QYF_SW1_IDX   0
+#define QYF_SW1_BIT   0x04
+#define QYF_SW2_IDX   0
+#define QYF_SW2_BIT   0x40
+#define QYF_SW3_IDX   1
+#define QYF_SW3_BIT   0x04
+#define QYF_SW4_IDX   1
+#define QYF_SW4_BIT   0x40
+
+#define QYF_SW5_IDX   2
+#define QYF_SW5_BIT   0x04
+#define QYF_SW6_IDX   2
+#define QYF_SW6_BIT   0x40
+#define QYF_SW7_IDX   3
+#define QYF_SW7_BIT   0x04
+#define QYF_SW8_IDX   3
+#define QYF_SW8_BIT   0x40
+
+#define QYF_SW9_IDX    0
+#define QYF_SW9_BIT    0x02
+#define QYF_SW10_IDX   0
+#define QYF_SW10_BIT   0x20
+#define QYF_SW11_IDX   1
+#define QYF_SW11_BIT   0x02
+#define QYF_SW12_IDX   1
+#define QYF_SW12_BIT   0x20
+
+#define QYF_SW13_IDX   2
+#define QYF_SW13_BIT   0x02
+#define QYF_SW14_IDX   2
+#define QYF_SW14_BIT   0x20
+#define QYF_SW15_IDX   3
+#define QYF_SW15_BIT   0x02
+#define QYF_SW16_IDX   3
+#define QYF_SW16_BIT   0x20
+
+/** Constructor for class for driving TM1638 controller as used in QYF
+  *
+  *  @brief Supports 8 Digits of 7 Segments + DP, Also supports a scanned keyboard of 16 keys.
+  *  
+  *  @param  PinName mosi, miso, sclk, cs SPI bus pins
+  */
+class TM1638_QYF : public TM1638, public Stream {
+ public:
+
+  /** Enums for Icons */
+  //  Grid encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+  enum Icon {
+    DP1  = (8<<24) | S7_DP1, /**<  Decimal Point 1 */
+    DP2  = (8<<24) | S7_DP2, /**<  Decimal Point 2 */
+    DP3  = (8<<24) | S7_DP3, /**<  Decimal Point 3 */
+    DP4  = (8<<24) | S7_DP4, /**<  Decimal Point 4 */
+    DP5  = (8<<24) | S7_DP5, /**<  Decimal Point 5 */
+    DP6  = (8<<24) | S7_DP6, /**<  Decimal Point 6 */
+    DP7  = (8<<24) | S7_DP7, /**<  Decimal Point 7 */
+    DP8  = (8<<24) | S7_DP8  /**<  Decimal Point 8 */  
+  };
+  
+  typedef char UDCData_t[QYF_NR_UDC];
+  
+ /** Constructor for class for driving TM1638 LED controller as used in QYF
+   *
+   * @brief Supports 8 Digits of 7 Segments + DP Icons. Also supports a scanned keyboard of 16 keys.
+   *  
+   * @param  PinName mosi, miso, sclk, cs SPI bus pins
+   */
+  TM1638_QYF(PinName mosi, PinName miso, PinName sclk, PinName cs);
+
+#if DOXYGEN_ONLY
+    /** Write a character to the Display
+     *
+     * @param c The character to write to the display
+     */
+    int putc(int c);
+
+    /** Write a formatted string to the Display
+     *
+     * @param format A printf-style format string, followed by the
+     *               variables to use in formatting the string.
+     */
+    int printf(const char* format, ...);   
+#endif
+
+     /** Locate cursor to a screen column
+     *
+     * @param column  The horizontal position from the left, indexed from 0
+     */
+    void locate(int column);
+    
+    /** Clear the screen and locate to 0
+     * @param bool clrAll Clear Icons also (default = false)
+     */
+    void cls(bool clrAll = false);
+
+    /** Set Icon
+     *
+     * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+     * @return none
+     */
+    void setIcon(Icon icon);
+
+    /** Clr Icon
+     *
+     * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+     * @return none
+     */
+    void clrIcon(Icon icon);
+
+   /** Set User Defined Characters (UDC)
+     *
+     * @param unsigned char udc_idx   The Index of the UDC (0..7)
+     * @param int udc_data            The bitpattern for the UDC (16 bits)       
+     */
+    void setUDC(unsigned char udc_idx, int udc_data);
+
+
+   /** Number of screen columns
+    *
+    * @param none
+    * @return columns
+    */
+    int columns();   
+
+   /** Write databyte to TM1638
+     *  @param  char data byte written at given address   
+     *  @param  int address display memory location to write byte
+     *  @return none
+     */ 
+    void writeData(char data, int address){
+      TM1638::writeData(data, address);
     }        
 
    /** Write Display datablock to TM1638
-    *  @param  DisplayData_t data Array of TM1638_DISPLAY_MEM (=16) bytes for displaydata (starting at address 0)
-    *  @param  length number bytes to write (valid range 0..(LEDKEY8_NR_GRIDS*2) (=16), starting at address 0)   
+    *  @param  DisplayData_t data Array of TM1638_DISPLAY_MEM (=16) bytes for displaydata
+    *  @param  length number bytes to write (valid range 0..(QYF_NR_GRIDS * TM1638_BYTES_PER_GRID) (=16), when starting at address 0)  
+    *  @param  int address display memory location to write bytes (default = 0) 
     *  @return none
     */   
-    void writeData(DisplayData_t data, int length = (LEDKEY8_NR_GRIDS*2)) {
-      TM1638::writeData(data, length);
+    void writeData(DisplayData_t data, int length = (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID), int address = 0) {
+      TM1638::writeData(data, length, address);
+    }  
+
+protected:  
+    // Stream implementation functions
+    virtual int _putc(int value);
+    virtual int _getc();
+
+private:
+    int _column;
+    int _columns;   
+    
+    DisplayData_t _displaybuffer;
+    UDCData_t _UDC_7S; 
+};
+#endif
+
+#if (LKM1638_TEST == 1) 
+// Derived class for TM1638 used in LKM1638 TM1638 display unit
+//
+
+#include "Font_7Seg.h"
+
+#define LKM1638_NR_GRIDS  8
+#define LKM1638_NR_DIGITS 8
+#define LKM1638_NR_UDC    8
+
+//Access to 8 Switches
+#define LKM1638_SW1_IDX   0
+#define LKM1638_SW1_BIT   0x01
+#define LKM1638_SW2_IDX   1
+#define LKM1638_SW2_BIT   0x01
+#define LKM1638_SW3_IDX   2
+#define LKM1638_SW3_BIT   0x01
+#define LKM1638_SW4_IDX   3
+#define LKM1638_SW4_BIT   0x01
+
+#define LKM1638_SW5_IDX   0
+#define LKM1638_SW5_BIT   0x10
+#define LKM1638_SW6_IDX   1
+#define LKM1638_SW6_BIT   0x10
+#define LKM1638_SW7_IDX   2
+#define LKM1638_SW7_BIT   0x10
+#define LKM1638_SW8_IDX   3
+#define LKM1638_SW8_BIT   0x10
+
+/** Constructor for class for driving TM1638 controller as used in LKM1638
+  *
+  *  @brief Supports 8 Digits of 7 Segments + DP, Also supports 8 Bi-color LEDs and a scanned keyboard of 8 keys.
+  *  
+  *  @param  PinName mosi, miso, sclk, cs SPI bus pins
+  */
+class TM1638_LKM1638 : public TM1638, public Stream {
+ public:
+
+  /** Enums for Icons */
+  //  Grid encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+  enum Icon {
+    DP1  = (1<<24) | S7_DP1, /**<  Decimal Point 1 */
+    DP2  = (2<<24) | S7_DP2, /**<  Decimal Point 2 */
+    DP3  = (3<<24) | S7_DP3, /**<  Decimal Point 3 */
+    DP4  = (4<<24) | S7_DP4, /**<  Decimal Point 4 */
+    DP5  = (5<<24) | S7_DP5, /**<  Decimal Point 5 */
+    DP6  = (6<<24) | S7_DP6, /**<  Decimal Point 6 */
+    DP7  = (7<<24) | S7_DP7, /**<  Decimal Point 7 */
+    DP8  = (8<<24) | S7_DP8, /**<  Decimal Point 8 */  
+    
+    GR1  = (1<<24) | S7_GR1, /**<  Green LED 1 */
+    GR2  = (2<<24) | S7_GR2, /**<  Green LED 2 */
+    GR3  = (3<<24) | S7_GR3, /**<  Green LED 3 */
+    GR4  = (4<<24) | S7_GR4, /**<  Green LED 4 */
+    GR5  = (5<<24) | S7_GR5, /**<  Green LED 5 */
+    GR6  = (6<<24) | S7_GR6, /**<  Green LED 6 */
+    GR7  = (7<<24) | S7_GR7, /**<  Green LED 7 */
+    GR8  = (8<<24) | S7_GR8, /**<  Green LED 8 */  
+
+    RD1  = (1<<24) | S7_RD1, /**<  Red LED 1 */
+    RD2  = (2<<24) | S7_RD2, /**<  Red LED 2 */
+    RD3  = (3<<24) | S7_RD3, /**<  Red LED 3 */
+    RD4  = (4<<24) | S7_RD4, /**<  Red LED 4 */
+    RD5  = (5<<24) | S7_RD5, /**<  Red LED 5 */
+    RD6  = (6<<24) | S7_RD6, /**<  Red LED 6 */
+    RD7  = (7<<24) | S7_RD7, /**<  Red LED 7 */
+    RD8  = (8<<24) | S7_RD8, /**<  Red LED 8 */  
+    
+    YL1  = (1<<24) | S7_YL1, /**<  Yellow LED 1 */
+    YL2  = (2<<24) | S7_YL2, /**<  Yellow LED 2 */
+    YL3  = (3<<24) | S7_YL3, /**<  Yellow LED 3 */
+    YL4  = (4<<24) | S7_YL4, /**<  Yellow LED 4 */
+    YL5  = (5<<24) | S7_YL5, /**<  Yellow LED 5 */
+    YL6  = (6<<24) | S7_YL6, /**<  Yellow LED 6 */
+    YL7  = (7<<24) | S7_YL7, /**<  Yellow LED 7 */
+    YL8  = (8<<24) | S7_YL8  /**<  Yellow LED 8 */  
+  };
+  
+  typedef char UDCData_t[LKM1638_NR_UDC];
+  
+ /** Constructor for class for driving TM1638 LED controller as used in LKM1638
+   *
+   * @brief Supports 8 Digits of 7 Segments + DP Icons. Also supports 8 Bi-Color LEDs and a scanned keyboard of 8 keys.
+   *  
+   * @param  PinName mosi, miso, sclk, cs SPI bus pins
+   */
+  TM1638_LKM1638(PinName mosi, PinName miso, PinName sclk, PinName cs);
+
+#if DOXYGEN_ONLY
+    /** Write a character to the Display
+     *
+     * @param c The character to write to the display
+     */
+    int putc(int c);
+
+    /** Write a formatted string to the Display
+     *
+     * @param format A printf-style format string, followed by the
+     *               variables to use in formatting the string.
+     */
+    int printf(const char* format, ...);   
+#endif
+
+     /** Locate cursor to a screen column
+     *
+     * @param column  The horizontal position from the left, indexed from 0
+     */
+    void locate(int column);
+    
+    /** Clear the screen and locate to 0
+     * @param bool clrAll Clear Icons also (default = false)
+     */
+    void cls(bool clrAll = false);
+
+    /** Set Icon
+     *
+     * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+     * @return none
+     */
+    void setIcon(Icon icon);
+
+    /** Clr Icon
+     *
+     * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
+     * @return none
+     */
+    void clrIcon(Icon icon);
+
+   /** Set User Defined Characters (UDC)
+     *
+     * @param unsigned char udc_idx   The Index of the UDC (0..7)
+     * @param int udc_data            The bitpattern for the UDC (16 bits)       
+     */
+    void setUDC(unsigned char udc_idx, int udc_data);
+
+
+   /** Number of screen columns
+    *
+    * @param none
+    * @return columns
+    */
+    int columns();   
+
+   /** Write databyte to TM1638
+     *  @param  char data byte written at given address   
+     *  @param  int address display memory location to write byte
+     *  @return none
+     */ 
+    void writeData(char data, int address){
+      TM1638::writeData(data, address);
+    }        
+
+   /** Write Display datablock to TM1638
+    *  @param  DisplayData_t data Array of TM1638_DISPLAY_MEM (=16) bytes for displaydata
+    *  @param  length number bytes to write (valid range 0..(LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID) (=16), when starting at address 0)  
+    *  @param  int address display memory location to write bytes (default = 0) 
+    *  @return none
+    */   
+    void writeData(DisplayData_t data, int length = (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID), int address = 0) {
+      TM1638::writeData(data, length, address);
     }  
 
 protected: