-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_activity.py
75 lines (62 loc) · 1.86 KB
/
led_activity.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
import RPi.GPIO as GPIO
import time
import threading
import signal
import sys
import inotify.adapters
# Configurazione
LED_PIN = 17
WATCHED_FILE = "/home/pi/sismografo.db"
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
# Stato iniziale: LED acceso fisso (indica che il sistema è vivo)
GPIO.output(LED_PIN, GPIO.HIGH)
# Flag per la gestione del lampeggio
blinking = False
stop_thread = False
def blink_led():
"""Thread che gestisce il lampeggio del LED."""
global blinking, stop_thread
while not stop_thread:
if blinking:
for _ in range(5): # Lampeggia 5 volte
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(0.2)
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(0.2)
blinking = False
else:
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(0.5)
def signal_activity():
"""Attiva il lampeggio."""
global blinking
blinking = True
def file_watcher():
"""Monitora il file sismografo.db e segnala ogni modifica."""
notifier = inotify.adapters.Inotify()
notifier.add_watch(WATCHED_FILE)
for event in notifier.event_gen(yield_nones=False):
(_, type_names, path, filename) = event
if 'MODIFY' in type_names:
signal_activity()
def clean_exit(signum, frame):
"""Pulisce e termina il programma."""
global stop_thread
stop_thread = True
GPIO.output(LED_PIN, GPIO.LOW)
GPIO.cleanup()
sys.exit(0)
# Collegamento segnali di sistema (CTRL+C o arresto systemd)
signal.signal(signal.SIGINT, clean_exit)
signal.signal(signal.SIGTERM, clean_exit)
# Avvia thread per il lampeggio
led_thread = threading.Thread(target=blink_led)
led_thread.daemon = True
led_thread.start()
# Avvia il watcher sul file (bloccante)
try:
file_watcher()
except KeyboardInterrupt:
clean_exit(None, None)