Display librairy manage 3 LED and a TextLCD (on, off, flash) (print message, clear)

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Display.cpp Source File

Display.cpp

00001 #include "Display.h"
00002 
00003 
00004 /* Create a Display interface (default : 16X2 screen)
00005 * 
00006 * @param pinGreenLED    greenLED broche
00007 * @param pinRedLED      redLED broche
00008 * @param pinOrangeLED   orangeLED broche
00009 * @param pinLCDrs       LCD Instruction/data control line
00010 * @param pinLCDe        LCD Enable line (clock)
00011 * @param pinLCDd0-d3    LCD Data lines     
00012 */
00013 Display::Display(PinName pinGreenLED, PinName pinRedLED, PinName pinOrangeLED, PinName pinLCDrs, PinName pinLCDe,
00014         PinName pinLCDd0, PinName pinLCDd1, PinName pinLCDd2, PinName pinLCDd3) : _greenLED(pinGreenLED), _redLED(pinRedLED), 
00015         _orangeLED(pinOrangeLED), _lcd(pinLCDrs, pinLCDe, pinLCDd0, pinLCDd1, pinLCDd2, pinLCDd3)
00016 {
00017     this->clear();
00018 }
00019 
00020 /* Create a Display interface
00021 * 
00022 * @param pinGreenLED    greenLED broche
00023 * @param pinRedLED      redLED broche
00024 * @param pinOrangeLED   orangeLED broche
00025 * @param pinLCDrs       LCD Instruction/data control line
00026 * @param pinLCDe        LCD Enable line (clock)
00027 * @param pinLCDd0-d3    LCD Data lines
00028 * @param typeScreen     LCD Sets the panel size/addressing mode (default = LCD16x2)
00029 */
00030 Display::Display(PinName pinGreenLED, PinName pinRedLED, PinName pinOrangeLED, PinName pinLCDrs, PinName pinLCDe,
00031         PinName pinLCDd0, PinName pinLCDd1, PinName pinLCDd2, PinName pinLCDd3, TextLCD::LCDType typeScreen) : 
00032             _greenLED(pinGreenLED), _redLED(pinRedLED), _orangeLED(pinOrangeLED), 
00033             _lcd(pinLCDrs, pinLCDe, pinLCDd0, pinLCDd1, pinLCDd2, pinLCDd3, typeScreen)
00034 {
00035     this->clear();
00036 }
00037 
00038 /* Destructor
00039 */
00040 Display::~Display()
00041 {
00042 }
00043 
00044 /* print message on LCD screen and change status of green, red and orange LED
00045 * all line except the last line, have to finish by '\n'. Warning : '\n' count like a character
00046 * don't end message by '\n'
00047 *
00048 * @param message            message will be display on screen
00049 * @param statusGreenLED     new status of greenLED (on, off, flash)
00050 * @param statusRedLED       new status of redLED (on, off, flash)
00051 * @param statusOrangeLED    new status of orangeLED (on, off, flash)
00052 */
00053 void Display::printMessage(string message, choiceStatusLED statusGreenLED, choiceStatusLED statusRedLED, choiceStatusLED statusOrangeLED) {
00054     // clear LCD screen and shutdown 3 LED
00055     this->clear();
00056     
00057     // change green LED status to statusGreenLED
00058     this->changeStatusLED(Display::green, statusGreenLED);
00059     // ...
00060     this->changeStatusLED(Display::red, statusRedLED);
00061     this->changeStatusLED(Display::orange, statusOrangeLED);
00062     
00063     if (this->checkMessage(message))    
00064         // print message on LCD screen
00065         this->_lcd.printf("%s\n", message.c_str());
00066     else
00067         this->_lcd.printf("this message can't show rigthly");
00068 }
00069 
00070 
00071 /* put off 3 LED and clear LCD screen
00072 */
00073 void Display::clear(void) {
00074     this->shutdownLED();
00075     this->cls();
00076 }
00077 
00078 
00079 
00080 /* change led status to statusLED
00081 * use by printMessage()
00082 *
00083 * @param led        the led (green, red, orange)
00084 * @param statusLED  new status (on, off, flash)
00085 */
00086 void Display::changeStatusLED(choiceLED led, choiceStatusLED statusLED) {
00087     Led *ptr_led;
00088     
00089     // select LED 
00090     switch (led) {
00091     case green : ptr_led = &_greenLED;
00092                 break;
00093     case red : ptr_led = &_redLED;
00094                 break;
00095     default : ptr_led = &_orangeLED;
00096                 break;
00097     }
00098     
00099     // change this status
00100     switch (statusLED) {
00101     case on : ptr_led->on();
00102                 break;
00103     case flash : ptr_led->flash();
00104                 break;
00105     default : ptr_led->off();
00106                 break;
00107     }
00108 }
00109 
00110 /* put off 3 LED
00111 * use by clear()
00112 */
00113 void Display::shutdownLED(void) {
00114     _greenLED.off(); _redLED.off(); _orangeLED.off();
00115 }
00116 
00117 /* clear LCD screen
00118 * use by clear()
00119 */
00120 void Display::cls(void) {
00121     _lcd.cls();
00122 }
00123 
00124 /* check if this message can be display correctly on LCD screen 
00125 * use by printMessage()
00126 *
00127 * @param message    the message to be verified
00128 * @return 
00129 *        true if message is ok
00130 *        flase else
00131 */
00132 bool Display::checkMessage(string message) {
00133     // check message length (don't exceed screen capacity)
00134     unsigned int screenSize = _lcd.columns() * _lcd.rows();
00135     if (message.length() > screenSize)
00136         return false;
00137     
00138     // check number of '\n'
00139     // TextLCD is circular, so if we are on last line, a '\n' we come back on first line
00140     int nbCRLF = this->calculNbCRLF(message);
00141     if (nbCRLF >= _lcd.rows())
00142         return false;
00143     
00144     // check all line length (don't exceed column capacity)
00145     std::vector<std::string> allLine = this->subAllLine(message);
00146     int lineLength;
00147     for (unsigned int i = 0; i < allLine.size(); i++) {
00148         lineLength = allLine[i].length();
00149         if (lineLength > _lcd.columns())
00150             return false;
00151     }
00152     
00153     
00154     return true;
00155 }
00156 
00157 /* calcul number of '\n'
00158 * use by checkMessage()
00159 *
00160 * @param message    the message to be analyse
00161 * @return number of '\n'
00162 */ 
00163 int Display::calculNbCRLF(string message) {
00164     unsigned int index = 0, nbCRLF = 0;
00165     
00166     while ((index = message.find('\n', index)) != std::string::npos) {      // npos is a static member constant of string,
00167         nbCRLF++;                                                           // defined with a value of -1
00168         index++;
00169     }
00170     
00171     return nbCRLF;
00172 }
00173 
00174 /* return vector of all line message
00175 * use by checkMessage()
00176 *
00177 * @param message    the message to be cut
00178 * @return   a vector which contains a line of the message by entry
00179 */
00180 vector<string> Display::subAllLine(string message) {
00181     std::vector<std::string> allLine;
00182     unsigned int index = 0, indexNext = 0; 
00183     
00184     // get next '\n' from last position
00185     while ((indexNext = message.find('\n', index)) != std::string::npos) {
00186         // sub new line (with '\n')
00187         allLine.push_back(message.substr(index, indexNext-index+1));
00188         // go to next line
00189         index = indexNext + 1;
00190     }
00191     
00192     // get last line
00193     allLine.push_back(message.substr(index));
00194         
00195     return allLine;
00196 }