got the thing working
This commit is contained in:
parent
e936b835e1
commit
23711b1b72
@ -1,23 +1,66 @@
|
||||
import math
|
||||
import socket
|
||||
import sqlite3
|
||||
|
||||
import pygame
|
||||
import os
|
||||
|
||||
DIAL_SCALING = 0.9
|
||||
QUIT_BUTTON_SIZE = 50
|
||||
BUTTON_SIZE = (150, 100)
|
||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
SERVER_IP = '172.20.100.100'
|
||||
TCP_PORT = 25813
|
||||
BUFFER_SIZE = 1024
|
||||
|
||||
set_temperature = 74
|
||||
|
||||
|
||||
def send_data(message: str):
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
s.connect((SERVER_IP, TCP_PORT))
|
||||
s.settimeout(10)
|
||||
s.send(message.encode('utf-8'))
|
||||
response = s.recv(BUFFER_SIZE)
|
||||
s.close()
|
||||
return response
|
||||
|
||||
|
||||
class Button(pygame.Surface):
|
||||
def __init__(self, button_text: str, button_position: tuple, temp_change: float):
|
||||
super().__init__(BUTTON_SIZE, pygame.SRCALPHA)
|
||||
self.rect = self.get_rect()
|
||||
|
||||
self.temp_change = temp_change
|
||||
self.text = button_text
|
||||
self.font = pygame.font.Font(SCRIPT_DIR + '/resources/tuffy.ttf', 32)
|
||||
|
||||
self.rect.centerx = button_position[0]
|
||||
self.rect.centery = button_position[1]
|
||||
self.active = False
|
||||
self.pressed = False
|
||||
|
||||
def update(self):
|
||||
self.fill((128, 128, 128))
|
||||
if self.pressed:
|
||||
self.fill((200, 200, 200))
|
||||
|
||||
text = self.font.render(self.text, True, (255, 255, 255))
|
||||
text_rect = text.get_rect()
|
||||
text_rect.center = (self.rect.width / 2, self.rect.height / 2)
|
||||
self.blit(text, text_rect)
|
||||
self.active = False
|
||||
|
||||
|
||||
class MouseHandler:
|
||||
def __init__(self):
|
||||
self.prev_mouse_pos = pygame.mouse.get_pos()
|
||||
self.mouse_pos = pygame.mouse.get_pos()
|
||||
self.active = False
|
||||
self.click = False
|
||||
|
||||
def update(self):
|
||||
self.prev_mouse_pos = self.mouse_pos
|
||||
self.mouse_pos = pygame.mouse.get_pos()
|
||||
self.click = False
|
||||
|
||||
|
||||
class QuitButton(pygame.Surface):
|
||||
@ -35,16 +78,27 @@ class Surface(pygame.Surface):
|
||||
self.running = True
|
||||
self.quit = False
|
||||
self.next_surface = ''
|
||||
self.mouse_frames = 0
|
||||
|
||||
dial_size = int(min(window_size[0], window_size[1]) * 0.9)
|
||||
self.quit_button = QuitButton()
|
||||
|
||||
self.buttons = [
|
||||
Button("+1", (360 + 250, 360 - 75), 1),
|
||||
Button("-1", (360 + 250, 360 + 75), -1),
|
||||
Button("+10", (360 - 250, 360 - 75), 10),
|
||||
Button("-10", (360 - 250, 360 + 75), -10)
|
||||
]
|
||||
|
||||
self.mouse_handler = MouseHandler()
|
||||
self.font = pygame.font.Font(SCRIPT_DIR + '/resources/tuffy.ttf', 128)
|
||||
self.small_font = pygame.font.Font(SCRIPT_DIR + '/resources/tuffy.ttf', 16)
|
||||
self.big_font = pygame.font.Font(SCRIPT_DIR + '/resources/tuffy.ttf', 96)
|
||||
|
||||
self.set_temp = 74
|
||||
self.frame_counter = 300
|
||||
self.current_temp = 0
|
||||
|
||||
def update(self):
|
||||
self.fill((32, 32, 32))
|
||||
self.frame_counter += 1
|
||||
self.fill((16, 16, 16))
|
||||
self.mouse_handler.update()
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
@ -54,19 +108,54 @@ class Surface(pygame.Surface):
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
self.running = False
|
||||
self.quit = True
|
||||
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
self.mouse_handler.active = True
|
||||
self.mouse_handler.update()
|
||||
self.mouse_handler.prev_mouse_pos = self.mouse_handler.mouse_pos
|
||||
self.mouse_handler.click = True
|
||||
|
||||
if event.type == pygame.MOUSEBUTTONUP:
|
||||
self.mouse_handler.active = False
|
||||
self.mouse_handler.click = False
|
||||
|
||||
if self.mouse_handler.click:
|
||||
|
||||
if self.mouse_handler.mouse_pos[0] <= QUIT_BUTTON_SIZE and \
|
||||
self.mouse_handler.mouse_pos[1] <= QUIT_BUTTON_SIZE:
|
||||
self.running = False
|
||||
self.quit = True
|
||||
|
||||
for button in self.buttons:
|
||||
if button.rect.x <= self.mouse_handler.mouse_pos[0] <= button.rect.x + button.rect.width and \
|
||||
button.rect.y <= self.mouse_handler.mouse_pos[1] <= button.rect.y + button.rect.height:
|
||||
if not button.pressed and not button.active:
|
||||
button.pressed = True
|
||||
button.active = True
|
||||
elif button.pressed and button.active:
|
||||
button.active = False
|
||||
|
||||
else:
|
||||
for button in self.buttons:
|
||||
button.pressed = False
|
||||
|
||||
self.blit(self.quit_button, self.quit_button.rect)
|
||||
for button in self.buttons:
|
||||
if button.active:
|
||||
self.set_temp += button.temp_change
|
||||
print(send_data(f'SET;{self.set_temp}'))
|
||||
button.update()
|
||||
self.blit(button, button.rect)
|
||||
if self.frame_counter > 300:
|
||||
request_data = send_data(f'REQ')
|
||||
split_data = request_data.decode('utf-8').split(';')
|
||||
if split_data[0] == 'SDATA':
|
||||
self.current_temp = int(round(float(split_data[1])))
|
||||
self.set_temp = float(split_data[2])
|
||||
self.frame_counter = 0
|
||||
|
||||
text = self.small_font.render(f'Set to {self.set_temp}°F', True, (255, 255, 255))
|
||||
text_rect = text.get_rect()
|
||||
text_rect.center = (360, 360 + 60)
|
||||
self.blit(text, text_rect)
|
||||
text = self.big_font.render(f'{self.current_temp}°F', True, (255, 255, 255))
|
||||
text_rect = text.get_rect()
|
||||
text_rect.center = (360, 360)
|
||||
self.blit(text, text_rect)
|
||||
|
Loading…
Reference in New Issue
Block a user