A mirror of my blog that I host. Mirroring it here just to provide an additional backup. Check out the real deal at http://www.justinandrewfincher.com/

Friday, December 30, 2011

Setting up my Pertelian LCD on my linux box showing recent tweets

If you didn't pick up on it from the title, this is going to be a pretty geeky post :-) I had a little time over Christmas break to finally play around with setting up my Pertelian X2040 LCD screen that Julie gave me LAST Christmas! How slow am I?  I ran into a couple snags and had to code up some things for it to display as I would like so I figured I would post it in case any fellow geeks started looking to do something similar. First, I had to get my screen talking to my linux box at work.  While it seems like a geeky little gift, Pertelian only lists it as being compatible with Windows (LAME!!) so I had to go looking around for an alternative means of connecting.  I came across lcd4linux that listed my screen as one they were working on supporting. I had to use the pre-release build for version 0.11 because the upcoming release is the first one to include the Pertelian drivers.  Getting it basically up and running was fairly simple (just a few edits to the included lcd4linux.config file to add my display.  
Display pert-lcd {    Driver 'Pertelian'    Port '/dev/ttyUSB0'    Size '20x4'    Backlight 1    Icons 1 }
  Anyhow, I got it so I could do some basic display things through the program so it was on to configure it for my exact application, displaying recent tweets!  I don't check Twitter that often, so I thought it would be cool if I could set up my little screen to constantly display recent tweets.  Twitter no longer provides an RSS feed for your timeline (all the tweets from people you follow), but with some searching I discovered this post describing using Roomatic to generate an RSS feed for you.  Once I got that, I put together a little bash script to parse out the 4 most recent tweets. Then I tried to use the Text layout from lcd4linux along with a readline to read each tweet in.  After much digging and frustration, I discovered that readline only reads the first 80 characters.  Not too handy for tweets that can be 140 characters!  So I resigned to just show the 2 most recent tweets each split across lines and was almost done with that when I remembered a brief example that I saw that concatenated strings. So I revamped my script to split each tweet into two 80 character lines, then concatenated them within my config file.  Here's an example:  
Widget Twitter1 {    class 'Text'    expression file::readline('timeline.txt', 1).file::readline('timeline.txt', 2)    width 20    align 'M'    speed 200     update tick }
  So everything that is displayed on the screen is done through widgets.  Then you have a Layout that tells the screen where to put each widget. In the above widget, I want to display the first tweet in my timeline. Since I parsed it to have half on line one and the other half on line 2, I use the . to concatenate these two strings.  I still find it odd that lcd4linux has no issue dealing with strings longer than 80 characters, yet readline has that limit. Anyhoo, I combine the widgets for each tweet into a layout for my screen.  
Layout Twitter {    Row01.Col1 'Twitter1'    Row02.Col1 'Twitter2'    Row03.Col1 'Twitter3'    Row04.col1 'Twitter4' }
  In the widget, you see that the align parameter is set to 'M' which means marquee.  This is what we want since a tweet can have over 140 characters, yet my screen is only 20 characters wide.  So the result is 4 lines, each slowly scrolling through the most recent 4 tweets in my timeline! Luckily, lcd4linux constantly performs the readline operations so I just tossed my timeline parsing script in my crontab to run every 15 minutes and as soon as it is updated the display updates with the current timeline. Success!
The only thing I'd really like to tweak now is how exactly it runs.  If I try to run the basic lcd4linux command, it just starts and closes without doing anything.  To make it actually use my designated layout, I have to run it with the -F option that prevents it from forking and runs it in the foreground.  The odd thing is that if I run 'lcd4linux -F &' then it puts it in the background and it runs fine.  No idea what they are doing when they fork off, but apparently my machine doesn't like it. Yay for geeky projects!

No comments:

Post a Comment