Test program for TM1638 LED controller. Supports LED&KEY, QYF-TM1638 and JY-LKM1638 module.

Dependencies:   TM1638 mbed

See here for more information.

Files at this revision

API Documentation at this revision

Comitter:
wim
Date:
Sun Jan 31 12:11:29 2016 +0000
Parent:
3:f1484bfa0574
Commit message:
Added QYF-TM1638 and LKM1638, refactoring of writeData()

Changed in this revision

TM1638.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f1484bfa0574 -r 0e1ffa381c62 TM1638.lib
--- a/TM1638.lib	Tue Jan 19 18:58:35 2016 +0000
+++ b/TM1638.lib	Sun Jan 31 12:11:29 2016 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/wim/code/TM1638/#25ddabfadc8c
+https://developer.mbed.org/users/wim/code/TM1638/#b2bbdc58967e
diff -r f1484bfa0574 -r 0e1ffa381c62 main.cpp
--- a/main.cpp	Tue Jan 19 18:58:35 2016 +0000
+++ b/main.cpp	Sun Jan 31 12:11:29 2016 +0000
@@ -1,6 +1,7 @@
 /* mbed TM1638 Test program, for TM1638 LED controller
  * Copyright (c) 2015, v01: WH, Initial version
  *               2016, v02: WH, Added ASCII alphabet test 
+ *               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
@@ -23,6 +24,36 @@
 #include "mbed.h"
 #include "TM1638.h"
 
+//                          01234567
+const char NATO[][9]  = { {"Alpha   "},
+                          {"Bravo   "},
+                          {"Charlie "},
+                          {"Delta   "},
+                          {"Echo    "},
+                          {"Foxtrot "},
+                          {"Golf    "},
+                          {"Hotel   "},
+                          {"India   "},
+                          {"Juliet  "},
+                          {"Kilo    "},
+                          {"Lima    "},
+                          {"Mike    "},
+                          {"November"},
+                          {"Oscar   "},
+                          {"Papa    "},
+                          {"Quebec  "},
+                          {"Romeo   "},
+                          {"Sierra  "},
+                          {"Tango   "},
+                          {"Uniform "},
+                          {"Victor  "},
+                          {"Whiskey "},
+                          {"X-ray   "},
+                          {"Yankee  "},
+                          {"Zulu    "}
+                        };     
+
+
 #if (LEDKEY8_TEST == 1)
 // LEDKEY8 TM1638 Test
 #include "Font_7Seg.h"
@@ -61,12 +92,9 @@
                                     {S7_E,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
                                     {S7_F,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
                                   };  
-// KeyData_t size is 3 bytes  
+// KeyData_t size is 4 bytes  
 TM1638::KeyData_t keydata; 
 
-// Switchdata is 1 byte  
-char switchdata; 
-
 //TM1638_LEDKEY8 declaration
 TM1638_LEDKEY8 LEDKEY8(p5,p6,p7, p8);
 
@@ -119,7 +147,7 @@
               LEDKEY8.cls(); 
 
               bits = 0x01 << bit;              
-              LEDKEY8.writeData(i, bits);
+              LEDKEY8.writeData(bits, i);
 
               pc.printf("Idx = %d, Bits = 0x%02x\r\n", i, bits);              
 //              wait(0.5);
@@ -138,36 +166,7 @@
 
 #if(1)
 //test to show all chars
-          pc.printf("Show all Alpha chars\r\n");
-
-//                          01234567
-const char NATO[][9]  = { {"Alpha   "},
-                          {"Bravo   "},
-                          {"Charlie "},
-                          {"Delta   "},
-                          {"Echo    "},
-                          {"Foxtrot "},
-                          {"Golf    "},
-                          {"Hotel   "},
-                          {"India   "},
-                          {"Juliet  "},
-                          {"Kilo    "},
-                          {"Lima    "},
-                          {"Mike    "},
-                          {"November"},
-                          {"Oscar   "},
-                          {"Papa    "},
-                          {"Quebec  "},
-                          {"Romeo   "},
-                          {"Sierra  "},
-                          {"Tango   "},
-                          {"Uniform "},
-                          {"Victor  "},
-                          {"Wiskey  "},
-                          {"X-ray   "},
-                          {"Yankee  "},
-                          {"Zulu    "}
-                        };     
+          pc.printf("Show all NATO Alpha chars\r\n");
 
           LEDKEY8.cls(); // clear all, preserve Icons
           for (int i=0; i<26; i++) {
@@ -175,7 +174,7 @@
             LEDKEY8.printf("%s", NATO[i]);
             wait(0.5);
           }  
-          pc.printf("Show all alpha chars done\r\n");
+          pc.printf("Show all NATO Alpha chars done\r\n");
 #endif
 
 #if(0)
@@ -213,17 +212,15 @@
           wait(1);          
           LEDKEY8.cls(); 
 
-          int val = 1; 
           for (int i=0; i<LEDKEY8_NR_DIGITS; i++) {
 
             for (int cnt=0; cnt<10; cnt++) {
-              LEDKEY8.locate(0);
-              LEDKEY8.printf("%07d", (val * cnt));
+              LEDKEY8.locate(i);
+              LEDKEY8.printf("%0d", cnt);
 
 //              wait(0.5);
               cmd = pc.getc(); // wait for key
             }  
-            val = val * 10;
           }
           pc.printf("\r\nShow all digits done\r\n");                    
 #endif                
@@ -236,18 +233,18 @@
 
           LEDKEY8.printf("%08x", 0x012345AB);
           cmd = pc.getc(); // wait for key                                     
-          
-          int val = 1; 
+          LEDKEY8.printf("%08x", 0x00);
+                    
           for (int i=0; i<LEDKEY8_NR_DIGITS; i++) {
 
             for (int cnt=0; cnt<0x10; cnt++) {
-              LEDKEY8.locate(0);
-              LEDKEY8.printf("%08x", (val * cnt));
+              LEDKEY8.locate(i);
+              LEDKEY8.printf("%0x", cnt);
 
 //              wait(0.5);
               cmd = pc.getc(); // wait for key
             }  
-            val = val * 0x10;
+
           }
           pc.printf("\r\nShow all hex digits done\r\n");          
 #endif                
@@ -355,6 +352,612 @@
 #endif
 
 
+#if (QYF_TEST == 1)
+// QYF TM1638 Test
+#include "Font_7Seg.h"
+
+Serial pc(USBTX, USBRX);
+DigitalOut myled(LED1);
+
+// DisplayData_t size is 16 bytes (8 Grids @ 10 Segments) 
+TM1638::DisplayData_t all_str   = {0xFF,0x03, 0xFF,0x03, 0xFF,0x03, 0xFF,0x03, 0xFF,0x03, 0xFF,0x03, 0xFF,0x03, 0xFF,0x03};  
+TM1638::DisplayData_t cls_str   = {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00};  
+TM1638::DisplayData_t animate[] = {
+                                    {S7_A1,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {S7_A2,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {S7_A3,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {S7_A4,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {S7_A5,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {S7_A6,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {S7_A7,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {S7_A8,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    
+                                    {0x00,0x00, S7_B8,0x00,  0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00,  0x00,0x00, S7_C8,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D8,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D7,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D6,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D5,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D4,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D3,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D2,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D1,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_E1,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00,  0x00,0x00, S7_F1,0x00, 0x00,0x00, 0x00,0x00},
+                                  };  
+// KeyData_t size is 4 bytes  
+TM1638::KeyData_t keydata; 
+
+//TM1638_QFY declaration
+TM1638_QYF QYF(p5,p6,p7, p8);
+
+char cmd, bits;
+int main() {
+    
+    pc.printf("Hello World\r\n"); //    
+    
+    QYF.cls(); 
+    QYF.writeData(all_str);
+    wait(2);
+    QYF.setBrightness(TM1638_BRT3);    
+    wait(1);
+    QYF.setBrightness(TM1638_BRT0);        
+    wait(1);
+    QYF.setBrightness(TM1638_BRT4);            
+
+    wait(1);
+    QYF.cls(true); 
+    QYF.printf("Hello"); 
+    
+    while (1) {
+     
+      // Check and read keydata
+      if (QYF.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[QYF_SW1_IDX] == QYF_SW1_BIT) { //sw1  
+          QYF.cls(); 
+          QYF.writeData(all_str);
+
+          wait(0.5);          
+          for (int i=0; i<20; i++) {            
+            QYF.writeData(animate[i]);
+            wait(0.05);
+          }  
+        }  
+
+        if (keydata[QYF_SW2_IDX] == QYF_SW2_BIT) { //sw2  
+          QYF.cls(); 
+//          QYF.writeData(hello_str);
+#if(1)
+//test to show all segs
+          pc.printf("Show all segs\r\n");
+          wait(1);          
+          QYF.cls(); 
+         
+          for (int i=0; i<TM1638_DISPLAY_MEM; i++) {            
+            for (int bit=0; bit<8; bit++) {
+              QYF.cls(); 
+
+              bits = 0x01 << bit;              
+              QYF.writeData(bits, i);
+              pc.printf("Idx = %d, Bits = 0x%02x\r\n", i, bits);              
+//              wait(0.5);
+              cmd = pc.getc(); // wait for key
+            }  
+          }
+          pc.printf("\r\nShow all segs done\r\n");                    
+#endif 
+          
+        }  
+
+        if (keydata[QYF_SW3_IDX] == QYF_SW3_BIT) { //sw3  
+//          QYF.cls(); 
+
+#if(1)
+//test to show all NATO alpha chars
+          pc.printf("Show all NATO Alpha chars\r\n");
+
+          QYF.cls(); // clear all, preserve Icons
+          for (int i=0; i<26; i++) {
+            QYF.locate(0);
+            QYF.printf("%s", NATO[i]);
+            wait(0.5);
+          }  
+          pc.printf("Show all NATO Alpha chars done\r\n");
+#endif
+
+#if(0)
+//test to show all alpha chars
+          pc.printf("Show all alpha chars\r\n");
+          wait(1);          
+          QYF.cls(); 
+         
+          for (int i=0; i<26; i++) {
+            QYF.printf("%c", char(i + 'A'));
+//            QYF.printf("%c", char(i + 'a'));            
+            wait(0.25);                      
+          }
+          pc.printf("Show all alpha chars done\r\n");                    
+#endif            
+
+#if(0)
+//test to show all chars
+          pc.printf("Show all chars\r\n");
+          wait(1);          
+          QYF.cls(); 
+
+          for (int i=FONT_7S_START; i<FONT_7S_END; i++) {
+            QYF.printf("%c", char(i));
+//            wait(0.25);
+            cmd = pc.getc(); // wait for key            
+          }
+          pc.printf("Show all chars done\r\n");                    
+#endif            
+
+
+        }
+
+        if (keydata[QYF_SW4_IDX] == QYF_SW4_BIT) { //sw4          
+
+#if(0)
+//test to show all digits (base is 10)
+          pc.printf("Show all digits\r\n");
+          wait(1);          
+          QYF.cls(); 
+
+          for (int i=0; i<QYF_NR_DIGITS; i++) {
+
+            for (int cnt=0; cnt<10; cnt++) {
+              QYF.locate(i);
+              QYF.printf("%0d", cnt);
+
+//              wait(0.5);
+              cmd = pc.getc(); // wait for key
+            }  
+          }
+          pc.printf("\r\nShow all digits done\r\n");                    
+#endif                
+
+#if(1)
+//test to show all digits (base is 0x10)
+          pc.printf("Show all hex digits\r\n");
+          wait(1);          
+          QYF.cls(); 
+
+          QYF.printf("%08x", 0x012345AB);
+          cmd = pc.getc(); // wait for key                                     
+          QYF.printf("%08x", 0x0);          
+
+          for (int i=0; i<QYF_NR_DIGITS; i++) {
+
+            for (int cnt=0; cnt<0x10; cnt++) {
+              QYF.locate(i);
+              QYF.printf("%0x", cnt);
+
+//              wait(0.5);
+              cmd = pc.getc(); // wait for key
+            }  
+           
+          }
+          pc.printf("\r\nShow all hex digits done\r\n");          
+#endif                          
+        }       
+
+        if (keydata[QYF_SW5_IDX] == QYF_SW5_BIT) { //sw5  
+          QYF.cls(); // clear all, preserve Icons
+
+#if(1)
+          QYF.printf("Count");                            
+          for (int cnt=0; cnt<=0xFF; cnt++) {
+            QYF.locate(6);
+            QYF.printf("%02x", cnt);
+            wait(0.2);
+          }  
+#endif
+        }
+
+        if (keydata[QYF_SW6_IDX] == QYF_SW6_BIT) { //sw6  
+          QYF.cls(); // clear all, preserve Icons
+          QYF.printf("%2.3f", -0.1234);  // test decimal point display                    
+          wait(0.5);
+          QYF.cls(); // clear all, preserve Icons          
+          QYF.printf("%2.3f", -012.345);  // test decimal point display
+        }
+        
+        if (keydata[QYF_SW7_IDX] == QYF_SW7_BIT) { //sw7  
+          float delay=0.1;
+          // Icons on             
+          QYF.setIcon(TM1638_QYF::DP1); wait(delay);
+          QYF.setIcon(TM1638_QYF::DP2); wait(delay);
+          QYF.setIcon(TM1638_QYF::DP3); wait(delay);
+          QYF.setIcon(TM1638_QYF::DP4); wait(delay);
+          QYF.setIcon(TM1638_QYF::DP5); wait(delay);
+          QYF.setIcon(TM1638_QYF::DP6); wait(delay);
+          QYF.setIcon(TM1638_QYF::DP7); wait(delay);
+          QYF.setIcon(TM1638_QYF::DP8); wait(delay);
+
+          wait(delay);
+ 
+          // Icons off      
+          QYF.clrIcon(TM1638_QYF::DP8); wait(delay);
+          QYF.clrIcon(TM1638_QYF::DP7); wait(delay);
+          QYF.clrIcon(TM1638_QYF::DP6); wait(delay);
+          QYF.clrIcon(TM1638_QYF::DP5); wait(delay);
+          QYF.clrIcon(TM1638_QYF::DP4); wait(delay);
+          QYF.clrIcon(TM1638_QYF::DP3); wait(delay);
+          QYF.clrIcon(TM1638_QYF::DP2); wait(delay);
+          QYF.clrIcon(TM1638_QYF::DP1); wait(delay);          
+        }
+
+        if (keydata[QYF_SW8_IDX] == QYF_SW8_BIT) { //sw8  
+          QYF.cls(); 
+          QYF.printf("Hello");                
+        }
+        
+        if (keydata[QYF_SW9_IDX] == QYF_SW9_BIT) { //sw9  
+          QYF.cls(); // clear all, preserve Icons
+          QYF.printf("Bye");          
+        }
+     
+      } //if Key
+
+      myled = !myled;
+      wait(0.3);      
+    } //while
+}
+#endif
+
+
+#if (LKM1638_TEST == 1)
+// LKM1638 TM1638 Test
+#include "Font_7Seg.h"
+
+Serial pc(USBTX, USBRX);
+DigitalOut myled(LED1);
+
+// DisplayData_t size is 16 bytes (8 Grids @ 10 Segments) 
+TM1638::DisplayData_t all_str   = {0xFF,0x03, 0xFF,0x03, 0xFF,0x03, 0xFF,0x03, 0xFF,0x03, 0xFF,0x03, 0xFF,0x03, 0xFF,0x03};  
+TM1638::DisplayData_t cls_str   = {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00};  
+TM1638::DisplayData_t hello_str = {C7_H,0x00, C7_E,0x00, C7_L,0x00, C7_L,0x00, C7_O,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00};
+TM1638::DisplayData_t bye_str   = {C7_B,0x00, C7_Y,0x00, C7_E,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00};
+
+TM1638::DisplayData_t animate[] = {
+                                    {S7_A,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, S7_A,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, S7_A,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, S7_A,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_A,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_A,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_A,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_A,0x00},
+                                                                                                            
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_B,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_C,0x00},
+
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, 0x00,0x00, S7_D,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, 0x00,0x00, S7_D,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {0x00,0x00, S7_D,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {S7_D,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+
+                                    {S7_E,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                    {S7_F,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00},
+                                  };  
+// KeyData_t size is 4 bytes  
+TM1638::KeyData_t keydata; 
+
+//TM1638_LKM1638 declaration
+TM1638_LKM1638 LKM1638(p5,p6,p7, p8);
+
+char cmd, bits;
+int main() {
+    
+    pc.printf("Hello World\r\n"); //    
+    
+    LKM1638.cls(); 
+    LKM1638.writeData(all_str);
+    wait(2);
+    LKM1638.setBrightness(TM1638_BRT3);    
+    wait(1);
+    LKM1638.setBrightness(TM1638_BRT0);        
+    wait(1);
+    LKM1638.setBrightness(TM1638_BRT4);            
+
+    wait(1);
+    LKM1638.cls(true); 
+    LKM1638.writeData(hello_str); 
+   
+    while (1) {
+     
+      // Check and read keydata
+      if (LKM1638.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[LKM1638_SW1_IDX] == LKM1638_SW1_BIT) { //sw1  
+          LKM1638.cls(); 
+          LKM1638.writeData(all_str);
+
+          wait(0.5);          
+          for (int i=0; i<20; i++) {            
+            LKM1638.writeData(animate[i]);
+            wait(0.05);
+          }  
+        }  
+
+        if (keydata[LKM1638_SW2_IDX] == LKM1638_SW2_BIT) { //sw2  
+          LKM1638.cls(); 
+//          LKM1638.writeData(hello_str);
+#if(1)
+//test to show all segs
+          pc.printf("Show all segs\r\n");
+          wait(1);          
+          LKM1638.cls(); 
+         
+          for (int i=0; i<TM1638_DISPLAY_MEM; i++) {            
+            for (int bit=0; bit<8; bit++) {
+              LKM1638.cls(); 
+
+              bits = 0x01 << bit;              
+              LKM1638.writeData(bits, i);
+
+              pc.printf("Idx = %d, Bits = 0x%02x\r\n", i, bits);              
+//              wait(0.5);
+              cmd = pc.getc(); // wait for key
+            }  
+          }
+          pc.printf("\r\nShow all segs done\r\n");                    
+#endif 
+          
+        }  
+
+        if (keydata[LKM1638_SW3_IDX] == LKM1638_SW3_BIT) { //sw3  
+//          LKM1638.cls(); 
+//          LKM1638.writeData(mbed_str);
+
+
+#if(1)
+//test to show all chars
+          pc.printf("Show all NATO Alpha chars\r\n");
+
+          LKM1638.cls(); // clear all, preserve Icons
+          for (int i=0; i<26; i++) {
+            LKM1638.locate(0);
+            LKM1638.printf("%s", NATO[i]);
+            wait(0.5);
+          }  
+          pc.printf("Show all NATO Alpha chars done\r\n");
+#endif
+
+#if(0)
+//test to show all chars
+          pc.printf("Show all alpha chars\r\n");
+          wait(1);          
+          LKM1638.cls(); 
+         
+          for (int i=0; i<26; i++) {
+            LKM1638.printf("%c", char(i + 'A'));
+//            LKM1638.printf("%c", char(i + 'a'));            
+            wait(0.25);                      
+          }
+          pc.printf("Show all alpha chars done\r\n");                    
+#endif            
+
+#if(0)
+//test to show all chars
+          pc.printf("Show all chars\r\n");
+          wait(1);          
+          LKM1638.cls(); 
+
+          for (int i=FONT_7S_START; i<FONT_7S_END; i++) {
+            LKM1638.printf("%c", char(i));
+//            wait(0.25);
+            cmd = pc.getc(); // wait for key            
+          }
+          pc.printf("Show all chars done\r\n");                    
+#endif            
+
+
+#if(0)
+//test to show all digits (base is 10)
+          pc.printf("Show all digits\r\n");
+          wait(1);          
+          LKM1638.cls(); 
+
+          for (int i=0; i<LKM1638_NR_DIGITS; i++) {
+
+            for (int cnt=0; cnt<10; cnt++) {
+              LKM1638.locate(i);
+              LKM1638.printf("%0d", cnt);
+
+//              wait(0.5);
+              cmd = pc.getc(); // wait for key
+            }  
+          }
+          pc.printf("\r\nShow all digits done\r\n");                    
+#endif                
+
+#if(1)
+//test to show all digits (base is 0x10)
+          pc.printf("Show all hex digits\r\n");
+          wait(1);          
+          LKM1638.cls(); 
+
+          LKM1638.printf("%08x", 0x012345AB);
+          cmd = pc.getc(); // wait for key                                     
+          LKM1638.printf("%08x", 0x00);
+                    
+          for (int i=0; i<LKM1638_NR_DIGITS; i++) {
+
+            for (int cnt=0; cnt<0x10; cnt++) {
+              LKM1638.locate(i);
+              LKM1638.printf("%0x", cnt);
+
+//              wait(0.5);
+              cmd = pc.getc(); // wait for key
+            }  
+
+          }
+          pc.printf("\r\nShow all hex digits done\r\n");          
+#endif                
+        }
+
+        if (keydata[LKM1638_SW4_IDX] == LKM1638_SW4_BIT) { //sw4          
+//          LKM1638.cls(); 
+//          LKM1638.writeData(mbed_str); 
+#if(1)
+//test to show all icons
+          pc.printf("Show all icons\r\n");
+          LKM1638.cls(true); // Also clear all Icons
+          
+          float delay=0.1;
+          // Icons on             
+          LKM1638.setIcon(TM1638_LKM1638::RD1); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::RD2); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::RD3); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::RD4); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::RD5); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::RD6); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::RD7); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::RD8); wait(delay);
+
+          wait(delay);
+ 
+          // Icons off      
+          LKM1638.clrIcon(TM1638_LKM1638::RD1); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::RD2); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::RD3); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::RD4); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::RD5); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::RD6); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::RD7); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::RD8); wait(delay);
+                 
+          wait(delay);
+          
+          // Icons on             
+          LKM1638.setIcon(TM1638_LKM1638::GR1); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::GR2); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::GR3); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::GR4); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::GR5); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::GR6); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::GR7); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::GR8); wait(delay);
+
+          wait(delay);
+ 
+          // Icons off      
+          LKM1638.clrIcon(TM1638_LKM1638::GR1); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::GR2); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::GR3); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::GR4); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::GR5); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::GR6); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::GR7); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::GR8); wait(delay);
+
+          wait(delay);
+          
+          // Icons on             
+          LKM1638.setIcon(TM1638_LKM1638::YL1); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::YL2); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::YL3); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::YL4); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::YL5); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::YL6); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::YL7); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::YL8); wait(delay);
+
+          wait(delay);
+ 
+          // Icons off 
+          // Note: Yellow is hardly recognisable. Red LED is much brighter than Green
+          LKM1638.clrIcon(TM1638_LKM1638::YL1); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::YL2); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::YL3); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::YL4); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::YL5); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::YL6); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::YL7); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::YL8); wait(delay);
+
+//          wait(1);          
+//          LKM1638.cls(); // clear all, preserve Icons
+          pc.printf("Show all icons done\r\n");                    
+#endif           
+        }       
+
+        if (keydata[LKM1638_SW5_IDX] == LKM1638_SW5_BIT) { //sw5  
+          LKM1638.cls(); // clear all, preserve Icons
+
+#if(0)
+          LKM1638.printf("Count");                            
+          for (int cnt=0; cnt<=0xFF; cnt++) {
+            LKM1638.locate(6);
+            LKM1638.printf("%02x", cnt);
+            wait(0.2);
+          }  
+#endif
+
+
+//          LKM1638.writeData(hello_str);
+          LKM1638.printf("hello");                  
+        }
+
+        if (keydata[LKM1638_SW6_IDX] == LKM1638_SW6_BIT) { //sw6  
+          LKM1638.cls(); // clear all, preserve Icons
+//          LKM1638.writeData(bye_str);
+          LKM1638.printf("Bye");          
+        }
+        
+        if (keydata[LKM1638_SW7_IDX] == LKM1638_SW7_BIT) { //sw7  
+          LKM1638.cls(); // clear all, preserve Icons
+          LKM1638.printf("%2.3f", -0.1234);  // test decimal point display                    
+          wait(0.5);
+          LKM1638.cls(); // clear all, preserve Icons          
+          LKM1638.printf("%2.3f", -012.345);  // test decimal point display
+        }
+        
+        if (keydata[LKM1638_SW8_IDX] == LKM1638_SW8_BIT) { //sw8  
+          float delay=0.1;
+          // Icons on             
+          LKM1638.setIcon(TM1638_LKM1638::DP1); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::DP2); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::DP3); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::DP4); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::DP5); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::DP6); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::DP7); wait(delay);
+          LKM1638.setIcon(TM1638_LKM1638::DP8); wait(delay);
+
+          wait(delay);
+ 
+          // Icons off      
+          LKM1638.clrIcon(TM1638_LKM1638::DP8); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::DP7); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::DP6); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::DP5); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::DP4); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::DP3); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::DP2); wait(delay);
+          LKM1638.clrIcon(TM1638_LKM1638::DP1); wait(delay);          
+        }
+     
+      } //if Key
+
+      myled = !myled;
+      wait(0.3);      
+    } //while
+}
+#endif
+
+
 
 #if (TM1638_TEST == 1)
 // Direct TM1638 Test
@@ -414,7 +1017,7 @@
               TM1638.cls(); 
 
               bits = 0x01 << bit;              
-              TM1638.writeData(i, bits);
+              TM1638.writeData(bits, i);
 
               pc.printf("Idx = %d, Bits = 0x%02x\r\n", i, bits);              
 //              wait(0.5);