[PYTHON] I tried Philips Hue that fluctuates in seven colors at 1 / f

I'm a man who can't do electronic work.

This morning, I saw Electronic work of LED lighting that fluctuates in seven colors with 1 / f (introduction level), and thought, "If you use Philips Hue, you can do electronic work even if you are a man?" Saw.

What is the fluctuation of 1 / f?

... I don't know ... After somehow Fourier transforming, I found that the intensity seems to be inversely proportional to the frequency ... Although it is also referred to in the original article, there is a detailed explanation in the article New model railroad layout production status # 2: Star fluctuation expression. (Thank you)

I fully understood it thanks to the graph.

create

(Material list) 材料

As a way to control Philips Hue programmatically, the one that comes to mind is phue that can be used in Python or cylon.js in Node.js. This time, let's implement it quickly using phue.

The color space of Philips Hue is HSV. Color spaces other than RGB are unknown to us human beings. It's just the universe. However, python has a standard library colorsys with rgb_to_hsv to convert from RGB to HSV.

Let Python do the boring things.

In this way, here is the one that has been finished in a snap by skipping the unknown things such as fluctuations and HSV color space.

#Run with ipython
!pip install phue
from colorsys import rgb_to_hsv
from random import uniform, randint
from time import sleep
from phue import Bridge


IP_OF_MY_BRIDGE = "192.168.0.1"  #IP address of Philips Hue Bridge
GROUP = [1,2,3,4]  #ID list of the light bulb you want to operate
MAX_RGB = 255
FLUCTUATION_THRESHOLD = 0.01  #Threshold for avoiding the problem that does not change if it is too large or too small in the original article(I tried to make it smaller as I like)


def init_rgb():
    return [randint(0, MAX_RGB) for _ in range(3)]
 
def fluctuate(x):
    if x < 0.5:
        x = x + 2 * x * x
    else:
        x = x - 2 * (1.0 - x) * (1.0 - x)
    if x < FLUCTUATION_THRESHOLD or x > 1 - FLUCTUATION_THRESHOLD:
        x = uniform(0.1, 0.9)     
    return x

def convert_rgb_to_next_state(rgb_list):
    for idx, rgb in enumerate(rgb_list):
        rgb_list[idx] = round(fluctuate(rgb/MAX_RGB) * MAX_RGB)
    return rgb_list

def set_right(light_idx,r:int, g:int, b:int):
    hsv = rgb_to_hsv(r/MAX_RGB, g/MAX_RGB, b/MAX_RGB)
    light = lights[light_idx]
    #print(hsv)
    light.hue = round(hsv[0] * 65535)  # max hue is 65535
    light.saturation = round(hsv[1] * 255)  # max saturation = 255
    light.brightness = round(hsv[2] * 255)  # max brightness = 255
    light.transitiontime = 10  # 1 second
    
    
def main():
    bridge = Bridge(IP_OF_MY_BRIDGE)
    bridge.connect()
    lights = bridge.get_light_objects('id')
    right_rgb_states = [init_rgb() for _ in GROUP]
    while True:
        sleep(1)
        for idx, right_rgb_state in enumerate(right_rgb_states):
            set_right(GROUP[idx], *right_rgb_state)
            right_rgb_states[idx] = convert_rgb_to_next_state(right_rgb_state)        
main()

It was a perfect play in the morning. Even if you don't understand anything, you can manage it unexpectedly. I would like to think more about how to use Philips Hue. Also, during the implementation, I thought "RGB is 0-255 for human beings", but when I saw it after the implementation, I felt that it was usually a decimal number. Introducing electronic work ...

Result (GIF)

ezgif.com-video-to-gif-2.gif

Recommended Posts

I tried Philips Hue that fluctuates in seven colors at 1 / f
Work memo that I tried i18n with Flask app
I tried "a program that removes duplicate statements in Python"
Movement that changes direction in the coordinate system I tried Python 3
I tried running GAN in Colaboratory
I tried Line notification in Python
I tried to develop a Formatter that outputs Python logs in JSON