def main(): parser = argparse.ArgumentParser(description="Hue to Spacebrew bridge") parser.add_argument("-s", "--server", help="Spacebrew server", default="sandbox.spacebrew.cc") parser.add_argument("-p", "--port", help="Spacebrew port", type=int, default=9000) parser.add_argument("-b", "--bridge", help="Hue bridge") args = parser.parse_args() print("Connecting to Spacebrew server: %s port %d"%(args.server, args.port)) brew = Spacebrew("Hue Bridge", server=args.server, port=args.port) if args.bridge is None: info = urllib.urlopen('http://www.meethue.com/api/nupnp').read() info = json.loads(info) if len(info) > 0: args.bridge = info[0][u'internalipaddress'] else: print("ERROR: Could not auto-detect Hue bridge IP") print(" Please specify --bridge manually") sys.exit(1) print("Connecting to Hue bridge at: %s" % args.bridge) bridge = None while bridge == None: try: bridge = Bridge(args.bridge) except phue.PhueRegistrationException as e: print(str(e)) sleep(5) lights = bridge.get_light_objects('name') brew_lights = [] print("Lights:") for name in lights: print(" - %s"%(name)) brew_lights.append(HueBulb(brew, name, lights[name])) print("Starting Spacebrew") brew.start() sleep(5) try: while True: for light in brew_lights: light.poll() # chill out man sleep(0.33) finally: brew.stop()
pos_remote = pos_local + 2 # display the label for the remote and local dice roll states stdscr.addstr(pos_local, 0, "local dice roll: ".encode(code), curses.A_BOLD) stdscr.addstr(pos_remote, 0, "remote dice roll: ".encode(code), curses.A_BOLD) # display the starting prompt next to local and remote dice state lines stdscr.addstr(pos_local, pos_state, "waiting for first roll".encode(code)) stdscr.addstr(pos_remote, pos_state, "waiting for first roll".encode(code)) stdscr.refresh() # listen for keypresses and handle input while 1: c = stdscr.getch() if (c == 10 or c == 13): local_state = random.randint(1,6) brew.publish("roll of the dice", local_state) stdscr.addstr(pos_local, pos_state, (" " * 30).encode(code)) stdscr.addstr(pos_local, pos_state, str(local_state).encode(code)) stdscr.refresh() # closing out the app and returning terminal to old settings finally: brew.stop() curses.nocbreak() stdscr.keypad(0) curses.echo() curses.endwin()
# pin_bit = 1 << i # # First check if transitioned from not touched to touched. # if current_touched & pin_bit and not last_touched & pin_bit: # print('{0} touched!'.format(i)) # # Next check if transitioned from touched to not touched. # if not current_touched & pin_bit and last_touched & pin_bit: # print('{0} released!'.format(i)) # # Update last state and wait a short period before repeating. # last_touched = current_touched # time.sleep(0.1) try: brew.start() #print("Should be looping") print("Press Ctrl-C to quit.") while True: #print("LOOP") # GPIO.output(GREEN_LED, False) if cap.is_touched(0): print("Button 0 Pushed") brew.publish('buttonPress', True) # GPIO.output(GREEN_LED, lightOn) time.sleep(CHECK_FREQ) finally: GPIO.cleanup() brew.stop()