Spark Fun LCD API

My apologies if this already exists somewhere.

This is a very simple bit of wrapper code that lets you use the Spark Fun SerLCD devices. The hook up for these is ridiculously simple - +5V/Gnd/Rx on LCD to +5/GND/RX on Arduino. The test program included will use the display to show H:M:S since boot time on the display.

I’ve been trying to figure out a way to make this into a library that I could post - any suggestions where I can look?
advance.

The unit I am using is this one:

#include <stdio.h>

// ------------------------------------------------------------
// SparkFun LCD display API
// ------------------------------------------------------------


extern void LCDOpen  ();                                            // open the LCD interface
extern void LCDClear ();                                            // clear the screen
extern void LCDText  ( int xPos , int yPos , char * message );      // write text at a location

extern void LCDSetSplash ();                                        // save current screen as the splash screen
extern void LDCSetBright ( int perCent );                           // set the display brightness in percent


// ------------------------------------------------------------
// LCDopen - open the LCD display on the serial interface
// ------------------------------------------------------------

void LCDOpen()
{
  // open the serial connection
  
  Serial.begin ( 9600 );
  
  // clear the screen
  
  LCDClear();
}


// ------------------------------------------------------------
// LCDclear- clear the LCD display
// ------------------------------------------------------------

void LCDClear()
{
  // send the screen clear command
  
  Serial.print ( 0xFE , BYTE );    // special display command
  Serial.print ( 0x01 , BYTE );    // clear screen
}

// ------------------------------------------------------------
// LCDText - draw a text message to the display at specified position
// ------------------------------------------------------------

void LCDText ( int xPos , int yPos , char * message )
{
  int crsPos;
  
  // compute the cursor position for the device
  
  switch ( yPos ) 
  {
    case 0  : crsPos = 0x00; break;
    case 1  : crsPos = 0x40; break;
    case 2  : crsPos = 0x10; break;
    case 3  : crsPos = 0x50; break;
    default : crsPos = 0x0; break;
  }
  
  crsPos += xPos; crsPos |= 0x80;
  
  
  // position the cursor
  
  Serial.print ( 0xFE   , BYTE );    // special display command
  Serial.print ( crsPos , BYTE );    // set the cursor position
  
  
  // print the message
  
  Serial.print ( message );
}

// ------------------------------------------------------------
// LCDSetSplash - save the current screen as the splash screen to EEPROM
// ------------------------------------------------------------

void LCDSetSplash()
{
  
  // send commands to save the current
  
  Serial.print ( 0x7C , BYTE );        // special EEPROM command
  Serial.print ( 0x0A , BYTE );        // save screen as splash
  
  
  // confirm to the display
  
  delay ( 3000 );                      // give the EEPROM time to write
  LCDClear();                          // clear the display
  LCDText ( 0 , 0 , "splash set" );    // confirmation message
  delay ( 3000 );                      // let the user see it
  
}

// ------------------------------------------------------------
// LDCSetBright - set the display brightness in percent
// ------------------------------------------------------------

void LCDSetBright ( int perCent )
{
  // compute backlight code from percentage
  
  int blCode = 128 + ( 157 - 128 ) * perCent / 100;
  
  // send command
  
  Serial.print ( 0x7C , BYTE );        // special EEPROM command
  Serial.print ( blCode , BYTE );      // send the brightness value
  
  
  // confirm to the display
  
  char msg[40];
  sprintf ( msg , "brt %d%%" , perCent , blCode );
  
  delay ( 3000 );                      // give the EEPROM time to write
  LCDClear();                          // clear the display
  LCDText ( 0 , 0 , msg );             // confirmation message
  delay ( 3000 );                      // let the user see it 
}



// ------------------------------------------------------------
// test program
// ------------------------------------------------------------


void setup ()
{
  
  // open the display
  
  LCDOpen();
  
  // set the splash screen and brigthness ( need this only once to configure ) 
  // LCDText    ( 0 , 0 , "in case of fire" );
  // LCDText    ( 0 , 1 , "  break glass  " );
  // LCDSetSplash();
  // LCDSetBright ( 80 );



}

void loop ()
{
  
  // clear the screen
  
  LCDClear();
  
  // calcuate time since boot in HH:MM:SS.MMM format
  
  unsigned long bootMS = millis();
  unsigned long tHours = bootMS / ( 1000l * 60l * 60l ); bootMS -= tHours * ( 1000l * 60l * 60l );
  unsigned long tMins  = bootMS / ( 1000l * 60l );       bootMS -= tMins  * ( 1000l * 60l );
  unsigned long tSecs  = bootMS / ( 1000l );             bootMS -= tSecs  * ( 1000l );
  unsigned long tMSecs = bootMS;
  
  
  // draw message to the display
  
  char msg[40];
  sprintf ( msg , "%.2ld:%.2ld:%.2ld.%.3ld" , tHours , tMins , tSecs , tMSecs );
  LCDText ( 2 , 0 , msg );
  
  
  // don't overrun the display
  
  delay ( 100 );
}

Hi,

I just bought a SparkFun SerLCD and found your code. Did you mean to use the Rx on the Arduino? Or is this a typo and you meant to say TX ?

to +5/GND/RX on Arduino

Apart from that, thanks! I had tried using the SLCD library from the playground but it gave me compilation errors. Your code worked great.

Ver

Heh, yes, that was a typo. I wrote that code quite a while ago so I’m happy to see that it still works. Thanks for the nice comments.

so how does this code compare to the SoftwareSerial library? I picked up the same Sparkfun LCD and had used SoftwareSerial to drive it without problems and didn't even know there was another library for the same thing.

Well, just scrolling through the code above should help you see.

The serial library knows how to send bytes across a serial link. It knows nothing about device protocols, like escape codes or cursor commands or other LCD-specific features.

The API above does not know how to send bytes across the serial link (it uses the Serial library to do that), but it knows the command protocols for this kind of LCD.

You could do all this yourself, but the libraries just help you do more with less effort.

Thanks! I think it can be useful. So I made some simple changes and I made a library called “SerLCD”.

I will write the download link in the next post (this is my first post and I don’t have privileges to post an external link…)

Unzip this into the “hardware/libraries” Arduino’s directory. Remember to restart Arduino after install the library.

This is a simple example code.

#include <SerLCD.h>

SerLCD lcd;
void setup () {
// open the display
lcd.open();

// set the splash screen and brigthness ( need this only once to configure )
lcd.print ( 0 , 0 , " splash " );
lcd.print ( 0 , 1 , " screen " );
lcd.splash();
// lcd.bright ( 80 );
}

void loop {
// clear the screen
lcd.clear();
lcd.print ( 2 , 0 , “hello!!!” ); //now you can write anything (string, char, int…)
}

SerLCD library: http://www.megaupload.com/?d=X46I00JD

Thanks for this. I have the 16x2 Sparkfun LCD that I purchased for a Canon DSLR Intervalometer project I am working on. Will give it a bash.

Hi,

I tried the library and example but I get these errors when I compile:

In file included from /Applications/arduino-0013/hardware/cores/arduino/WProgram.h:4,

/Applications/arduino-0013/hardware/tools/avr/bin/../lib/gcc/avr/4.3.0/../../../../avr/include/stdlib.h:111: error: expected unqualified-id before 'int'

/Applications/arduino-0013/hardware/tools/avr/bin/../lib/gcc/avr/4.3.0/../../../../avr/include/stdlib.h:111: error: expected `)' before 'int'

/Applications/arduino-0013/hardware/tools/avr/bin/../lib/gcc/avr/4.3.0/../../../../avr/include/stdlib.h:111: error: expected `)' before 'int'

In file included from /Applications/arduino-0013/hardware/cores/arduino/WProgram.h:6,

Any ideas why? Is it because I'm using Arduino 13?

Any help much appreciated.

Cheers

Boppyer