-
Notifications
You must be signed in to change notification settings - Fork 0
/
RELAYplate.py
217 lines (189 loc) · 6.09 KB
/
RELAYplate.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import spidev
import time
import string
import site
import sys
from numbers import Number
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
#Initialize
if (sys.version_info < (2,7,0)):
sys.stderr.write("You need at least python 2.7.0 to use this library")
exit(1)
GPIO.setmode(GPIO.BCM)
RELAYbaseADDR=24
ppFRAME = 25
ppINT = 22
GPIO.setup(ppFRAME,GPIO.OUT)
GPIO.output(ppFRAME,False) #Initialize FRAME signal
time.sleep(.001) #let Pi-Plate reset SPI engine if necessary
GPIO.setup(ppINT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
spi = spidev.SpiDev()
spi.open(0,1)
localPath=site.getsitepackages()[0]
helpPath=localPath+'/piplates/RELAYhelp.txt'
#helpPath='RELAYhelp.txt' #for development only
RPversion=1.1
# Version 1.0 - initial release
# Version 1.1 - adjusted timing on command functions to compensate for RPi SPI changes
RMAX = 2000
MAXADDR=8
relaysPresent = list(range(8))
#==============================================================================#
# HELP Functions #
#==============================================================================#
def Help():
help()
def HELP():
help()
def help():
valid=True
try:
f=open(helpPath,'r')
while(valid):
Count=0
while (Count<20):
s=f.readline()
if (len(s)!=0):
print (s[:len(s)-1])
Count = Count + 1
if (Count==20):
Input=raw_input('press \"Enter\" for more...')
else:
Count=100
valid=False
f.close()
except IOError:
print ("Can't find help file.")
def getPMrev():
return RPversion
#==============================================================================#
# RELAY Functions #
#==============================================================================#
def relayON(addr,relay):
VerifyADDR(addr)
VerifyRELAY(relay)
ppCMDr(addr,0x10,relay,0,0)
def relayOFF(addr,relay):
VerifyADDR(addr)
VerifyRELAY(relay)
ppCMDr(addr,0x11,relay,0,0)
def relayTOGGLE(addr,relay):
VerifyADDR(addr)
VerifyRELAY(relay)
ppCMDr(addr,0x12,relay,0,0)
def relayALL(addr,relays):
VerifyADDR(addr)
assert ((relays>=0) and (relays<=127)),"Argument out of range. Must be between 0 and 127"
ppCMDr(addr,0x13,relays,0,0)
def relaySTATE(addr):
VerifyADDR(addr)
resp=ppCMDr(addr,0x14,0,0,1)
return resp[0]
#==============================================================================#
# LED Functions #
#==============================================================================#
def setLED(addr):
VerifyADDR(addr)
resp=ppCMDr(addr,0x60,0,0,0)
def clrLED(addr):
VerifyADDR(addr)
resp=ppCMDr(addr,0x61,0,0,0)
def toggleLED(addr):
VerifyADDR(addr)
resp=ppCMDr(addr,0x62,0,0,0)
#==============================================================================#
# SYSTEM Functions #
#==============================================================================#
def getID(addr):
global RELAYbaseADDR
VerifyADDR(addr)
addr=addr+RELAYbaseADDR
id=""
arg = list(range(4))
resp = []
arg[0]=addr;
arg[1]=0x1;
arg[2]=0;
arg[3]=0;
ppFRAME = 25
GPIO.output(ppFRAME,True)
null = spi.writebytes(arg)
count=0
# time.sleep(.01)
while (count<20):
dummy=spi.xfer([00],500000,20)
if (dummy[0] != 0):
num = dummy[0]
id = id + chr(num)
count = count + 1
else:
count=20
GPIO.output(ppFRAME,False)
return id
def getHWrev(addr):
global RELAYbaseADDR
VerifyADDR(addr)
resp=ppCMDr(addr,0x02,0,0,1)
rev = resp[0]
whole=float(rev>>4)
point = float(rev&0x0F)
return whole+point/10.0
def getFWrev(addr):
global RELAYbaseADDR
VerifyADDR(addr)
resp=ppCMDr(addr,0x03,0,0,1)
rev = resp[0]
whole=float(rev>>4)
point = float(rev&0x0F)
return whole+point/10.0
def getVersion():
return RPversion
#==============================================================================#
# LOW Level Functions #
#==============================================================================#
def VerifyRELAY(relay):
assert ((relay>=1) and (relay<=7)),"Relay number out of range. Must be between 1 and 7"
def VerifyADDR(addr):
assert ((addr>=0) and (addr<MAXADDR)),"RELAYplate address out of range"
addr_str=str(addr)
assert (relaysPresent[addr]==1),"No RELAYplate found at address "+addr_str
def ppCMDr(addr,cmd,param1,param2,bytes2return):
global RELAYbaseADDR
arg = []
resp = []
arg.append(addr+RELAYbaseADDR)
arg.append(cmd)
arg.append(param1)
arg.append(param2)
GPIO.output(ppFRAME,True)
# null=spi.xfer(arg,300000,60)
null = spi.writebytes(arg)
if bytes2return>0:
time.sleep(.0001)
for i in range(0,bytes2return):
dummy=spi.xfer([00],500000,20)
resp.append(dummy[0])
time.sleep(.001)
GPIO.output(ppFRAME,False)
time.sleep(.001)
return resp
def getADDR(addr):
global RELAYbaseADDR
resp=ppCMDr(addr,0x00,0,0,1)
return resp[0]-RELAYbaseADDR
def quietPoll():
global relaysPresent
ppFoundCount=0
for i in range (0,8):
relaysPresent[i]=0
rtn = getADDR(i)
if (rtn==i):
relaysPresent[i]=1
ppFoundCount += 1
#RESET(i)
def RESET(addr):
VerifyADDR(addr)
resp=ppCMDr(addr,0x0F,0,0,0)
time.sleep(.10)
quietPoll()