-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelayControl.py
More file actions
executable file
·119 lines (103 loc) · 3.46 KB
/
Copy pathRelayControl.py
File metadata and controls
executable file
·119 lines (103 loc) · 3.46 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from pydbus.generic import signal as dbus_signal
from pydbus import SessionBus, SystemBus
from gi.repository import GLib
import RPi.GPIO as Pin
import time
import math
try:
import board
import neopixel
RelayPin = 22
LEDPIN = board.D18
bus = SystemBus()
runOnPI = True
print("Running on Pi: \n Relay Pin: {}, LED Pin: {}".format(RelayPin,LEDPIN))
except ModuleNotFoundError:
#Assuming not running on pi
RelayPin = 22
LEDPIN = 18
bus = SessionBus()
runOnPI = False
print("Not on a Pi")
busName = "com.sisalma.pydbus"
RELAYON = 0
RELAYOFF = 1
LEDGREEN = (0,255,12)
LEDBLUE = (12,0,255)
LEDRED = (255,12,0)
LEDOFF = (0,0,0)
class RelayLogic:
"""
<node>
<interface name='com.sisalma.pydbus'>
<method name='BluetoothKeyStatus'>
<arg type='b' name='a' direction='in'/>
<arg type='b' name='response' direction='out'/>
</method>
<method name='HelmetStatus'>
<arg type='b' name='a' direction='in'/>
<arg type='b' name='response' direction='out'/>
</method>
<property name="bluetoothKeyVerified" type="b" access="read">
<annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="true"/>
</property>
</interface>
</node>
"""
def __init__(self):
self.useLED = runOnPI
if self.useLED:
self.pixels = neopixel.NeoPixel(board.D18, 1)
self.RelayState = RELAYOFF
self._helmetDetected = False
self._bluetoothKeyVerified = False
Pin.setmode(Pin.BCM)
Pin.setup(RelayPin, Pin.OUT)
self.lockUnlockIO()
def BluetoothKeyStatus(self, s):
#Only trigger on State Changes
if s != self._bluetoothKeyVerified:
self._bluetoothKeyVerified = s
self.lockUnlockIO()
def HelmetStatus(self, s):
#Only trigger on State Changes
if s != self._helmetDetected:
self._helmetDetected = s
self.lockUnlockIO()
def lockUnlockIO(self):
if self._bluetoothKeyVerified and self._helmetDetected:
self.RelayState = RELAYON
self.changeLEDColor(LEDGREEN)
elif self._bluetoothKeyVerified and not self._helmetDetected:
self.changeLEDColor(LEDBLUE)
self.RelayState = RELAYOFF
elif not self._bluetoothKeyVerified :
self.RelayState = RELAYOFF
self.changeLEDColor(LEDRED)
#Send signal to stop detecting helmet on other script listening
self.PropertiesChanged(busName, {"bluetoothKeyVerified": self._bluetoothKeyVerified}, [])
self.stateAction()
def stateAction(self):
if self.RelayState == RELAYON:
Pin.output(RelayPin,Pin.HIGH)
print("Relay state changes to: ON")
elif self.RelayState == RELAYOFF:
Pin.output(RelayPin,Pin.LOW)
print("Relay state changes to: OFF")
@property
def bluetoothKeyVerified(self):
return self._bluetoothKeyVerified
def changeLEDColor(self,color):
if self.useLED:
self.pixels[0] = color
else:
print("Program not running on Pi, disabling neopixel library")
def stopObject(self):
self.changeLEDColor(LEDOFF)
PropertiesChanged = dbus_signal()
def main():
loop = GLib.MainLoop()
RelayObject = RelayLogic()
bus.publish(busName,RelayObject)
loop.run()
main()