lighting-controller/main.py

88 lines
3.3 KiB
Python

import asyncio
import tkinter as tk
from tkinter import ttk
from async_tkinter_loop import async_handler, async_mainloop
from networking import send_to_server
import color_utils
class App:
def __init__(self) -> None:
self.root = tk.Tk()
self.root.attributes('-fullscreen', True)
# List of servers (IP, Port)
self.servers = [("192.168.0.206", 80), ("192.168.0.208", 80)]
# Create Notebook for tabs
self.notebook = ttk.Notebook(self.root)
self.notebook.pack(expand=1, fill="both")
# Create a tab for each server
self.tabs = {}
for server in self.servers:
tab = ttk.Frame(self.notebook)
self.notebook.add(tab, text=f"{server[0]}:{server[1]}")
self.create_sliders(tab)
self.tabs[server] = tab
async_mainloop(self.root)
def create_sliders(self, tab):
"""Create sliders for each tab."""
slider_length = 800
slider_width = 50
# Red Slider
red_slider = tk.Scale(tab, from_=255, to=0, orient=tk.VERTICAL, length=slider_length, width=slider_width, label="Red")
red_slider.set(0)
red_slider.pack(side=tk.LEFT, padx=10)
red_slider.bind("<ButtonRelease-1>", lambda event, srv=tab: self.update_colour(event, srv))
# Green Slider
green_slider = tk.Scale(tab, from_=255, to=0, orient=tk.VERTICAL, length=slider_length, width=slider_width, label="Green")
green_slider.set(0)
green_slider.pack(side=tk.LEFT, padx=10)
green_slider.bind("<ButtonRelease-1>", lambda event, srv=tab: self.update_colour(event, srv))
# Blue Slider
blue_slider = tk.Scale(tab, from_=255, to=0, orient=tk.VERTICAL, length=slider_length, width=slider_width, label="Blue")
blue_slider.set(0)
blue_slider.pack(side=tk.LEFT, padx=10)
blue_slider.bind("<ButtonRelease-1>", lambda event, srv=tab: self.update_colour(event, srv))
# Brightness Slider
brightness_slider = tk.Scale(tab, from_=255, to=0, orient=tk.VERTICAL, length=slider_length, width=slider_width, label="Brightness")
brightness_slider.set(127)
brightness_slider.pack(side=tk.LEFT, padx=10)
brightness_slider.bind("<ButtonRelease-1>", lambda event, srv=tab: self.update_colour(event, srv))
tab.widgets = {
'red_slider': red_slider,
'green_slider': green_slider,
'blue_slider': blue_slider,
'brightness_slider': brightness_slider,
}
@async_handler
async def update_colour(self, event, tab):
"""Update color based on the slider values and send to the selected server."""
red_slider = tab.widgets['red_slider']
green_slider = tab.widgets['green_slider']
blue_slider = tab.widgets['blue_slider']
brightness_slider = tab.widgets['brightness_slider']
r = red_slider.get()
g = green_slider.get()
b = blue_slider.get()
brightness = brightness_slider.get()
color = color_utils.adjust_brightness((r, b, g), brightness)
print(color, brightness)
selected_server = self.notebook.tab(self.notebook.select(), "text").split(":")
await send_to_server(bytes(color), (selected_server[0], int(selected_server[1])))
if __name__ == "__main__":
app = App()