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:
Mon Dec 21 22:39:22 2015 +0000
Child:
1:1f2453ed85d7
Commit message:
First version

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
TM1638_Config.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Font_7Seg.cpp	Mon Dec 21 22:39:22 2015 +0000
@@ -0,0 +1,69 @@
+/* mbed LED Font Library, for TM1638 LED controller
+ * Copyright (c) 2015, v01: WH, Initial version, Test in LEDKEY8
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "Font_7Seg.h"
+
+// Select one of the testboards for TM1638 LED controller
+#if (LEDKEY8_TEST == 1) 
+
+//Mask for blending out and restoring Icons
+const char MASK_ICON_GRID[][2] = {
+                                   {LO(S7_ICON_GR1), HI(S7_ICON_GR1)},
+                                   {LO(S7_ICON_GR2), HI(S7_ICON_GR2)},
+                                   {LO(S7_ICON_GR3), HI(S7_ICON_GR3)},
+                                   {LO(S7_ICON_GR4), HI(S7_ICON_GR4)},
+                                   {LO(S7_ICON_GR5), HI(S7_ICON_GR5)},
+                                   {LO(S7_ICON_GR6), HI(S7_ICON_GR6)},
+                                   {LO(S7_ICON_GR7), HI(S7_ICON_GR7)},
+                                   {LO(S7_ICON_GR8), HI(S7_ICON_GR8)},
+                                   {LO(S7_ICON_GR9), HI(S7_ICON_GR9)},
+                                   {LO(S7_ICON_GR10), HI(S7_ICON_GR10)}                                   
+                                 };
+
+
+
+
+// ASCII Font definition table for transmission to TM1638
+//
+//#define FONT_7S_START     0x20
+//#define FONT_7S_END       0x7F
+//#define FONT_7S_NR_CHARS (FONT_7_END - FONT_7S_START + 1)
+ 
+const char FONT_7S[] = { 
+                         C7_0, //48 0x30
+                         C7_1,
+                         C7_2,
+                         C7_3,
+                         C7_4,                   
+                         C7_5,
+                         C7_6,
+                         C7_7,
+                         C7_8,
+                         C7_9,
+                         C7_A, //65 0x41, A
+                         C7_B,
+                         C7_C,
+                         C7_D,
+                         C7_E,
+                         C7_F
+                             };// 127                             
+  
+#endif 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Font_7Seg.h	Mon Dec 21 22:39:22 2015 +0000
@@ -0,0 +1,181 @@
+/* mbed LED Font Library, for TM1638 LED Controller
+ * Copyright (c) 2015, v01: WH, Initial version, Test in LEDKEY8
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef MBED_FONT_7SEG_H
+#define MBED_FONT_7SEG_H
+
+// Select one of the testboards for TM1638 controller
+#include "TM1638_Config.h"
+
+#if ((LEDKEY8_TEST == 1) || (TM1638_TEST == 1))
+// Segment bit positions for 7 Segment display using the LEDKEY8 mapping for TM1638
+// Modify this table for different 'bit-to-segment' mappings. The ASCII character defines and the FONT_7S const table below 
+// will be adapted automatically according to the bit-to-segment mapping. Obviously this will only work when the segment
+// mapping is identical for every digit position. This will be the case unless the hardware designer really hates software developers.
+//
+#define S7_A    0x0001
+#define S7_B    0x0002
+#define S7_C    0x0004
+#define S7_D    0x0008
+#define S7_E    0x0010
+#define S7_F    0x0020
+#define S7_G    0x0040 
+
+//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}
+
+//Icons Grid 1
+#define S7_DP1  0x0080
+#define S7_LD1  0x0100
+#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)
+
+//Icons Grid 3
+#define S7_DP3  0x0080
+#define S7_LD3  0x0100
+#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)
+
+//Icons Grid 5
+#define S7_DP5  0x0080
+#define S7_LD5  0x0100
+#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)
+
+//Icons Grid 7
+#define S7_DP7  0x0080
+#define S7_LD7  0x0100
+#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)
+
+//Icons Grid 9
+#define S7_DP9  0x0080
+#define S7_LD9  0x0100
+#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)
+
+//Mask for blending out and restoring Icons
+extern const char MASK_ICON_GRID[][2]; 
+#endif
+
+
+// ASCII Font definitions for segments in each character
+//
+//32 0x20  Symbols
+#define C7_SPC  ()
+
+//48 0x30  Digits
+#define C7_0    (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F)
+#define C7_1    (S7_B | S7_C)
+#define C7_2    (S7_A | S7_B | S7_D | S7_E | S7_G)
+#define C7_3    (S7_A | S7_B | S7_C | S7_D | S7_G)
+#define C7_4    (S7_B | S7_C | S7_F | S7_G)
+#define C7_5    (S7_A | S7_C | S7_D | S7_F | S7_G)
+#define C7_6    (S7_A | S7_C | S7_D | S7_E | S7_F | S7_G)
+#define C7_7    (S7_A | S7_B | S7_C)
+#define C7_8    (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F | S7_G)
+#define C7_9    (S7_A | S7_B | S7_C | S7_D | S7_F | S7_G)
+//64 0x40  Upper case alphabet
+#define C7_A    (S7_A | S7_B | S7_C | S7_E | S7_F | S7_G )
+#define C7_B    (S7_C | S7_D | S7_E | S7_F | S7_G)
+#define C7_C    (S7_A | S7_D | S7_E | S7_F)
+#define C7_D    (S7_B | S7_C | S7_D | S7_E | S7_G)
+#define C7_E    (S7_A | S7_D | S7_E | S7_F | S7_G)
+#define C7_F    (S7_A | S7_E | S7_F | S7_G)
+
+#define C7_G    (S7_A | S7_B | S7_C | S7_F | S7_G)
+#define C7_H    (S7_B | S7_C | S7_E | S7_F | S7_G)
+#define C7_I    (S7_B | S7_C)
+#define C7_J    (S7_B | S7_C | S7_D)
+#define C7_L    (S7_D | S7_E | S7_F)
+#define C7_M    (S7_C | S7_E | S7_G)
+#define C7_N    (S7_C | S7_E | S7_G)
+#define C7_O    (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F)
+#define C7_P    (S7_A | S7_B | S7_E | S7_F | S7_G)
+#define C7_Q    (S7_A | S7_B | S7_C | S7_F | S7_G)
+#define C7_R    (S7_E | S7_G)
+#define C7_S    (S7_A | S7_C | S7_D | S7_F | S7_G)
+#define C7_T    (S7_D | S7_E | S7_F | S7_G)
+#define C7_U    (S7_B | S7_C | S7_D | S7_E | S7_F)
+#define C7_Y    (S7_B | S7_C | S7_D | S7_F | S7_G)
+#define C7_Z    (S7_A | S7_B | S7_D | S7_E | S7_G)
+
+//97 0x61  Lower case alphabet
+#define C7_a     C7_A
+#define C7_b     C7_B
+#define C7_c     C7_C
+#define C7_d     C7_D
+#define C7_e     C7_E
+#define C7_f     C7_H
+
+#define C7_g     C7_G
+#define C7_h     C7_H
+#define C7_i     C7_I
+#define C7_j     C7_J
+#define C7_l     C7_L
+#define C7_m     C7_M
+#define C7_n     C7_N
+#define C7_o     C7_O
+#define C7_p     C7_P
+#define C7_q     C7_Q
+#define C7_r     C7_R
+#define C7_s     C7_S
+#define C7_t     C7_T
+#define C7_u     C7_U
+#define C7_y     C7_Y
+#define C7_z     C7_Z
+
+
+//User Defined Characters (some examples)
+                                                                          
+// Font data selection for transmission to TM1638 memory
+#define LO(x)  ( x & 0xFF)
+#define HI(x)  ((x >> 8) & 0xFF)
+
+
+// ASCII Font definition table
+//
+//#define FONT_7S_START     0x20
+//#define FONT_7S_END       0x7F
+//#define FONT_7S_NR_CHARS (FONT_7S_END - FONT_7S_START + 1)
+extern const char FONT_7S[]; 
+
+#endif    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TM1638.cpp	Mon Dec 21 22:39:22 2015 +0000
@@ -0,0 +1,503 @@
+/* mbed TM1638 Library, for TM1638 LED controller
+ * Copyright (c) 2015, v01: WH, Initial version
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "mbed.h" 
+#include "TM1638.h"
+
+/** Constructor for class for driving TM1638 LED controller with SPI bus interface device. 
+ *  @brief Supports 8 digits @ 10 segments. 
+ *         Also supports a scanned keyboard of upto 24 keys.
+ *   
+ *  @param  PinName mosi, miso, sclk, cs SPI bus pins
+*/
+TM1638::TM1638(PinName mosi, PinName miso, PinName sclk, PinName cs) : _spi(mosi,miso,sclk), _cs(cs) {
+
+  _init();
+}
+
+/** Init the SPI interface and the controller
+  * @param  none
+  * @return none
+  */ 
+void TM1638::_init(){
+  
+//init SPI
+  _cs=1;
+  _spi.format(8,3); //TM1638 uses mode 3 (Clock High on Idle, Data latched on second (=rising) edge)
+  _spi.frequency(500000);   
+
+//init controller  
+  _display = TM1638_DSP_ON;
+  _bright  = TM1638_BRT_DEF; 
+  _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright );                                 // Display control cmd, display on/off, brightness   
+  
+  _writeCmd(TM1638_DATA_SET_CMD, TM1638_DATA_WR | TM1638_ADDR_INC | TM1638_MODE_NORM); // Data set cmd, normal mode, auto incr, write data  
+}   
+
+
+/** Clear the screen and locate to 0
+ */  
+void TM1638::cls() {
+  
+  _cs=0;
+  wait_us(1);    
+  _spi.write(_flip(TM1638_ADDR_SET_CMD | 0x00)); // Address set cmd, 0
+      
+  for (int cnt=0; cnt<TM1638_DISPLAY_MEM; cnt++) {
+    _spi.write(0x00); // data 
+  }
+  
+  wait_us(1);
+  _cs=1;      
+}  
+
+/** Set Brightness
+  *
+  * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)  
+  * @return none
+  */
+void TM1638::setBrightness(char brightness){
+
+  _bright = brightness & TM1638_BRT_MSK; // mask invalid bits
+  
+  _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright );  // Display control cmd, display on/off, brightness  
+}
+
+/** Set the Display mode On/off
+  *
+  * @param bool display mode
+  */
+void TM1638::setDisplay(bool on) {
+  
+  if (on) {
+    _display = TM1638_DSP_ON;
+  }
+  else {
+    _display = TM1638_DSP_OFF;
+  }
+  
+  _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright );  // Display control cmd, display on/off, brightness   
+}
+
+/** Write databyte to TM1638
+  *  @param  int address display memory location to write byte
+  *  @param  char data byte written at given address
+  *  @return none
+  */ 
+void TM1638::writeData(int address, char data) {
+  _cs=0;
+  wait_us(1);    
+  _spi.write(_flip(TM1638_ADDR_SET_CMD | (address & TM1638_ADDR_MSK))); // Set Address cmd
+      
+  _spi.write(_flip(data)); // data 
+  
+  wait_us(1);
+  _cs=1;             
+}
+
+/** 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 (valide range 0..TM1638_DISPLAY_MEM (=16), starting at address 0)     
+  *  @return none
+  */ 
+void TM1638::writeData(DisplayData_t data, int length) {
+  _cs=0;
+  wait_us(1);    
+  _spi.write(_flip(TM1638_ADDR_SET_CMD | 0x00)); // Set Address at 0
+      
+// sanity check
+  if (length < 0) {length = 0;}
+  if (length > TM1638_DISPLAY_MEM) {length = TM1638_DISPLAY_MEM;}
+
+//  for (int idx=0; idx<TM1638_DISPLAY_MEM; idx++) {  
+  for (int idx=0; idx<length; idx++) {    
+    _spi.write(_flip(data[idx])); // data 
+  }
+  
+  wait_us(1);
+  _cs=1;             
+}
+
+
+/** 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
+  *
+  * Note: Due to the hardware configuration the TM1638 key matrix scanner will detect multiple keys pressed at same time,
+  *       but this may also result in some spurious keys being set in keypress data array.
+  *       It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting.  
+  */ 
+bool TM1638::getKeys(KeyData_t *keydata) {
+  int keypress = 0;
+  char data;
+
+  // Read keys
+  _cs=0;
+  wait_us(1);    
+  
+  // Enable Key Read mode
+  _spi.write(_flip(TM1638_DATA_SET_CMD | TM1638_KEY_RD | TM1638_ADDR_INC | TM1638_MODE_NORM)); // Data set cmd, normal mode, auto incr, read data
+
+  for (int idx=0; idx < TM1638_KEY_MEM; idx++) {
+    data = _flip(_spi.write(0xFF));    // read keys and correct bitorder
+
+    if (data != 0) {  // Check for any pressed key
+      for (int bit=0; bit < TM1638_KEY_BITS; bit++) {
+        if (data & (1 << bit)) {keypress++;} // Test all significant bits
+      }
+    }  
+
+    (*keydata)[idx] = data;            // Store keydata after correcting bitorder
+  }
+
+  wait_us(1);
+  _cs=1;    
+
+  // Restore Data Write mode
+  _writeCmd(TM1638_DATA_SET_CMD, TM1638_DATA_WR | TM1638_ADDR_INC | TM1638_MODE_NORM); // Data set cmd, normal mode, auto incr, write data  
+      
+#if(1)
+// Dismiss multiple keypresses at same time
+  return (keypress == 1);    
+#else
+// Allow multiple keypress and accept possible spurious keys
+  return (keypress > 0);
+#endif  
+}
+    
+
+/** Helper to reverse all command or databits. The TM1638 expects LSB first, whereas SPI is MSB first
+  *  @param  char data
+  *  @return bitreversed data
+  */ 
+char TM1638::_flip(char data) {
+ char value=0;
+  
+ if (data & 0x01) {value |= 0x80;} ;  
+ if (data & 0x02) {value |= 0x40;} ;
+ if (data & 0x04) {value |= 0x20;} ;
+ if (data & 0x08) {value |= 0x10;} ;
+ if (data & 0x10) {value |= 0x08;} ;
+ if (data & 0x20) {value |= 0x04;} ;
+ if (data & 0x40) {value |= 0x02;} ;
+ if (data & 0x80) {value |= 0x01;} ;
+ return value;       
+}
+
+
+/** Write command and parameter to TM1638
+  *  @param  int cmd Command byte
+  *  &Param  int data Parameters for command
+  *  @return none
+  */  
+void TM1638::_writeCmd(int cmd, int data){
+    
+  _cs=0;
+  wait_us(1);    
+//  _spi.write(_flip( (cmd & 0xF0) | (data & 0x0F)));  
+  _spi.write(_flip( (cmd & TM1638_CMD_MSK) | (data & ~TM1638_CMD_MSK)));   
+ 
+  wait_us(1);
+  _cs=1;          
+}  
+
+
+#if (LEDKEY8_TEST == 1) 
+// Derived class for TM1638 used in LED&KEY display unit
+//
+
+/** Constructor for class for driving TM1638 LED controller as used in LEDKEY8
+  *
+  *  @brief Supports 8 Digits of 7 Segments + DP + LED Icons. Also supports a scanned keyboard of 8.
+  *   
+  *  @param  PinName mosi, miso, sclk, cs SPI bus pins
+  */
+TM1638_LEDKEY8::TM1638_LEDKEY8(PinName mosi, PinName miso, PinName sclk, PinName cs) : TM1638(mosi, miso, sclk, cs) {
+  _column  = 0;
+  _columns = LEDKEY8_NR_DIGITS;    
+}  
+
+#if(0)
+#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
+#endif
+
+/** Locate cursor to a screen column
+  *
+  * @param column  The horizontal position from the left, indexed from 0
+  */
+void TM1638_LEDKEY8::locate(int column) {
+  //sanity check
+  if (column < 0) {column = 0;}
+  if (column > (_columns - 1)) {column = _columns - 1;}  
+  
+  _column = column;       
+}
+
+
+/** Number of screen columns
+  *
+  * @param none
+  * @return columns
+  */
+int TM1638_LEDKEY8::columns() {
+    return _columns;
+}
+
+    
+/** Clear the screen and locate to 0
+  * @param bool clrAll Clear Icons also (default = false)
+  */ 
+void TM1638_LEDKEY8::cls(bool clrAll) {  
+
+  if (clrAll) {
+    //clear local buffer (including Icons)
+    for (int idx=0; idx < (LEDKEY8_NR_GRIDS << 1); idx++) {
+      _displaybuffer[idx] = 0x00;  
+    }
+  }  
+  else {
+    //clear local buffer (preserving Icons)
+    for (int idx=0; idx < LEDKEY8_NR_GRIDS; idx++) {
+      _displaybuffer[(idx<<1)]     = _displaybuffer[(idx<<1)]     & MASK_ICON_GRID[idx][0];  
+      _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1];
+    }  
+  }
+
+  writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2));
+
+  _column = 0;   
+}     
+
+/** Set Icon
+  *
+  * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 24 LSBs
+  * @return none
+  */
+void TM1638_LEDKEY8::setIcon(Icon icon) {
+  int addr, icn;
+
+   icn =        icon  & 0xFFFFFF;
+  addr = (icon >> 24) & 0xFF; 
+  addr = (addr - 1) << 1;   
+    
+  //Save char...and set bits for icon to write
+  _displaybuffer[addr]   = _displaybuffer[addr]   | LO(icn);      
+  _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn);      
+  writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2));
+}
+
+/** Clr Icon
+  *
+  * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 24 LSBs
+  * @return none
+  */
+void TM1638_LEDKEY8::clrIcon(Icon icon) {
+  int addr, icn;
+
+   icn =        icon  & 0xFFFFFF;
+  addr = (icon >> 24) & 0xFF; 
+  addr = (addr - 1) << 1;   
+    
+  //Save char...and clr bits for icon to write
+  _displaybuffer[addr]   = _displaybuffer[addr]   & ~LO(icn);      
+  _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn);      
+  writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2));
+}
+
+
+/** 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 (8 bits)       
+  */
+void TM1638_LEDKEY8::setUDC(unsigned char udc_idx, int udc_data) {
+
+  //Sanity check
+  if (udc_idx > (LEDKEY8_NR_UDC-1)) {
+    return;
+  }
+  // Mask out Icon bits?
+
+  _UDC_7S[udc_idx] = LO(udc_data);
+}
+
+
+#if(1)
+/** Write a single character (Stream implementation)
+  */
+int TM1638_LEDKEY8::_putc(int value) {
+    int addr;
+    bool validChar = false;
+    char pattern   = 0x00;
+    
+    if ((value == '\n') || (value == '\r')) {
+      //No character to write
+      validChar = false;
+      
+      //Update Cursor      
+      _column = 0;
+    }
+    else if ((value >= 0) && (value < LEDKEY8_NR_UDC)) {
+      //Character to write
+      validChar = true;
+      pattern = _UDC_7S[value];
+    }  
+    else if ((value >= (int)'0') && (value <= (int) '9')) {   
+      //Character to write
+      validChar = true;
+      pattern = FONT_7S[value - (int) '0'];
+    }
+    else if ((value >= (int) 'A') && (value <= (int) 'F')) {   
+      //Character to write
+      validChar = true;
+      pattern = FONT_7S[10 + value - (int) 'A'];
+    }
+    else if ((value >= (int) 'a') && (value <= (int) 'f')) {   
+      //Character to write
+      validChar = true;
+      pattern = FONT_7S[10 + value - (int) 'a'];
+    } //else
+
+    if (validChar) {
+      //Character to write
+ 
+      //Translate between _column and displaybuffer entries
+      addr = _column << 1;
+
+      //Save icons...and set bits for character to write
+      _displaybuffer[addr]   = (_displaybuffer[addr]   & MASK_ICON_GRID[_column][0]) | pattern;
+//      _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][0]) | pattern;
+
+      writeData(_displaybuffer, (LEDKEY8_NR_GRIDS*2));
+                                
+      //Update Cursor
+      _column++;
+      if (_column > (LEDKEY8_NR_DIGITS - 1)) {
+        _column = 0;
+      }
+
+    } // if validChar           
+
+    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() {
+    return -1;
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TM1638.h	Mon Dec 21 22:39:22 2015 +0000
@@ -0,0 +1,337 @@
+/* mbed TM1638 Library, for TM1638 LED controller
+ * Copyright (c) 2015, v01: WH, Initial version
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef TM1638_H
+#define TM1638_H
+
+// Select one of the testboards for TM1638 LED controller
+#include "TM1638_Config.h"
+
+/** An interface for driving TM1638 LED controller
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "TM1638.h" 
+ * 
+ * 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  
+ * TM1638::KeyData_t keydata; 
+ *
+ * // TM1638 declaration
+ * TM1638 TM1638(p5,p6,p7, p8);
+ *
+ * int main() {
+ *   TM1638.cls(); 
+ *   TM1638.writeData(all_str);
+ *   wait(1);
+ *   TM1638.setBrightness(TM1638_BRT0);
+ *   wait(1);
+ *   TM1638.setBrightness(TM1638_BRT3);
+ *
+ *   while (1) {
+ *    // Check and read keydata
+ *    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   
+ *        TM1638.cls(); 
+ *        TM1638.writeData(all_str);
+ *      }  
+ *    } 
+ *   }   
+ * }
+ * @endcode
+ */
+
+//Memory size in bytes for Display and Keymatrix
+#define TM1638_DISPLAY_MEM    16
+#define TM1638_KEY_MEM         4
+//Significant bits Keymatrix data
+#define TM1638_KEY_BITS        6 
+
+//Reserved bits for commands
+#define TM1638_CMD_MSK      0xC0
+
+//Data setting commands
+#define TM1638_DATA_SET_CMD 0x40
+#define TM1638_DATA_WR      0x00
+#define TM1638_KEY_RD       0x02
+#define TM1638_ADDR_INC     0x00
+#define TM1638_ADDR_FIXED   0x04
+#define TM1638_MODE_NORM    0x00
+#define TM1638_MODE_TEST    0x08
+
+//Address setting commands
+#define TM1638_ADDR_SET_CMD 0xC0
+#define TM1638_ADDR_MSK     0x0F
+
+//Display control commands
+#define TM1638_DSP_CTRL_CMD 0x80
+#define TM1638_BRT_MSK      0x07
+#define TM1638_BRT0         0x00 //Pulsewidth 1/16
+#define TM1638_BRT1         0x01
+#define TM1638_BRT2         0x02
+#define TM1638_BRT3         0x03
+#define TM1638_BRT4         0x04
+#define TM1638_BRT5         0x05
+#define TM1638_BRT6         0x06
+#define TM1638_BRT7         0x07 //Pulsewidth 14/16
+
+#define TM1638_BRT_DEF      TM1638_BRT3
+
+#define TM1638_DSP_OFF      0x00
+#define TM1638_DSP_ON       0x08
+
+
+/** A class for driving TM1638 LED controller
+ *
+ * @brief Supports 8 Grids @ 10 Segments. 
+ *        Also supports a scanned keyboard of upto 24 keys.
+ *        SPI bus interface device. 
+ */
+class TM1638 {
+ public:
+
+  /** Datatypes for display and keymatrix data */
+  typedef char DisplayData_t[TM1638_DISPLAY_MEM];
+  typedef char KeyData_t[TM1638_KEY_MEM];
+    
+ /** Constructor for class for driving TM1638 LED controller
+  *
+  * @brief Supports 8 Grids @ 10 segments. 
+  *        Also supports a scanned keyboard of upto 24 keys.
+  *        SPI bus interface device. 
+  *
+  *  @param  PinName mosi, miso, sclk, cs SPI bus pins
+  */
+  TM1638(PinName mosi, PinName miso, PinName sclk, PinName cs);
+      
+  /** Clear the screen and locate to 0
+   */ 
+  void cls();  
+
+  /** Write databyte to TM1638
+   *  @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); 
+ 
+ /** 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
+   *
+   * Note: Due to the hardware configuration the TM1638 key matrix scanner will detect multiple keys pressed at same time,
+   *       but this may result in some spurious keys also being set in keypress data array.
+   *       It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting.
+   */   
+  bool getKeys(KeyData_t *keydata);
+
+  /** Set Brightness
+    *
+    * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)  
+    * @return none
+    */
+  void setBrightness(char brightness = TM1638_BRT_DEF);
+  
+  /** Set the Display mode On/off
+    *
+    * @param bool display mode
+    */
+  void setDisplay(bool on);
+  
+ private:  
+  SPI _spi;
+  DigitalOut _cs;
+  char _display;
+  char _bright; 
+  
+  /** Init the SPI interface and the controller
+    * @param  none
+    * @return none
+    */ 
+  void _init();
+
+  /** Helper to reverse all command or databits. The TM1638 expects LSB first, whereas SPI is MSB first
+    *  @param  char data
+    *  @return bitreversed data
+    */ 
+  char _flip(char data);
+
+  /** Write command and parameter to TM1638
+    *  @param  int cmd Command byte
+    *  &Param  int data Parameters for command
+    *  @return none
+    */ 
+  void _writeCmd(int cmd, int data);  
+};
+
+
+#if (LEDKEY8_TEST == 1) 
+// Derived class for TM1638 used in LED&KEY display unit
+//
+
+#include "Font_7Seg.h"
+
+#define LEDKEY8_NR_GRIDS  8
+#define LEDKEY8_NR_DIGITS 8
+#define LEDKEY8_DIG1_IDX  1
+#define LEDKEY8_NR_UDC    8
+
+/** 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.
+  *  
+  *  @param  PinName mosi, miso, sclk, cs SPI bus pins
+  */
+class TM1638_LEDKEY8 : public TM1638, public Stream {
+ public:
+
+  /** Enums for Icons */
+  //  Grid encoded in 8 MSBs, Icon pattern encoded in 24 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,
+                                
+    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                            
+  };
+  
+  typedef char UDCData_t[LEDKEY8_NR_UDC];
+  
+ /** Constructor for class for driving TM1638 LED controller as used in LEDKEY8
+   *
+   * @brief Supports 8 Digits of 7 Segments + DP + LED Icons. Also supports a scanned keyboard of 8 keys.
+   *  
+   * @param  PinName mosi, miso, sclk, cs SPI bus pins
+   */
+  TM1638_LEDKEY8(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 24 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
+     * @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  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);
+    }        
+
+   /** 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)   
+    *  @return none
+    */   
+    void writeData(DisplayData_t data, int length = (LEDKEY8_NR_GRIDS*2)) {
+      TM1638::writeData(data, length);
+    }  
+
+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
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TM1638_Config.h	Mon Dec 21 22:39:22 2015 +0000
@@ -0,0 +1,30 @@
+/* mbed TM1638 Library, for TM1638 LEDcontroller
+ * Copyright (c) 2015, v01: WH, Initial version
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef TM1638_CONFIG_H
+#define TM1638_CONFIG_H
+
+// Select one of the testboards for TM1638 LED controller
+#define TM1638_TEST  0
+#define LEDKEY8_TEST 1 
+
+#endif
\ No newline at end of file