add ambient light feature and multi server support

This commit is contained in:
Lucca Ketterer 2020-07-27 11:29:54 +02:00
parent db4040f8d9
commit 3d21483e3e
6 changed files with 87 additions and 22 deletions

View File

@ -1,2 +1,2 @@
install : install :
cp client.py /bin/ledcontroll cp client.py /bin/lc

View File

@ -82,7 +82,7 @@ data_format = ascii
; bit_format = 8bit ; bit_format = 8bit
# Ascii max value. In 'ascii' mode range will run from 0 to value specified here # Ascii max value. In 'ascii' mode range will run from 0 to value specified here
ascii_max_range = 255 ascii_max_range = 1000
# Ascii delimiters. In ascii format each bar and frame is separated by a delimiters. # Ascii delimiters. In ascii format each bar and frame is separated by a delimiters.
# Use decimal value in ascii table (i.e. 59 = ';' and 10 = '\n' (line feed)). # Use decimal value in ascii table (i.e. 59 = ';' and 10 = '\n' (line feed)).

View File

@ -1,34 +1,94 @@
#!/usr/bin/env python3 #!/usr/bin/python3
import socket import socket
import getopt import getopt
import sys import sys
import subprocess import subprocess
import pyscreenshot
from PIL import Image, ImageDraw, ImageFont
import cv2
import numpy as np
from skimage import io
from threading import Thread
import time
client_socket = socket.socket() s1 = socket.socket()
server_IP = "192.168.188.64" s2 = socket.socket()
server = ["192.168.188.64", "192.168.188.76"]
def connect(host, port = 5000):
client_socket.connect((host, port))
def connect(port = 5000):
s1.connect((server[0], port))
s2.connect((server[1], port))
def disconnect(): def disconnect():
client_socket.close() # close the connection s1.close() # close the connection
s2.close()
def send(data): def send(data):
client_socket.send(data.encode()) # send message s1.send(data.encode()) # send message
s2.send(data.encode()) # send message
def helpmenu(): def helpmenu():
print("light controll\n")
print("Options:") print("Options:")
print("-h show help") print("-h show help")
print("-s <color> set static color in hex") print("-s <hex-color> set static color")
print("-v visualizer()") print("-v visualizer")
def visualizer(): def visualizer():
cava = subprocess.Popen(["cava", "-p", "./config"], stdout=subprocess.PIPE) cava = subprocess.Popen(["cava", "-p", "./cava.conf"], stdout=subprocess.PIPE)
sed = subprocess.Popen(["sed", "-u", "s/;.*;$//"], stdin=cava.stdout, stdout=subprocess.PIPE) sed = subprocess.Popen(["sed", "-u", "s/;.*;$//"], stdin=cava.stdout, stdout=subprocess.PIPE)
subprocess.Popen([sys.argv[0]], stdin=sed.stdout, shell=True) subprocess.Popen([sys.argv[0]], stdin=sed.stdout, shell=False)
cava.stdout.close() cava.stdout.close()
sed.stdout.close() sed.stdout.close()
_r,_g,_b = 0,0,0
def ambient_light_thread():
r,g,b = 0,0,0
brighness = 1
while True:
if r > _r:
r-=1
elif r < _r:
r+=1
if g > _g:
g-=1
elif g < _g:
g+=1
if b > _b:
b-=1
elif b < _b:
b+=1
print(r,g,b)
send(rgb_to_hex(int(r*brighness),int(g*brighness),int(b*brighness)))
time.sleep(0.01)
def ambient_light():
t = Thread(target=ambient_light_thread)
t.start()
global _r,_g,_b
while True:
# screenshot
img = pyscreenshot.grab(backend="mss", childprocess=False, bbox=(1920,0,4480,1440))
# find dominant color
img = np.array(img)
pixels = np.float32(img.reshape(-1, 3))
n_colors = 1
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
flags = cv2.KMEANS_RANDOM_CENTERS
_, labels, palette = cv2.kmeans(pixels, n_colors, None, criteria, 10, flags)
_, counts = np.unique(labels, return_counts=True)
dominant = palette[np.argmax(counts)]
_r,_g,_b = int(dominant[0]), int(dominant[1]), int(dominant[2])
def rgb_to_hex(r,g,b): def rgb_to_hex(r,g,b):
return "%02x%02x%02x" % (r,g,b) return "%02x%02x%02x" % (r,g,b)
@ -40,11 +100,10 @@ def main(argv):
volume = int(volume) volume = int(volume)
hex_color = rgb_to_hex(volume,0,0) hex_color = rgb_to_hex(volume,0,0)
send(hex_color) send(hex_color)
print(hex_color)
sys.exit() sys.exit()
try: try:
opts, args = getopt.getopt(argv, "s:vh") opts, args = getopt.getopt(argv, "s:v:ah")
except getopt.GetoptError: except getopt.GetoptError:
print(sys.argv[0], "invalid option") print(sys.argv[0], "invalid option")
print("Try", sys.argv[0], "-h for help") print("Try", sys.argv[0], "-h for help")
@ -55,8 +114,14 @@ def main(argv):
helpmenu() helpmenu()
sys.exit() sys.exit()
elif opt == "-s": elif opt == "-s":
connect(server_IP) connect()
send(arg) send(arg)
disconnect()
sys.exit()
elif opt == "-a":
connect()
ambient_light()
disconnect()
sys.exit() sys.exit()
elif opt == "-v": elif opt == "-v":
visualizer() visualizer()

View File

@ -1,2 +0,0 @@
scp server/* pi@192.168.188.61:~/led_controll/
scp server/* pi@192.168.188.64:~/led_controll/

2
server/copy_on_pi.sh Executable file
View File

@ -0,0 +1,2 @@
scp *.py pi@192.168.188.76:~/led_controll/
scp *.py pi@192.168.188.64:~/led_controll/

View File

@ -29,5 +29,5 @@ def start():
ws2801.set_color(r,g,b) ws2801.set_color(r,g,b)
else: else:
print("incorrect format. use hex color code.") print("incorrect format. use hex color code.")
conn.close() # close the connection conn.close() # close the connection