-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
192 lines (167 loc) · 7.02 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os
import pygame, sys
from pygame.locals import *
from printrun.printcore import printcore
from printrun import gcoder
import xmlrpc.client
import time
# rpc = xmlrpc.client.ServerProxy('http://localhost:7978')
p = printcore('COM3', 115200)
gcode = []
pygame.init()
pygame.font.init()
background = (30, 30, 30)
COLOR_INACTIVE = pygame.Color('lightskyblue3')
COLOR_ACTIVE = pygame.Color('dodgerblue2')
FONT = pygame.font.Font(None, 32)
DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
w, h = pygame.display.get_surface().get_size()
print("Width of screen:", w, "::", "Height of screen:", h)
mainLoop = True
current_state = [0]
printcount = 0
def do_nothing(*args):
pass
def display_loading():
text_wait.visible = True
text_wait.draw(DISPLAYSURF)
time.sleep(2)
text_wait.clear()
def is_printing():
return p.printing or p.paused
def get_state():
if not is_printing:
return 0
elif p.printing:
return 1
elif p.paused:
return 2
def connect_printer():
global p
p = printcore('COM3', 115200)
def begin_print(fname, state_add = 0):
global gcode, current_state, printcount
printcount += 1
current_state[0] = 1
if state_add:
current_state.append(state_add)
gcode = [i.strip() for i in open(fname)]
gcode = gcoder.LightGCode(gcode)
p.startprint(gcode)
print("PRINTING", fname)
def pause_print():
p.pause()
current_state[0] = 2
while p.printing:
display_loading()
def resume_print():
p.resume()
current_state[0] = 1
def disconnect_printer():
global current_state
p.disconnect()
connect_printer()
current_state = [0]
class ImageButton():
def __init__(self, x, y, image, imageselected, scale = 1, onclick = (lambda x: 0), visibleon = [0]):
self.x = x
self.y = y
self.scale = scale
self.image = image
self.imageselected = imageselected
self.img = image
self.imgrect = self.img.get_rect()
self.visible = False
self.visibleon = set(visibleon)
self.onclick = onclick
self.update_image(image)
def update_image(self, img):
self.img = img
self.img = pygame.transform.rotozoom(self.img, 0, self.scale)
self.imgrect = img.get_rect()
self.imgrect.left = self.x
self.imgrect.top = self.y
self.imgrect.width = self.imgrect.width * self.scale
self.imgrect.height = self.imgrect.height * self.scale
def handle_event(self, event):
# if event.__dict__['printing']:
# self.visible = False
# elif event.__dict__['selection']:
# self.visible = True
if self.visible and event.type == pygame.MOUSEBUTTONUP and self.imgrect.collidepoint(pygame.mouse.get_pos()):
self.onclick()
def update(self):
if not (self.visible == (not self.visibleon.isdisjoint(current_state))):
self.visible = not self.visible
DISPLAYSURF.fill(pygame.Color("black"))
if self.visible:
if self.imgrect.collidepoint(pygame.mouse.get_pos()):
self.update_image(self.imageselected)
else:
self.update_image(self.image)
def draw(self, screen):
if self.visible:
screen.blit(self.img, self.imgrect)
class TextOut:
def __init__(self, x, y, text, color = COLOR_ACTIVE, visibleon = []):
self.x = x
self.y = y
self.color = color
self.text = text
self.txt_surface = FONT.render(text, True, self.color)
self.txt_surface_clear = FONT.render(text, True, background)
self.visible = False
self.visibleon = visibleon
def handle_event(self, event):
pass
def update(self):
if not (self.visible == (current_state[0] in self.visibleon)):
self.visible = not self.visible
DISPLAYSURF.fill(pygame.Color("black"))
def clear(self):
DISPLAYSURF.blit(self.txt_surface_clear, (self.x, self.y))
def draw(self, screen):
if self.visible:
screen.blit(self.txt_surface, (self.x, self.y))
text_select = TextOut(w/2 - 70, h/5, "Select a print:", visibleon=[0])
text_refill = TextOut(w/2 - 200, h/5, "Please ask staff to refill the printer.", visibleon=[3])
text_printing = TextOut(w/2 - 50, h/5, "Printing:", visibleon=[1])
text_paused = TextOut(w/2 - 30, h/4, "Paused.", visibleon=[2])
text_wait = TextOut(w/2 - 70, h/4, "PLEASE WAIT...")
shape_triangle = ImageButton(w/2 - 300 - 150, h/2 - 150, pygame.image.load("images/" + "triangle.png"), pygame.image.load("images/" + "triangleselected.png"), 1, lambda: begin_print('printfiles/trianglehandcraft2.gcode', 90), [0])
shape_circle = ImageButton(w/2 - 0 - 150, h/2 - 150, pygame.image.load("images/" + "circle.png"), pygame.image.load("images/" + "circleselected.png"), 1, lambda: begin_print('printfiles/circlehandcraft2.gcode', 91), [0])
shape_square = ImageButton(w/2 + 300 - 150, h/2 - 150, pygame.image.load("images/" + "square.png"), pygame.image.load("images/" + "squareselected.png"), 1, lambda: begin_print('printfiles/squarehandcraft2.gcode', 92), [0])
selected_triangle = ImageButton(w/2 - 0 - 150, h/2 - 150, pygame.image.load("images/" + "triangle.png"), pygame.image.load("images/" + "triangle.png"), 1, do_nothing, [90])
selected_circle = ImageButton(w/2 - 0 - 150, h/2 - 150, pygame.image.load("images/" + "circle.png"), pygame.image.load("images/" + "circle.png"), 1, do_nothing, [91])
selected_square = ImageButton(w/2 - 0 - 150, h/2 - 150, pygame.image.load("images/" + "square.png"), pygame.image.load("images/" + "square.png"), 1, do_nothing, [92])
shape_play = ImageButton(w/2 - 150 - 75, 3*h/4 - 75, pygame.image.load("images/" + "play.png"), pygame.image.load("images/" + "playselected.png"), 0.5, lambda: resume_print(), [2])
shape_pause = ImageButton(w/2 - 0 - 75, 3*h/4 - 75, pygame.image.load("images/" + "pause.png"), pygame.image.load("images/" + "pauseselected.png"), 0.5, lambda: pause_print(), [1])
shape_stop = ImageButton(w/2 + 150 - 75, 3*h/4 - 75, pygame.image.load("images/" + "stop.png"), pygame.image.load("images/" + "stopselected.png"), 0.5, lambda: disconnect_printer(), [1, 2])
items = [text_select, text_printing, text_paused, text_refill,
shape_triangle, shape_circle, shape_square,
selected_triangle, selected_circle, selected_square,
shape_play, shape_pause, shape_stop]
while mainLoop:
# if get_state():
# current_state[0] = get_state()
# else:
# current_state[0] = 0
if printcount >= 10:
current_state[0] = 3
for item in items:
item.update()
item.draw(DISPLAYSURF)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
mainLoop = False
# print(current_state)
if event.type == pygame.QUIT:
mainLoop = False
for item in items:
item.handle_event(event)
item.update()
item.draw(DISPLAYSURF)
pygame.display.update()
p.disconnect()
pygame.quit()