-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.py
99 lines (79 loc) · 2.2 KB
/
gpio.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Piment GPIO - API for GPIO
You may use any Piment project under the terms
of the GNU General Public License (GPL) Version 3.
(c) 2016 Emilio Mariscal (emi420 [at] gmail.com)
'''
import RPi.GPIO as GPIO
import threading
from btpair import BTPair
from time import sleep
PIN_RELAY = {
"2.1": 37,
"2.2": 35,
"3.1": 31,
"3.2": 33,
"4.1": 40,
"4.2": 38,
"5.1": 32,
"5.2": 36,
"6.1": 26,
"6.2": 24,
"7.1": 11,
"7.2": 13,
"8.1": 10,
"8.2": 8,
"st": 22,
}
STATE_ON_RELAYS = []
class Relay(object):
def __init__(self):
GPIO.setmode(GPIO.BOARD)
for pin in PIN_RELAY:
GPIO.setup(PIN_RELAY[pin], GPIO.OUT)
if pin not in STATE_ON_RELAYS:
GPIO.output(PIN_RELAY[pin],False)
else:
GPIO.output(PIN_RELAY[pin],True)
GPIO.setup(29, GPIO.IN, pull_up_down=GPIO.PUD_UP)
self.btpair = BTPair()
self.listen()
def listen(self):
def watch_button():
while True:
input_state = GPIO.input(29)
if input_state == False:
res = self.btpair.wait()
print(res)
sleep(0.10)
thread = threading.Thread(target=watch_button, args=())
thread.daemon = True
thread.start()
def stop(self):
GPIO.cleanup()
def on(self, pin):
if pin:
GPIO.output(PIN_RELAY[pin],True)
return pin + ":true"
return "No pin provided."
def off(self, pin):
if pin:
GPIO.output(PIN_RELAY[pin],False)
return pin + ":false"
return "No pin provided."
def onoff(self, pin):
if pin:
GPIO.output(PIN_RELAY[pin],True)
sleep(.2)
GPIO.output(PIN_RELAY[pin],False)
return pin + ":false"
return "No pin provided."
def offon(self, pin):
if pin:
GPIO.output(PIN_RELAY[pin],False)
sleep(.5)
GPIO.output(PIN_RELAY[pin],True)
return pin + ":true"
return "No pin provided."