RPi.GPIO中断未足够长时间调用函数

时间:2018-11-08 00:21:37

标签: python raspberry-pi character gpio lcd

因此,我正在尝试设计一个LCD屏幕,使其具有一个Menu(菜单)和一系列不同的功能,并且应该带有4个按钮。选择,菜单和上,下。现在,我只是尝试使用“菜单”按钮。我希望它始终显示时钟,除非按下菜单按钮。但是,当我按下按钮时,它应该在5秒钟内不会停留在屏幕上。我不知道为什么也不会。我查找了RPi中断并进行了跟踪,但是它仍然不会停留。 “嗨”出现甚至不到半秒钟。我还使用了raspberry pi spy的LCD库,可以根据需要链接该库。我所做的唯一一件事就是修改了一些计时内容,使其与我的显示内容相匹配,并且增加了将文字添加到四行中任何一行的能力,因为看到的都是20x4的显示。如果有人可以帮助我,将不胜感激。

代码如下:

#!usr/bin/python3
import RPi.GPIO as GPIO
import time
import sys
sys.path.append('/home/pi/Downloads')
import lcd

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
#GPIO.setup(33, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)


lcd.lcd_init()

def start():
    for x in range(0,8):
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}>----TestOS----<{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}<>---TestOS-----{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}-<>--TestOS-----{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}--<>-TestOS-----{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}---<>TestOS-----{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}----<TestOS>----{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}-----TestOS<>---{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}-----TestOS-<>--{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}-----TestOS--<>-{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}-----TestOS---<>{]", 1)
        time.sleep(0.1)


    lcd.lcd_init()

    for x in range(0,120):
        lcd.lcd_byte(0xFF,True)

    time.sleep(5)
    lcd.lcd_init()


def Menu():
    lcd.lcd_init()
    lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
    lcd.lcd_string("hi", 1)
    lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
    lcd.lcd_string("hi", 1)
    time.sleep(5)

def clock():
    lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
    lcd.lcd_string("+------------------+", 1)
    lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
    lcd.lcd_string(time.strftime("|%I:%M %p          |"), 1)
    lcd.lcd_byte(lcd.LCD_LINE_3, lcd.LCD_CMD)
    lcd.lcd_string(time.strftime("|%a, %b %d, %Y |"), 1)
    lcd.lcd_byte(lcd.LCD_LINE_4, lcd.LCD_CMD)
    lcd.lcd_string("+------------------+", 1)
    time.sleep(1)
    lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
    lcd.lcd_string("+------------------+", 1)
    lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
    lcd.lcd_string(time.strftime("|%I %M %p          |"), 1)
    lcd.lcd_byte(lcd.LCD_LINE_3, lcd.LCD_CMD)
    lcd.lcd_string(time.strftime("|%a, %b %d, %Y |"), 1)
    lcd.lcd_byte(lcd.LCD_LINE_4, lcd.LCD_CMD)
    lcd.lcd_string("+------------------+", 1)
    time.sleep(1)

GPIO.add_event_detect(11, GPIO.FALLING, callback=Menu, bouncetime=100)

start()
try:
    while True:
        clock()

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

PS:lcd.lcd_init加倍以清除屏幕,这就是为什么我这么称呼它。

1 个答案:

答案 0 :(得分:1)

您的方法clock()无限循环运行,始终会随着时间覆盖显示的文本。

运行Menu()函数时必须停止clock()方法。

我添加了按下全局变量菜单:

#!usr/bin/python3
import RPi.GPIO as GPIO
import time
import sys
sys.path.append('/home/pi/Downloads')
import lcd

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
#GPIO.setup(33, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)

menupressed = False

lcd.lcd_init()

def start():
    for x in range(0,8):
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}>----TestOS----<{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}<>---TestOS-----{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}-<>--TestOS-----{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}--<>-TestOS-----{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}---<>TestOS-----{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}----<TestOS>----{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}-----TestOS<>---{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}-----TestOS-<>--{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}-----TestOS--<>-{]", 1)
        time.sleep(0.1)
        lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
        lcd.lcd_string("[}-----TestOS---<>{]", 1)
        time.sleep(0.1)


    lcd.lcd_init()

    for x in range(0,120):
        lcd.lcd_byte(0xFF,True)

    time.sleep(5)
    lcd.lcd_init()


def Menu():
    global menupressed
    menupressed = True
    lcd.lcd_init()
    lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
    lcd.lcd_string("hi", 1)
    lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
    lcd.lcd_string("hi", 1)
    time.sleep(5)
    menupressed = False

def clock():
    if(menupressed):
         return #when menu button is pressed, return - dont show nothing on display
    lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
    lcd.lcd_string("+------------------+", 1)
    lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
    lcd.lcd_string(time.strftime("|%I:%M %p          |"), 1)
    lcd.lcd_byte(lcd.LCD_LINE_3, lcd.LCD_CMD)
    lcd.lcd_string(time.strftime("|%a, %b %d, %Y |"), 1)
    lcd.lcd_byte(lcd.LCD_LINE_4, lcd.LCD_CMD)
    lcd.lcd_string("+------------------+", 1)
    time.sleep(1)
    lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
    lcd.lcd_string("+------------------+", 1)
    lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
    lcd.lcd_string(time.strftime("|%I %M %p          |"), 1)
    lcd.lcd_byte(lcd.LCD_LINE_3, lcd.LCD_CMD)
    lcd.lcd_string(time.strftime("|%a, %b %d, %Y |"), 1)
    lcd.lcd_byte(lcd.LCD_LINE_4, lcd.LCD_CMD)
    lcd.lcd_string("+------------------+", 1)
    time.sleep(1)

GPIO.add_event_detect(11, GPIO.FALLING, callback=Menu, bouncetime=100)

start()
try:
    while True:
        clock()

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

希望这很有帮助