def __init__(self, color, gpio_pin): self.color = color self.pin = gpio_pin self.status = False # Set gpio pin to input and activate pull up GPIO.setup(gpio_pin, GPIO.OUT, pull_up_down=GPIO.PUD_UP)
def main(): """Main function""" # Set GPIO numbering mode GPIO.setmode(GPIO.BCM) btnList = [ColorButton("red", 4), ColorButton("blue", 17), ColorButton("green", 27), ColorButton("yellow", 22), ColorButton("white", 10)] ledDict = {"red" : ButtonLed("red", 18), "blue" : ButtonLed("blue", 23), "green" : ButtonLed("green", 24), "yellow" : ButtonLed("yellow", 25), "white" : ButtonLed("white", 8)} lamp = HueLamp("http://192.168.1.103/api/" + HUE_USER + "/lights/1") while(True): # Main loop time.sleep(0.1) pressed = False for btn in btnList: pressed = btn.poll() or pressed # Start over if no button is pressed if pressed == False: continue # Set button led value colorList = [] for btn in btnList: color = btn.color ledDict[btn.color].setStatus(btn.getStatus) # Append this color to list of colors if btn.getStatus: colorList.append(color) lamp.setState(True, colorList, 100)
def poll(self): if DBG_MOCK == True: if self.color == "blue" or self.color == "green": self.status = True return True if GPIO.input(self.pin) == True: self.status = not self.status return True return False
def setStatus(self, status): self.status = status GPIO.output(self.pin, not self.status)