Lib for Noritake Itron CU209SCPB VFD Module (1 Line, 20 Chars), Serial interface

Dependents:   mbed_CU209SCPB_T20

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CU209SCPB.cpp Source File

CU209SCPB.cpp

00001 /* mbed Library for Noritake Itron CU209SCPB-T20 VFD module
00002  * 
00003  * Copyright (c) 2017, v01: WH, Initial version
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  */
00023 
00024 #include "mbed.h" 
00025 #include "CU209SCPB.h"
00026 #include "CU209SCPB_UDC.inc"
00027 
00028 
00029  /** Constructor for class for driving Noritake Itron CU209SCPB VFD module
00030   *
00031   *  @brief Supports 1 line of 20 chars (5x7 matrix segments).
00032   *         Serial bus interface device. 
00033   *  @param  PinName TX Serial bus pin
00034   *  @param  Baud baud selects baudrate (default 19200)  
00035   *  @param  Int Parity selects paritybits (default Even)  
00036   */
00037 CU209SCPB::CU209SCPB(PinName TXD, Baud baud, SerialBase::Parity parity) : _serial(TXD,NC), _baud(baud), _parity(parity) {
00038 
00039   _init();
00040 
00041   _column   = 0;
00042   _columns  = CU209SCPB_NR_COLS; 
00043 }
00044 
00045 /** Init the CU209SCPB interface and the module
00046   *
00047   * @param  none
00048   * @return none
00049   */ 
00050 void CU209SCPB::_init(){
00051   
00052 //init Serial
00053   _serial.baud(_baud);
00054   _serial.format(8, _parity, 1); // CU209SCPB uses 8 databits, parity, 1 stopbit  
00055 
00056 //init module  
00057 //  _serial.putc(D_ESC);
00058 //  _serial.putc(int('I'));  // Reset all settings
00059 //
00060 //  _serial.putc(D_DC1);     // Automatic CR mode, Cursor to 0 when reaching right end (Default)
00061 //  _serial.putc(D_DC2);     // Overputc mode, Overputc when reaching right end, Cursor does not move
00062 //  _serial.putc(D_DC3);     // Scroll mode, Text shifts left when reaching right end, Cursor does not move
00063 //
00064 //  _serial.putc(D_CT0);     // International Character Font (Default)
00065 //  _serial.putc(D_CT1);     // Katakana Character Font
00066 //
00067 //  _serial.putc(D_ESC);
00068 //  _serial.putc(int('T'));  
00069 //  _serial.putc(20);        // Cursor Blinkspeed = 20 * 14.5ms (Default)
00070   _serial.putc(80);        // Cursor Blinkspeed = 80 * 14.5ms
00071 
00072   setBrightness(CU209SCPB_BRT_DEF); // Default Brightness
00073 
00074   // Clear the DCRAM (undefined at Reset)
00075   cls(); 
00076 
00077   // Clear the UDC RAM (undefined at Reset)
00078   const char udc_none[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 
00079   for (int idx=0; idx < CU209SCPB_NR_UDC; idx++) {
00080     setUDC(idx, (char *)udc_none);
00081   }     
00082 }   
00083 
00084 
00085 /** Clear the screen and locate to 0
00086   *
00087   * @param none
00088   * @return none  
00089   */ 
00090 void CU209SCPB::cls() {  
00091   _serial.putc(D_CLR); //Clear
00092   _serial.putc(D_CR);  //Cursor left
00093   
00094   _column = 0;   
00095 }    
00096 
00097 
00098 /** Locate cursor to a screen column
00099   *
00100   * @param column  The horizontal position from the left, indexed from 0
00101   * @return none  
00102   */
00103 void CU209SCPB::locate(int column) {
00104   //sanity check
00105   if (column < 0) {column = 0;}
00106   if (column > (_columns - 1)) {column = _columns - 1;}  
00107 
00108   _serial.putc(D_ESC);
00109   _serial.putc(int('H'));  
00110   _serial.putc(column);    
00111   
00112   _column = column;       
00113 }
00114 
00115 
00116 /** Number of screen columns
00117   *
00118   * @param none
00119   * @return columns
00120   */
00121 int CU209SCPB::columns() {
00122     return _columns;
00123 }
00124 
00125 
00126 /** Set Brightness
00127   *
00128   * @param  char brightness (valid range 0..FF)  
00129   * @return none
00130   */
00131 void CU209SCPB::setBrightness(char brightness){
00132   
00133 //Sanity check
00134   brightness = brightness & CU209SCPB_BRT_MSK; // mask invalid bits
00135  
00136   _serial.putc(D_ESC);
00137   _serial.putc(int('L'));  
00138   _serial.putc(brightness);   
00139 }
00140 
00141 /** Set the Cursor mode On/off
00142   *
00143   * @param bool cursor mode
00144   * @return none  
00145   */
00146 void CU209SCPB::setCursor(bool on) {
00147      
00148   if (on) {
00149     _serial.putc(D_DC5); // Blinking Cursor
00150   }
00151   else {
00152     _serial.putc(D_DC4); // Invisible
00153   }
00154 }
00155 
00156 /** Set Font
00157   *
00158   * @param  int font (valid range: 0 == International, other == Katakana)
00159   * @return none
00160   */
00161 void CU209SCPB::setFont(int font){
00162   
00163   if (font == 0) {
00164     _serial.putc(D_CT0); // International (Default)
00165   }
00166   else {
00167     _serial.putc(D_CT1); // Katakana
00168   }
00169 }
00170 
00171 /** Set User Defined Characters (UDC)
00172   *
00173   * @param unsigned char udc_idx   The Index of the UDC (0..7)
00174   * @param UDCData_t udc_data      The bitpattern for the UDC (7 bytes)
00175   * @return none  
00176   */
00177 void CU209SCPB::setUDC(unsigned char udc_idx, UDCData_t udc_data) {
00178 
00179 //Sanity check
00180   udc_idx = udc_idx & CU209SCPB_UADR_MSK; // mask invalid bits
00181 
00182   _serial.putc(D_ESC);
00183   _serial.putc(int('C'));  // UDC command
00184   _serial.putc(udc_idx);   
00185 
00186   _serial.putc( ((udc_data[1] & 0x07) << 5) | ((udc_data[0] & 0x1F) << 0) );                               // P8  ......  P1
00187   _serial.putc( ((udc_data[3] & 0x01) << 7) | ((udc_data[2] & 0x1F) << 2) | ((udc_data[1] & 0x18) >> 3) ); // P16 ......  P9
00188   _serial.putc( ((udc_data[4] & 0x0F) << 4) | ((udc_data[3] & 0x1E) >> 1) );                               // P24 ......  P17  
00189   _serial.putc( ((udc_data[6] & 0x03) << 6) | ((udc_data[5] & 0x1F) << 1) | ((udc_data[4] & 0x10) >> 4) ); // P32 ......  P25
00190   _serial.putc( ((udc_data[6] & 0x1C) >> 2) );                                                             // ... P35 P34 P33       
00191 }
00192 
00193 /** putc a single character (Stream implementation)
00194   * 
00195   * @param value char to print
00196   * @return value;
00197   */
00198 int CU209SCPB::_putc(int value) {
00199     
00200     if ((value == '\n') || (value == '\r')) {
00201       //No character to display
00202       _serial.putc(D_CR); // Cursor to 0, Text remains as is
00203       
00204       //Update Cursor
00205       _column = 0;
00206     }
00207     else if ( ((value >= 0) && (value < CU209SCPB_NR_UDC)) ||
00208               ((value >= CU209SCPB_CHR_STRT) && (value < CU209SCPB_CHR_END)) ){
00209 
00210       //UDCs or Characters to display
00211       _serial.putc(value);
00212                               
00213       //Update Cursor
00214       _column++;
00215       if (_column > (CU209SCPB_NR_COLS - 1)) {        
00216         _column = 0;
00217       }
00218     }
00219 
00220     return value;
00221 }
00222 
00223 /** Get a single character (Stream implementation)
00224   *
00225   * @param none  
00226   * @return -1
00227   */
00228 int CU209SCPB::_getc() {
00229     return -1;
00230 }