Electric Imp and serial LCD 16x2 display

I’ve hooked up a Sparkfun serial 16x2 LCD display to an electric imp, and I’ve been able to display very basic text messages. But I’d like to improve my control of the text: add scrolling right to left, or scrolling up – fancy effects stolen directly from my local grocery store window display : )

I’m interested in any suggestions or advice for upping my text display capabilities. I’ve pasted the code I’m currently using, below.

Thanks in advance.

Agent

`// Log the URLs we need
server.log("Turn LED On: " + http.agenturl() + “?led=1”);
server.log("Turn LED Off: " + http.agenturl() + “?led=0”);

function requestHandler(request, response) {
try {
// check if the user sent led as a query parameter
if (“led” in request.query) {
local ledState = request.query.led.tostring();

    // send "led" message to device, and send ledState as the data

    device.send("led", ledState);  }

// send a response back saying everything was OK.

response.send(200, “OK”);
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);`

Device

`// create a global variabled called led,
// and assign pin9 to it
led <- hardware.pin9;
lcd <- hardware.uart12;

button <- hardware.pin5;

function buttonPress() {
local state = button.read();
if (state == 1) {
// when the button is released
server.log(“release”);
} else {
// when the button is pressed
server.log(“press”);
led.write(0);
lcd.write(254);
lcd.write(0x01);
}

}

button.configure(DIGITAL_IN_PULLUP, buttonPress);

// configure led to be a digital output
led.configure(DIGITAL_OUT);
lcd.configure(9600, 8, PARITY_NONE, 1, NO_RX);
lcd.write(254);
lcd.write(0x01);
lcd.write(124);
lcd.write(155);

// function to turn LED on or off
function setLed(ledState) {
server.log("Set LED: " + ledState);
lcd.write(254);
lcd.write(0x01);

lcd.write(ledState);
led.write(1);
}

// register a handler for “led” messages from the agent
agent.on(“led” , function(ledState) {
setLed(ledState);
});`

Here is a class that does some scrolling, should be good reference.

Thanks MakeDeck. I tried to use the class, and the code checked out, but I’m not getting through to the LCD.

Here’s the code

Device

`// Configure the UART port
local port0 = hardware.uart57
port0.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);

// Boot!
server.log(“booting!”);

// Allocate and start a screen
screen <- SerLCD(port0);
screen.clear_screen();
screen.start();

screen.set0(“Hello”); // Write the first line
screen.set1(“World”); // Write the second line

// register a handler for “led” messages from the agent
agent.on(“led” , function(ledState) {
screen.set0(ledstate); // Write the first line
});`

Agent

`// Log the URLs we need
server.log("Turn LED On: " + http.agenturl() + “?led=1”);
server.log("Turn LED Off: " + http.agenturl() + “?led=0”);

function requestHandler(request, response) {
try {
// check if the user sent led as a query parameter
if (“led” in request.query) {

    local ledState = request.query.led.tostring();

    // send "led" message to device, and send ledState as the data
    device.send("led", ledState); 

}
// send a response back saying everything was OK.
response.send(200, "OK");

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);`

Log…

2014-06-04 19:47:19 UTC-4 [Status] Device disconnected; 40 bytes sent, 0 received, 40 total 2014-06-04 19:47:20 UTC-4 [Status] Device booting; 2.08% program storage used 2014-06-04 19:47:20 UTC-4 [Device] booting! 2014-06-04 19:47:20 UTC-4 [Device] ERROR: the index 'SerLCD' does not exist 2014-06-04 19:47:20 UTC-4 [Device] ERROR: at main:9 2014-06-04 19:47:25 UTC-4 [Status] Device disconnected; 40 bytes sent, 0 received, 40 total

You’ll need to put the entire SerLCD class into the device code, otherwise you can’t instantiate a screen object.

okay, got it. thanks!

Now I’m trying to figure out how to create scrolling text.

Any ideas?

I remember using some displays years ago that has some smart features like scrolling and pushing lines up or down. I guess your LCD display isn’t that sophisticated?

I don’t have any code to show you, but you put your entire line of text in a string and then have 2 variables that change every .2 seconds (or whatever) and display only a portion of the line of text.

Displaying only a portion of your line of text …
https://electricimp.com/docs/squirrel/string/slice/

An example of how it might be processed … Not sure how fast you can refresh. Maybe my concept is way off.

Provide the whole string of text.
local fullString = "The meeting will start promptly at 6:30PM - In the Auditorium. Bring a friend if you wish. ";

pad the end of your text with spaces so it scrolls off the display?

This is how many characters you entered for your text.
local stringLen = fullString.len();

Define how many characters fit on one line of your display.
local displaylen=16;

Then begin with the 2 values of the first 16 characters.
startstring=1;
endstring=displaylen-1;

Now loop every .2 seconds or whatever speed it can go.
Display the newString that is created by slicing.

local newString = fullString.slice(startstring, endstring);

send newString to your display.

Now it will get tricky …

When the endstring count reaches the end of your text, you can either scroll it off until blank and restart, or wrap the text around.

startstring=startstring+1;
endstring=endstring+1;

if(endstring>stringLen){
// you reached the end of the text
// start it all over again or whatever
}

loop back to display the next section of text…

looping fast enough will create a persistence of vision effect, thus scrolling.