-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.py
More file actions
301 lines (266 loc) · 8.94 KB
/
App.py
File metadata and controls
301 lines (266 loc) · 8.94 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
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
295
296
297
298
299
300
301
import os, json, threading, time
from pathlib import Path
try:
import RPi.GPIO as GPIO
USING_GPIO = True
except Exception:
USING_GPIO = False
class MockGPIO:
BOARD = BCM = OUT = IN = HIGH = 1; LOW = 0
def setmode(self, *_): pass
def setup(self, *_): pass
def output(self, pin, val): print(f"[MOCK GPIO] pin {pin} -> {'HIGH' if val else 'LOW'}")
def cleanup(self): pass
GPIO = MockGPIO()
from flask import Flask, render_template, request, redirect, url_for, jsonify, flash
app = Flask(__name__)
app.secret_key = "traffic-secret" # for flash messages
# -----------------------------
# GPIO / Lights configuration
# -----------------------------
BOARD_MODE = True
if BOARD_MODE:
GPIO.setmode(GPIO.BOARD)
PINS = {"RED": 11, "YELLOW": 13, "GREEN": 15}
else:
GPIO.setmode(GPIO.BCM)
PINS = {"RED": 17, "YELLOW": 27, "GREEN": 22}
for p in PINS.values():
GPIO.setup(p, GPIO.OUT)
GPIO.output(p, GPIO.LOW)
# -----------------------------
# Persistence for presets
# -----------------------------
DATA_DIR = Path(__file__).parent
PRESETS_FILE = DATA_DIR / "presets.json"
def load_presets():
if PRESETS_FILE.exists():
try:
with open(PRESETS_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return {}
return {}
def save_presets(presets):
with open(PRESETS_FILE, "w", encoding="utf-8") as f:
json.dump(presets, f, indent=2)
presets = load_presets()
# Provide a few defaults if file empty
if not presets:
presets = {
"Default": {"red": 5.0, "yellow": 3.0, "green": 5.0, "flash": 0.5},
"School": {"red": 30.0, "yellow": 15.0, "green": 30.0, "flash": 0.5},
"City": {"red": 25.0, "yellow": 3.0, "green": 25.0, "flash": 0.5}
}
save_presets(presets)
# -----------------------------
# State
# -----------------------------
state_lock = threading.Lock()
state = {
"running": False,
"mode": "STOP", # STOP | SEQUENCE | HOLD_* | FLASH_*
"durations": dict(presets.get("Default", {"red":5.0,"yellow":3.0,"green":5.0,"flash":0.5})),
"active_preset": "Default"
}
# threads
sequence_thread = None
flash_threads = {c: None for c in ("RED", "YELLOW", "GREEN")}
flash_flags = {c: threading.Event() for c in ("RED", "YELLOW", "GREEN")}
# -----------------------------
# Helpers
# -----------------------------
def all_off():
for pin in PINS.values():
GPIO.output(pin, GPIO.LOW)
def set_only(color):
for k, pin in PINS.items():
GPIO.output(pin, GPIO.HIGH if k == color else GPIO.LOW)
def _sequence_should_continue():
"""Return True only while we really want the sequence running."""
with state_lock:
return state["running"] and state["mode"] == "SEQUENCE"
def _sleep_interruptible(total):
"""
Sleep for up to `total` seconds, but wake early if sequence is stopped
or mode changes away from SEQUENCE. Returns False if we should abort.
"""
end = time.time() + total
while True:
if not _sequence_should_continue():
return False
remaining = end - time.time()
if remaining <= 0:
return True
time.sleep(min(0.1, remaining))
# -----------------------------
# Threads
# -----------------------------
def sequence_worker():
"""
Traffic-light sequence:
RED -> GREEN -> YELLOW -> repeat
Can be interrupted quickly by Hold/Stop/Flash actions.
"""
while _sequence_should_continue():
# copy durations under lock
with state_lock:
d = dict(state["durations"])
# RED phase
set_only("RED")
if not _sleep_interruptible(d["red"]):
break
# GREEN phase
set_only("GREEN")
if not _sleep_interruptible(d["green"]):
break
# YELLOW phase
set_only("YELLOW")
if not _sleep_interruptible(d["yellow"]):
break
# IMPORTANT: do NOT call all_off() here.
# Stop / Hold / Flash handlers manage the LEDs explicitly.
def start_sequence():
global sequence_thread
stop_all_flashes()
with state_lock:
state["mode"] = "SEQUENCE"
state["running"] = True
sequence_thread = threading.Thread(target=sequence_worker, daemon=True)
sequence_thread.start()
def stop_sequence():
with state_lock:
state["running"] = False
state["mode"] = "STOP"
all_off()
def flash_worker(color):
pin = PINS[color]
e = flash_flags[color]
while True:
with state_lock:
if e.is_set() or state["mode"] != f"FLASH_{color}":
break
interval = state["durations"]["flash"]
GPIO.output(pin, GPIO.HIGH)
time.sleep(interval)
GPIO.output(pin, GPIO.LOW)
time.sleep(interval)
GPIO.output(pin, GPIO.LOW)
def start_flash(color):
stop_sequence()
stop_all_flashes()
with state_lock:
state["mode"] = f"FLASH_{color}"
flash_flags[color].clear()
t = threading.Thread(target=flash_worker, args=(color,), daemon=True)
flash_threads[color] = t
t.start()
def stop_flash(color):
flash_flags[color].set()
t = flash_threads.get(color)
if t and t.is_alive():
t.join(timeout=0.05)
GPIO.output(PINS[color], GPIO.LOW)
def stop_all_flashes():
for c in ("RED", "YELLOW", "GREEN"):
stop_flash(c)
def hold_color(color):
# Stop any running sequences or flashes, then solidly drive one color.
stop_sequence()
stop_all_flashes()
with state_lock:
state["mode"] = f"HOLD_{color}"
set_only(color)
def _maybe_float(x):
try:
return max(0.1, float(x))
except Exception:
return None
# -----------------------------
# Routes
# -----------------------------
@app.route("/", methods=["GET"])
def home():
with state_lock:
s = dict(state)
s["durations"] = dict(state["durations"])
return render_template("index.html", state=s, pins=PINS, presets=sorted(presets.keys()))
@app.route("/control", methods=["POST"])
def control():
action = request.form.get("action")
# Optional timing updates
red = _maybe_float(request.form.get("red"))
yellow = _maybe_float(request.form.get("yellow"))
green = _maybe_float(request.form.get("green"))
flash_int = _maybe_float(request.form.get("flash"))
with state_lock:
if red is not None: state["durations"]["red"] = red
if yellow is not None: state["durations"]["yellow"] = yellow
if green is not None: state["durations"]["green"] = green
if flash_int is not None: state["durations"]["flash"] = flash_int
if action == "START_SEQUENCE":
start_sequence()
elif action == "STOP":
stop_sequence()
elif action in ("HOLD_RED","HOLD_YELLOW","HOLD_GREEN"):
hold_color(action.split("_")[1])
elif action in ("FLASH_RED","FLASH_YELLOW","FLASH_GREEN"):
start_flash(action.split("_")[1])
return redirect(url_for("home"))
@app.route("/preset/save", methods=["POST"])
def preset_save():
name = (request.form.get("preset_name") or "").strip()
if not name:
flash("Preset name is required.", "error")
return redirect(url_for("home"))
with state_lock:
presets[name] = dict(state["durations"])
state["active_preset"] = name
save_presets(presets)
flash(f"Saved preset '{name}'.", "ok")
return redirect(url_for("home"))
@app.route("/preset/apply", methods=["POST"])
def preset_apply():
name = request.form.get("preset_select")
if not name or name not in presets:
flash("Choose a valid preset to apply.", "error")
return redirect(url_for("home"))
with state_lock:
state["durations"] = dict(presets[name])
state["active_preset"] = name
flash(f"Applied preset '{name}'.", "ok")
return redirect(url_for("home"))
@app.route("/preset/delete", methods=["POST"])
def preset_delete():
name = request.form.get("preset_select")
if not name or name not in presets:
flash("Choose a valid preset to delete.", "error")
return redirect(url_for("home"))
if len(presets) <= 1:
flash("Cannot delete the last remaining preset.", "error")
return redirect(url_for("home"))
with state_lock:
was_active = (state["active_preset"] == name)
del presets[name]
save_presets(presets)
if was_active:
fallback = "Default" if "Default" in presets else sorted(presets.keys())[0]
with state_lock:
state["durations"] = dict(presets[fallback])
state["active_preset"] = fallback
flash(f"Deleted preset '{name}'.", "ok")
return redirect(url_for("home"))
@app.route("/api/state")
def api_state():
with state_lock:
s = dict(state)
s["durations"] = dict(state["durations"])
return jsonify(s)
@app.route("/shutdown", methods=["POST"])
def shutdown():
stop_sequence()
stop_all_flashes()
GPIO.cleanup()
return "OK"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=False)