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.

Files at this revision

API Documentation at this revision

Comitter:
wim
Date:
Sat Jan 09 14:30:43 2016 +0000
Parent:
0:201dfacbe0e5
Child:
2:532ce15ea9ec
Commit message:
TM1638 LED controller, Initial version.; Supports LED&KEY module.

Changed in this revision

Font_7Seg.cpp Show annotated file Show diff for this revision Revisions of this file
Font_7Seg.h Show annotated file Show diff for this revision Revisions of this file
TM1638.cpp Show annotated file Show diff for this revision Revisions of this file
TM1638.h Show annotated file Show diff for this revision Revisions of this file
--- a/Font_7Seg.cpp	Mon Dec 21 22:39:22 2015 +0000
+++ b/Font_7Seg.cpp	Sat Jan 09 14:30:43 2016 +0000
@@ -39,8 +39,6 @@
                                  };
 
 
-
-
 // ASCII Font definition table for transmission to TM1638
 //
 //#define FONT_7S_START     0x20
--- a/Font_7Seg.h	Mon Dec 21 22:39:22 2015 +0000
+++ b/Font_7Seg.h	Sat Jan 09 14:30:43 2016 +0000
@@ -38,6 +38,7 @@
 #define S7_E    0x0010
 #define S7_F    0x0020
 #define S7_G    0x0040 
+#define S7_DP   0x0080 
 
 //Mask for blending out and setting 7 segments digits
 #define MASK_7S_ALL = (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F | S7_G}
@@ -45,52 +46,62 @@
 //Icons Grid 1
 #define S7_DP1  0x0080
 #define S7_LD1  0x0100
-#define S7_ICON_GR1 (S7_LD1 | S7_DP1)
+#define S7_ICON_GR1 (S7_LD1)
+//#define S7_ICON_GR1 (S7_LD1 | S7_DP1)
 
 //Icons Grid 2
 #define S7_DP2  0x0080
 #define S7_LD2  0x0100
-#define S7_ICON_GR2 (S7_LD2 | S7_DP2)
+#define S7_ICON_GR2 (S7_LD2)
+//#define S7_ICON_GR2 (S7_LD2 | S7_DP2)
 
 //Icons Grid 3
 #define S7_DP3  0x0080
 #define S7_LD3  0x0100
-#define S7_ICON_GR3 (S7_LD3 | S7_DP3)
+#define S7_ICON_GR3 (S7_LD3)
+//#define S7_ICON_GR3 (S7_LD3 | S7_DP3)
 
 //Icons Grid 4
 #define S7_DP4  0x0080
 #define S7_LD4  0x0100
-#define S7_ICON_GR4 (S7_LD4 | S7_DP4)
+#define S7_ICON_GR4 (S7_LD4)
+//#define S7_ICON_GR4 (S7_LD4 | S7_DP4)
 
 //Icons Grid 5
 #define S7_DP5  0x0080
 #define S7_LD5  0x0100
-#define S7_ICON_GR5 (S7_LD5 | S7_DP5)
+#define S7_ICON_GR5 (S7_LD5)
+//#define S7_ICON_GR5 (S7_LD5 | S7_DP5)
 
 //Icons Grid 6
 #define S7_DP6  0x0080
 #define S7_LD6  0x0100
-#define S7_ICON_GR6 (S7_LD6 | S7_DP6)
+#define S7_ICON_GR6 (S7_LD6)
+//#define S7_ICON_GR6 (S7_LD6 | S7_DP6)
 
 //Icons Grid 7
 #define S7_DP7  0x0080
 #define S7_LD7  0x0100
-#define S7_ICON_GR7 (S7_LD7 | S7_DP7)
+#define S7_ICON_GR7 (S7_LD7)
+//#define S7_ICON_GR7 (S7_LD7 | S7_DP7)
 
 //Icons Grid 8
 #define S7_DP8  0x0080
 #define S7_LD8  0x0100
-#define S7_ICON_GR8 (S7_LD8 | S7_DP8)
+#define S7_ICON_GR8 (S7_LD8)
+//#define S7_ICON_GR8 (S7_LD8 | S7_DP8)
 
 //Icons Grid 9
 #define S7_DP9  0x0080
 #define S7_LD9  0x0100
-#define S7_ICON_GR9 (S7_LD9 | S7_DP9)
+#define S7_ICON_GR9 (S7_LD9)
+//#define S7_ICON_GR9 (S7_LD9 | S7_DP9)
 
 //Icons Grid 10
 #define S7_DP10 0x0080
 #define S7_LD10 0x0100
-#define S7_ICON_GR10 (S7_LD10 | S7_DP10)
+#define S7_ICON_GR10 (S7_LD10)
+//#define S7_ICON_GR10 (S7_LD10 | S7_DP10)
 
 //Mask for blending out and restoring Icons
 extern const char MASK_ICON_GRID[][2]; 
@@ -100,7 +111,10 @@
 // ASCII Font definitions for segments in each character
 //
 //32 0x20  Symbols
-#define C7_SPC  ()
+#define C7_SPC  (0x0000)
+#define C7_CMA  (S7_DP)
+#define C7_DPT  (S7_DP)
+#define C7_MIN  (S7_G)
 
 //48 0x30  Digits
 #define C7_0    (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F)
--- a/TM1638.cpp	Mon Dec 21 22:39:22 2015 +0000
+++ b/TM1638.cpp	Sat Jan 09 14:30:43 2016 +0000
@@ -301,13 +301,13 @@
 
 /** Set Icon
   *
-  * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 24 LSBs
+  * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
   * @return none
   */
 void TM1638_LEDKEY8::setIcon(Icon icon) {
   int addr, icn;
 
-   icn =        icon  & 0xFFFFFF;
+   icn =        icon  & 0xFFFF;
   addr = (icon >> 24) & 0xFF; 
   addr = (addr - 1) << 1;   
     
@@ -319,13 +319,13 @@
 
 /** Clr Icon
   *
-  * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 24 LSBs
+  * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
   * @return none
   */
 void TM1638_LEDKEY8::clrIcon(Icon icon) {
   int addr, icn;
 
-   icn =        icon  & 0xFFFFFF;
+   icn =        icon  & 0xFFFF;
   addr = (icon >> 24) & 0xFF; 
   addr = (addr - 1) << 1;   
     
@@ -353,7 +353,6 @@
 }
 
 
-#if(1)
 /** Write a single character (Stream implementation)
   */
 int TM1638_LEDKEY8::_putc(int value) {
@@ -368,6 +367,31 @@
       //Update Cursor      
       _column = 0;
     }
+    else if ((value == '.') || (value == ',')) {
+      //No character to write
+      validChar = false;
+      pattern = S7_DP; // placeholder for all DPs
+      
+      // Check to see that DP can be shown for current column
+      if (_column > 0) {
+        //Translate between _column and displaybuffer entries
+        //Add DP to bitpattern of digit left of current column.
+        addr = (_column - 1) << 1;
+      
+        //Save icons...and set bits for decimal point to write
+        _displaybuffer[addr]   = _displaybuffer[addr] | pattern;
+//        _displaybuffer[addr+1] = _displaybuffer[addr+1] | pattern;
+
+        writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2));
+        
+        //No Cursor Update
+      }
+    }  
+    else if (value == '-') {
+      //No character to write
+      validChar = true;
+      pattern = C7_MIN;         
+    }
     else if ((value >= 0) && (value < LEDKEY8_NR_UDC)) {
       //Character to write
       validChar = true;
@@ -412,88 +436,6 @@
     return value;
 }
 
-#else
-
-/** Write a single character (Stream implementation)
-  */
-int TM1638_LEDKEY8::_putc(int value) {
-  int addr;
-    
-    if ((value == '\n') || (value == '\r')) {
-      //No character to write
-      
-      //Update Cursor      
-      _column = 0;
-    }
-    else if ((value >= 0) && (value < LEDKEY8_NR_UDC)) {
-      //Character to write
-      addr = (LEDKEY8_DIG1_IDX + ((LEDKEY8_NR_DIGITS - 1) - _column)) << 1; 
-      
-      //Save icons...and set bits for character to write
-//      _displaybuffer[addr]   = (_displaybuffer[addr]   & LO(S_ICON_MSK)) | _UDC_16S[value][0];      
-//      _displaybuffer[addr+1] = (_displaybuffer[addr+1] & HI(S_ICON_MSK)) | _UDC_16S[value][1];      
-      writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2));
-               
-      //Update Cursor
-      _column++;
-      if (_column > (LEDKEY8_NR_DIGITS - 1)) {
-        _column = 0;
-      }          
-    }  
-    else if ((value >= (int)'0') && (value <= (int) '9')) {   
-      //Character to write
-      value = value - (int) '0';
-      addr = (LEDKEY8_DIG1_IDX + ((LEDKEY8_NR_DIGITS - 1) - _column)) << 1; 
-      
-      //Save icons...and set bits for character to write
-//      _displaybuffer[addr]   = (_displaybuffer[addr]   & LO(S_ICON_MSK)) | FONT_7S[value][0];      
-//      _displaybuffer[addr+1] = (_displaybuffer[addr+1] & HI(S_ICON_MSK)) | FONT_7S[value][1];      
-      writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2));
-               
-      //Update Cursor
-      _column++;
-      if (_column > (LEDKEY8_NR_DIGITS - 1)) {
-        _column = 0;
-      }
-    }            
-    else if ((value >= (int) 'A') && (value <= (int) 'F')) {   
-      //Character to write
-      value = 10 + value - (int) 'A';
-      addr = (LEDKEY8_DIG1_IDX + ((LEDKEY8_NR_DIGITS - 1) - _column)) << 1; 
-      
-      //Save icons...and set bits for character to write
-//      _displaybuffer[addr]   = (_displaybuffer[addr]   & LO(S_ICON_MSK)) | FONT_7S[value][0];      
-//      _displaybuffer[addr+1] = (_displaybuffer[addr+1] & HI(S_ICON_MSK)) | FONT_7S[value][1];      
-      writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2));
-               
-      //Update Cursor
-      _column++;
-      if (_column > (LEDKEY8_NR_DIGITS - 1)) {
-        _column = 0;
-      }
-    }  
-    else if ((value >= (int) 'a') && (value <= (int) 'f')) {   
-      //Character to write
-      value = 10 + value - (int) 'a';
-      addr = (LEDKEY8_DIG1_IDX + ((LEDKEY8_NR_DIGITS - 1) - _column)) << 1; 
-      
-      //Save icons...and set bits for character to write
-//      _displaybuffer[addr]   = (_displaybuffer[addr]   & LO(S_ICON_MSK)) | FONT_7S[value][0];      
-//      _displaybuffer[addr+1] = (_displaybuffer[addr+1] & HI(S_ICON_MSK)) | FONT_7S[value][1];      
-      writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2));
-               
-      //Update Cursor
-      _column++;
-      if (_column > (LEDKEY8_NR_DIGITS - 1)) {
-        _column = 0;
-      }          
-
-    } //else
-
-    return value;
-}
-#endif
-
 
 // get a single character (Stream implementation)
 int TM1638_LEDKEY8::_getc() {
--- a/TM1638.h	Mon Dec 21 22:39:22 2015 +0000
+++ b/TM1638.h	Sat Jan 09 14:30:43 2016 +0000
@@ -35,7 +35,7 @@
  * DisplayData_t size is 16 bytes (8 grids @ 10 segments)
  * TM1638::DisplayData_t all_str  = {0xFF,0x3F, 0xFF,0x3F, 0xFF,0x3F, 0xFF,0x3F, 0xFF,0x3F, 0xFF,0x3F, 0xFF,0x3F, 0xFF,0x3F};  
  *
- * // KeyData_t size is 5 bytes  
+ * // KeyData_t size is 4 bytes  
  * TM1638::KeyData_t keydata; 
  *
  * // TM1638 declaration
@@ -54,7 +54,7 @@
  *    if (TM1638.getKeys(&keydata)) {
  *      pc.printf("Keydata 0..3 = 0x%02x 0x%02x 0x%02x 0x%02x\r\n", keydata[0], keydata[1], keydata[2], keydata[3]);
  *
- *      if (keydata[0] == 0x10) { //sw2   
+ *      if (keydata[0] == 0x01) { //sw1   
  *        TM1638.cls(); 
  *        TM1638.writeData(all_str);
  *      }  
@@ -203,9 +203,27 @@
 
 #define LEDKEY8_NR_GRIDS  8
 #define LEDKEY8_NR_DIGITS 8
-#define LEDKEY8_DIG1_IDX  1
 #define LEDKEY8_NR_UDC    8
 
+//Access to 8 Switches
+#define LEDKEY8_SW1_IDX   0
+#define LEDKEY8_SW1_BIT   0x01
+#define LEDKEY8_SW2_IDX   1
+#define LEDKEY8_SW2_BIT   0x01
+#define LEDKEY8_SW3_IDX   2
+#define LEDKEY8_SW3_BIT   0x01
+#define LEDKEY8_SW4_IDX   3
+#define LEDKEY8_SW4_BIT   0x01
+
+#define LEDKEY8_SW5_IDX   0
+#define LEDKEY8_SW5_BIT   0x10
+#define LEDKEY8_SW6_IDX   1
+#define LEDKEY8_SW6_BIT   0x10
+#define LEDKEY8_SW7_IDX   2
+#define LEDKEY8_SW7_BIT   0x10
+#define LEDKEY8_SW8_IDX   3
+#define LEDKEY8_SW8_BIT   0x10
+
 /** Constructor for class for driving TM1638 controller as used in LEDKEY8
   *
   *  @brief Supports 8 Digits of 7 Segments + DP + LED Icons, Also supports a scanned keyboard of 8 keys.
@@ -216,7 +234,7 @@
  public:
 
   /** Enums for Icons */
-  //  Grid encoded in 8 MSBs, Icon pattern encoded in 24 LSBs
+  //  Grid encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
   enum Icon {
     LD1  = (1<<24) | S7_LD1,
     LD2  = (2<<24) | S7_LD2,
@@ -275,14 +293,14 @@
 
     /** Set Icon
      *
-     * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 24 LSBs
+     * @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 24 LSBs
+     * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
      * @return none
      */
     void clrIcon(Icon icon);