lundi 13 juin 2016

Python GPIO add_event_detect each state individually


I currently have a few lever-type on off switch that I would like to have the status printed as soon as it switches on/off independantly off all the other switches.

So far, I have come this far:

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)

GPIO.setup(7, GPIO.IN) # switch 2
GPIO.setup(11, GPIO.IN) # switch 3

def print_func(pin):
        if GPIO.input(7) == 0:
                print "switch 2 on"
        elif GPIO.input(7) == 1:
               print "switch 2 off"
        elif GPIO.input(11) == 0:
                print "switch 3 on"
        elif GPIO.input(11) == 1:
               print "switch 3 off"


GPIO.add_event_detect(7, GPIO.BOTH, callback=print_func, bouncetime=300)
GPIO.add_event_detect(11, GPIO.BOTH, callback=print_func, bouncetime=300)

while True:
        sleep(1)

However, this doesn't get me anywhere. I can't figure out how to just mentioned the status of the lever that just move, without going through the loop mentioning the status for each one..

Any help would be really appreciated!


Aucun commentaire:

Enregistrer un commentaire