Menu system for LCD Plates
Moderators: adafruit_support_bill, adafruit

Menu system for LCD Plates

by aufder on Sun Feb 17, 2013 1:30 am

Hey all,
(shameless plug)
I posted a menu system for the Raspberry Pi that uses an Adafruit LCD Plate (I'm using the 16x2 Positive) as the primary interface to the Pi. It allows you to define your own nested menu structure in an XML file, that then gets rendered as a navigable menu enabling you to create functions to run.
It allows you to get the IP address when it's headless and DHCP. It allows you to shut it down safely, and much much more.
Have fun figuring it out, and using it. It's my first project on git, so hope it all works for you.
https://github.com/aufder/RaspberryPiLcdMenu

Alan
aufder
 
Posts: 13
Joined: Tue Jan 15, 2013 9:07 pm

Re: Menu system for LCD Plates

by adafruit_support_rick on Sun Feb 17, 2013 3:30 pm

Thanks for posting this! :D
User avatar
adafruit_support_rick
 
Posts: 11613
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Menu system for LCD Plates

by bbkiwi on Sun Feb 17, 2013 11:31 pm

Very nice. I ran it using WebIDE. I had to change line 32 of lcdmenu.py to ON to see the display.
bbkiwi
 
Posts: 9
Joined: Thu Jan 31, 2013 9:08 pm

Re: Menu system for LCD Plates

by aufder on Mon Feb 18, 2013 11:03 am

bbkiwi,
Maybe your contrast dial needs a tweak?
On mine, I can read the text even with the backlight off. Then I can optionally go into the LCD Color menu and pick a color which turns on the backlight.
Enjoy
aufder
 
Posts: 13
Joined: Tue Jan 15, 2013 9:07 pm

Re: Menu system for LCD Plates

by bbkiwi on Mon Feb 18, 2013 9:22 pm

Ive got a Adafruit Blue&White 16x2 LCD+Keypad Kit for Raspberry Pi - ID: 1115. I never noticed that the display showed with
the backlight off. Even with the pot turned to the extreme, the characters are very faint with the backlight off.
I'm new to Python and your code is very instructive. Nice work.
bbkiwi
 
Posts: 9
Joined: Thu Jan 31, 2013 9:08 pm

Re: Menu system for LCD Plates

by aufder on Mon Feb 18, 2013 9:38 pm

To assist bbkiwi, I just added support for some settings in the xml config file. You can set the lcd on and to a color by default on startup if you wish, or leave it off. Note the new settings by pulling the latest.
aufder
 
Posts: 13
Joined: Tue Jan 15, 2013 9:07 pm

Re: Menu system for LCD Plates

by TxBobS on Wed Feb 20, 2013 4:11 pm

Thanks for this. I pieced a simple little menu together last night but was hoping I could find some code that was more versatile. Thanks!
TxBobS
 
Posts: 8
Joined: Wed Jan 30, 2013 2:11 pm

Re: Menu system for LCD Plates

by wildtang3nt on Mon Mar 04, 2013 1:26 am

Hi, I've added a function to show the kernel info, but I'd like to know how to add a second line for additional uname options. I was thinking uname -mo for the top line and uname -r for the lower. I don't have much python experience, I've been fumbling around with this unsuccessfully.

Here's what I have so far:

Code: Select all | TOGGLE FULL SIZE
def ShowKernel():
    if DEBUG:
        print('in ShowKernel')
    lcd.clear()
    lcd.message(commands.getoutput("/bin/uname -sr"))
    while 1:
         if lcd.buttonPressed(lcd.LEFT):
             break
         sleep(0.25)
Proud owner of (among other wonderful things):
Li-Poly MintyBoost
RGB LCD Pi Plate & Pi Dish
Ice Tube Clock & USBTinyISP
wildtang3nt
 
Posts: 32
Joined: Thu Aug 09, 2012 1:31 pm
Location: Hamilton, Ontario, Canada

Re: Menu system for LCD Plates

by aufder on Mon Mar 04, 2013 10:02 am

wildtang,
If you want to get the output of multiple commands, you'd have to join them together with a newline in between. Assuming each command does output only one line:
Code: Select all | TOGGLE FULL SIZE
    line1 = commands.getoutput("/bin/uname -mo")
    line2 = commands.getoutput("/bin/uname -r")
    lcd.message(line1 + '\n' + line2)


If in fact the command does output multiple lines already and you just want the first line from each, then you can do:
Code: Select all | TOGGLE FULL SIZE
    line1 = commands.getoutput("/bin/uname -mo").split('\n')
    line2 = commands.getoutput("/bin/uname -r").split('\n')
    lcd.message(line1[0] + '\n' + line2[0])


Hope that helps.
aufder
 
Posts: 13
Joined: Tue Jan 15, 2013 9:07 pm

Re: Menu system for LCD Plates

by wildtang3nt on Mon Mar 04, 2013 1:48 pm

Thanks! That worked perfectly :D
Proud owner of (among other wonderful things):
Li-Poly MintyBoost
RGB LCD Pi Plate & Pi Dish
Ice Tube Clock & USBTinyISP
wildtang3nt
 
Posts: 32
Joined: Thu Aug 09, 2012 1:31 pm
Location: Hamilton, Ontario, Canada

Re: Menu system for LCD Plates

by wildtang3nt on Fri Mar 08, 2013 1:41 pm

I've modified added a couple useful functions to your code and thought it would be the neighbourly thing to do to share :D

http://dl.dropbox.com/u/14023002/lcdmenu.py

The parts I've added or extended:
Code: Select all | TOGGLE FULL SIZE
def DoReboot():
    lcd.clear()
    lcd.message('Are you sure?\nPress Sel for Y')
    while 1:
        if lcd.buttonPressed(lcd.LEFT):
            break
        if lcd.buttonPressed(lcd.SELECT):
            lcd.clear()
            lcd.backlight(lcd.OFF)
            commands.getoutput("sudo shutdown -r now")
            quit()
        sleep(0.25)
def DoEnableUSB():
    lcd.clear()
    lcd.message('Are you sure?\nPress Sel for Y')
    while 1:
        if lcd.buttonPressed(lcd.LEFT):
            break
        if lcd.buttonPressed(lcd.SELECT):
            lcd.clear()
            commands.getoutput("echo 0x1 > /sys/devices/platform/bcm2708_usb/buspower")
            break
        sleep(0.25)

def DoDisableUSB():
    lcd.clear()
    lcd.message('Are you sure?\nPress Sel for Y')
    while 1:
        if lcd.buttonPressed(lcd.LEFT):
            break
        if lcd.buttonPressed(lcd.SELECT):
            lcd.clear()
            commands.getoutput("echo 0x0 > /sys/devices/platform/bcm2708_usb/buspower")
            break
        sleep(0.25)

def UpTime():
 
    try:
        f = open( "/proc/uptime" )
        contents = f.read().split()
        f.close()
    except:
       return "Cannot open file"

    total_seconds = float(contents[0])

    MINUTE  = 60
    HOUR    = MINUTE * 60
    DAY     = HOUR * 24
 
    days    = int( total_seconds / DAY )
    hours   = int( ( total_seconds % DAY ) / HOUR )
    minutes = int( ( total_seconds % HOUR ) / MINUTE )
    seconds = int( total_seconds % MINUTE )
 
    string = "Uptime:\n"
    if days > 0:
        string += str(days) + "d "
    if len(string) > 0 or hours > 0:
        string += str(hours) + "h "
    if len(string) > 0 or minutes > 0:
        string += str(minutes) + "m "
    string += str(seconds) + "s"
 
    return string;

def ShowUptime():
    if DEBUG:
        print('in ShowUptime')
    lcd.clear()
    while not(lcd.buttonPressed(lcd.LEFT)):
        sleep(0.25)
        lcd.home()
        lcd.message(UpTime())

def ShowKernel():
    if DEBUG:
        print('in ShowKernel')
    lcd.clear()
    line1 = commands.getoutput("/bin/uname -mo").split('\n')
    line2 = commands.getoutput("/bin/uname -r").split('\n')
    lcd.message(line1[0] + '\n' + line2[0])
    while 1:
        if lcd.buttonPressed(lcd.LEFT):
            break
        sleep(0.25)

def ShowIPAddress():
    if DEBUG:
        print('in ShowIPAddress')
    lcd.clear()
    line1 = commands.getoutput("/bin/hostname")
    line2 = commands.getoutput("/sbin/ifconfig").split('\n')[1].split()[1][5:]
    lcd.message(line1 + '\n' + line2)
    while 1:
        if lcd.buttonPressed(lcd.LEFT):
            break
        sleep(0.25)
Proud owner of (among other wonderful things):
Li-Poly MintyBoost
RGB LCD Pi Plate & Pi Dish
Ice Tube Clock & USBTinyISP
wildtang3nt
 
Posts: 32
Joined: Thu Aug 09, 2012 1:31 pm
Location: Hamilton, Ontario, Canada

Re: Menu system for LCD Plates

by djr on Tue Apr 16, 2013 3:23 pm

Very nicely done! Cant wait to start hacking it!! :)

I do, however, seem to be having an issue when pressing the buttons on the plate, it seems to jump rather quickly across certain menus choices. I havent done anything to the code. It starts off at System Info and Sensors but if I press the down button, it jumps all the way down to Quit and Shutdown. If i press the up button, it might jump up to Astronomy and Camera but usually it jumps back to the top. Should the dash follow the choice?

Any thoughts?
djr
 
Posts: 5
Joined: Sat Feb 16, 2013 8:31 pm

Re: Menu system for LCD Plates

by azrith on Fri May 10, 2013 9:33 pm

djr

I had the same issue. I found the issue to be the main loop that monitors buttons presses. It's looping so quickly it's catching multiple presses before you release the button. Adding a sleep delay in the loop should correct it.
Thanks for the awesome code everyone. I'll be using this for an automatic sprinkler timer I've put together.

Found in lcdmenu.py
Code: Select all | TOGGLE FULL SIZE
while 1:
if (lcd.buttonPressed(lcd.LEFT)):
display.update('l')
display.display()

if (lcd.buttonPressed(lcd.UP)):
display.update('u')
display.display()

if (lcd.buttonPressed(lcd.DOWN)):
display.update('d')
display.display()

if (lcd.buttonPressed(lcd.RIGHT)):
display.update('r')
display.display()

if (lcd.buttonPressed(lcd.SELECT)):
display.update('s')
display.display()
# Add the line below
sleep(.25)
azrith
 
Posts: 8
Joined: Fri May 10, 2013 9:28 pm

Re: Menu system for LCD Plates

by sub50hz on Fri May 24, 2013 9:37 am

Sorry, this may seem like a noobish question, but where do I place the files so they can import the Adafruit modules?
sub50hz
 
Posts: 2
Joined: Wed May 01, 2013 7:30 pm

Re: Menu system for LCD Plates

by sub50hz on Fri May 24, 2013 1:38 pm

Wow, ignore -- feeling stupid today. Might be worth a mention that using the init.d method, I had to add an LSB block to the file header in order for it to run, then set it using insserv, even with proper permissions on the script.

Code: Select all | TOGGLE FULL SIZE
### BEGIN INIT INFO
# Provides:          scriptname
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO
sub50hz
 
Posts: 2
Joined: Wed May 01, 2013 7:30 pm