LCD菜单,其中的选项可以与R-Pi-3 Python上的按钮进行交互

时间:2018-10-19 11:09:52

标签: python python-3.x lcd

我想创建一个可以通过按钮进行交互的菜单。我目前正在R-Pi 3 Model 316x2 LCD上用Python编程。

我提出的程序如下所示。我制作了菜单的第一层,但是您可以帮助我选择其中的一个并在这两个主菜单中创建另一个菜单。就像单个选项下的另一层一样。

我需要编程方面的帮助。我要问的是,如果我有两个主菜单,例如:水果和蔬菜,当我的光标指向蔬菜,然后按另一个按钮时,它将进入蔬菜子菜单,例如显示豌豆,卷心菜等不同的蔬菜,菠菜等,然后再单击另一个按钮返回主菜单。

我最多有四个按钮可以与菜单进行交互。

from RPLCD import CharLCD, cleared, cursor
import RPi.GPIO as GPIO
import time
from gpiozero import Button
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

lcd = CharLCD(numbering_mode=GPIO.BCM, cols=16, rows=2, pin_rs=13, 
pin_e=6, pins_data=[5,22,17,27,12,25,24,23])

def level0():
    #main menu
    lcd.cursor_pos = (0, 2)
    lcd.write_string('Vegetables')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('Fruits')
def level1():
    #sub menu for cursor(0,0)
    lcd.cursor_pos = (0, 2)
    lcd.write_string('Cabbage')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('Spinach')
def level2():
    #sub menu for cursor(1,0)
    lcd.cursor_pos = (0, 2)
    lcd.write_string('Apple')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('Mango')

button = Button(21, pull_up=False, bounce_time=0.001)#down
button2 = Button(26, pull_up=False, bounce_time=0.001)#up
button3 = Button(19, pull_up=False, bounce_time=0.001)#next
button4 = Button(20, pull_up=False, bounce_time=0.001)#back

try:
    while 1:
        for position in range(0,1):
            level0()

            b = button2.wait_for_press()
                if b == 1:
                lcd.clear()
                level0()
                print(position)
                lcd.cursor_pos = (position, 0)
                lcd.write_string('>')

            a = button.wait_for_press()
            if a == 1:
                position = position + 1
                print(position)
                lcd.clear()
                level0()
                lcd.cursor_pos = (position, 0)
                lcd.write_string('>')
                time.sleep(0.3)
except:
    pass

finally:
    lcd.clear()
    GPIO.cleanup()

非常感谢任何帮助。

0 个答案:

没有答案