使用Python编码GPIO输入?

时间:2014-07-15 00:11:59

标签: python raspberry-pi gpio

这就是我正在做的事情,我的任务是制作一个显示器,它与来自Raspberry-PI的GPIO输入作出反应,我有显示器我想要它,我需要添加其他一些东西,但我有无法让代码显示显示值的变化,我需要+ 1,-1,并为公司整体和网站本身重置为零。除了显示事故发生的日期我是尝试从当前日期减去或添加天数。如果有人知道如何做到这一点,或者如果我不远,我真的很感激帮助。第一次编码并且它很粗糙。

现在发生的一切都是初始显示,但不会更新。

 import sys
    from turtle import *
    import turtle
    """turtle.setup()"""
    import time
    from time import sleep
    import math
    import RPi.GPIO as GPIO
    import datetime


    #CREATE TWO SQUARES
    def drawrectangle(x=0,y=0,width=10,height=10,color="white"):
        turtle.speed(10)
        turtle.pensize(10)
        turtle.color(color)
        turtle.begin_fill()
        turtle.penup()
        turtle.goto(x+width/2,y+height/2)
        turtle.pendown()
        turtle.right(90)
        turtle.forward(height)
        turtle.right(90)
        turtle.forward(width)
        turtle.right(90)
        turtle.forward(height)
        turtle.right(90)
        turtle.forward(width)
        turtle.end_fill()

    #drawrectangle(x=-320,y=0,width=600,height=475)
    #drawrectangle(x=320,y=0,width=600,height=475)

    def writeTexts(s,x,y,color="white"):
        turtle.speed(10)
        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()
        turtle.write(s,font=("Arial",15,"bold"))

    def displayInfo(companyIncidentDate,siteIncidentDate,currentDate):
        #calculates the current number of days for the company and site based on the respective dates
        #Redraws the filled rectangles to erase old text,then writes the new values
        companySafeDays=abs(currentDate - companyIncidentDate).days
        siteSafeDays=abs(currentDate - siteIncidentDate).days
        currentDate=today

        drawrectangle(-100,-200,90,40,"red") #company date
        turtle.color("black")
        writeTexts(companyIncidentDate ,-150,-215)

        drawrectangle(550,-200,90,40,"red") #site date
        turtle.color("black")
        writeTexts(siteIncidentDate, 500,-215)

        drawrectangle(-370,70,90,40,"blue") #company days
        turtle.color("orange")    
        writeTexts(companySafeDays, -350,50)

        drawrectangle(280,70,90,40,"blue") #site days
        turtle.color("orange")
        writeTexts(siteSafeDays, 300,50)

        drawrectangle(550,-300,90,40,"red")#curret date
        turtle.color("black")
        writeTexts(currentDate, 500,-310)
    #setup the turtle and screenMaximie the screen, hide the turtle, and make it animate as fast as possible
    turtle.speed("fastest")
    turtle.hideturtle()
    turtle.setup(1.,1.)
    turtle.title("Alstom Safty Sign")

    drawrectangle(-320,0,600,475,"green") #Company Block
    drawrectangle(320,0,600,475,"green") #Site Block
    turtle.color("white")
    writeTexts("Alstom: TLS North America",-450,150)
    writeTexts("Has Worked",-385,105)
    writeTexts("Days",-305,60)
    writeTexts("Since Our Last",-395,15)
    writeTexts("Lost Time",-370,-30)
    writeTexts("Accident",-365,-75)
    writeTexts("This Site",280,150)
    writeTexts("Has Worked",265,105)
    writeTexts("Days",345,60)
    writeTexts("Since Our Last",255,15)
    writeTexts("Lost Time",280,-30)
    writeTexts("Accident",285,-75)
    writeTexts("Last Incident",-275,-215)
    writeTexts("Last Incident",375,-215)
    turtle.color("green")
    writeTexts("Accidents Are Avoidable",-410,-280)
    writeTexts("Accidents Are Avoidable",235,265)
    turtle.color("red")
    writeTexts("Zero",-410,260)
    writeTexts("Zero",250,-280)
    turtle.color("blue")
    writeTexts("Deviation",-360,260)
    writeTexts("Deviation",300,-280)


    diference1=datetime.timedelta(days=-1)
    today=datetime.date.today()
    companyIncidentDate=today
    siteIncidentDate=today
    siteIncidentDate=today



    def writeTexts(s,x,y):
        turtle.speed(10)
        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()
        turtle.write(s,font=("Arial",15,"bold"))



    turtle.pencolor("black")
    company = today
    site = today
    startValue= 0000
    writeTexts (today ,500,-310)

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(17, GPIO.IN)
    GPIO.setup(18, GPIO.IN)
    GPIO.setup(27, GPIO.IN)
    GPIO.setup(22, GPIO.IN)
    GPIO.setup(23, GPIO.IN)
    GPIO.setup(24, GPIO.IN)

    #display the initial data
    displayInfo(companyIncidentDate,siteIncidentDate,today)

    while True:
        diference1=datetime.timedelta(days=-1)
        today=datetime.date.today()
        companyIncidentDate=today
        siteIncidentDate=today
        updateDisplay= False

        if today < datetime.date(1,1,1): #date has changed
            today=datetime.date.today #date has changed
            updateDisplay= true

        try:
            if(GPIO.input(17)==1): #decrease company last incident date
                companyIncidentDate +=diference1
                updateDisplay =True
            if(GPIO.input(18)==1): #increase company last incident date
                companyIncidentDate -=diference1
                updateDisplay =True
            if(GPIO.input(27)==1):#company reset
                companyIncidentDate=today
                updateDisplay =True
            if(GPIO.input(22)==1): #decrease site last incident date
                siteIncidentDate +=diference1
                updateDisplay =True
            if(GPIO.input(23)==1): #increase site last incident date
                siteIncidentDate -=diference1
                updateDisplay =True
            if(GPIO.input(24)==1): #site reset
                siteIncidentDate =today
                updateDisplay =True
        except KeyboardInterrupt:
                exit()
        if updateDisplay:
            #diplayInfo(companyIncidentDate,SiteIncidentDate,today)
            time.sleep(.01)

1 个答案:

答案 0 :(得分:0)

你的while循环中有两个问题:

today=datetime.date.today应该是today = datetime.date.today()你错过了parens所以today等于对方法的引用而不是实际值。

In [20]: print datetime.date.today
<built-in method today of type object at 0x7f58c56bed40>

In [21]: print datetime.date.today()
2014-07-15

updateDisplay= true应为updateDisplay = True首都T