pi-thermostat/thermostat/surfaces/button_pad.py

162 lines
5.4 KiB
Python
Raw Normal View History

2023-08-19 18:42:43 -04:00
import socket
import sqlite3
2023-08-18 20:13:55 -04:00
import pygame
import os
DIAL_SCALING = 0.9
QUIT_BUTTON_SIZE = 50
2023-08-19 18:42:43 -04:00
BUTTON_SIZE = (150, 100)
2023-08-18 20:13:55 -04:00
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
2023-08-19 18:42:43 -04:00
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
2023-08-18 20:13:55 -04:00
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()
2023-08-19 18:42:43 -04:00
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)
]
2023-08-18 20:13:55 -04:00
self.mouse_handler = MouseHandler()
2023-08-19 18:42:43 -04:00
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
2023-08-18 20:13:55 -04:00
def update(self):
2023-08-19 18:42:43 -04:00
self.frame_counter += 1
self.fill((16, 16, 16))
2023-08-18 20:13:55 -04:00
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
2023-08-19 18:42:43 -04:00
2023-08-18 20:13:55 -04:00
if event.type == pygame.MOUSEBUTTONDOWN:
self.mouse_handler.update()
self.mouse_handler.click = True
if event.type == pygame.MOUSEBUTTONUP:
2023-08-19 18:42:43 -04:00
self.mouse_handler.click = False
2023-08-18 20:13:55 -04:00
if self.mouse_handler.click:
2023-08-19 18:42:43 -04:00
2023-08-18 20:13:55 -04:00
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
2023-08-19 18:42:43 -04:00
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
2023-08-18 20:13:55 -04:00
self.blit(self.quit_button, self.quit_button.rect)
2023-08-19 18:42:43 -04:00
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)