pangren
Posts: 42
Joined: Thu Dec 19, 2013 2:52 pm

LCD with buttons that does things.

Tue Jan 27, 2015 2:32 am

Hi Forum,
I’m a beginner user of Raspberry pi. Familiar with basic python , and basic linux. I’ve created a media server Raspbian and also a different build with OpenElec. I also brought a LCD from adafruit, http://www.adafruit.com/product/1115

I’ve tinkered with the LCD, but don’t really know how to use it realistically. Now I want to have the LCD to display some “menu items” like up and down buttons will trigger the lcd to display “Shutdown” / “restart” and then pressing the select button will trigger linux commands like ‘sudo reboot’ and ‘sudo shutdown -h now’.

Can anyone point me some direction as to how i can create this “menu” . Sudo code would be great. Of course actual code would help too. And also do I need like a cron job or something? I’m totally unfamiliar with how this code would start at startup.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: LCD with buttons that does things.

Tue Jan 27, 2015 2:39 am

pangren wrote:Hi Forum,
I’m a beginner user of Raspberry pi. Familiar with basic python , and basic linux. I’ve created a media server Raspbian and also a different build with OpenElec. I also brought a LCD from adafruit, http://www.adafruit.com/product/1115

I’ve tinkered with the LCD, but don’t really know how to use it realistically. Now I want to have the LCD to display some “menu items” like up and down buttons will trigger the lcd to display “Shutdown” / “restart” and then pressing the select button will trigger linux commands like ‘sudo reboot’ and ‘sudo shutdown -h now’.

Can anyone point me some direction as to how i can create this “menu” . Sudo code would be great. Of course actual code would help too. And also do I need like a cron job or something? I’m totally unfamiliar with how this code would start at startup.
Did you read the product information page that you linked to?

On there is a link to another page about using the display with Python.
https://learn.adafruit.com/adafruit-16x ... y-pi/usage

You might need a cron job or something, but that comes later.

pangren
Posts: 42
Joined: Thu Dec 19, 2013 2:52 pm

Re: LCD with buttons that does things.

Tue Jan 27, 2015 4:24 am

Yep I already used the library and used it. But the usage is kinda basic. like a button will trigger a display. But I do not know how to create sort of a menu, then up and down will scroll through a list, and select will trigger a command.

Then how to get this python script to run on startup , how to add it to like cron job . (which is kind of still alien to me)

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: LCD with buttons that does things.

Tue Jan 27, 2015 4:33 am

One day you'll learn how to use Google. Until then:
http://forums.adafruit.com/viewtopic.php?f=47&t=37191

User avatar
PangolinPaws
Posts: 89
Joined: Wed Mar 05, 2014 9:04 pm
Location: Wiltshire, UK
Contact: Website

Re: LCD with buttons that does things.

Tue Jan 27, 2015 9:59 am

Hello, I recently bought a similar LCD and came up with a basic menu.

I used a list to hold my menu options:

Code: Select all

menuOptions = ['Run_program', 'Settings', 'Show_IP', 'Shutdown']
And two variables to hold which options were currently on screen:

Code: Select all

topLine = 0
botLine = 1
And this bit prints the options to the screen:

Code: Select all

lcd.message(' %s\n> %s\n' % (menuOptions[topLine], menuOptions[botLine]))
Then all you need to do is get the Up and Down buttons to change the values of the topLine and botLine variables, stick the whole thing in a loop and you have a scrollable menu.

I haven't found an elegant solution to selecting items yet, so let us know how you get on and I may steal your idea...
https://github.com/PangolinPaw

pangren
Posts: 42
Joined: Thu Dec 19, 2013 2:52 pm

Re: LCD with buttons that does things.

Wed Jan 28, 2015 10:02 am

Hi Pangolin!, I've tried to create a menu with some sudo code below.

Code: Select all

import os
#from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate

Menu = { 0:{'WELCOME to Home Box':'WELCOME' } ,1 : {'Shutdown' : 'sudo -shutdown -h now'} , 2 : {'Reboot': 'sudo reboot'}}
myIndex=0

def plusOne():
    global myIndex
    if 0<=myIndex<=1 : 
        myIndex = myIndex + 1
        print (Menu[myIndex])
    #check which index number youre at
    #add one
    #print resultant index

def minusOne():
    global myIndex
    if 1<=myIndex<=2:
        myIndex = myIndex-1
        print (Menu[myIndex])

def selectMenu():
    global myIndex

    if myIndex ==0:
        print ('HomeBox \nMenu:')
    
    elif myIndex == 1:
        print ('Shutting down...')
        print (Menu[1]['Shutdown'])
    elif myIndex ==2:
        print ('Rebooting now')
        print ( Menu[2]['Reboot'])
    
I did print statements so i can test it with my pc first. But I'd gradually change the print statements to your lcd.messages() command. Its just a preliminary, but I'll post here if I improve on it.

User avatar
PangolinPaws
Posts: 89
Joined: Wed Mar 05, 2014 9:04 pm
Location: Wiltshire, UK
Contact: Website

Re: LCD with buttons that does things.

Wed Jan 28, 2015 1:27 pm

I like the way you're using a dictionary with (if I've read it right) the Key as the message shown on the LCD and the Item as the command run when said item is selected.

My inelegant solution was to have the displayed message e.g. 'run_program' also be the name of a function defined within the code. For example:

Code: Select all

# Here are the functions:
def run_program():
    os.system('python timelapse.py')

def shutdown():
    os.system('sudo halt')

def restart():
    os.syste,('sudo reboot')

# List of functions
menuOptions = ['run_program', 'shutdown', 'reboot']

# All the scrolling & selecting code goes in here

# Display selected option on the LCD
lcd.message(menuOption[0]) 

# Execute the corresponding function
menuOption[0]() 
https://github.com/PangolinPaw

Return to “Python”