lcd.C

Go to the documentation of this file.
00001 /*!@file Devices/lcd.C Interface to an LCD screen via serial port */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/lcd.C $
00035 // $Id: lcd.C 6100 2006-01-17 01:59:41Z rjpeters $
00036 //
00037 
00038 #include "Devices/lcd.H"
00039 
00040 #include "rutz/compat_snprintf.h"
00041 
00042 // character byte sequences for non-printing functions
00043 //
00044 // Clear screen              : <254> <1>
00045 // Scroll left               : <254> <24>
00046 // Scroll right              : <254> <28>
00047 // Move cursor to top left   : <254> <2>
00048 // Move cursor left             : <254> <16>
00049 // Move cursor right             : <254> <20>
00050 // Make cursor underline     : <254> <14>
00051 // Make blink-block cursor   : <254> <13>
00052 // Make cursor invisible     : <254> <12>
00053 // Set cursor position             : <254> <128 + position>
00054 
00055 //! Char to be sent to gain access to special functions:
00056 #define LCD_FUNCTION 254
00057 
00058 // ######################################################################
00059 lcd::lcd(OptionManager& mgr, const std::string& descrName,
00060          const std::string& tagName) :
00061   ModelComponent(mgr, descrName, tagName),
00062   itsPort(new Serial(mgr)),
00063   itsRows("LCDrows", this, 4),
00064   itsCols("LCDcols", this, 20)
00065 {
00066   // set a default config for our serial port:
00067   itsPort->configure("/dev/ttyS0", 9600, "8N1", false, false, 1);
00068 
00069   // attach our port as a subcomponent:
00070   addSubComponent(itsPort);
00071 }
00072 
00073 // ######################################################################
00074 lcd::~lcd()
00075 { }
00076 
00077 // ######################################################################
00078 bool lcd::printf(const int x, const int y, const char *fmt, ...)
00079 {
00080   if (itsRows.getVal() != 4)
00081     LFATAL("FIXME: need some code to support various numbers of rows!");
00082 
00083   // figure out our starting offset:
00084   int offset = x;
00085   switch(y)
00086     {
00087     case 0: break;
00088     case 1: offset += 64; break;
00089     case 2: offset += itsCols.getVal(); break;
00090     case 3: offset += 64 + itsCols.getVal(); break;
00091     default: LFATAL("Row number %d out of range 0..3", y);
00092     }
00093 
00094   // get to that position:
00095   if (setCursorPosition(offset) == false) return false;
00096 
00097   // format and write out our message, truncating at our number of columns:
00098   char txt[itsCols.getVal() + 1];
00099   va_list a; va_start(a, fmt);
00100   vsnprintf(txt, itsCols.getVal()+1, fmt, a);
00101   va_end(a);
00102   return writeString(txt);
00103 }
00104 
00105 // ######################################################################
00106 bool lcd::writeString(const char *s)
00107 { return (itsPort->write(s, strlen(s)) == int(strlen(s))); }
00108 
00109 // ######################################################################
00110 bool lcd::clear()
00111 { return writeCommand(1); }
00112 
00113 // ######################################################################
00114 bool lcd::scrollLeft(const int i)
00115 {
00116   // uses for loop to scroll sequentially:
00117   for (int j = 0; j < i; j ++)
00118     if (writeCommand(24) == false) return false;
00119   return true;
00120 }
00121 
00122 // ######################################################################
00123 bool lcd::scrollRight(const int i)
00124 {
00125   // uses for loop to scroll sequentially:
00126   for (int j = 0; j < i; j ++)
00127     if (writeCommand(28) == false) return false;
00128   return true;
00129 }
00130 
00131 // ######################################################################
00132 bool lcd::home()
00133 { return writeCommand(2); }
00134 
00135 // ######################################################################
00136 bool lcd::moveCursorLeft(const int i)
00137 {
00138   // uses for loop to move sequentially:
00139   for (int j = 0; j < i; j ++)
00140     if (writeCommand(16) == false) return false;
00141   return true;
00142 }
00143 
00144 // ######################################################################
00145 bool lcd::moveCursorRight(const int i)
00146 {
00147   // uses for loop to move sequentially:
00148   for (int j = 0; j < i; j ++)
00149     if (writeCommand(20) == false) return false;
00150   return true;
00151 }
00152 
00153 // ######################################################################
00154 bool lcd::blinkBlock()
00155 { return writeCommand(13); }
00156 
00157 // ######################################################################
00158 bool lcd::blinkUnderline()
00159 { return writeCommand(14); }
00160 
00161 // ######################################################################
00162 bool lcd::invisibleCursor()
00163 { return writeCommand(12); }
00164 
00165 // ######################################################################
00166 bool lcd::setCursorPosition(const int i)
00167 {
00168   if (i < 0 || i > 127) { LERROR("Invalid position %d", i); return false; }
00169   return writeCommand(128 + i);
00170 }
00171 
00172 // ######################################################################
00173 bool lcd::writeCommand(const byte cmd)
00174 {
00175   byte s[2]; s[0] = LCD_FUNCTION; s[1] = cmd;
00176   if (itsPort->write((char *)s, 2) != 2) return false;
00177   return true;
00178 }
00179 
00180 // ######################################################################
00181 /* So things look consistent in everyone's emacs... */
00182 /* Local Variables: */
00183 /* indent-tabs-mode: nil */
00184 /* End: */
Generated on Sun May 8 08:40:37 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3