-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.py
More file actions
170 lines (131 loc) · 3.63 KB
/
Copy pathtimer.py
File metadata and controls
170 lines (131 loc) · 3.63 KB
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
import csv
from datetime import datetime, timedelta
import time
import atexit
import threading
import os
UPDATE_TIME = 60*10
# UPDATE_TIME = 20
update_list = []
updating_list = []
run_update_thread = True
try:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
pass
except RuntimeError:
print("Run with sudo!")
print('Initialising timetables')
times_list = []
with open('times.csv', newline='') as times_csv:
raw_list = csv.reader(times_csv)
for row in raw_list:
if row:
try:
new_row = {
'timer_no': int(row[0]),
'start_time': (datetime.strptime(row[1], "%H%M") - timedelta(hours=12)) .time(),
'end_time': (datetime.strptime(row[2], "%H%M") - timedelta(hours=12)).time()
}
times_list.append(new_row)
except ValueError as e:
print(e)
print('Initialising pin layout')
pins = {}
with open('pins.csv', newline='') as times_csv:
raw_list = csv.reader(times_csv)
for row in raw_list:
if row:
timer = int(row[0])
input_pin = int(row[1])
output_pin = int(row[2])
new_obj = {
timer: {
'input_pin': input_pin,
'output_pin': output_pin
}
}
pins.update(new_obj)
list_of_timers = []
for each_timer_row in times_list:
list_of_timers.append(each_timer_row['timer_no'])
list_of_timers = list(set(list_of_timers))
def update_thread():
print('Start Thread')
while run_update_thread:
while update_list:
timer_to_power = update_list.pop()
updating_list.append(timer_to_power)
print('Updating Timer ', timer_to_power)
pin_to_power = pins[timer_to_power]['output_pin']
GPIO.output(pin_to_power, True)
time.sleep(UPDATE_TIME)
GPIO.output(pin_to_power, False)
time.sleep(5)
GPIO.output(pin_to_power, True)
time.sleep(UPDATE_TIME)
GPIO.output(pin_to_power, False)
time.sleep(5)
GPIO.output(pin_to_power, True)
time.sleep(UPDATE_TIME)
GPIO.output(pin_to_power, False)
print('Powering Off Timer ', timer_to_power)
updating_list.pop()
print('Stop Thread')
def update_timers():
timer_state = {}
print('UpdateList ', updating_list, update_list)
for x in list_of_timers:
timer_state.update({x: False})
for row in times_list:
time_now = (datetime.now() - timedelta(hours=12)).time()
timer = row['timer_no']
start_time = row['start_time']
end_time = row['end_time']
if start_time <= time_now < end_time:
timer_state[timer] = True
for y in timer_state:
set_pin(y, timer_state[y])
print()
print()
def set_pin(this_timer, state):
this_pin = pins[this_timer]['output_pin']
if this_timer not in updating_list:
if state:
# print(this_timer, ' - ', this_pin, ' ON')
GPIO.output(this_pin, True)
else:
# print(this_timer, ' - ', this_pin, ' OFF')
GPIO.output(this_pin, False)
def update_filimin(channel):
for x in pins:
if channel == pins[x]['input_pin']:
timer_to_power = x
if timer not in update_list:
update_list.append(timer_to_power)
else:
print('Already updating')
@atexit.register
def goodbye():
global run_update_thread
run_update_thread = False
print("Resetting GPIO and quitting")
GPIO.cleanup()
os._exit(0)
print('Setting Output Pins')
for each_timer in pins:
GPIO.setup(pins[each_timer]['output_pin'], GPIO.OUT, initial=GPIO.LOW)
print('Setting Input Pins\n\n')
for each_timer in pins:
GPIO.setup(pins[each_timer]['input_pin'], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pins[each_timer]['input_pin'], GPIO.FALLING, callback=update_filimin, bouncetime=1000)
# Starting update thread
this_thread = threading.Thread(target=update_thread, daemon=True)
this_thread.start()
print('Starting Loop')
while True:
try:
update_timers()
time.sleep(60)
except KeyboardInterrupt:
goodbye()