Library for TM1651 LED controller Initial version, Battery monitor

Dependents:   mbed_TM1651

See here for more information.

Files at this revision

API Documentation at this revision

Comitter:
wim
Date:
Wed Oct 04 19:54:30 2017 +0000
Child:
1:799f85133209
Commit message:
Library for TM1651 LED controller; Initial version, Battery monitor

Changed in this revision

TM1651.cpp Show annotated file Show diff for this revision Revisions of this file
TM1651.h Show annotated file Show diff for this revision Revisions of this file
TM1651_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/TM1651.cpp	Wed Oct 04 19:54:30 2017 +0000
@@ -0,0 +1,413 @@
+/* mbed TM1651 Library, for TM1651 LED controller
+ * Copyright (c) 2016, v01: WH, Initial version, Battery monitor
+ *
+ * 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, inclumosig 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, INCLUmosiG 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 "TM1651.h"
+
+
+/** Constructor for class for driving TM1651 LED controller with Serial bus interface device. 
+ *  @brief Supports 4 digits @ 7 segments and 7 Keys. 
+ *   
+ *  @param  PinName dio Serial bus DIO pin 
+ *  @param  PinName clk Serial bus CLK pin
+*/
+TM1651::TM1651(PinName dio, PinName clk) : _dio(dio), _clk(clk) {
+
+  _init();
+}
+
+
+/** Init the Serial interface and the controller
+  * @param  none
+  * @return none
+  */ 
+void TM1651::_init(){
+  
+//TM1651 uses a Serial bus that looks like I2C, but really is not.
+//It has Start and Stop conditions like I2C and an Ack pulse, but instead of slaveaddresses and a RW bit it just transmits commands and data.
+
+//init Serial bus
+  _dio.output();
+//  _dio.mode(PullUp);
+  wait_us(1);
+
+  _dio=1;  
+  _clk=1;  
+
+//init controller  
+  _display = TM1651_DSP_ON;
+  _bright  = TM1651_BRT_DEF; 
+  _writeCmd(TM1651_DSP_CTRL_CMD, _display | _bright );                                 // Display control cmd, display on/off, brightness   
+  
+  _writeCmd(TM1651_DATA_SET_CMD, TM1651_DATA_WR | TM1651_ADDR_INC | TM1651_MODE_NORM); // Data set cmd, normal mode, auto incr, write data  
+}   
+
+
+/** Clear the screen and locate to 0
+ */  
+void TM1651::cls() {
+
+  _start();  
+
+  _write(TM1651_ADDR_SET_CMD | 0x00); // Address set cmd, 0      
+  for (int cnt=0; cnt<TM1651_DISPLAY_MEM; cnt++) {
+    _write(0x00); // data 
+  }
+
+  _stop();  
+}  
+
+/** Set Brightness
+  *
+  * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)  
+  * @return none
+  */
+void TM1651::setBrightness(char brightness){
+
+  _bright = brightness & TM1651_BRT_MSK; // mask invalid bits
+  
+  _writeCmd(TM1651_DSP_CTRL_CMD, _display | _bright );  // Display control cmd, display on/off, brightness  
+}
+
+/** Set the Display mode On/off
+  *
+  * @param bool display mode
+  */
+void TM1651::setDisplay(bool on) {
+  
+  if (on) {
+    _display = TM1651_DSP_ON;
+  }
+  else {
+    _display = TM1651_DSP_OFF;
+  }
+  
+  _writeCmd(TM1651_DSP_CTRL_CMD, _display | _bright );  // Display control cmd, display on/off, brightness   
+}
+
+/** Write databyte to TM1651
+  *  @param  int address display memory location to write byte
+  *  @param  char data byte written at given address
+  *  @return none
+  */ 
+void TM1651::writeData(char data, int address) {
+  
+  _start();
+
+  _write(TM1651_ADDR_SET_CMD | (address & TM1651_ADDR_MSK)); // Set Address cmd     
+  _write(data); // data 
+
+  _stop();  
+}
+
+/** Write Display datablock to TM1651
+  *  @param  DisplayData_t data Array of TM1651_DISPLAY_MEM (=4) bytes for displaydata
+  *  @param  length number bytes to write (valid range 0..(TM1651_MAX_NR_GRIDS * TM1651_BYTES_PER_GRID) (=4), when starting at address 0)  
+  *  @param  int address display memory location to write bytes (default = 0) 
+  *  @return none
+  */  
+void TM1651::writeData(DisplayData_t data, int length, int address) {
+
+  _start();
+
+// sanity check
+  address &= TM1651_ADDR_MSK;
+  if (length < 0) {length = 0;}
+  if ((length + address) > TM1651_DISPLAY_MEM) {length = (TM1651_DISPLAY_MEM - address);}
+    
+//  _write(TM1651_ADDR_SET_CMD | 0x00); // Set Address at 0
+  _write(TM1651_ADDR_SET_CMD | address); // Set Address
+  
+  for (int idx=0; idx<length; idx++) {    
+//    _write(data[idx]); // data 
+    _write(data[address + idx]); // data 
+  }
+  
+  _stop();  
+}
+
+/** Read keydata block from TM1651
+  *  @param  *keydata Ptr to bytes for keydata
+  *  @return bool keypress True when at least one key was pressed
+  */   
+bool TM1651::getKeys(KeyData_t *keydata) {
+
+  _start();
+
+  // Enable Key Read mode
+  _write(TM1651_DATA_SET_CMD | TM1651_KEY_RD | TM1651_ADDR_INC | TM1651_MODE_NORM); // Data set cmd, normal mode, auto incr, read data
+
+  // Read keys
+  // Bitpattern S0 S1 S2 K1 K2 1 1 1  
+  *keydata = _read();
+//  printf("Key = 0x%02x\r\n", *keydata);
+
+  _stop();  
+  
+  // Restore Data Write mode
+  _writeCmd(TM1651_DATA_SET_CMD, TM1651_DATA_WR | TM1651_ADDR_INC | TM1651_MODE_NORM); // Data set cmd, normal mode, auto incr, write data  
+      
+  return (*keydata != TM1651_SW_NONE);    
+}
+  
+
+/** Generate Start condition for TM1651
+  *  @param  none
+  *  @return none
+  */ 
+void TM1651::_start() {
+
+  _dio=0;
+  wait_us(1);
+  _clk=0;
+  wait_us(1);
+}
+  
+/** Generate Stop condition for TM1651
+  *  @param  none
+  *  @return none
+  */ 
+void TM1651::_stop() {
+
+  _dio=0;
+  wait_us(1);  
+  _clk=1;
+  wait_us(1);
+  _dio=1;
+  wait_us(1);
+}
+
+/** Send byte to TM1651
+  *  @param  int data
+  *  @return none
+  */ 
+void TM1651::_write(int data) {
+ 
+  for (int bit=0; bit<8; bit++) {    
+    //The TM1651 expects LSB first
+    if (((data >> bit) & 0x01) == 0x01) {
+      _dio=1;      
+    }
+    else {    
+      _dio=0;      
+    }  
+    wait_us(1);
+    _clk=1;
+    wait_us(1);
+    _clk=0;  
+    wait_us(1);
+  }  
+
+  _dio=1;
+  
+  // Prepare DIO to read data
+  _dio.input();
+  wait_us(3);
+      
+  // dummy Ack
+  _clk=1;
+  wait_us(1);
+//  _ack = _dio;  
+  _clk=0;  
+  wait_us(1); 
+  
+  // Return DIO to output mode
+  _dio.output();  
+  wait_us(3);  
+
+  _dio=1; //idle  
+}
+
+/** Read byte from TM1651
+  *  @return read byte 
+  */ 
+char TM1651::_read() {
+  char keycode = 0;
+
+  // Prepare DIO to read data
+  _dio.input();
+  wait_us(3);
+    
+  for (int bit=0; bit<8; bit++) {    
+   
+    //The TM1651 sends bitpattern: S0 S1 S2 K1 K2 1 1 1
+    //Data is shifted out by the TM1651 on the falling edge of CLK
+    //Observe sufficient delay to allow the Open Drain DIO to rise to H levels
+    // Prepare to read next bit, LSB (ie S0) first. 
+    // The code below flips bits for easier matching with datasheet
+    keycode = keycode << 1;  
+
+    _clk=1;
+    wait_us(1);
+    
+    // Read next bit
+    if (_dio) { keycode |= 0x01; }        
+
+    _clk=0;        
+    wait_us(5); // Delay to allow for slow risetime
+  }  
+  
+  // Return DIO to output mode
+  _dio.output();
+  wait_us(3);  
+
+  // dummy Ack
+  _dio=0; //Ack   
+  wait_us(1);
+  
+  _clk=1;
+  wait_us(1);
+  _clk=0;  
+  wait_us(1); 
+
+  _dio=1; //idle
+
+  return keycode;
+}
+
+/** Write command and parameter to TM1651
+  *  @param  int cmd Command byte
+  *  &Param  int data Parameters for command
+  *  @return none
+  */  
+void TM1651::_writeCmd(int cmd, int data){
+    
+  _start();
+
+  _write((cmd & TM1651_CMD_MSK) | (data & ~TM1651_CMD_MSK));   
+ 
+  _stop();          
+}  
+
+
+#if (OPENSMART_TEST == 1) 
+// Derived class for TM1651 used in OPEN_SMART battery display unit
+//
+
+/** Constructor for class for driving TM1651 LED controller
+ *
+ * @brief Supports battery display unit with 10 segments.
+ *        Serial bus interface device. 
+ *
+ *  @param  PinName dio Serial bus DIO pin
+ *  @param  PinName sck Serial bus CLK pin 
+ */
+TM1651_OPENSMART::TM1651_OPENSMART(PinName dio, PinName clk) : TM1651(dio, clk) {
+
+}  
+    
+/** Clear the screen and locate to 0
+  * @param none
+  * @return none
+  */ 
+void TM1651_OPENSMART::cls() {  
+
+  //clear local buffer
+  for (int idx=0; idx < OPENSMART_NR_GRIDS; idx++) {
+    _displaybuffer[idx] = 0x00;  
+  }
+
+  writeData(_displaybuffer, (OPENSMART_NR_GRIDS * TM1651_BYTES_PER_GRID), 0);
+}     
+
+/** Set Icon
+  *
+  * @param Icon Icon Enums Icon has Grid position encoded in 8 MSBs, Pattern encoded in 16 LSBs
+  * @return none
+  */
+void TM1651_OPENSMART::setIcon(Icon icon) {
+  int addr, ld;
+
+   ld =          icon & 0x7F;
+  addr = (icon >> 24) & 0xFF; 
+  addr = (addr - 1);
+    
+  //Set bits for Icon to write
+  _displaybuffer[addr] |= ld;      
+  writeData(_displaybuffer, TM1651_BYTES_PER_GRID, addr);  
+}
+
+/** Clr Icon
+  *
+  * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Pattern encoded in 16 LSBs
+  * @return none
+  */
+void TM1651_OPENSMART::clrIcon(Icon icon) {
+  int addr, ld;
+
+   ld =          icon & 0x7F;
+  addr = (icon >> 24) & 0xFF; 
+  addr = (addr - 1);
+    
+  //Set bits for Icon to clear
+  _displaybuffer[addr] &= ~ld;      
+  writeData(_displaybuffer, TM1651_BYTES_PER_GRID, addr);  
+}
+
+
+/** Set Level
+  *
+  * @param Level level Enums Level indicates the Battery level to be displayed  
+  * @return none
+  */
+void TM1651_OPENSMART::setLevel(Level level) {
+
+  //clear local buffer
+  _displaybuffer[0] = 0x00;  
+
+  //Set bits for level to write
+  switch (level) {
+    case LVL_0 :
+            _displaybuffer[0] = R12;
+            break;
+
+    case LVL_1 :
+            _displaybuffer[0] = R12 | Y3;
+            break;
+
+    case LVL_2 :
+            _displaybuffer[0] = R12 | Y3 | Y4;
+            break;
+
+    case LVL_3 :
+            _displaybuffer[0] = R12 | Y3 | Y4 | Y5;
+            break;
+
+    case LVL_4 :
+            _displaybuffer[0] = R12 | Y3 | Y4 | Y5 | G67;
+            break;
+
+    case LVL_5 :
+            _displaybuffer[0] = R12 | Y3 | Y4 | Y5 | G67 | G89;
+            break;
+
+    case LVL_6 :
+            _displaybuffer[0] = R12 | Y3 | Y4 | Y5 | G67 | G89 | B10;
+            break;
+
+    default:
+            break;
+  }    
+ 
+  writeData(_displaybuffer, TM1651_BYTES_PER_GRID, 0);  
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TM1651.h	Wed Oct 04 19:54:30 2017 +0000
@@ -0,0 +1,401 @@
+/* mbed TM1651 Library, for TM1651 LED controller
+ * Copyright (c) 2016, v01: WH, Initial version, Battery monitor
+ *
+ * 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, inclumosig 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, INCLUmosiG 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 TM1651_H
+#define TM1651_H
+
+// Select one of the testboards for TM1651 LED controller
+#include "TM1651_Config.h"
+
+/** An interface for driving TM1651 LED controller
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "TM1651.h" 
+ * 
+ * Serial pc(USBTX, USBRX);
+ *
+ * //DisplayData_t size is 4 bytes (4 grids @ 7 segments)
+ * TM1651::DisplayData_t all_str  = {0x7F, 0x7F, 0x7F, 0x7F};  
+ *
+ * // KeyData_t size is 1 bytes  
+ * TM1651::KeyData_t keydata; 
+ *
+ * // TM1651 declaration, Select the desired type in TM1651_Config.h
+ * TM1651_OPENSMART OPENSMART(p6, p7);       //LPC1768
+ * //TM1651_OPENSMART OPENSMART(D9, D10);      //F401
+ *
+ * int main() {
+ *   OPENSMART.cls(); 
+ *   OPENSMART.writeData(all_str);
+ *   wait(1);
+ *   OPENSMART.setBrightness(TM1651_BRT0);
+ *   wait(1);
+ *   OPENSMART.setBrightness(TM1651_BRT3);
+ *
+ *   float delay=0.1;
+ *   while (1) {
+ *     OPENSMART.cls(); 
+ *     wait(0.5); 
+ *
+ *     // Levels
+ *     OPENSMART.setLevel(TM1651_OPENSMART::LVL_0); wait(delay);
+ *     OPENSMART.setLevel(TM1651_OPENSMART::LVL_1); wait(delay);
+ *     OPENSMART.setLevel(TM1651_OPENSMART::LVL_2); wait(delay);
+ *     OPENSMART.setLevel(TM1651_OPENSMART::LVL_3); wait(delay);
+ *     OPENSMART.setLevel(TM1651_OPENSMART::LVL_4); wait(delay);
+ *     OPENSMART.setLevel(TM1651_OPENSMART::LVL_5); wait(delay);
+ *     OPENSMART.setLevel(TM1651_OPENSMART::LVL_6); wait(delay);
+ *     // Levels off
+ *     OPENSMART.setLevel(TM1651_OPENSMART::LVL_5); wait(delay);
+ *     OPENSMART.setLevel(TM1651_OPENSMART::LVL_4); wait(delay);
+ *
+ *     // Check and read keydata
+ *     if (OPENSMART.getKeys(&keydata)) {
+ *       pc.printf("Keydata = 0x%02x\r\n", keydata);
+ *
+ *       if (keydata == TM1651_SW9_BIT) { //sw9  
+ *         OPENSMART.cls(); 
+ *         OPENSMART.printf("--09"); 
+ *       }  
+ *     } // Check keydata
+ *   } // while 
+ * }
+ * @endcode
+ */
+
+
+//TM1651 Display data
+#define TM1651_MAX_NR_GRIDS    4
+#define TM1651_BYTES_PER_GRID  1
+
+//Significant bits Keymatrix data
+//#define TM1638_KEY_MSK      0xFF 
+
+//Memory size in bytes for Display and Keymatrix
+#define TM1651_DISPLAY_MEM  (TM1651_MAX_NR_GRIDS * TM1651_BYTES_PER_GRID)
+#define TM1651_KEY_MEM         1
+
+//Reserved bits for commands
+#define TM1651_CMD_MSK      0xC0
+
+//Data setting commands
+#define TM1651_DATA_SET_CMD 0x40
+#define TM1651_DATA_WR      0x00
+#define TM1651_KEY_RD       0x02
+#define TM1651_ADDR_INC     0x00
+#define TM1651_ADDR_FIXED   0x04
+#define TM1651_MODE_NORM    0x00
+#define TM1651_MODE_TEST    0x08
+
+//Address setting commands
+#define TM1651_ADDR_SET_CMD 0xC0
+#define TM1651_ADDR_MSK     0x03 //0..3
+
+//Display control commands
+#define TM1651_DSP_CTRL_CMD 0x80
+#define TM1651_BRT_MSK      0x07
+#define TM1651_BRT0         0x00 //Pulsewidth 1/16
+#define TM1651_BRT1         0x01
+#define TM1651_BRT2         0x02
+#define TM1651_BRT3         0x03
+#define TM1651_BRT4         0x04
+#define TM1651_BRT5         0x05
+#define TM1651_BRT6         0x06
+#define TM1651_BRT7         0x07 //Pulsewidth 14/16
+
+#define TM1651_BRT_DEF      TM1651_BRT3
+
+#define TM1651_DSP_OFF      0x00
+#define TM1651_DSP_ON       0x08
+
+
+//Access to 8 Switches
+//S0 S1 S2 K1 K2 1 1 1
+//K1 = 0 1
+//K2 =   1
+#define TM1651_SW1_BIT      0xEF
+#define TM1651_SW2_BIT      0x6F
+#define TM1651_SW3_BIT      0xAF
+#define TM1651_SW4_BIT      0x2F
+#define TM1651_SW5_BIT      0xCF
+#define TM1651_SW6_BIT      0x4F
+#define TM1651_SW7_BIT      0x8F
+
+#define TM1651_SW_NONE      0xFF
+
+
+//Segments
+#define S7_S1            (1 << 0)
+#define S7_S2            (1 << 1)
+#define S7_S3            (1 << 2)
+#define S7_S4            (1 << 3)
+#define S7_S5            (1 << 4)
+#define S7_S6            (1 << 5)
+#define S7_S7            (1 << 6)
+
+
+/** A class for driving TM1651 LED controller
+ *
+ * @brief Supports 4 Grids @ 7 Segments and 7 Keys. 
+ *        Serial bus interface device. 
+ */
+class TM1651 {
+ public:
+
+  /** Datatype for displaydata */
+  typedef char DisplayData_t[TM1651_DISPLAY_MEM];
+
+  /** Datatypes for keymatrix data */
+  typedef char KeyData_t;
+
+
+ /** Constructor for class for driving TM1651 LED controller
+  *
+  * @brief Supports 4 Grids @ 7 segments and 7 Keys. 
+  *        Serial bus interface device. 
+  *
+  *  @param  PinName dio Serial bus DIO pin
+  *  @param  PinName sck Serial bus CLK pin 
+  */
+  TM1651(PinName dio, PinName clk);
+
+
+  /** Clear the screen and locate to 0
+   */ 
+  void cls();  
+
+  /** Write databyte to TM1651
+   *  @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); 
+
+   /** Write Display datablock to TM1651
+    *  @param  DisplayData_t data Array of TM1651_DISPLAY_MEM (=4) bytes for displaydata
+    *  @param  length number bytes to write (valid range 0..(TM1651_MAX_NR_GRIDS * TM1651_BYTES_PER_GRID) (=4), 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 = (TM1651_MAX_NR_GRIDS * TM1651_BYTES_PER_GRID), int address = 0);
+
+  /** Read keydata block from TM1651
+   *  @param  *keydata Ptr to bytes for keydata
+   *  @return bool keypress True when at least one key was pressed
+   *
+   */   
+  bool getKeys(KeyData_t *keydata);
+
+  /** Set Brightness
+    *
+    * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/16 dutycycle)  
+    * @return none
+    */
+  void setBrightness(char brightness = TM1651_BRT_DEF);
+  
+  /** Set the Display mode On/off
+    *
+    * @param bool display mode
+    */
+  void setDisplay(bool on);
+  
+ private:  
+    DigitalInOut _dio;
+    DigitalOut   _clk;  
+
+    char _display;
+    char _bright; 
+  
+  /** Init the Serial interface and the controller
+    * @param  none
+    * @return none
+    */ 
+    void _init();
+
+
+  /** Generate Start condition for TM1651
+    *  @param  none
+    *  @return none
+    */ 
+    void _start();
+  
+  /** Generate Stop condition for TM1651
+    *  @param  none
+    *  @return none
+    */ 
+    void _stop();
+
+  /** Send byte to TM1651
+    *  @param  int data
+    *  @return none
+    */ 
+    void _write(int data);
+
+  /** Read byte from TM1651
+    *  @return read byte 
+    */ 
+    char _read();
+
+  /** Write command and parameter to TM1651
+    *  @param  int cmd Command byte
+    *  &Param  int data Parameters for command
+    *  @return none
+    */ 
+    void _writeCmd(int cmd, int data);  
+};
+
+#if (OPENSMART_TEST == 1) 
+// Derived class for TM1651 used in OPEN-SMART battery display unit with 10 Segments
+//
+
+#define OPENSMART_NR_GRIDS  4
+#define OPENSMART_NR_DIGITS 4
+#define OPENSMART_NR_UDC    0
+
+//Battery level Icons mapping onto Segments
+#define R12                S7_S1
+#define Y3                 S7_S2
+#define Y4                 S7_S3
+#define Y5                 S7_S4
+#define G67                S7_S5
+#define G89                S7_S6
+#define B10                S7_S7
+
+
+/** Constructor for class for driving TM1651 controller as used in OPENSMART
+  *
+  *  @brief Supports battery display unit with 10 Segments.
+  *  
+  *  @param  PinName dio Serial bus DIO pin  
+  *  @param  PinName clk Serial bus CLK pin 
+  */
+class TM1651_OPENSMART : public TM1651 {
+ public:
+
+  /** Enums for Icons */
+  //  Grid encoded in 8 MSBs, Pattern encoded in 16 LSBs
+  enum Icon {
+    LD12  = ( 1<<24) | R12,   /**<  LED1 (Red) & LED2 (Red)*/
+    LD3   = ( 1<<24) | Y3,    /**<  LED3 (Yellow)*/
+    LD4   = ( 1<<24) | Y4,    /**<  LED4 (Yellow)*/
+    LD5   = ( 1<<24) | Y5,    /**<  LED5 (Yellow)*/
+    LD67  = ( 1<<24) | G67,   /**<  LED6 (Green) & LED7 (Green)*/
+    LD89  = ( 1<<24) | G89,   /**<  LED8 (Green) & LED9 (Green)*/
+    LD10  = ( 1<<24) | B10,   /**<  LED10 (Blue) */
+  };
+
+  /** Enums for Batterylevels */
+  enum Level {
+    LVL_0,   /**<  Level 0 = R12       */
+    LVL_1,   /**<  Level 1 = R12 - Y3  */
+    LVL_2,   /**<  Level 2 = R12 - Y4  */
+    LVL_3,   /**<  Level 3 = R12 - Y5  */
+    LVL_4,   /**<  Level 4 = R12 - G67 */
+    LVL_5,   /**<  Level 5 = R12 - G89 */    
+    LVL_6    /**<  Level 6 = R12 - B10 */    
+
+#if(0)    
+    LVL_0   = ( 1<<24) | R12,                                   /**<  Level 0 */
+    LVL_1   = ( 1<<24) | R12 | Y3,                              /**<  Level 1 */
+    LVL_2   = ( 1<<24) | R12 | Y3 | Y4,                         /**<  Level 2 */
+    LVL_3   = ( 1<<24) | R12 | Y3 | Y4 | Y5,                    /**<  Level 3 */
+    LVL_4   = ( 1<<24) | R12 | Y3 | Y4 | Y5 | G67,              /**<  Level 4 */
+    LVL_5   = ( 1<<24) | R12 | Y3 | Y4 | Y5 | G67 | G89,        /**<  Level 5 */
+    LVL_6   = ( 1<<24) | R12 | Y3 | Y4 | Y5 | G67 | G89 | B10,  /**<  Level 6 */   
+#endif   
+  };
+  
+  
+ /** Constructor for class for driving TM1651 LED controller
+  *
+  * @brief Supports battery display unit with 10 Segments.
+  *        Serial bus interface device. 
+  *
+  *  @param  PinName dio Serial bus DIO pin
+  *  @param  PinName sck Serial bus CLK pin 
+  */
+  TM1651_OPENSMART(PinName dio, PinName clk);
+   
+    /** Clear the screen and locate to 0
+     * @param none
+     * @return none         
+     */
+    void cls();
+
+  /** Set Icon
+    *
+    * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, 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, Pattern encoded in 16 LSBs
+     * @return none
+     */
+    void clrIcon(Icon icon);
+
+   /** Set Level
+     *
+     * @param Level level Enums Level indicates the Battery level to be displayed
+     * @return none
+     */
+    void setLevel(Level level);
+
+   /** Clr Level
+     *
+     * @param Level level Enums Level has Grid position encoded in 8 MSBs, Pattern encoded in 16 LSBs
+     * @return none
+     */
+    void clrLevel(Level level);
+
+
+   /** Write databyte to TM1651
+     *  @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){
+      TM1651::writeData(data, address);
+    }        
+
+   /** Write Display datablock to TM1651
+     *  @param  DisplayData_t data Array of TM1651_DISPLAY_MEM (=4) bytes for displaydata
+     *  @param  length number bytes to write (valid range 0..(OPENSMART_NR_GRIDS * TM1651_BYTES_PER_GRID) (=4), 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 = (OPENSMART_NR_GRIDS * TM1651_BYTES_PER_GRID), int address = 0) {
+      TM1651::writeData(data, length, address);
+    }  
+
+protected:  
+
+private:
+    DisplayData_t _displaybuffer;
+
+};
+#endif
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TM1651_Config.h	Wed Oct 04 19:54:30 2017 +0000
@@ -0,0 +1,33 @@
+/* mbed TM1651 Library, for TM1651 LED controller
+ * Copyright (c) 2016, v01: WH, Initial version, Battery monitor
+ *
+ * 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 TM1651_CONFIG_H
+#define TM1651_CONFIG_H
+
+// Select one of the testboards for TM1651 LED controller
+#define TM1651_TEST    0
+#define OPENSMART_TEST 1 
+
+// Select the display mode: only digits and hex or ASCII
+#define SHOW_ASCII     1 
+
+#endif
\ No newline at end of file