def main(): # Locate your light lifx = LifxLAN(1) # Turn on the light lifx.set_power_all_lights(True) # Change the colors of the light colors = [RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, PURPLE, PINK] sleep_time_s = 0.1 for color in colors: lifx.set_color_all_lights(color) sleep(sleep_time_s) lifx.set_power_all_lights(False)
def setlightcolor(color): print("Discovering lights...") lifx = LifxLAN(1) devices = lifx.get_lights() print(f"Found light: {devices}") original_colors = lifx.get_color_all_lights() color_up = str(color.upper()) if color_up == "OFF": lifx.set_power_all_lights("off") print("Turned off the light..") return () color_name = { "RED": RED, "BLUE": BLUE, "CYAN": CYAN, "GREEN": GREEN, "ORANGE": ORANGE, "PURPLE": PURPLE, "YELLOW": YELLOW } print("Turning on the light...") lifx.set_power_all_lights("on") if color_up in color_name: print(f"Setting color to {color_up}") lifx.set_color_all_lights(color_name[color_up]) else: print("Color not found, defaulting to pink") lifx.set_color_all_lights(PINK)
rainbow(lights[0], 5.0) empty = True count = 0 sleep(1) print "-----------------------------------------" print "-------------- LIFX Motion --------------" print "-----------------------------------------" if GPIO.input(7): ## lights[0].set_power("on", 1000) ## lights[1].set_power("on", 1000) lifx.set_color_all_lights(original_color) lifx.set_power_all_lights("on", 1000) empty = False else: ## lights[0].set_power("off", 5000) ## lights[1].set_power("off", 5000) lifx.set_power_all_lights("off", 5000) empty = True try: while True: if GPIO.input(7): if empty: print "Turning Lights On" ## lights[0].set_power("on", 1000) ## lights[1].set_power("on", 1000)
if len(sys.argv) == 2: if sys.argv[1].lower() not in colors: print(error_message) sys.exit() else: color = colors[sys.argv[1].lower()] elif len(sys.argv) == 5: color = [] for (i, value) in enumerate(sys.argv[1:]): try: value = int(value) except: print("Problem with {}.".format(value)) print(error_message) sys.exit() if i == 3 and (value < 2500 or value > 9000): print("{} out of valid range.".format(value)) print(error_message) sys.exit() elif value < 0 or value > 65535: print("{} out of valid range.".format(value)) print(error_message) sys.exit() color.append(value) else: print(error_message) sys.exit() print(color) lifxlan.set_color_all_lights(color, rapid=True)
import sys import vlc from lifxlan import LifxLAN from halflifx import gradient lan = LifxLAN() file = sys.argv[1] start = [0, 65535, 0, 5500] end = [0, 65535, 32767, 5500] steps = 300 player = vlc.MediaPlayer(file) lan.set_color_all_lights(start) player.play() while player.get_position() < 1: pass gradient(start, end, 10, 100, lan.set_color_all_lights)
class LifxProjectionStatusBarApp(rumps.App): ICON = 'eye.png' def __init__(self, **kwargs): menu = [ 'Enable/disable LIFX color projection', 'Status', ] # click provides us with ability to set command line options, # here we convert those options into UI elements in the menu. self.opts = kwargs def set_opt_from_ui(opt): def inner(_): previous_value = str(self.opts[opt.name]) response = rumps.Window("Set value for '%s'..." % (opt.name), opt.help, dimensions=(200, 20), default_text=previous_value).run() if response.text: self.opts[opt.name] = opt.type(response.text) return inner for opt in main.params: menu.append( rumps.MenuItem('Preference: set %s' % opt.name, callback=set_opt_from_ui(opt)), ) use_icon = self.ICON if os.path.exists(self.ICON) else None super(LifxProjectionStatusBarApp, self).__init__("LIFX", icon=use_icon, menu=menu) # defaults self.enable_lifx_projection = False self.lifx_found_any_lights = False # lifx self.lifx = LifxLAN(None, verbose=False) self.lifx.set_power_all_lights("on", rapid=False) def _project_screenshot_to_lifx(self): color = average_color_from_screenshot() color_scaled = ( color[0], # TODO: We might want to use gamma/power instead of multiplication min(1.0, color[1] * self.opts['saturation']), min(1.0, color[2] * self.opts['brightness']), ) print('Hue: %.3f Sat: %.3f Bri: %.3f' % color_scaled) color_lifx = [ int(color_scaled[0] * 65535), int(color_scaled[1] * 65535), int(color_scaled[2] * 65535), self.opts['temperature'] ] self.lifx.set_color_all_lights(color_lifx, duration=TRANSITION_DURATION * 0.9, rapid=True) @rumps.clicked('Enable/disable LIFX color projection') def menu_enable_lifx_projection(self, sender): sender.state = not sender.state self.enable_lifx_projection = sender.state @rumps.timer(TRANSITION_DURATION) def projection_timer(self, timer): if self.enable_lifx_projection and self.lifx_found_any_lights: self._project_screenshot_to_lifx() @rumps.timer(10) def reconfiguration_timer(self, timer): print('Reconfiguration') self.lifx_found_any_lights = bool(self.lifx.get_lights()) @rumps.clicked('Status') def menu_status(self, _): lights = [] try: for light in self.lifx.get_power_all_lights(): lights.append(str(light[0])) except Exception as e: lights.append('Caught an exception: %s' % str(e)) status = ('Enable projection: %d\n' 'Found any lights: %d\n' 'Scale brightness: %.3f\n' 'Scale saturation: %.3f\n' 'Use temperature when changing colors: %d K\n' '\nList of lights (%d): \n\n%s' % (self.enable_lifx_projection, self.lifx_found_any_lights, self.opts['brightness'], self.opts['saturation'], self.opts['temperature'], len(lights), '\n'.join(lights) if lights else 'No lights found')) window = rumps.Window('', 'Status', default_text=status, dimensions=(700, 600)) window.run()
def broadcast_blue(): lifxlan = LifxLAN() color = colors["blue"] print(color) lifxlan.set_color_all_lights(color, rapid=True) return "room is now blue"
if sys.argv[1].lower() not in colors: print(error_message) sys.exit() else: color = colors[sys.argv[1].lower()] elif len(sys.argv) == 5: color = [] for (i, value) in enumerate(sys.argv[1:]): try: value = int(value) except: print("Problem with {}.".format(value)) print(error_message) sys.exit() if i == 3 and (value < 2500 or value > 9000): print("{} out of valid range.".format(value)) print(error_message) sys.exit() elif value < 0 or value > 65535: print("{} out of valid range.".format(value)) print(error_message) sys.exit() color.append(value) else: print(error_message) sys.exit() print(color) lifxlan.set_color_all_lights(color, rapid=True)
lp.LedCtrlXY(0, 1, r, g, b) else: lp.LedCtrlXY(0, 1, 0, 0, 0) break x, y, value = buttons_hit if value == 0: if (x, y) == last_hold_down_button_pressed: last_hold_down_button_pressed = None elif value: if (x, y) == (4, 0): print("toggle lights") toggle_lights() continue elif (x, y) == (5, 0): print("set lights white") lifx.set_color_all_lights([0, 0, map_to_base(brightness, 65536), white_temp], duration=0, rapid=True) lifx_is_rgb = False elif (x, y) in {(8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6)}: last_hold_down_button_pressed = (x, y) elif (x, y) == (0, 0): if are_launchpad_lights_on: print("launchpad lights off") set_launchpad_wave(mode="off") are_launchpad_lights_on = False else: print("launchpad lights on") set_launchpad_wave(mode="rgb") are_launchpad_lights_on = True if not (x, y) in hsvs: continue h, s, v = hsvs[(x, y)]
class LightsController: WARM_WHITE = [58275, 0, 65535, 3000] MAX_VALUE = 65535 SCALE = [MAX_VALUE / 360, MAX_VALUE / 100, MAX_VALUE / 100, 1] ROTATE_CHANGE_COLOR_DELTA = 1 * config.ChangeColorSpeed['value'] DIM_DELTA = 5.0 def __init__(self, number_of_devices=1): self.device_controller = LifxLAN(number_of_devices) self.colour = None self.power = None self.reset_color() self.turn_on() if config.PrintLog: print "Light switched on with warm white" def reset_color(self): self.colour = [49, 0, 50.0, 3000] self.set_colour() def put_colour_in_loop(self): if self.colour[1] == 0: self.colour[1] = 80 def take_colour_out_of_loop(self): self.colour[1] = 0 def roll_forward(self): if not self.power: return self.put_colour_in_loop() self.colour[0] = (self.colour[0] + self.ROTATE_CHANGE_COLOR_DELTA) % 360 self.set_colour() def roll_backward(self): if not self.power: return self.put_colour_in_loop() self.colour[0] = (self.colour[0] - self.ROTATE_CHANGE_COLOR_DELTA) % 360 self.set_colour() def location_on_circle(self, x, y): degrees = math.degrees(math.atan(y/x)) if x > 0 and y < 0: degrees += 180 elif x > 0 and y > 0: degrees += 180 elif x < 0 and y > 0: degrees += 360 length = min(100, math.sqrt(x*x + y*y)) self.colour[0] = degrees self.colour[1] = length self.set_colour() def increase_brightness(self): self.colour[2] = min(100.0, self.colour[2] + self.DIM_DELTA) self.set_colour() def decrease_brightness(self): self.colour[2] = max(0.2, self.colour[2] - self.DIM_DELTA) self.set_colour() def switch_power(self): self.power = not self.power if self.power: self.reset_color() self.set_power() def turn_off(self): if not self.power: return self.power = False self.set_power() def turn_on(self): if self.power: return self.power = True self.reset_color() self.set_power() def set_colour(self): if config.PrintLog: print "Setting colours: ", [self.SCALE[i] * self.colour[i] for i in range(4)] self.device_controller.set_color_all_lights([self.SCALE[i] * self.colour[i] for i in range(4)]) def set_power(self): self.device_controller.set_power_all_lights(self.power)
UDP_PORT_NO = 420 data = {'ports': [6, 9], 'name': 'samsung', 'cloud_ip': '69.4.20.69'} data_json = json.dumps(data) clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) clientSock.sendto(bytes(data_json, 'utf-8'), (UDP_IP_ADDRESS, UDP_PORT_NO)) if __name__ == "__main__": ##SNIFF BULBS FIRST## lifx = LifxLAN() devices = lifx.get_lights() bulb = devices[0] toggle_device_power(bulb, "on") lifx.set_color_all_lights(BLUE, rapid=True) #enroll() #send_dummy() while True: websocket.enableTrace(True) ws = websocket.WebSocketApp("ws://13.55.147.2:42915/", on_message=on_message, on_error=on_error, on_close=on_close) ws.on_open = on_open ws.run_forever(ping_interval=30, ping_timeout=10)