TEST

Dependents:   ADXL345Test ADXL345Test1

Fork of TM1638 by Wim Huiskamp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TM1638.cpp Source File

TM1638.cpp

00001 
00002 /* mbed TM1638 Library, for TM1638 LED controller
00003  * Copyright (c) 2015, v01: WH, Initial version
00004  *               2016, v02: WH, Added ASCII alphabet display selector, refactored display and keyboard defines 
00005  *               2016, v03: WH, Added QYF-TM1638 and LKM1638, refactoring of writeData() 
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, including without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  */
00025 #include "mbed.h" 
00026 #include "TM1638.h"
00027 
00028 /** Constructor for class for driving TM1638 LED controller with SPI bus interface device. 
00029  *  @brief Supports 8 digits @ 10 segments. 
00030  *         Also supports a scanned keyboard of upto 24 keys.
00031  *   
00032  *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00033 */
00034 TM1638::TM1638(PinName mosi, PinName miso, PinName sclk, PinName cs) : _spi(mosi,miso,sclk), _cs(cs) {
00035 
00036   _init();
00037 }
00038 
00039 /** Init the SPI interface and the controller
00040   * @param  none
00041   * @return none
00042   */ 
00043 void TM1638::_init(){
00044   
00045 //init SPI
00046   _cs=1;
00047   _spi.format(8,3); //TM1638 uses mode 3 (Clock High on Idle, Data latched on second (=rising) edge)
00048   _spi.frequency(500000);   
00049 
00050 //init controller  
00051   _display = TM1638_DSP_ON;
00052   _bright  = TM1638_BRT_DEF; 
00053   _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright );                                 // Display control cmd, display on/off, brightness   
00054   
00055   _writeCmd(TM1638_DATA_SET_CMD, TM1638_DATA_WR | TM1638_ADDR_INC | TM1638_MODE_NORM); // Data set cmd, normal mode, auto incr, write data  
00056 }   
00057 //
00058 #if(0)
00059 void TM1638::Switches() {
00060    int Switches = 0;
00061    if (keydata[LEDKEY8_SW2_IDX])  Switches = 1;
00062    
00063 }  
00064 #endif
00065 
00066 
00067 /** Clear the screen and locate to 0
00068  */  
00069 void TM1638::cls() {
00070   
00071   _cs=0;
00072   wait_us(1);    
00073   _spi.write(_flip(TM1638_ADDR_SET_CMD | 0x00)); // Address set cmd, 0
00074       
00075   for (int cnt=0; cnt<TM1638_DISPLAY_MEM; cnt++) {
00076     _spi.write(0x00); // data 
00077   }
00078   
00079   wait_us(1);
00080   _cs=1;      
00081 }  
00082 
00083 /** Set Brightness
00084   *
00085   * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)  
00086   * @return none
00087   */
00088 void TM1638::setBrightness(char brightness){
00089 
00090   _bright = brightness & TM1638_BRT_MSK; // mask invalid bits
00091   
00092   _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright );  // Display control cmd, display on/off, brightness  
00093 }
00094 
00095 /** Set the Display mode On/off
00096   *
00097   * @param bool display mode
00098   */
00099 void TM1638::setDisplay(bool on) {
00100   
00101   if (on) {
00102     _display = TM1638_DSP_ON;
00103   }
00104   else {
00105     _display = TM1638_DSP_OFF;
00106   }
00107   
00108   _writeCmd(TM1638_DSP_CTRL_CMD, _display | _bright );  // Display control cmd, display on/off, brightness   
00109 }
00110 
00111 
00112 /** Write databyte to TM1638
00113   *  @param  char data byte written at given address
00114   *  @param  int address display memory location to write byte
00115   *  @return none
00116   */ 
00117 void TM1638::writeData(char data, int address) {
00118   _cs=0;
00119   wait_us(1);    
00120   _spi.write(_flip(TM1638_ADDR_SET_CMD | (address & TM1638_ADDR_MSK))); // Set Address cmd
00121       
00122   _spi.write(_flip(data)); // data 
00123   
00124   wait_us(1);
00125   _cs=1;             
00126 }
00127 
00128 
00129 /** Write Display datablock to TM1638
00130   *  @param  DisplayData_t data Array of TM1638_DISPLAY_MEM (=16) bytes for displaydata
00131   *  @param  length number bytes to write (valid range 0..TM1638_DISPLAY_MEM (=16), when starting at address 0)
00132   *  @param  int address display memory location to write bytes (default = 0)   
00133   *  @return none
00134   */ 
00135 void TM1638::writeData(DisplayData_t data, int length, int address) {
00136   _cs=0;
00137   wait_us(1);    
00138        
00139 // sanity check
00140   address &= TM1638_ADDR_MSK;
00141   if (length < 0) {length = 0;}
00142   if ((length + address) > TM1638_DISPLAY_MEM) {length = (TM1638_DISPLAY_MEM - address);}
00143 
00144   // _spi.write(_flip(TM1638_ADDR_SET_CMD | 0x00)); // Set Address at 0
00145   _spi.write(_flip(TM1638_ADDR_SET_CMD | address)); // Set Address
00146 
00147   for (int idx=0; idx<length; idx++) {    
00148     //_spi.write(_flip(data[idx])); // data 
00149     _spi.write(_flip(data[address + idx])); // data 
00150   }
00151   
00152   wait_us(1);
00153   _cs=1;             
00154 }
00155 
00156 
00157 /** Read keydata block from TM1638
00158   *  @param  *keydata Ptr to Array of TM1638_KEY_MEM (=4) bytes for keydata
00159   *  @return bool keypress True when at least one key was pressed
00160   *
00161   * Note: Due to the hardware configuration the TM1638 key matrix scanner will detect multiple keys pressed at same time,
00162   *       but this may also result in some spurious keys being set in keypress data array.
00163   *       It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting.  
00164   */ 
00165 bool TM1638::getKeys(KeyData_t *keydata) {
00166   int keypress = 0;
00167   char data;
00168 
00169   // Read keys
00170   _cs=0;
00171   wait_us(1);    
00172   
00173   // Enable Key Read mode
00174   _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
00175 
00176   for (int idx=0; idx < TM1638_KEY_MEM; idx++) {
00177     data = _flip(_spi.write(0xFF));    // read keys and correct bitorder
00178 
00179     data = data & TM1638_KEY_MSK; // Mask valid bits
00180     if (data != 0) {  // Check for any pressed key
00181       for (int bit=0; bit < 8; bit++) {
00182         if (data & (1 << bit)) {keypress++;} // Test all significant bits
00183       }
00184     }  
00185 
00186     (*keydata)[idx] = data;            // Store keydata after correcting bitorder
00187   }
00188 
00189   wait_us(1);
00190   _cs=1;    
00191 
00192   // Restore Data Write mode
00193   _writeCmd(TM1638_DATA_SET_CMD, TM1638_DATA_WR | TM1638_ADDR_INC | TM1638_MODE_NORM); // Data set cmd, normal mode, auto incr, write data  
00194       
00195 #if(1)
00196 // Dismiss multiple keypresses at same time
00197   return (keypress == 1);    
00198 #else
00199 // Allow multiple keypress and accept possible spurious keys
00200   return (keypress > 0);
00201 #endif  
00202 }
00203     
00204 
00205 /** Helper to reverse all command or databits. The TM1638 expects LSB first, whereas SPI is MSB first
00206   *  @param  char data
00207   *  @return bitreversed data
00208   */ 
00209 #if(1)
00210 char TM1638::_flip(char data) {
00211  char value=0;
00212   
00213  if (data & 0x01) {value |= 0x80;} ;  
00214  if (data & 0x02) {value |= 0x40;} ;
00215  if (data & 0x04) {value |= 0x20;} ;
00216  if (data & 0x08) {value |= 0x10;} ;
00217  if (data & 0x10) {value |= 0x08;} ;
00218  if (data & 0x20) {value |= 0x04;} ;
00219  if (data & 0x40) {value |= 0x02;} ;
00220  if (data & 0x80) {value |= 0x01;} ;
00221  return value;       
00222 }
00223 #else
00224 char TM1638::_flip(char data) {
00225 
00226  data   = (((data & 0xAA) >> 1) | ((data & 0x55) << 1));
00227  data   = (((data & 0xCC) >> 2) | ((data & 0x33) << 2));
00228  return   (((data & 0xF0) >> 4) | ((data & 0x0F) << 4));
00229 }
00230 #endif
00231 
00232 /** Write command and parameter to TM1638
00233   *  @param  int cmd Command byte
00234   *  &Param  int data Parameters for command
00235   *  @return none
00236   */  
00237 void TM1638::_writeCmd(int cmd, int data){
00238     
00239   _cs=0;
00240   wait_us(1);    
00241 //  _spi.write(_flip( (cmd & 0xF0) | (data & 0x0F)));  
00242   _spi.write(_flip( (cmd & TM1638_CMD_MSK) | (data & ~TM1638_CMD_MSK)));   
00243  
00244   wait_us(1);
00245   _cs=1;          
00246 }  
00247 
00248 
00249 #if (LEDKEY8_TEST == 1) 
00250 // Derived class for TM1638 used in LED&KEY display unit
00251 //
00252 
00253 /** Constructor for class for driving TM1638 LED controller as used in LEDKEY8
00254   *
00255   *  @brief Supports 8 Digits of 7 Segments + DP + LED Icons. Also supports a scanned keyboard of 8.
00256   *   
00257   *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00258   */
00259 TM1638_LEDKEY8::TM1638_LEDKEY8(PinName mosi, PinName miso, PinName sclk, PinName cs) : TM1638(mosi, miso, sclk, cs) {
00260   _column  = 0;
00261   _columns = LEDKEY8_NR_DIGITS;    
00262 }  
00263 
00264 #if(0)
00265 #if DOXYGEN_ONLY
00266     /** Write a character to the Display
00267      *
00268      * @param c The character to write to the display
00269      */
00270     int putc(int c);
00271 
00272     /** Write a formatted string to the Display
00273      *
00274      * @param format A printf-style format string, followed by the
00275      *               variables to use in formatting the string.
00276      */
00277     int printf(const char* format, ...);   
00278 #endif
00279 #endif
00280 
00281 /** Locate cursor to a screen column
00282   *
00283   * @param column  The horizontal position from the left, indexed from 0
00284   */
00285 void TM1638_LEDKEY8::locate(int column) {
00286   //sanity check
00287   if (column < 0) {column = 0;}
00288   if (column > (_columns - 1)) {column = _columns - 1;}  
00289   
00290   _column = column;       
00291 }
00292 
00293 
00294 /** Number of screen columns
00295   *
00296   * @param none
00297   * @return columns
00298   */
00299 int TM1638_LEDKEY8::columns() {
00300     return _columns;
00301 }
00302 
00303     
00304 /** Clear the screen and locate to 0
00305   * @param bool clrAll Clear Icons also (default = false)
00306   */ 
00307 void TM1638_LEDKEY8::cls(bool clrAll) {  
00308 
00309   if (clrAll) {
00310     //clear local buffer (including Icons)
00311     for (int idx=0; idx < (LEDKEY8_NR_GRIDS << 1); idx++) {
00312       _displaybuffer[idx] = 0x00;  
00313     }
00314   }  
00315   else {
00316     //clear local buffer (preserving Icons)
00317     for (int idx=0; idx < LEDKEY8_NR_GRIDS; idx++) {
00318       _displaybuffer[(idx<<1)]     = _displaybuffer[(idx<<1)]     & MASK_ICON_GRID[idx][0];  
00319       _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1];
00320     }  
00321   }
00322 
00323   writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
00324 
00325   _column = 0;   
00326 }     
00327 
00328 /** Set Icon
00329   *
00330   * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00331   * @return none
00332   */
00333 void TM1638_LEDKEY8::setIcon(Icon icon) {
00334   int addr, icn;
00335 
00336    icn =        icon  & 0xFFFF;
00337   addr = (icon >> 24) & 0xFF; 
00338   addr = (addr - 1) << 1;   // * TM1638_BYTES_PER_GRID
00339     
00340   //Save char...and set bits for icon to write
00341   _displaybuffer[addr]   = _displaybuffer[addr]   | LO(icn);      
00342   _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn);      
00343 //  writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
00344   writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);      
00345 }
00346 
00347 /** Clr Icon
00348   *
00349   * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00350   * @return none
00351   */
00352 void TM1638_LEDKEY8::clrIcon(Icon icon) {
00353   int addr, icn;
00354 
00355    icn =        icon  & 0xFFFF;
00356   addr = (icon >> 24) & 0xFF; 
00357   addr = (addr - 1) << 1;   // * TM1638_BYTES_PER_GRID
00358     
00359   //Save char...and clr bits for icon to write
00360   _displaybuffer[addr]   = _displaybuffer[addr]   & ~LO(icn);      
00361   _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn);      
00362 //  writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
00363   writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);      
00364 }
00365 
00366 
00367 /** Set User Defined Characters (UDC)
00368   *
00369   * @param unsigned char udc_idx  The Index of the UDC (0..7)
00370   * @param int udc_data           The bitpattern for the UDC (8 bits)       
00371   */
00372 void TM1638_LEDKEY8::setUDC(unsigned char udc_idx, int udc_data) {
00373 
00374   //Sanity check
00375   if (udc_idx > (LEDKEY8_NR_UDC-1)) {
00376     return;
00377   }
00378   // Mask out Icon bits?
00379 
00380   _UDC_7S[udc_idx] = LO(udc_data);
00381 }
00382 
00383 
00384 /** Write a single character (Stream implementation)
00385   */
00386 int TM1638_LEDKEY8::_putc(int value) {
00387     int addr;
00388     bool validChar = false;
00389     char pattern   = 0x00;
00390     
00391     if ((value == '\n') || (value == '\r')) {
00392       //No character to write
00393       validChar = false;
00394       
00395       //Update Cursor      
00396       _column = 0;
00397     }
00398     else if ((value == '.') || (value == ',')) {
00399       //No character to write
00400       validChar = false;
00401       pattern = S7_DP; // placeholder for all DPs
00402       
00403       // Check to see that DP can be shown for current column
00404       if (_column > 0) {
00405         //Translate between _column and displaybuffer entries
00406         //Add DP to bitpattern of digit left of current column.
00407         addr = (_column - 1) << 1; // * TM1638_BYTES_PER_GRID
00408       
00409         //Save icons...and set bits for decimal point to write
00410         _displaybuffer[addr]   = _displaybuffer[addr] | pattern;
00411 //        _displaybuffer[addr+1] = _displaybuffer[addr+1] | pattern;
00412 
00413 //        writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
00414         writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);    
00415           
00416         //No Cursor Update
00417       }
00418     }
00419     else if ((value >= 0) && (value < LEDKEY8_NR_UDC)) {
00420       //Character to write
00421       validChar = true;
00422       pattern = _UDC_7S[value];
00423     }  
00424     
00425 #if (SHOW_ASCII == 1)
00426     //display all ASCII characters
00427     else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {   
00428       //Character to write
00429       validChar = true;
00430       pattern = FONT_7S[value - FONT_7S_START];
00431     } // else
00432 #else    
00433     //display only digits and hex characters      
00434     else if (value == '-') {
00435       //Character to write
00436       validChar = true;
00437       pattern = C7_MIN;         
00438     }
00439     else if ((value >= (int)'0') && (value <= (int) '9')) {   
00440       //Character to write
00441       validChar = true;
00442       pattern = FONT_7S[value - (int) '0'];
00443     }
00444     else if ((value >= (int) 'A') && (value <= (int) 'F')) {   
00445       //Character to write
00446       validChar = true;
00447       pattern = FONT_7S[10 + value - (int) 'A'];
00448     }
00449     else if ((value >= (int) 'a') && (value <= (int) 'f')) {   
00450       //Character to write
00451       validChar = true;
00452       pattern = FONT_7S[10 + value - (int) 'a'];
00453     } //else
00454 #endif
00455 
00456     if (validChar) {
00457       //Character to write
00458  
00459       //Translate between _column and displaybuffer entries
00460       addr = _column << 1; // * TM1638_BYTES_PER_GRID
00461 
00462       //Save icons...and set bits for character to write
00463       _displaybuffer[addr]   = (_displaybuffer[addr]   & MASK_ICON_GRID[_column][0]) | pattern;
00464 //      _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][0]) | pattern;
00465 
00466 //      writeData(_displaybuffer, (LEDKEY8_NR_GRIDS * TM1638_BYTES_PER_GRID));
00467       writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
00468       
00469       //Update Cursor
00470       _column++;
00471       if (_column > (LEDKEY8_NR_DIGITS - 1)) {
00472         _column = 0;
00473       }
00474 
00475     } // if validChar           
00476 
00477     return value;
00478 }
00479 
00480 
00481 // get a single character (Stream implementation)
00482 int TM1638_LEDKEY8::_getc() {
00483     return -1;
00484 }
00485 
00486 #endif
00487 
00488 
00489 #if (QYF_TEST == 1) 
00490 // Derived class for TM1638 used in QYF-TM1638 display unit
00491 //
00492 
00493 /** Constructor for class for driving TM1638 LED controller as used in QYF
00494   *
00495   *  @brief Supports 8 Digits of 7 Segments + DP. Also supports a scanned keyboard of 16 keys.
00496   *   
00497   *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00498   */
00499 TM1638_QYF::TM1638_QYF(PinName mosi, PinName miso, PinName sclk, PinName cs) : TM1638(mosi, miso, sclk, cs) {
00500   _column  = 0;
00501   _columns = QYF_NR_DIGITS;    
00502 }  
00503 
00504 #if(0)
00505 #if DOXYGEN_ONLY
00506     /** Write a character to the Display
00507      *
00508      * @param c The character to write to the display
00509      */
00510     int putc(int c);
00511 
00512     /** Write a formatted string to the Display
00513      *
00514      * @param format A printf-style format string, followed by the
00515      *               variables to use in formatting the string.
00516      */
00517     int printf(const char* format, ...);   
00518 #endif
00519 #endif
00520 
00521 /** Locate cursor to a screen column
00522   *
00523   * @param column  The horizontal position from the left, indexed from 0
00524   */
00525 void TM1638_QYF::locate(int column) {
00526   //sanity check
00527   if (column < 0) {column = 0;}
00528   if (column > (_columns - 1)) {column = _columns - 1;}  
00529   
00530   _column = column;       
00531 }
00532 
00533 
00534 /** Number of screen columns
00535   *
00536   * @param none
00537   * @return columns
00538   */
00539 int TM1638_QYF::columns() {
00540     return _columns;
00541 }
00542 
00543     
00544 /** Clear the screen and locate to 0
00545   * @param bool clrAll Clear Icons also (default = false)
00546   */ 
00547 void TM1638_QYF::cls(bool clrAll) {  
00548 
00549   if (clrAll) {
00550     //clear local buffer (including Icons)
00551     for (int idx=0; idx < (QYF_NR_GRIDS << 1); idx++) {
00552       _displaybuffer[idx] = 0x00;  
00553     }
00554   }  
00555   else {
00556     //clear local buffer (preserving Icons)
00557     for (int idx=0; idx < QYF_NR_GRIDS; idx++) {
00558       _displaybuffer[(idx<<1)]     = _displaybuffer[(idx<<1)]     & MASK_ICON_GRID[idx][0];  
00559       _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1];
00560     }  
00561   }
00562 
00563   writeData(_displaybuffer, (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID));
00564 
00565   _column = 0;   
00566 }     
00567 
00568 /** Set Icon
00569   *
00570   * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00571   * @return none
00572   */
00573 void TM1638_QYF::setIcon(Icon icon) {
00574   int addr, icn;
00575 
00576    icn =        icon  & 0xFFFF;
00577   addr = (icon >> 24) & 0xFF; 
00578   addr = (addr - 1) << 1;   // * TM1638_BYTES_PER_GRID
00579     
00580   //Save char...and set bits for icon to write
00581   _displaybuffer[addr]   = _displaybuffer[addr]   | LO(icn);      
00582   _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn);      
00583 //  writeData(_displaybuffer, (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID));
00584   writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);    
00585 }
00586 
00587 /** Clr Icon
00588   *
00589   * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00590   * @return none
00591   */
00592 void TM1638_QYF::clrIcon(Icon icon) {
00593   int addr, icn;
00594 
00595    icn =        icon  & 0xFFFF;
00596   addr = (icon >> 24) & 0xFF; 
00597   addr = (addr - 1) << 1;   // * TM1638_BYTES_PER_GRID
00598     
00599   //Save char...and clr bits for icon to write
00600   _displaybuffer[addr]   = _displaybuffer[addr]   & ~LO(icn);      
00601   _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn);      
00602 //  writeData(_displaybuffer, (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID));
00603   writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);  
00604 }
00605 
00606 
00607 /** Set User Defined Characters (UDC)
00608   *
00609   * @param unsigned char udc_idx  The Index of the UDC (0..7)
00610   * @param int udc_data           The bitpattern for the UDC (8 bits)       
00611   */
00612 void TM1638_QYF::setUDC(unsigned char udc_idx, int udc_data) {
00613 
00614   //Sanity check
00615   if (udc_idx > (QYF_NR_UDC-1)) {
00616     return;
00617   }
00618   // Mask out Icon bits?
00619 
00620   _UDC_7S[udc_idx] = LO(udc_data);
00621 }
00622 
00623 
00624 /** Write a single character (Stream implementation)
00625   */
00626 int TM1638_QYF::_putc(int value) {
00627     bool validChar = false;
00628     char pattern   = 0x00;
00629     char bit       = 0x00;
00630         
00631     if ((value == '\n') || (value == '\r')) {
00632       //No character to write
00633       validChar = false;
00634       
00635       //Update Cursor      
00636       _column = 0;
00637     }
00638     else if ((value == '.') || (value == ',')) {
00639       //No character to write
00640       validChar = false;
00641       pattern = S7_DP; // placeholder for all DPs
00642       
00643       // Check to see that DP can be shown for current column
00644       if (_column > 0) {
00645         //Add DP to bitpattern of digit left of current column.
00646         bit = 1 << (8 - _column); // bitposition for the previous _column
00647 
00648         _displaybuffer[14] = (_displaybuffer[14] | bit); // set bit
00649 
00650         writeData(_displaybuffer, (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID));
00651         
00652         //No Cursor Update
00653       }
00654     }
00655     else if ((value >= 0) && (value < QYF_NR_UDC)) {
00656       //Character to write
00657       validChar = true;
00658       pattern = _UDC_7S[value];
00659     }  
00660     
00661 #if (SHOW_ASCII == 1)
00662     //display all ASCII characters
00663     else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {   
00664       //Character to write
00665       validChar = true;
00666       pattern = FONT_7S[value - FONT_7S_START];
00667     } // else
00668 #else    
00669     //display only digits and hex characters      
00670     else if (value == '-') {
00671       //Character to write
00672       validChar = true;
00673       pattern = C7_MIN;         
00674     }
00675     else if ((value >= (int)'0') && (value <= (int) '9')) {   
00676       //Character to write
00677       validChar = true;
00678       pattern = FONT_7S[value - (int) '0'];
00679     }
00680     else if ((value >= (int) 'A') && (value <= (int) 'F')) {   
00681       //Character to write
00682       validChar = true;
00683       pattern = FONT_7S[10 + value - (int) 'A'];
00684     }
00685     else if ((value >= (int) 'a') && (value <= (int) 'f')) {   
00686       //Character to write
00687       validChar = true;
00688       pattern = FONT_7S[10 + value - (int) 'a'];
00689     } //else
00690 #endif
00691 
00692     if (validChar) {
00693       //Character to write
00694 
00695       // Very annoying bitmapping :(
00696       // This display module uses a single byte of each grid to drive a specific segment of all digits.
00697       // So the bits in byte 0 (Grid 1) drive all A-segments, the bits in byte 2 (Grid 2) drive all B-segments etc.
00698       // Bit0 is for the segment in Digit 8, Bit1 is for the segment in Digit 7 etc.. This bit manipulation is handled in _putc().
00699       
00700       bit = 1 << (7 - _column); // bitposition for the current _column
00701 
00702       if (pattern & S7_A) {_displaybuffer[0] = (_displaybuffer[0] | bit); } // set bit
00703       else                {_displaybuffer[0] = (_displaybuffer[0] & ~bit);} // clr bit       
00704 
00705       if (pattern & S7_B) {_displaybuffer[2] = (_displaybuffer[2] | bit); } // set bit
00706       else                {_displaybuffer[2] = (_displaybuffer[2] & ~bit);} // clr bit       
00707 
00708       if (pattern & S7_C) {_displaybuffer[4] = (_displaybuffer[4] | bit); } // set bit
00709       else                {_displaybuffer[4] = (_displaybuffer[4] & ~bit);} // clr bit       
00710 
00711       if (pattern & S7_D) {_displaybuffer[6] = (_displaybuffer[6] | bit); } // set bit
00712       else                {_displaybuffer[6] = (_displaybuffer[6] & ~bit);} // clr bit       
00713 
00714       if (pattern & S7_E) {_displaybuffer[8] = (_displaybuffer[8] | bit); } // set bit
00715       else                {_displaybuffer[8] = (_displaybuffer[8] & ~bit);} // clr bit       
00716 
00717       if (pattern & S7_F) {_displaybuffer[10] = (_displaybuffer[10] | bit); } // set bit
00718       else                {_displaybuffer[10] = (_displaybuffer[10] & ~bit);} // clr bit       
00719 
00720       if (pattern & S7_G) {_displaybuffer[12] = (_displaybuffer[12] | bit); } // set bit
00721       else                {_displaybuffer[12] = (_displaybuffer[12] & ~bit);} // clr bit       
00722 
00723       if (pattern & S7_DP) {_displaybuffer[14] = (_displaybuffer[14] | bit); } // set bit
00724       else                 {_displaybuffer[14] = (_displaybuffer[14] & ~bit);} // clr bit       
00725 
00726       //Save icons...and set bits for character to write
00727 //      _displaybuffer[addr]   = (_displaybuffer[addr]   & MASK_ICON_GRID[_column][0]) | pattern;
00728 //      _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][0]) | pattern;
00729 
00730       writeData(_displaybuffer, (QYF_NR_GRIDS * TM1638_BYTES_PER_GRID));
00731                                 
00732       //Update Cursor
00733       _column++;
00734       if (_column > (QYF_NR_DIGITS - 1)) {
00735         _column = 0;
00736       }
00737 
00738     } // if validChar           
00739 
00740     return value;
00741 }
00742 
00743 
00744 // get a single character (Stream implementation)
00745 int TM1638_QYF::_getc() {
00746     return -1;
00747 }
00748 
00749 #endif
00750 
00751 
00752 
00753 #if (LKM1638_TEST == 1) 
00754 // Derived class for TM1638 used in LMK1638 display unit
00755 //
00756 
00757 /** Constructor for class for driving TM1638 LED controller as used in LKM1638
00758   *
00759   *  @brief Supports 8 Digits of 7 Segments + DP + Bi-Color LED Icons. Also supports a scanned keyboard of 8.
00760   *   
00761   *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00762   */
00763 TM1638_LKM1638::TM1638_LKM1638(PinName mosi, PinName miso, PinName sclk, PinName cs) : TM1638(mosi, miso, sclk, cs) {
00764   _column  = 0;
00765   _columns = LKM1638_NR_DIGITS;    
00766 }  
00767 
00768 #if(0)
00769 #if DOXYGEN_ONLY
00770     /** Write a character to the Display
00771      *
00772      * @param c The character to write to the display
00773      */
00774     int putc(int c);
00775 
00776     /** Write a formatted string to the Display
00777      *
00778      * @param format A printf-style format string, followed by the
00779      *               variables to use in formatting the string.
00780      */
00781     int printf(const char* format, ...);   
00782 #endif
00783 #endif
00784 
00785 /** Locate cursor to a screen column
00786   *
00787   * @param column  The horizontal position from the left, indexed from 0
00788   */
00789 void TM1638_LKM1638::locate(int column) {
00790   //sanity check
00791   if (column < 0) {column = 0;}
00792   if (column > (_columns - 1)) {column = _columns - 1;}  
00793   
00794   _column = column;       
00795 }
00796 
00797 
00798 /** Number of screen columns
00799   *
00800   * @param none
00801   * @return columns
00802   */
00803 int TM1638_LKM1638::columns() {
00804     return _columns;
00805 }
00806 
00807     
00808 /** Clear the screen and locate to 0
00809   * @param bool clrAll Clear Icons also (default = false)
00810   */ 
00811 void TM1638_LKM1638::cls(bool clrAll) {  
00812 
00813   if (clrAll) {
00814     //clear local buffer (including Icons)
00815     for (int idx=0; idx < (LKM1638_NR_GRIDS << 1); idx++) {
00816       _displaybuffer[idx] = 0x00;  
00817     }
00818   }  
00819   else {
00820     //clear local buffer (preserving Icons)
00821     for (int idx=0; idx < LKM1638_NR_GRIDS; idx++) {
00822       _displaybuffer[(idx<<1)]     = _displaybuffer[(idx<<1)]     & MASK_ICON_GRID[idx][0];  
00823       _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1];
00824     }  
00825   }
00826 
00827   writeData(_displaybuffer, (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID));
00828 
00829   _column = 0;   
00830 }     
00831 
00832 /** Set Icon
00833   *
00834   * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00835   * @return none
00836   */
00837 void TM1638_LKM1638::setIcon(Icon icon) {
00838   int addr, icn;
00839 
00840    icn =        icon  & 0xFFFF;
00841   addr = (icon >> 24) & 0xFF; 
00842   addr = (addr - 1) << 1;   // * TM1638_BYTES_PER_GRID
00843     
00844   //Save char...and set bits for icon to write
00845   _displaybuffer[addr]   = _displaybuffer[addr]   | LO(icn);      
00846   _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn);      
00847 //  writeData(_displaybuffer, (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID));
00848   writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);      
00849 }
00850 
00851 /** Clr Icon
00852   *
00853   * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00854   * @return none
00855   */
00856 void TM1638_LKM1638::clrIcon(Icon icon) {
00857   int addr, icn;
00858 
00859    icn =        icon  & 0xFFFF;
00860   addr = (icon >> 24) & 0xFF; 
00861   addr = (addr - 1) << 1;   // * TM1638_BYTES_PER_GRID
00862     
00863   //Save char...and clr bits for icon to write
00864   _displaybuffer[addr]   = _displaybuffer[addr]   & ~LO(icn);      
00865   _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn);      
00866 //  writeData(_displaybuffer, (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID));
00867   writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);      
00868 }
00869 
00870 
00871 /** Set User Defined Characters (UDC)
00872   *
00873   * @param unsigned char udc_idx  The Index of the UDC (0..7)
00874   * @param int udc_data           The bitpattern for the UDC (8 bits)       
00875   */
00876 void TM1638_LKM1638::setUDC(unsigned char udc_idx, int udc_data) {
00877 
00878   //Sanity check
00879   if (udc_idx > (LKM1638_NR_UDC-1)) {
00880     return;
00881   }
00882   // Mask out Icon bits?
00883 
00884   _UDC_7S[udc_idx] = LO(udc_data);
00885 }
00886 
00887 
00888 /** Write a single character (Stream implementation)
00889   */
00890 int TM1638_LKM1638::_putc(int value) {
00891     int addr;
00892     bool validChar = false;
00893     char pattern   = 0x00;
00894     
00895     if ((value == '\n') || (value == '\r')) {
00896       //No character to write
00897       validChar = false;
00898       
00899       //Update Cursor      
00900       _column = 0;
00901     }
00902     else if ((value == '.') || (value == ',')) {
00903       //No character to write
00904       validChar = false;
00905       pattern = S7_DP; // placeholder for all DPs
00906       
00907       // Check to see that DP can be shown for current column
00908       if (_column > 0) {
00909         //Translate between _column and displaybuffer entries
00910         //Add DP to bitpattern of digit left of current column.
00911         addr = (_column - 1) << 1; // * TM1638_BYTES_PER_GRID
00912       
00913         //Save icons...and set bits for decimal point to write
00914         _displaybuffer[addr]   = _displaybuffer[addr] | pattern;
00915 //        _displaybuffer[addr+1] = _displaybuffer[addr+1] | pattern;
00916 
00917 //        writeData(_displaybuffer, (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID));
00918         writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);    
00919           
00920         //No Cursor Update
00921       }
00922     }
00923     else if ((value >= 0) && (value < LKM1638_NR_UDC)) {
00924       //Character to write
00925       validChar = true;
00926       pattern = _UDC_7S[value];
00927     }  
00928     
00929 #if (SHOW_ASCII == 1)
00930     //display all ASCII characters
00931     else if ((value >= FONT_7S_START) && (value <= FONT_7S_END)) {   
00932       //Character to write
00933       validChar = true;
00934       pattern = FONT_7S[value - FONT_7S_START];
00935     } // else
00936 #else    
00937     //display only digits and hex characters      
00938     else if (value == '-') {
00939       //Character to write
00940       validChar = true;
00941       pattern = C7_MIN;         
00942     }
00943     else if ((value >= (int)'0') && (value <= (int) '9')) {   
00944       //Character to write
00945       validChar = true;
00946       pattern = FONT_7S[value - (int) '0'];
00947     }
00948     else if ((value >= (int) 'A') && (value <= (int) 'F')) {   
00949       //Character to write
00950       validChar = true;
00951       pattern = FONT_7S[10 + value - (int) 'A'];
00952     }
00953     else if ((value >= (int) 'a') && (value <= (int) 'f')) {   
00954       //Character to write
00955       validChar = true;
00956       pattern = FONT_7S[10 + value - (int) 'a'];
00957     } //else
00958 #endif
00959 
00960     if (validChar) {
00961       //Character to write
00962  
00963       //Translate between _column and displaybuffer entries
00964       addr = _column << 1; // * TM1638_BYTES_PER_GRID
00965 
00966       //Save icons...and set bits for character to write
00967       _displaybuffer[addr]   = (_displaybuffer[addr]   & MASK_ICON_GRID[_column][0]) | pattern;
00968 //      _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][0]) | pattern;
00969 
00970 //      writeData(_displaybuffer, (LKM1638_NR_GRIDS * TM1638_BYTES_PER_GRID));
00971       writeData(_displaybuffer, TM1638_BYTES_PER_GRID, addr);
00972       
00973       //Update Cursor
00974       _column++;
00975       if (_column > (LKM1638_NR_DIGITS - 1)) {
00976         _column = 0;
00977       }
00978 
00979     } // if validChar           
00980 
00981     return value;
00982 }
00983 
00984 
00985 // get a single character (Stream implementation)
00986 int TM1638_LKM1638::_getc() {
00987     return -1;
00988 }
00989 
00990 #endif
00991