-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus_led_service.py
More file actions
60 lines (51 loc) · 2.03 KB
/
status_led_service.py
File metadata and controls
60 lines (51 loc) · 2.03 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
#!/usr/bin/env python3
import gpiod
from gpiod.line import Direction, Value
import time
import sys
import logging
import os
# --- Configuration ---
GPIO_CHIP_NAME = 'gpiochip0'
GPIO_CHIP_PATH = f'/dev/{GPIO_CHIP_NAME}'
STATUS_LED_PIN = 6
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def main():
"""
Initializes and controls the status LED using the gpiod v2 API.
For now, it just turns the LED on to indicate power and that the service is running.
"""
logging.info("Status LED service starting.")
request = None # Define here to ensure it's in scope for finally block
try:
if not os.path.exists(GPIO_CHIP_PATH):
logging.error(f"FATAL: GPIO chip device not found at '{GPIO_CHIP_PATH}'.")
logging.error("Please check your hardware configuration and device tree.")
sys.exit(1)
logging.info(f"Requesting line {STATUS_LED_PIN} on {GPIO_CHIP_PATH} using gpiod v2 API.")
# The gpiod v2 API requests lines directly.
# The returned 'request' object holds the lock on the lines.
# It must be kept in scope for the lines to remain configured.
request = gpiod.request_lines(
GPIO_CHIP_PATH,
consumer='status-led',
config={
STATUS_LED_PIN: gpiod.LineSettings(
direction=Direction.OUTPUT,
output_value=Value.ACTIVE
)
}
)
logging.info(f"Status LED on pin {STATUS_LED_PIN} has been turned ON.")
# Keep the script running to hold the pin state. The service will manage this process.
while True:
time.sleep(3600) # The pin state is held by the kernel, so we can sleep for long periods.
except Exception as e:
logging.error(f"An error occurred: {e}")
sys.exit(1)
finally:
if request:
request.release()
logging.info("Status LED service stopping.")
if __name__ == "__main__":
main()