-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.py
294 lines (233 loc) · 7.91 KB
/
window.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# FOR MAC
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkmacosx import Button as MacBtn
from simple_rpc import Interface
from glob import glob
import cgitb
from time import sleep
import time
from threading import Thread
import random
portSet = False
mainsOn = None
canvas = None
tree = None
startStatus = None
mainsStatus = None
portName = None
p = None
loopAnimationState = False
portsString = None
treeValues = [False,False,False,False,False,False,False]
def setPort():
global portsString, interface, portSet
if portsString.get() != "Choose Port":
interface = Interface(portsString.get())
portSet = True
def start():
global startStatus, portName, p
if portSet:
if messagebox.askokcancel("Animation", "Start Animation?"):
print("START")
startStatus.config(text="Animation On,")
p = Thread(target=animate)
p.start()
else:
messagebox.showinfo("Set Port!","Set Port!")
def loopAnimationUpdate():
global loopAnimationState, loopAnimation
loopAnimationState = loopAnimation.get()
def mains():
global mainsOn, mainsStatus
if portSet:
if mainsOn:
print("MAINS OFF")
mainsOn = False
mainsStatus.config(text="Mains Off")
mainsRelay(False)
else:
if messagebox.askokcancel("120VAC On/Off", "Turn On 120VAC?"):
print("MAINS ON")
mainsOn = True
mainsStatus.config(text="Mains On")
mainsRelay(True)
else:
messagebox.showinfo("Set Port!","Set Port!")
def callback(event):
if event.y >= 5 and event.y <= 35:
if event.x < 35:
toggleTree(0)
elif event.x >= 35 and event.x < 65:
toggleTree(1)
elif event.x >= 65 and event.x < 95:
toggleTree(2)
elif event.x >= 95 and event.x < 125:
toggleTree(3)
elif event.x >= 125 and event.x < 155:
toggleTree(4)
elif event.x >= 155 and event.x < 185:
toggleTree(5)
elif event.x >= 185:
toggleTree(6)
def animate():
global loopAnimationState, portset, startStatus
going = True
if portSet:
while going:
allOff()
for i in range(0, 7):
setTree(i, True)
sleep(1 / 7)
for i in range(0, 7):
setTree(i, False)
sleep(1 / 7)
sleep(1)
allOn()
sleep(1)
trees = [False,False,False,False,False,False,False]
for i in range(0, 7):
validTree = True
while validTree:
treeNum = random.randint(0, 6) ## 6 IS included
if trees[treeNum] == False:
validTree = False
trees[treeNum] = True
break;
setTree(treeNum, False)
sleep(1 / 7)
allOff()
sleep(0.5)
trees = [True,True,True,True,True,True,True]
for i in range(0, 7):
validTree = True
while validTree:
treeNum = random.randint(0, 6) ## 6 IS included
if trees[treeNum]:
validTree = False
trees[treeNum] = False
break;
setTree(treeNum, True)
sleep(0.75 / 7)
allOn()
sleep(1)
allOff()
going = loopAnimationState
startStatus.config(text="Animation Off,")
else:
messagebox.showinfo("Set Port!","Set Port!")
def allOff():
global portSet
if portSet:
for i in range(0, 7):
setTree(i, False)
else:
messagebox.showinfo("Set Port!","Set Port!")
def allOn():
global portSet
if portSet:
for i in range(0, 7):
setTree(i, True)
else:
messagebox.showinfo("Set Port!","Set Port!")
def statusLed(value):
global interface
setPin(13, value)
def mainsRelay(value):
global interface
setPin(12, value)
def setTree(treeNum, value):
global interface, treeValues
treeValues[treeNum] = value
treeIndicator(treeNum, value)
setPin(treeNum + 2, value)
def toggleTree(treeNum):
global treeValues
if portSet:
if treeValues[treeNum]:
setTree(treeNum, False)
else:
setTree(treeNum, True)
else:
messagebox.showinfo("Set Port!","Set Port!")
def setPin(pinNum, value):
if portSet:
interface.digital_write(pinNum, value)
else:
messagebox.showinfo("Set Port!","Set Port!")
def getPin(pinNum):
if portSet:
return interface.digital_read(pinNum)
else:
messagebox.showinfo("Set Port!","Set Port!")
def treeIndicator(treeNum, value):
global canvas, tree
if value:
canvas.itemconfig(tree[treeNum], fill='red')
else:
canvas.itemconfig(tree[treeNum], fill='grey')
def detectDoorbell():
global portSet
while not portSet:
nothing = "This does nothing, but I must obey the compiler"
while True:
if getPin(11) == False:
while not getPin(11):
nothing = "This does nothing, but I must obey the compiler"
messagebox.showinfo("Doorbell!","Someone has Pressed the Doorbell!\nPausing Animation...\nHit OK to resume")
sleep(0.1)
def main():
global portSet, going, mainsOn, canvas, tree, startStatus, mainsStatus, loopAnimation, portsString
cgitb.enable()
p2 = Thread(target=detectDoorbell)
p2.start()
portSet = False
mainsOn = False
ports = glob('/dev/tty.*')
root = Tk()
root.title("X-Mas Lights Controller")
root.configure(width=1000, height=600, bg="lightgray")
padded = Frame(root, bg="lightgray")
closeBtn = MacBtn(root, text="Close", command=root.destroy, bg="black", fg="white")
closeBtn.pack(side=TOP, anchor=NW, padx=5, pady=5)
portSelect = Frame(padded)
portsString = StringVar(portSelect)
portsString.set("Choose Port")
port = OptionMenu(portSelect, portsString, *ports)
port.pack(side=LEFT)
portBtn = MacBtn(portSelect, text="Select", command=setPort)
portBtn.pack(side=LEFT)
portSelect.pack(side=TOP)
buttons = Frame(padded, bg="lightgray")
animationFrame = Frame(buttons)
startBtn = MacBtn(animationFrame, text="Start Animation", command=start, bg="green", fg="white").pack(side=LEFT)
loopAnimation = IntVar()
loopBox = Checkbutton(animationFrame, text='Loop Animation', variable=loopAnimation, onvalue=1, offvalue=0, command=loopAnimationUpdate).pack(side=LEFT)
animationFrame.pack(side=TOP)
mainsBtn = MacBtn(buttons, text="Turn 120VAC On/Off", command=mains, bg="#2d8cff", fg="white").pack(side=TOP)
buttons.pack(side=TOP)
status = Frame(padded)
startStatus = Label(status, text="Animation Off,", bg="lightgray")
startStatus.pack(side=LEFT)
mainsStatus = Label(status, text="Mains Off", bg="lightgray")
mainsStatus.pack(side=LEFT)
status.pack(side=TOP)
canvas = Canvas(padded, width=220, height=40, bg="lightgray")
canvas.bind("<Button-1>", callback)
canvas.pack(side=TOP)
offOnBtns = Frame(padded, bg="lightgray")
allOnBtn = MacBtn(offOnBtns, text="All On", command=allOn, bg="red", fg="white").pack(side=LEFT)
allOffBtn = MacBtn(offOnBtns, text="All Off", command=allOff, bg="gray", fg="white").pack(side=LEFT)
offOnBtns.pack(side=TOP)
tree = [None, None, None, None, None, None, None]
for i in range(0, 7):
tree[i] = canvas.create_rectangle((i * 30) + 5, 35, (i * 30) + 35, 5, outline='black', fill="grey")
padded.pack(padx=50, pady=50)
root.mainloop()
if portSet:
mainsRelay(False)
allOff()
print("Window Closed")
if __name__ == "__main__":
main()