-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappCorePC.py
128 lines (104 loc) · 3.51 KB
/
appCorePC.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
import tkinter as tk
import tkinter.font
from functools import partial
import time
import threading
import sys
#import RPi.GPIO as GPIO
## Hardware ##
# 8 10 11 12 13 15 16 18
#relayPins = [14, 15, 17, 18, 27, 22, 23, 24]
#GPIO.setmode(GPIO.BCM)
#for i in range(8):
# GPIO.setup(relayPins[i], GPIO.OUT)
## TOGGLE VARIABLES ##
control = [True, True, True, True, True, True, True, True];
names = ["Basen", "Oczko", "Wtyczka_1", "Wtyczka_2", "Wtyczka_3", "Wtyczka_4", "Wtyczka_5", "Wtyczka_6"]
## GUI Settings ##
root = tk.Tk()
root.title("Automatyczny Ogrod")
font = tkinter.font.Font(family = "Helvetica", size = 36, weight = "bold")
w=root.winfo_screenwidth()
h=root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))
## EVENT FUNCTIONS ##
def toggle(i):
if control[i]:
control[i] = False
buttons[i]["text"] = "Wlacz " + names[i]
buttons[i]["bg"] = "red"
buttons[i]["activebackground"] = "red"
#GPIO.output(relayPins[i], GPIO.HIGH)
else:
control[i] = True
buttons[i]["text"] = "Wylacz " + names[i]
buttons[i]["bg"] = "green"
buttons[i]["activebackground"] = "green"
#GPIO.output(relayPins[i], GPIO.LOW)
def close():
#GPIO.cleanup();
root.destroy()
sys.exit()
## WIDGETS ##
buttons = []
for i in range(8):
button = tk.Button(root, text = names[i], font=font, bg = "red", height = 9, width = 17)
buttons.append(button)
toggle(i)
buttons[i]["command"] = partial(toggle, i)
if (i <= 3):
button.grid(row = 2, column = i)
else:
button.grid(row = 4, column = i - 4)
exitButton = tk.Button(root, text = "Zakoncz", command = close, font=font, bg = "red", height = 1, width = 17)
exitButton.grid(row = 5, column = 3)
root.protocol("WM_DELETE_WINDOW", close) # exit cleanly
## TIMER FUNCTION ##
def timedToggles(threadName, button, startH, endH, interval):
toggled = True
while True:
print("Timer Thread")
global exitFlag
hours = int(time.localtime()[3])
minutes = int(time.localtime()[4])
seconds = int(time.localtime()[5])
if (minutes%interval == 0 and hours >= startH and hours <= endH and not toggled):
button.invoke()
toggled = True
print("A button has been pressed automaticaly!")
elif (minutes%interval == 0 and hours >= startH and hours <= endH and toggled):
toggled = True
else:
toggled = False
time.sleep(30)
## MULTITHREADING ##
class timer1Thread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("Starting " + self.name)
timedToggles(self.name, buttons[0], 8, 21, 15)
print ("Exiting " + self.name)
print("Loading classes")
class timer2Thread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("Starting " + self.name)
timedToggles(self.name, buttons[1], 8, 22, 1)
print ("Exiting " + self.name)
# Create new threads
threadTimer1 = timer1Thread(1, "Thread-1", 1)
threadTimer2 = timer2Thread(2, "Thread-2", 2)
## START THREADS ##
threadTimer1.daemon = True
threadTimer2.daemon = True
threadTimer1.start()
threadTimer2.start()
root.mainloop()