-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe_shutdown.py
More file actions
27 lines (21 loc) · 919 Bytes
/
safe_shutdown.py
File metadata and controls
27 lines (21 loc) · 919 Bytes
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
import logging
from typing import Final
import RPi.GPIO as GPIO
from util import shutdown
class SafeShutdown:
def __init__(self,
power_pin: int = 26,
power_en_pin: int = 27,
):
super().__init__()
self._log = logging.getLogger(__name__)
self._log.info(f'Configuring GPIO pin "{power_pin}" and "{power_en_pin}".')
GPIO.setmode(GPIO.BCM)
GPIO.setup(power_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(power_en_pin, GPIO.OUT)
GPIO.output(power_en_pin, GPIO.HIGH)
self._log.info(f'GPIO pin "{power_pin}" and {power_en_pin} are configured now, waiting for "{power_pin}" fall.')
GPIO.add_event_detect(power_pin, GPIO.FALLING, callback=self)
def __call__(self, power_pin: int):
self._log.info(f'Power pin "{power_pin}" dropped, initiate safe shutdown now.')
shutdown()