Hardware Reference
In-Depth Information
# Don't time out we don't care about responses
"Timeout" => false)
# Send the hello command to start communicating
pilcd.puts("hello")
# Add a screen and set it to foreground
pilcd.puts("screen_add s1")
pilcd.puts("screen_set s1 -priority 1")
# Create two widgets for each row
pilcd.puts("widget_add s1 w1 title")
pilcd.puts("widget_add s1 w2 string")
# Write to each widget to each row of the LCD
pilcd.puts("widget_set s1 w1 {I2C LCD}")
pilcd.cmd("widget_set s1 w2 4 2 {MOAR Pi !}") { |c| print c }
Let me talk you through the parts of the code that interact with the
LCDd
server. The rest of the code is basic Ruby.
The code will start off with the
hello
command: this tells the server you want to talk to it. Then you need to add a
screen; remember the screens in
lcdproc
? This is no different; it's just our very own screen that I have called
s1
. How
exciting! As I only have one screen, I want it to always be displayed on the LCD; this is where the following line comes in:
screen_set s1 -priority 1
Without this line
LCDd
would scroll though all your defined screens. Now that you have an active screen it's time
to define two widgets. A widget is a container for text and formatting that will end up on each line of the physical LCD
when you run the client. The first widget is called
w1
and is a
title
widget; this gives the nice square block effect you
see in Figure
4-21
. The second widget is called
w2
and is just your everyday boring string type. So your widgets and
screens are done: now what? Now you should set the widgets on the screen. I set
w1
on
s1
and then wrote the text “I2C
LCD.” As this is a title for the widget, you don't set the location on the screen as it will take up the whole top row (it's a
title, remember?). The second widget,
w2
, is also set on
s1
but this time you need to set the location. You start off with
the X axis; ask for the first character to start on the fourth LCD block. Then set the Y axis as 2, which means the second
row of the physical LCD. After that, you are just feeding it the string of “MOAR Pi!” In Figure
4-21
you can see the LCD
working and printing the text we asked for.