-
Notifications
You must be signed in to change notification settings - Fork 0
/
power.py
executable file
·51 lines (36 loc) · 1.13 KB
/
power.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
#!/bin/env python3
import RPi.GPIO as GPIO
import time
import subprocess
from datetime import datetime
DEBUG = False
# Setup the GPIO pin numbers
POWER = 5
switches = [POWER]
GPIO.setmode(GPIO.BOARD)
for switch in switches:
DEBUG and print(switch)
GPIO.setup(switch, GPIO.IN)
oldButtonState = False
def power_callback(channel):
global oldButtonState
buttonState = GPIO.input(POWER)
if buttonState != oldButtonState and buttonState:
if DEBUG:
# Switch is in off position
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print(f"{current_time}: Switched Off")
DEBUG and print("halting")
subprocess.call("halt", shell=True)
DEBUG and print("halted")
if buttonState != oldButtonState and not buttonState:
if DEBUG:
# Switch is in on position
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print(f"{current_time}: Switched On")
oldButtonState = buttonState
GPIO.add_event_detect(POWER, GPIO.RISING, callback=power_callback)
while(True):
time.sleep(0.1)