pi-thermostat/thermostat/surfaces/button_pad.py
2023-08-19 19:08:19 -04:00

167 lines
5.7 KiB
Python

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.click = False
def update(self):
self.prev_mouse_pos = self.mouse_pos
self.mouse_pos = pygame.mouse.get_pos()
class QuitButton(pygame.Surface):
def __init__(self):
super().__init__((QUIT_BUTTON_SIZE, QUIT_BUTTON_SIZE), pygame.SRCALPHA)
self.rect = self.get_rect()
pygame.draw.rect(self, (255, 0, 0), self.rect, 3)
pygame.draw.line(self, (255, 0, 0), (0, 0), (QUIT_BUTTON_SIZE, QUIT_BUTTON_SIZE), 3)
pygame.draw.line(self, (255, 0, 0), (0, QUIT_BUTTON_SIZE), (QUIT_BUTTON_SIZE, 0), 3)
class Surface(pygame.Surface):
def __init__(self, window_size):
super().__init__(window_size, pygame.SRCALPHA)
self.running = True
self.quit = False
self.next_surface = ''
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.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.mouse_frame_counter = 75
self.timeout_frame_counter = 0
self.current_temp = 0
def update(self):
self.mouse_frame_counter += 1
self.timeout_frame_counter += 1
self.fill((16, 16, 16))
self.mouse_handler.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
self.quit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
self.quit = True
if event.type == pygame.MOUSEBUTTONDOWN:
self.mouse_handler.update()
self.mouse_handler.click = True
if event.type == pygame.MOUSEBUTTONUP:
self.mouse_handler.click = False
if self.mouse_handler.click:
self.timeout_frame_counter = 0
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.mouse_frame_counter > 75:
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.mouse_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)
if self.timeout_frame_counter >= 15*30:
self.timeout_frame_counter = 15*30
self.fill((0, 0, 0))